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
|
2019-06-21 07:46:53 +00:00
|
|
|
* 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';
|
Update lib/src/client.dart, lib/src/user.dart, lib/src/timeline.dart, lib/src/room.dart, lib/src/presence.dart, lib/src/event.dart, lib/src/utils/profile.dart, lib/src/utils/receipt.dart, test/client_test.dart, test/event_test.dart, test/presence_test.dart, test/room_test.dart, test/timeline_test.dart, test/user_test.dart files
2020-01-04 17:56:17 +00:00
|
|
|
import 'package:famedlysdk/src/room.dart';
|
|
|
|
import 'package:famedlysdk/src/event.dart';
|
2019-07-12 09:26:07 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}) {
|
2020-03-30 09:08:38 +00:00
|
|
|
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-03-30 09:08:38 +00:00
|
|
|
typeKey: 'm.room.member',
|
2019-08-08 09:54:39 +00:00
|
|
|
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.
|
2020-03-30 09:08:38 +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) {
|
2020-03-30 09:08:38 +00:00
|
|
|
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.
|
2020-04-24 07:24:06 +00:00
|
|
|
Uri get avatarUrl => content != null && content['avatar_url'] is String
|
|
|
|
? Uri.parse(content['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.
|
|
|
|
String calcDisplayname({bool formatLocalpart = true}) {
|
|
|
|
if (displayName?.isNotEmpty ?? false) {
|
|
|
|
return displayName;
|
|
|
|
}
|
|
|
|
if (stateKey != null) {
|
|
|
|
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';
|
|
|
|
}
|
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:09:26 +00:00
|
|
|
|
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 {
|
2019-06-11 11:09:26 +00:00
|
|
|
// Try to find an existing direct chat
|
2020-03-30 09:08:38 +00:00
|
|
|
var roomID = await room.client?.getDirectChatFromUserId(id);
|
2019-06-11 11:09:26 +00:00
|
|
|
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(
|
2019-07-12 09:26:07 +00:00
|
|
|
type: HTTPType.POST,
|
2020-03-30 09:08:38 +00:00
|
|
|
action: '/client/r0/createRoom',
|
2019-07-12 09:26:07 +00:00
|
|
|
data: {
|
2020-03-30 09:08:38 +00:00
|
|
|
'invite': [id],
|
|
|
|
'is_direct': true,
|
|
|
|
'preset': 'trusted_private_chat'
|
2019-07-12 09:26:07 +00:00
|
|
|
});
|
2019-06-11 11:09:26 +00:00
|
|
|
|
2020-03-30 09:08:38 +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-11 11:09:26 +00:00
|
|
|
}
|
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];
|
|
|
|
|
2020-01-27 08:24:57 +00:00
|
|
|
/// 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
|
|
|
}
|