diff --git a/lib/src/Event.dart b/lib/src/Event.dart index 7f969ff..eba53c8 100644 --- a/lib/src/Event.dart +++ b/lib/src/Event.dart @@ -42,7 +42,7 @@ class Event extends RawEvent { String typeKey, String eventId, String roomId, - String sender, + String senderId, ChatTime time, dynamic unsigned, Room room}) @@ -51,25 +51,24 @@ class Event extends RawEvent { typeKey: typeKey, eventId: eventId, roomId: roomId, - sender: sender, + senderId: senderId, time: time, unsigned: unsigned, room: room); /// Get a State event from a table row or from the event stream. - factory Event.fromJson( - Map jsonPayload, int status, Room room) { + factory Event.fromJson(Map jsonPayload, Room room) { final Map content = RawEvent.getMapFromPayload(jsonPayload['content']); final Map unsigned = RawEvent.getMapFromPayload(jsonPayload['unsigned']); return Event( - status: status, + status: jsonPayload['status'] ?? 1, content: content, typeKey: jsonPayload['type'], eventId: jsonPayload['event_id'], roomId: jsonPayload['room_id'], - sender: jsonPayload['sender'], + senderId: jsonPayload['sender'], time: ChatTime(jsonPayload['origin_server_ts']), unsigned: unsigned, room: room); diff --git a/lib/src/RawEvent.dart b/lib/src/RawEvent.dart index f8aeb8b..c4bf7b5 100644 --- a/lib/src/RawEvent.dart +++ b/lib/src/RawEvent.dart @@ -22,6 +22,7 @@ */ import 'dart:convert'; +import 'package:famedlysdk/famedlysdk.dart'; import 'package:meta/meta.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart'; import './Room.dart'; @@ -41,7 +42,9 @@ class RawEvent { final String roomId; /// The user who has sent this event if it is not a global account data event. - final String sender; + final String senderId; + + User get sender => room.states[senderId] ?? User(senderId); /// The time this event has received at the server. May be null for events like /// account data. @@ -58,7 +61,7 @@ class RawEvent { @required this.typeKey, this.eventId, this.roomId, - this.sender, + this.senderId, this.time, this.unsigned, this.room}); @@ -79,7 +82,7 @@ class RawEvent { typeKey: jsonPayload['type'], eventId: jsonPayload['event_id'], roomId: jsonPayload['room_id'], - sender: jsonPayload['sender'], + senderId: jsonPayload['sender'], time: ChatTime(jsonPayload['origin_server_ts']), unsigned: unsigned, room: room); diff --git a/lib/src/Room.dart b/lib/src/Room.dart index c4b896a..a44e80e 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -122,6 +122,7 @@ class Room { this.mInvitedMemberCount, this.mJoinedMemberCount, this.states, + this.roomAccountData, }); /// Calculates the displayname. First checks if there is a name, then checks for a canonical alias and @@ -393,7 +394,8 @@ class Room { /// state are also given, the method will await them. static Future getRoomFromTableRow( Map row, Client matrix, - {Future>> states}) async { + {Future>> states, + Future>> roomAccountData}) async { Room newRoom = Room( id: row["id"], notificationCount: row["notification_count"], @@ -416,22 +418,20 @@ class Room { newRoom.states = newStates; } + Map newRoomAccountData = {}; + if (roomAccountData != null) { + List> rawRoomAccountData = await roomAccountData; + for (int i = 0; i < rawRoomAccountData.length; i++) { + RoomAccountData newData = + RoomAccountData.fromJson(rawRoomAccountData[i], newRoom); + newRoomAccountData[newData.typeKey] = newData; + } + newRoom.roomAccountData = newRoomAccountData; + } + return newRoom; } - @Deprecated("Use client.store.getRoomById(String id) instead!") - static Future getRoomById(String id, Client matrix) async { - Room room = await matrix.store.getRoomById(id); - return room; - } - - /// Load a room from the store including all room events. - static Future loadRoomEvents(String id, Client matrix) async { - Room room = await matrix.store.getRoomById(id); - await room.loadEvents(); - return room; - } - /// Creates a timeline from the store. Returns a [Timeline] object. Future getTimeline( {onTimelineUpdateCallback onUpdate, @@ -467,14 +467,7 @@ class Room { 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"] ?? "", - membership: Membership.values.firstWhere((e) => - e.toString() == - 'Membership.' + res["chunk"][i]["content"]["membership"] ?? - ""), - avatarUrl: MxContent(res["chunk"][i]["content"]["avatar_url"] ?? ""), - room: this); + User newUser = State.fromJson(res["chunk"][i], this) as User; if (newUser.membership != Membership.leave) participants.add(newUser); } @@ -489,7 +482,7 @@ class Room { if (resp is ErrorResponse) return null; // Somehow we miss the mxid in the response and only get the content of the event. resp["matrix_id"] = mxID; - return User.fromJson(resp, this); + return State.fromJson(resp, this) as User; } /// Searches for the event in the store. If it isn't found, try to request it diff --git a/lib/src/RoomAccountData.dart b/lib/src/RoomAccountData.dart index 2ca75db..5b1f416 100644 --- a/lib/src/RoomAccountData.dart +++ b/lib/src/RoomAccountData.dart @@ -21,12 +21,29 @@ * along with famedlysdk. If not, see . */ +import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/src/AccountData.dart'; +import 'package:famedlysdk/src/RawEvent.dart'; class RoomAccountData extends AccountData { /// The user who has sent this event if it is not a global account data event. - final String room_id; + final String roomId; - RoomAccountData({this.room_id, Map content, String typeKey}) + final Room room; + + RoomAccountData( + {this.roomId, this.room, Map content, String typeKey}) : super(content: content, typeKey: typeKey); + + /// Get a State event from a table row or from the event stream. + factory RoomAccountData.fromJson( + Map jsonPayload, Room room) { + final Map content = + RawEvent.getMapFromPayload(jsonPayload['content']); + return RoomAccountData( + content: content, + typeKey: jsonPayload['type'], + roomId: jsonPayload['room_id'], + room: room); + } } diff --git a/lib/src/State.dart b/lib/src/State.dart index 0eaae0a..dfd5502 100644 --- a/lib/src/State.dart +++ b/lib/src/State.dart @@ -20,6 +20,7 @@ * You should have received a copy of the GNU General Public License * along with famedlysdk. If not, see . */ +import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart'; import './Room.dart'; @@ -35,6 +36,8 @@ class State extends RawEvent { /// the overwriting semantics for this piece of room state. final String stateKey; + User get stateKeyUser => room.states[stateKey] ?? User(stateKey); + State( {this.prevContent, this.stateKey, @@ -42,7 +45,7 @@ class State extends RawEvent { String typeKey, String eventId, String roomId, - String sender, + String senderId, ChatTime time, dynamic unsigned, Room room}) @@ -51,7 +54,7 @@ class State extends RawEvent { typeKey: typeKey, eventId: eventId, roomId: roomId, - sender: sender, + senderId: senderId, time: time, unsigned: unsigned, room: room); @@ -71,7 +74,7 @@ class State extends RawEvent { typeKey: jsonPayload['type'], eventId: jsonPayload['event_id'], roomId: jsonPayload['room_id'], - sender: jsonPayload['sender'], + senderId: jsonPayload['sender'], time: ChatTime(jsonPayload['origin_server_ts']), unsigned: unsigned, room: room); diff --git a/lib/src/Store.dart b/lib/src/Store.dart index cb50bd8..1b77729 100644 --- a/lib/src/Store.dart +++ b/lib/src/Store.dart @@ -25,6 +25,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:core'; +import 'package:famedlysdk/src/State.dart'; import 'package:path/path.dart' as p; import 'package:sqflite/sqflite.dart'; @@ -297,7 +298,7 @@ class Store { "SELECT * FROM States WHERE state_key=? AND room_id=?", [matrixID, room.id]); if (res.length != 1) return null; - return User.fromJson(res[0], room); + return State.fromJson(res[0], room) as User; } /// Loads all Users in the database to provide a contact list @@ -308,7 +309,8 @@ class Store { [client.userID, exceptRoomID]); List userList = []; for (int i = 0; i < res.length; i++) - userList.add(User.fromJson(res[i], Room(id: "", client: client))); + userList + .add(State.fromJson(res[i], Room(id: "", client: client)) as User); return userList; } @@ -324,7 +326,7 @@ class Store { List participants = []; for (num i = 0; i < res.length; i++) { - participants.add(User.fromJson(res[i], room)); + participants.add(State.fromJson(res[i], room) as User); } return participants; diff --git a/lib/src/User.dart b/lib/src/User.dart index 260e68f..6152341 100644 --- a/lib/src/User.dart +++ b/lib/src/User.dart @@ -32,7 +32,7 @@ enum Membership { join, invite, leave, ban } /// Represents a Matrix User which may be a participant in a Matrix Room. class User extends State { - User(String sender) : super(sender: sender); + User(String userId) : super(senderId: userId); /// The full qualified Matrix ID in the format @username:server.abc. String get id => stateKey;