diff --git a/lib/src/Client.dart b/lib/src/Client.dart index 28dcb07..263b207 100644 --- a/lib/src/Client.dart +++ b/lib/src/Client.dart @@ -86,6 +86,8 @@ class Client { /// Returns the current login state. bool isLogged() => accessToken != null; + RoomList roomList; + /// Checks the supported versions of the Matrix protocol and the supported /// login types. Returns false if the server is not compatible with the /// client. Automatically sets [matrixVersions] and [lazyLoadMembers]. diff --git a/lib/src/Connection.dart b/lib/src/Connection.dart index 68cdecf..4d86f2a 100644 --- a/lib/src/Connection.dart +++ b/lib/src/Connection.dart @@ -25,6 +25,8 @@ import 'dart:async'; import 'dart:convert'; import 'dart:core'; +import 'package:famedlysdk/src/Room.dart'; +import 'package:famedlysdk/src/RoomList.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; @@ -149,6 +151,18 @@ class Connection { client.store?.storeClient(); + List rooms = await client.store + ?.getRoomList(onlyLeft: false, onlyGroups: false, onlyDirect: false); + client.roomList = RoomList( + client: client, + onlyLeft: false, + onlyDirect: false, + onlyGroups: false, + onUpdate: null, + onInsert: null, + onRemove: null, + rooms: rooms); + onLoginStateChanged.add(LoginState.logged); _sync(); diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 613fe25..b83682e 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -294,7 +294,8 @@ class Room { /// Call the Matrix API to unban a banned user from this room. Future setPower(String userID, int power) async { - Map powerMap = await client.store.getPowerLevels(id); + Map powerMap = states["m.room.power_levels"].content["users"]; + if (powerMap == null) return null; powerMap[userID] = power; dynamic res = await client.connection.jsonRequest( diff --git a/lib/src/RoomList.dart b/lib/src/RoomList.dart index c3d1f2d..44ede0f 100644 --- a/lib/src/RoomList.dart +++ b/lib/src/RoomList.dart @@ -24,6 +24,8 @@ import 'dart:async'; import 'dart:core'; +import 'package:famedlysdk/src/State.dart'; + import 'Client.dart'; import 'Event.dart'; import 'Room.dart'; @@ -87,7 +89,6 @@ class RoomList { // Add the new chat to the list Room newRoom = Room( id: chatUpdate.id, - name: "", membership: chatUpdate.membership, prev_batch: chatUpdate.prev_batch, highlightCount: chatUpdate.highlight_count, @@ -125,11 +126,6 @@ class RoomList { } void _handleEventUpdate(EventUpdate eventUpdate) { - // Is the event necessary for the chat list? If not, then return - if (!(eventUpdate.type == "timeline" || - eventUpdate.eventType == "m.room.avatar" || - eventUpdate.eventType == "m.room.name")) return; - // Search the room in the rooms num j = 0; for (j = 0; j < rooms.length; j++) { @@ -138,34 +134,10 @@ class RoomList { final bool found = (j < rooms.length && rooms[j].id == eventUpdate.roomID); if (!found) return; - // Is this an old timeline event? Then stop here... - /*if (eventUpdate.type == "timeline" && - ChatTime(eventUpdate.content["origin_server_ts"]) <= - rooms[j].timeCreated) return;*/ - - if (eventUpdate.type == "timeline") { - User stateKey = null; - if (eventUpdate.content["state_key"] is String) - stateKey = User(eventUpdate.content["state_key"]); - // Update the last message preview - rooms[j].lastEvent = Event( - eventUpdate.content["id"], - User(eventUpdate.content["sender"]), - ChatTime(eventUpdate.content["origin_server_ts"]), - room: rooms[j], - stateKey: stateKey, - content: eventUpdate.content["content"], - environment: eventUpdate.eventType, - status: 2, - ); - } - if (eventUpdate.eventType == "m.room.name") { - // Update the room name - rooms[j].name = eventUpdate.content["content"]["name"]; - } else if (eventUpdate.eventType == "m.room.avatar") { - // Update the room avatar - rooms[j].avatar = MxContent(eventUpdate.content["content"]["url"]); - } + State stateEvent = State.fromJson(eventUpdate.content, rooms[j]); + if (rooms[j].states[stateEvent.key] != null && + rooms[j].states[stateEvent.key].time > stateEvent.time) return; + rooms[j].states[stateEvent.key] = stateEvent; sortAndUpdate(); } diff --git a/lib/src/Timeline.dart b/lib/src/Timeline.dart index 6fd2adb..36e4907 100644 --- a/lib/src/Timeline.dart +++ b/lib/src/Timeline.dart @@ -47,8 +47,8 @@ class Timeline { int _findEvent({String event_id, String unsigned_txid}) { int i; for (i = 0; i < events.length; i++) { - if (events[i].id == event_id || - (unsigned_txid != null && events[i].id == unsigned_txid)) break; + if (events[i].eventId == event_id || + (unsigned_txid != null && events[i].eventId == unsigned_txid)) break; } return i; } @@ -82,33 +82,7 @@ class Timeline { eventUpdate.content["avatar_url"] = senderUser.avatarUrl.mxc; } - User stateKeyUser; - if (eventUpdate.content.containsKey("state_key")) { - stateKeyUser = await room.client.store?.getUser( - matrixID: eventUpdate.content["state_key"], room: room); - } - - if (senderUser != null && stateKeyUser != null) { - newEvent = Event.fromJson(eventUpdate.content, room, - senderUser: senderUser, stateKeyUser: stateKeyUser); - } else if (senderUser != null) { - newEvent = Event.fromJson(eventUpdate.content, room, - senderUser: senderUser); - } else if (stateKeyUser != null) { - newEvent = Event.fromJson(eventUpdate.content, room, - stateKeyUser: stateKeyUser); - } else { - newEvent = Event.fromJson(eventUpdate.content, room); - } - - // TODO update to type check when https://gitlab.com/famedly/famedlysdk/merge_requests/28/ is merged - if (newEvent.content.containsKey("m.relates_to")) { - Map relates_to = newEvent.content["m.relates_to"]; - if (relates_to.containsKey("m.in_reply_to")) { - newEvent.replyEvent = await room.getEventById(newEvent - .content["m.relates_to"]["m.in_reply_to"]["event_id"]); - } - } + newEvent = Event.fromJson(eventUpdate.content, room); events.insert(0, newEvent); if (onInsert != null) onInsert(0);