import 'dart:convert'; 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 './User.dart'; import 'package:famedlysdk/src/Event.dart'; /// FIXME use actual Matrix Stuff. This is a placeholder class Room { final String roomID; String name; String lastMessage; MxContent avatar; ChatTime timeCreated; int notificationCount; int highlightCount; String topic; User user; final Client matrix; List events = []; Room({ this.roomID, this.name, this.lastMessage, this.avatar, this.timeCreated, this.notificationCount, this.highlightCount, this.topic, this.user, this.matrix, this.events, }); String get status { if (this.user != null) { return this.user.status; } return this.topic; } Future setName(String newName) async{ dynamic res = await matrix.connection.jsonRequest( type: "PUT", action: "/client/r0/rooms/${roomID}/send/m.room.name/${new DateTime.now()}", data: {"name": newName}); if (res is ErrorResponse) matrix.connection.onError.add(res); return res; } Future setDescription(String newName) async{ dynamic res = await matrix.connection.jsonRequest( type: "PUT", action: "/client/r0/rooms/${roomID}/send/m.room.topic/${new DateTime.now()}", data: {"topic": newName}); if (res is ErrorResponse) matrix.connection.onError.add(res); return res; } Stream> get eventsStream { return Stream>.fromIterable(Iterable>.generate( this.events.length, (int index) => this.events)).asBroadcastStream(); } Future sendText(String message) async { dynamic res = await matrix.connection.jsonRequest( type: "PUT", action: "/client/r0/rooms/${roomID}/send/m.room.message/${new DateTime.now()}", data: {"msgtype": "m.text", "body": message}); if (res["errcode"] == "M_LIMIT_EXCEEDED") matrix.connection.onError.add(res["error"]); } Future leave() async { dynamic res = await matrix.connection.jsonRequest( type: "POST", action: "/client/r0/rooms/${roomID}/leave"); if (res is ErrorResponse) matrix.connection.onError.add(res); return res; } Future forget() async { dynamic res = await matrix.connection.jsonRequest( type: "POST", action: "/client/r0/rooms/${roomID}/forget"); if (res is ErrorResponse) matrix.connection.onError.add(res); return res; } Future kick(String userID) async { dynamic res = await matrix.connection.jsonRequest( type: "POST", action: "/client/r0/rooms/${roomID}/kick", data: {"user_id": userID}); if (res is ErrorResponse) matrix.connection.onError.add(res); return res; } Future ban(String userID) async { dynamic res = await matrix.connection.jsonRequest( type: "POST", action: "/client/r0/rooms/${roomID}/ban", data: {"user_id": userID}); if (res is ErrorResponse) matrix.connection.onError.add(res); return res; } Future unban(String userID) async { dynamic res = await matrix.connection.jsonRequest( type: "POST", action: "/client/r0/rooms/${roomID}/unban", data: {"user_id": userID}); if (res is ErrorResponse) matrix.connection.onError.add(res); return res; } Future invite(String userID) async { dynamic res = await matrix.connection.jsonRequest( type: "POST", action: "/client/r0/rooms/${roomID}/invite", data: {"user_id": userID}); if (res is ErrorResponse) matrix.connection.onError.add(res); return res; } static Future getRoomFromTableRow( Map 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( roomID: row["id"], name: name, lastMessage: content_body, avatar: MxContent(avatarMxcUrl), timeCreated: ChatTime(row["origin_server_ts"]), notificationCount: row["notification_count"], highlightCount: row["highlight_count"], topic: "", matrix: matrix, events: [], ); } static Future getRoomById(String id, Client matrix) async { List> res = await matrix.store.db.rawQuery("SELECT * FROM Chats WHERE id=?", [id]); if (res.length != 1) return null; return getRoomFromTableRow(res[0], matrix); } static Future loadRoomEvents(String id, Client matrix) async { Room room = await Room.getRoomById(id, matrix); room.events = await Event.getEventList(matrix, id); return room; } Future> requestParticipants(Client matrix) async { List participants = []; dynamic res = await matrix.connection.jsonRequest( type: "GET", action: "/client/r0/rooms/${roomID}/members"); if (res is ErrorResponse || !(res["chunk"] is List)) 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); } return participants; } }