famedlysdk/lib/src/user.dart

182 lines
6.1 KiB
Dart
Raw Normal View History

2019-06-09 11:57:33 +00:00
/*
2020-06-03 10:16:01 +00:00
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
2019-06-09 11:57:33 +00:00
*
2020-06-03 10:16:01 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-06-09 11:57:33 +00:00
*
2020-06-03 10:16:01 +00:00
* This program 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 Affero General Public License for more details.
2019-06-09 11:57:33 +00:00
*
2020-06-03 10:16:01 +00:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-06-09 11:57:33 +00:00
*/
import '../famedlysdk.dart';
import '../matrix_api.dart';
import 'event.dart';
import 'room.dart';
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,
}) {
var content = <String, String>{};
if (membership != null) content['membership'] = membership;
if (displayName != null) content['displayname'] = displayName;
if (avatarUrl != null) content['avatar_url'] = avatarUrl;
2019-08-08 09:54:39 +00:00
return User.fromState(
stateKey: id,
content: content,
2020-06-03 10:16:01 +00:00
typeKey: EventTypes.RoomMember,
2019-08-08 09:54:39 +00:00
roomId: room?.id,
room: room,
2020-06-03 10:16:01 +00:00
originServerTs: 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-06-03 10:16:01 +00:00
DateTime originServerTs,
2019-08-08 08:31:39 +00:00
dynamic unsigned,
Room room})
: super(
stateKey: stateKey,
prevContent: prevContent,
content: content,
2020-06-03 10:16:01 +00:00
type: typeKey,
2019-08-08 08:31:39 +00:00
eventId: eventId,
roomId: roomId,
senderId: senderId,
2020-06-03 10:16:01 +00:00
originServerTs: originServerTs,
2019-08-08 08:31:39 +00:00
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.
String get displayName =>
content != null && content.containsKey('displayname')
? content['displayname']
: (prevContent != null ? prevContent['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) {
2019-08-07 07:54:08 +00:00
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.
Uri get avatarUrl => content != null && content.containsKey('avatar_url')
? (content['avatar_url'] is String
? Uri.parse(content['avatar_url'])
: null)
: (prevContent != null && prevContent['avatar_url'] is String
? Uri.parse(prevContent['avatar_url'])
: null);
2019-06-09 11:57:33 +00:00
/// Returns the displayname or the local part of the Matrix ID if the user
2020-04-23 09:46:10 +00:00
/// has no displayname. If [formatLocalpart] is true, then the localpart will
/// be formatted in the way, that all "_" characters are becomming white spaces and
/// the first character of each word becomes uppercase.
/// If [mxidLocalPartFallback] is true, then the local part of the mxid will be shown
/// if there is no other displayname available. If not then this will return "Unknown user".
String calcDisplayname(
{bool formatLocalpart = true, bool mxidLocalPartFallback = true}) {
2020-04-23 09:46:10 +00:00
if (displayName?.isNotEmpty ?? false) {
return displayName;
}
if (stateKey != null && mxidLocalPartFallback) {
2020-04-23 09:46:10 +00:00
if (!formatLocalpart) {
return stateKey.localpart;
}
var words = stateKey.localpart.replaceAll('_', ' ').split(' ');
for (var i = 0; i < words.length; i++) {
if (words[i].isNotEmpty) {
words[i] = words[i][0].toUpperCase() + words[i].substring(1);
}
}
return words.join(' ');
}
return 'Unknown user';
2020-04-23 09:46:10 +00:00
}
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
var roomID = await room.client?.getDirectChatFromUserId(id);
if (roomID != null) return roomID;
// Start a new direct chat
2020-08-11 16:11:51 +00:00
final newRoomID = await room.client.createRoom(
2020-06-03 10:16:01 +00:00
invite: [id],
isDirect: true,
preset: CreateRoomPreset.trusted_private_chat,
);
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
}