[User] Rewrite User class

This commit is contained in:
Christian Pauly 2019-08-07 09:54:08 +02:00
parent 2cea10b01a
commit 5184f34251

View file

@ -22,6 +22,7 @@
*/ */
import 'package:famedlysdk/src/Room.dart'; import 'package:famedlysdk/src/Room.dart';
import 'package:famedlysdk/src/State.dart';
import 'package:famedlysdk/src/responses/ErrorResponse.dart'; import 'package:famedlysdk/src/responses/ErrorResponse.dart';
import 'package:famedlysdk/src/utils/MxContent.dart'; import 'package:famedlysdk/src/utils/MxContent.dart';
@ -30,85 +31,34 @@ import 'Connection.dart';
enum Membership { join, invite, leave, ban } enum Membership { join, invite, leave, ban }
/// Represents a Matrix User which may be a participant in a Matrix Room. /// Represents a Matrix User which may be a participant in a Matrix Room.
class User { class User extends State {
/// The full qualified Matrix ID in the format @username:server.abc. /// The full qualified Matrix ID in the format @username:server.abc.
final String id; String get id => stateKey;
/// The displayname of the user if the user has set one. /// The displayname of the user if the user has set one.
final String displayName; String get displayName => content["displayname"];
/// The membership status of the user. One of: /// The membership status of the user. One of:
/// join /// join
/// invite /// invite
/// leave /// leave
/// ban /// ban
Membership membership; Membership get membership => Membership.values.firstWhere((e) {
if (content["membership"] != null) {
return e.toString() == 'Membership.' + content['membership'];
}
return false;
});
/// The avatar if the user has one. /// The avatar if the user has one.
MxContent avatarUrl; MxContent avatarUrl;
/// The powerLevel of the user. Normally:
/// 0=Normal user
/// 50=Moderator
/// 100=Admin
int powerLevel = 0;
/// All users normally belong to a room.
final Room room;
@Deprecated("Use membership instead!")
String get status => membership.toString().split('.').last;
@Deprecated("Use ID instead!")
String get mxid => id;
@Deprecated("Use avatarUrl instead!")
MxContent get avatar_url => avatarUrl;
User(
String id, {
this.membership,
this.displayName,
this.avatarUrl,
this.powerLevel,
this.room,
}) : this.id = id ?? "";
/// Returns the displayname or the local part of the Matrix ID if the user /// Returns the displayname or the local part of the Matrix ID if the user
/// has no displayname. /// has no displayname.
String calcDisplayname() => (displayName == null || displayName.isEmpty) String calcDisplayname() => (displayName == null || displayName.isEmpty)
? id.replaceFirst("@", "").split(":")[0] ? id.replaceFirst("@", "").split(":")[0]
: displayName; : displayName;
/// Creates a new User object from a json string like a row from the database.
static User fromJson(Map<String, dynamic> json, Room room) {
return User(json['matrix_id'] ?? json['sender'],
displayName: json['displayname'],
avatarUrl: MxContent(json['avatar_url']),
membership: Membership.values.firstWhere((e) {
if (json["membership"] != null) {
return e.toString() == 'Membership.' + json['membership'];
}
return false;
}, orElse: () => null),
powerLevel: json['power_level'],
room: room);
}
/// Checks if the client's user has the permission to kick this user.
Future<bool> get canKick async {
final int ownPowerLevel = await room.client.store.getPowerLevel(room.id);
return ownPowerLevel > powerLevel &&
ownPowerLevel >= room.powerLevels["power_kick"];
}
/// Checks if the client's user has the permission to ban or unban this user.
Future<bool> get canBan async {
final int ownPowerLevel = await room.client.store.getPowerLevel(room.id);
return ownPowerLevel > powerLevel &&
ownPowerLevel >= room.powerLevels["power_ban"];
}
/// Call the Matrix API to kick this user from this room. /// Call the Matrix API to kick this user from this room.
Future<dynamic> kick() async { Future<dynamic> kick() async {
dynamic res = await room.kick(id); dynamic res = await room.kick(id);
@ -137,7 +87,7 @@ class User {
/// Returns null on error. /// Returns null on error.
Future<String> startDirectChat() async { Future<String> startDirectChat() async {
// Try to find an existing direct chat // Try to find an existing direct chat
String roomID = await room.client?.store?.getDirectChatRoomID(id); String roomID = await room.client?.rooms.getDirectChatRoomID(id);
if (roomID != null) return roomID; if (roomID != null) return roomID;
// Start a new direct chat // Start a new direct chat