famedlysdk/lib/src/user.dart

169 lines
5.3 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
*/
2019-11-30 09:21:11 +00:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:famedlysdk/src/room.dart';
import 'package:famedlysdk/src/event.dart';
import 'package:famedlysdk/src/utils/mx_content.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.
2020-01-02 14:09:49 +00:00
class User extends Event {
2019-08-08 09:54:39 +00:00
factory User(
String id, {
String membership,
String displayName,
String avatarUrl,
Room room,
}) {
Map<String, String> content = {};
if (membership != null) content["membership"] = membership;
if (displayName != null) content["displayname"] = displayName;
if (avatarUrl != null) content["avatar_url"] = avatarUrl;
return User.fromState(
stateKey: id,
content: content,
typeKey: "m.room.member",
roomId: room?.id,
room: room,
2020-01-02 14:09:49 +00:00
time: DateTime.now(),
2019-08-08 09:54:39 +00:00
);
}
User.fromState(
2019-08-08 08:31:39 +00:00
{dynamic prevContent,
String stateKey,
dynamic content,
String typeKey,
String eventId,
String roomId,
String senderId,
2020-01-02 14:09:49 +00:00
DateTime time,
2019-08-08 08:31:39 +00:00
dynamic unsigned,
Room room})
: super(
stateKey: stateKey,
prevContent: prevContent,
content: content,
typeKey: typeKey,
eventId: eventId,
roomId: roomId,
senderId: senderId,
time: time,
unsigned: unsigned,
room: room);
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-08 09:41:42 +00:00
String get displayName => content != null ? content["displayname"] : null;
2019-06-09 11:57:33 +00:00
2019-09-03 15:57:27 +00:00
/// Returns the power level of this user.
int get powerLevel => room?.getPowerLevelByUserId(id);
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-12-18 12:44:31 +00:00
}, orElse: () => Membership.join);
2019-06-11 09:13:14 +00:00
2019-06-09 11:57:33 +00:00
/// The avatar if the user has one.
2019-08-08 09:41:42 +00:00
MxContent get avatarUrl => content != null && content["avatar_url"] is String
? MxContent(content["avatar_url"])
: MxContent("");
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-11-07 09:46:18 +00:00
? (stateKey != null
? stateKey.replaceFirst("@", "").split(":")[0]
: "Unknown User")
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.
2019-12-29 10:28:33 +00:00
Future<void> kick() => room.kick(id);
2019-06-11 09:13:14 +00:00
/// Call the Matrix API to ban this user from this room.
2019-12-29 10:28:33 +00:00
Future<void> ban() => room.ban(id);
2019-06-11 09:13:14 +00:00
/// Call the Matrix API to unban this banned user from this room.
2019-12-29 10:28:33 +00:00
Future<void> unban() => room.unban(id);
2019-06-11 11:32:14 +00:00
/// Call the Matrix API to change the power level of this user.
2019-12-29 10:28:33 +00:00
Future<void> setPower(int power) => room.setPower(id, power);
2019-06-11 11:32:14 +00:00
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 10:06:28 +00:00
String roomID = await room.client?.getDirectChatFromUserId(id);
if (roomID != null) return roomID;
// Start a new direct chat
2020-01-02 14:09:49 +00:00
final dynamic resp = await room.client.jsonRequest(
type: HTTPType.POST,
action: "/client/r0/createRoom",
data: {
"invite": [id],
"is_direct": true,
"preset": "trusted_private_chat"
});
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-11-26 06:38:44 +00:00
2019-11-30 09:21:11 +00:00
/// The newest presence of this user if there is any and null if not.
Presence get presence => room.client.presences[id];
/// Whether the client is able to ban/unban this user.
bool get canBan =>
membership != Membership.ban &&
room.canBan &&
powerLevel < room.ownPowerLevel;
/// Whether the client is able to kick this user.
bool get canKick =>
[Membership.join, Membership.invite].contains(membership) &&
room.canKick &&
powerLevel < room.ownPowerLevel;
2019-11-26 06:38:44 +00:00
/// Whether the client is allowed to change the power level of this user.
/// Please be aware that you can only set the power level to at least your own!
bool get canChangePowerLevel =>
room.canChangePowerLevel && powerLevel < room.ownPowerLevel;
2019-06-09 10:16:48 +00:00
}