famedlysdk/lib/src/Room.dart

334 lines
10 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/ChatTime.dart';
import 'package:famedlysdk/src/utils/MxContent.dart';
import 'package:famedlysdk/src/responses/ErrorResponse.dart';
import 'package:famedlysdk/src/Event.dart';
2019-06-09 11:57:33 +00:00
import './User.dart';
2019-06-09 10:16:48 +00:00
2019-06-09 12:33:25 +00:00
/// Represents a Matrix room.
2019-06-09 10:16:48 +00:00
class Room {
2019-06-11 08:51:45 +00:00
/// The full qualified Matrix ID for the room in the format '!localid:server.abc'.
final String id;
/// Membership status of the user for this room.
String membership;
/// The name of the room if set by a participant.
2019-06-09 10:16:48 +00:00
String name;
2019-06-11 08:51:45 +00:00
/// The topic of the room if set by a participant.
String topic;
/// The avatar of the room if set by a participant.
2019-06-09 10:16:48 +00:00
MxContent avatar;
2019-06-11 08:51:45 +00:00
/// The count of unread notifications.
2019-06-09 10:16:48 +00:00
int notificationCount;
2019-06-11 08:51:45 +00:00
/// The count of highlighted notifications.
2019-06-09 10:16:48 +00:00
int highlightCount;
2019-06-11 08:51:45 +00:00
String prev_batch;
String draft;
/// Time when the user has last read the chat.
ChatTime unread;
/// ID of the fully read marker event.
String fullyRead;
/// The address in the format: #roomname:homeserver.org.
String canonicalAlias;
/// If this room is a direct chat, this is the matrix ID of the user
String directChatMatrixID;
/// Must be one of [all, mention]
String notificationSettings;
/// Are guest users allowed?
String guestAccess;
/// Who can see the history of this room?
String historyVisibility;
/// Who is allowed to join this room?
String joinRules;
/// The needed power levels for all actions.
Map<String,int> powerLevels = {};
/// The list of events in this room. If the room is created by the
/// [getRoomList()] of the [Store], this will contain only the last event.
2019-06-09 10:16:48 +00:00
List<Event> events = [];
2019-06-11 08:51:45 +00:00
/// The list of participants in this room. If the room is created by the
/// [getRoomList()] of the [Store], this will contain only the sender of the
/// last event.
List<User> participants = [];
/// Your current client instance.
final Client client;
@Deprecated("Rooms.roomID is deprecated! Use Rooms.id instead!")
String get roomID =>this.id;
@Deprecated("Rooms.matrix is deprecated! Use Rooms.client instead!")
Client get matrix => this.client;
@Deprecated("Rooms.status is deprecated! Use Rooms.membership instead!")
String get status => this.membership;
2019-06-09 10:16:48 +00:00
Room({
2019-06-11 08:51:45 +00:00
this.id,
this.membership,
2019-06-09 10:16:48 +00:00
this.name,
2019-06-11 08:51:45 +00:00
this.topic,
2019-06-09 10:16:48 +00:00
this.avatar,
this.notificationCount,
this.highlightCount,
2019-06-11 08:51:45 +00:00
this.prev_batch,
this.draft,
this.unread,
this.fullyRead,
this.canonicalAlias,
this.directChatMatrixID,
this.notificationSettings,
this.guestAccess,
this.historyVisibility,
this.joinRules,
this.powerLevels,
2019-06-09 10:16:48 +00:00
this.events,
2019-06-11 08:51:45 +00:00
this.participants,
this.client,
2019-06-09 10:16:48 +00:00
});
2019-06-11 08:51:45 +00:00
/// The last message sent to this room.
String get lastMessage {
if (events.length > 0)
return events[0].getBody();
else return "";
}
/// When the last message received.
ChatTime get timeCreated {
if (events.length > 0)
return events[0].time;
else return ChatTime.now();
2019-06-09 10:16:48 +00:00
}
2019-06-11 08:51:45 +00:00
/// Call the Matrix API to change the name of this room.
2019-06-09 10:16:48 +00:00
Future<dynamic> setName(String newName) async{
2019-06-11 08:51:45 +00:00
dynamic res = await client.connection.jsonRequest(
2019-06-09 10:16:48 +00:00
type: "PUT",
action:
2019-06-11 08:51:45 +00:00
"/client/r0/rooms/${id}/send/m.room.name/${new DateTime.now()}",
2019-06-09 10:16:48 +00:00
data: {"name": newName});
2019-06-11 08:51:45 +00:00
if (res is ErrorResponse) client.connection.onError.add(res);
2019-06-09 10:16:48 +00:00
return res;
}
2019-06-11 08:51:45 +00:00
/// Call the Matrix API to change the topic of this room.
2019-06-09 10:16:48 +00:00
Future<dynamic> setDescription(String newName) async{
2019-06-11 08:51:45 +00:00
dynamic res = await client.connection.jsonRequest(
2019-06-09 10:16:48 +00:00
type: "PUT",
action:
2019-06-11 08:51:45 +00:00
"/client/r0/rooms/${id}/send/m.room.topic/${new DateTime.now()}",
2019-06-09 10:16:48 +00:00
data: {"topic": newName});
2019-06-11 08:51:45 +00:00
if (res is ErrorResponse) client.connection.onError.add(res);
2019-06-09 10:16:48 +00:00
return res;
}
2019-06-11 08:51:45 +00:00
@Deprecated("Use the client.connection streams instead!")
2019-06-09 10:16:48 +00:00
Stream<List<Event>> get eventsStream {
return Stream<List<Event>>.fromIterable(Iterable<List<Event>>.generate(
this.events.length, (int index) => this.events)).asBroadcastStream();
}
2019-06-11 08:51:45 +00:00
/// Call the Matrix API to send a simple text message.
2019-06-09 10:16:48 +00:00
Future<void> sendText(String message) async {
2019-06-11 08:51:45 +00:00
dynamic res = await client.connection.jsonRequest(
2019-06-09 10:16:48 +00:00
type: "PUT",
action:
2019-06-11 08:51:45 +00:00
"/client/r0/rooms/${id}/send/m.room.message/${new DateTime.now()}",
2019-06-09 10:16:48 +00:00
data: {"msgtype": "m.text", "body": message});
2019-06-11 08:51:45 +00:00
if (res["errcode"] == "M_LIMIT_EXCEEDED") client.connection.onError.add(res["error"]);
2019-06-09 10:16:48 +00:00
}
2019-06-11 08:51:45 +00:00
/// Call the Matrix API to leave this room.
2019-06-09 10:16:48 +00:00
Future<dynamic> leave() async {
2019-06-11 08:51:45 +00:00
dynamic res = await client.connection.jsonRequest(
2019-06-09 10:16:48 +00:00
type: "POST",
action:
2019-06-11 08:51:45 +00:00
"/client/r0/rooms/${id}/leave");
if (res is ErrorResponse) client.connection.onError.add(res);
2019-06-09 10:16:48 +00:00
return res;
}
2019-06-11 08:51:45 +00:00
/// Call the Matrix API to forget this room if you already left it.
2019-06-09 10:16:48 +00:00
Future<dynamic> forget() async {
2019-06-11 08:51:45 +00:00
dynamic res = await client.connection.jsonRequest(
2019-06-09 10:16:48 +00:00
type: "POST",
action:
2019-06-11 08:51:45 +00:00
"/client/r0/rooms/${id}/forget");
if (res is ErrorResponse) client.connection.onError.add(res);
2019-06-09 10:16:48 +00:00
return res;
}
2019-06-11 08:51:45 +00:00
/// Call the Matrix API to kick a user from this room.
2019-06-09 10:16:48 +00:00
Future<dynamic> kick(String userID) async {
2019-06-11 08:51:45 +00:00
dynamic res = await client.connection.jsonRequest(
2019-06-09 10:16:48 +00:00
type: "POST",
action:
2019-06-11 08:51:45 +00:00
"/client/r0/rooms/${id}/kick",
2019-06-09 10:16:48 +00:00
data: {"user_id": userID});
2019-06-11 08:51:45 +00:00
if (res is ErrorResponse) client.connection.onError.add(res);
2019-06-09 10:16:48 +00:00
return res;
}
2019-06-11 08:51:45 +00:00
/// Call the Matrix API to ban a user from this room.
2019-06-09 10:16:48 +00:00
Future<dynamic> ban(String userID) async {
2019-06-11 08:51:45 +00:00
dynamic res = await client.connection.jsonRequest(
2019-06-09 10:16:48 +00:00
type: "POST",
action:
2019-06-11 08:51:45 +00:00
"/client/r0/rooms/${id}/ban",
2019-06-09 10:16:48 +00:00
data: {"user_id": userID});
2019-06-11 08:51:45 +00:00
if (res is ErrorResponse) client.connection.onError.add(res);
2019-06-09 10:16:48 +00:00
return res;
}
2019-06-11 08:51:45 +00:00
/// Call the Matrix API to unban a banned user from this room.
2019-06-09 10:16:48 +00:00
Future<dynamic> unban(String userID) async {
2019-06-11 08:51:45 +00:00
dynamic res = await client.connection.jsonRequest(
2019-06-09 10:16:48 +00:00
type: "POST",
action:
2019-06-11 08:51:45 +00:00
"/client/r0/rooms/${id}/unban",
2019-06-09 10:16:48 +00:00
data: {"user_id": userID});
2019-06-11 08:51:45 +00:00
if (res is ErrorResponse) client.connection.onError.add(res);
2019-06-09 10:16:48 +00:00
return res;
}
2019-06-11 08:51:45 +00:00
/// Call the Matrix API to invite a user to this room.
2019-06-09 10:16:48 +00:00
Future<dynamic> invite(String userID) async {
2019-06-11 08:51:45 +00:00
dynamic res = await client.connection.jsonRequest(
2019-06-09 10:16:48 +00:00
type: "POST",
action:
2019-06-11 08:51:45 +00:00
"/client/r0/rooms/${id}/invite",
2019-06-09 10:16:48 +00:00
data: {"user_id": userID});
2019-06-11 08:51:45 +00:00
if (res is ErrorResponse) client.connection.onError.add(res);
2019-06-09 10:16:48 +00:00
return res;
}
2019-06-11 08:51:45 +00:00
/// Returns a Room from a json String which comes normally from the store.
2019-06-09 10:16:48 +00:00
static Future<Room> getRoomFromTableRow(
Map<String, dynamic> row, Client matrix) async {
String name = row["topic"];
if (name == "") name = await matrix.store.getChatNameFromMemberNames(row["id"]);
String content_body = row["content_body"];
if (content_body == null || content_body == "")
content_body = "Keine vorhergehenden Nachrichten";
String avatarMxcUrl = row["avatar_url"];
if (avatarMxcUrl == "")
avatarMxcUrl = await matrix.store.getAvatarFromSingleChat(row["id"]);
return Room(
2019-06-11 08:51:45 +00:00
id: row["id"],
2019-06-09 10:16:48 +00:00
name: name,
avatar: MxContent(avatarMxcUrl),
notificationCount: row["notification_count"],
highlightCount: row["highlight_count"],
topic: "",
2019-06-11 08:51:45 +00:00
client: matrix,
2019-06-09 10:16:48 +00:00
events: [],
2019-06-11 08:51:45 +00:00
participants: [],
2019-06-09 10:16:48 +00:00
);
}
2019-06-11 08:51:45 +00:00
@Deprecated("Use client.store.getRoomById(String id) instead!")
2019-06-09 10:16:48 +00:00
static Future<Room> getRoomById(String id, Client matrix) async {
2019-06-11 08:51:45 +00:00
Room room = await matrix.store.getRoomById(id);
return room;
2019-06-09 10:16:48 +00:00
}
2019-06-11 08:51:45 +00:00
/// Load a room from the store including all room events.
2019-06-09 10:16:48 +00:00
static Future<Room> loadRoomEvents(String id, Client matrix) async {
2019-06-11 08:51:45 +00:00
Room room = await matrix.store.getRoomById(id);
await room.loadEvents();
2019-06-09 10:16:48 +00:00
return room;
}
2019-06-11 08:51:45 +00:00
/// Load all events for a given room from the store. This includes all
/// senders of those events, who will be added to the participants list.
Future<List<Event>> loadEvents() async {
this.events = await client.store.getEventList(id);
Map<String,bool> participantMap = {};
for (num i = 0; i < events.length; i++) {
if (!participantMap.containsKey(events[i].sender.mxid)) {
participants.add(events[i].sender);
participantMap[events[i].sender.mxid] = true;
}
}
return this.events;
}
/// Load all participants for a given room from the store.
Future<List<User>> loadParticipants() async {
this.participants = await client.store.loadParticipants(id);
return this.participants;
}
/// Request the full list of participants from the server. The local list
/// from the store is not complete if the client uses lazy loading.
2019-06-09 10:16:48 +00:00
Future<List<User>> requestParticipants(Client matrix) async {
List<User> participants = [];
dynamic res = await matrix.connection.jsonRequest(
2019-06-11 08:51:45 +00:00
type: "GET", action: "/client/r0/rooms/${id}/members");
2019-06-09 10:16:48 +00:00
if (res is ErrorResponse || !(res["chunk"] is List<dynamic>))
return participants;
for (num i = 0; i < res["chunk"].length; i++) {
User newUser = User(res["chunk"][i]["state_key"],
displayName: res["chunk"][i]["content"]["displayname"] ?? "",
status: res["chunk"][i]["content"]["membership"] ?? "",
directChatRoomId: "",
avatar_url:
MxContent(res["chunk"][i]["content"]["avatar_url"] ?? ""));
if (newUser.status != "leave") participants.add(newUser);
}
2019-06-11 08:51:45 +00:00
this.participants = participants;
return this.participants;
2019-06-09 10:16:48 +00:00
}
}