famedlysdk/lib/src/User.dart

108 lines
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 Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
2019-06-09 10:16:48 +00:00
import 'package:famedlysdk/src/Client.dart';
import 'package:famedlysdk/src/utils/MxContent.dart';
import 'package:famedlysdk/src/Room.dart';
2019-06-09 11:57:33 +00:00
/// Represents a Matrix User which may be a participant in a Matrix Room.
2019-06-09 10:16:48 +00:00
class User {
2019-06-09 11:57:33 +00:00
2019-06-11 09:13:14 +00:00
/// The full qualified Matrix ID in the format @username:server.abc.
final String id;
2019-06-09 11:57:33 +00:00
/// The displayname of the user if the user has set one.
2019-06-09 10:16:48 +00:00
final String 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
String membership;
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
2019-06-11 09:13:14 +00:00
/// The powerLevel of the user. Normally:
/// 0=Normal user
/// 50=Moderator
/// 100=Admin
int powerLevel = 0;
2019-06-09 11:57:33 +00:00
2019-06-11 09:13:14 +00:00
/// All users normally belong to a room.
2019-06-09 10:16:48 +00:00
final Room room;
2019-06-11 09:13:14 +00:00
@Deprecated("Use membership instead!")
String get status => membership;
@Deprecated("Use ID instead!")
String get mxid => id;
@Deprecated("Use avatarUrl instead!")
MxContent get avatar_url => avatarUrl;
User(this.id, {
this.membership,
2019-06-09 10:16:48 +00:00
this.displayName,
2019-06-11 09:13:14 +00:00
this.avatarUrl,
this.powerLevel,
2019-06-09 10:16:48 +00:00
this.room,
});
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-06-11 09:13:14 +00:00
String calcDisplayname() =>
displayName.isEmpty
? mxid.replaceFirst("@", "").split(":")[0]
: displayName;
2019-06-09 10:16:48 +00:00
2019-06-09 11:57:33 +00:00
/// Creates a new User object from a json string like a row from the database.
2019-06-11 09:13:14 +00:00
static User fromJson(Map<String, dynamic> json, Room room) {
2019-06-09 10:16:48 +00:00
return User(json['matrix_id'],
displayName: json['displayname'],
2019-06-11 09:13:14 +00:00
avatarUrl: MxContent(json['avatar_url']),
2019-06-11 09:48:31 +00:00
membership: json['membership'],
2019-06-11 09:53:32 +00:00
powerLevel: json['power_level'],
2019-06-11 09:13:14 +00:00
room: room);
}
/// 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
}
}