famedlysdk/lib/src/User.dart

119 lines
3.6 KiB
Dart
Raw Normal View History

2019-06-09 11:57:33 +00:00
/*
* Copyright (c) 2019 Zender & Kurtz GbR.
*
* Authors:
* Christian Pauly <krille@famedly.com>
* Marcel Radzio <mtrnord@famedly.com>
*
* This file is part of famedlysdk.
*
* famedlysdk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* famedlysdk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
2019-06-09 11:57:33 +00:00
*/
import 'package:famedlysdk/src/Room.dart';
2019-08-07 07:54:08 +00:00
import 'package:famedlysdk/src/State.dart';
2019-06-12 08:04:54 +00:00
import 'package:famedlysdk/src/responses/ErrorResponse.dart';
2019-06-09 10:16:48 +00:00
import 'package:famedlysdk/src/utils/MxContent.dart';
import 'Connection.dart';
enum Membership { join, invite, leave, ban }
2019-06-09 10:16:48 +00:00
2019-06-09 11:57:33 +00:00
/// Represents a Matrix User which may be a participant in a Matrix Room.
2019-08-07 07:54:08 +00:00
class User extends State {
2019-08-07 08:46:59 +00:00
User(String userId) : super(senderId: userId);
2019-08-07 08:17:03 +00:00
2019-06-11 09:13:14 +00:00
/// The full qualified Matrix ID in the format @username:server.abc.
2019-08-07 07:54:08 +00:00
String get id => stateKey;
2019-06-09 11:57:33 +00:00
/// The displayname of the user if the user has set one.
2019-08-07 07:54:08 +00:00
String get displayName => content["displayname"];
2019-06-09 11:57:33 +00:00
2019-06-11 09:13:14 +00:00
/// The membership status of the user. One of:
/// join
/// invite
/// leave
/// ban
2019-08-07 07:54:08 +00:00
Membership get membership => Membership.values.firstWhere((e) {
if (content["membership"] != null) {
return e.toString() == 'Membership.' + content['membership'];
}
return false;
});
2019-06-11 09:13:14 +00:00
2019-06-09 11:57:33 +00:00
/// The avatar if the user has one.
2019-06-11 09:13:14 +00:00
MxContent avatarUrl;
2019-06-09 11:57:33 +00:00
/// Returns the displayname or the local part of the Matrix ID if the user
/// has no displayname.
2019-07-03 09:27:46 +00:00
String calcDisplayname() => (displayName == null || displayName.isEmpty)
2019-06-27 10:33:02 +00:00
? id.replaceFirst("@", "").split(":")[0]
2019-06-12 08:04:54 +00:00
: displayName;
2019-06-09 10:16:48 +00:00
2019-06-11 09:13:14 +00:00
/// Call the Matrix API to kick this user from this room.
Future<dynamic> kick() async {
dynamic res = await room.kick(id);
return res;
}
/// Call the Matrix API to ban this user from this room.
Future<dynamic> ban() async {
dynamic res = await room.ban(id);
return res;
}
/// Call the Matrix API to unban this banned user from this room.
Future<dynamic> unban() async {
dynamic res = await room.unban(id);
return res;
2019-06-09 10:16:48 +00:00
}
2019-06-11 11:32:14 +00:00
/// Call the Matrix API to change the power level of this user.
Future<dynamic> setPower(int power) async {
dynamic res = await room.setPower(id, power);
return res;
}
2019-06-12 11:54:04 +00:00
/// Returns an existing direct chat ID with this user or creates a new one.
2019-06-14 06:07:51 +00:00
/// Returns null on error.
Future<String> startDirectChat() async {
// Try to find an existing direct chat
2019-08-07 07:54:08 +00:00
String roomID = await room.client?.rooms.getDirectChatRoomID(id);
if (roomID != null) return roomID;
// Start a new direct chat
final dynamic resp = await room.client.connection.jsonRequest(
type: HTTPType.POST,
action: "/client/r0/createRoom",
data: {
"invite": [id],
"is_direct": true,
"preset": "trusted_private_chat"
});
2019-06-14 06:07:51 +00:00
if (resp is ErrorResponse) {
room.client.connection.onError.add(resp);
return null;
}
2019-06-12 08:02:07 +00:00
2019-06-12 10:04:52 +00:00
final String newRoomID = resp["room_id"];
2019-06-12 08:02:07 +00:00
2019-06-14 06:07:51 +00:00
if (newRoomID == null) return newRoomID;
2019-06-12 11:58:40 +00:00
await Room(id: newRoomID, client: room.client).addToDirectChat(id);
2019-06-12 08:02:07 +00:00
2019-06-12 10:04:52 +00:00
return newRoomID;
}
2019-06-09 10:16:48 +00:00
}