From 23bcc1acde7d525f1bd43d82c53eabc86c2ce92d Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 28 Jun 2019 08:59:00 +0000 Subject: [PATCH] [Room] Add method to get an event by the ID. --- lib/src/Room.dart | 13 +++++++++++++ lib/src/Store.dart | 8 ++++++++ test/FakeMatrixApi.dart | 14 ++++++++++++++ test/Room_test.dart | 6 ++++++ 4 files changed, 41 insertions(+) diff --git a/lib/src/Room.dart b/lib/src/Room.dart index d189a8c7..a05e161c 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -468,4 +468,17 @@ class Room { return participants; } + + /// Searches for the event in the store. If it isn't found, try to request it + /// from the server. Returns null if not found. + Future getEventById(String eventID) async { + if (client.store != null) { + final Event storeEvent = await client.store.getEventById(eventID, this); + if (storeEvent != null) return storeEvent; + } + final dynamic resp = await client.connection.jsonRequest( + type: "GET", action: "/client/r0/rooms/$id/event/$eventID"); + if (resp is ErrorResponse) return null; + return Event.fromJson(resp, this); + } } diff --git a/lib/src/Store.dart b/lib/src/Store.dart index a8271fad..fdb436cf 100644 --- a/lib/src/Store.dart +++ b/lib/src/Store.dart @@ -597,6 +597,14 @@ class Store { return; } + /// Searches for the event in the store. + Future getEventById(String eventID, Room room) async { + List> res = await db.rawQuery( + "SELECT * FROM Events WHERE id=? AND chat_id=?", [eventID, room.id]); + if (res.length == 0) return null; + return Event.fromJson(res[0], room); + } + /// The database sheme for the Client class. static final String ClientsScheme = 'CREATE TABLE IF NOT EXISTS Clients(' + 'client TEXT PRIMARY KEY, ' + diff --git a/test/FakeMatrixApi.dart b/test/FakeMatrixApi.dart index 9e561c43..cd6b1e72 100644 --- a/test/FakeMatrixApi.dart +++ b/test/FakeMatrixApi.dart @@ -62,6 +62,20 @@ class FakeMatrixApi extends MockClient { static final Map> api = { "GET": { + "/client/r0/rooms/!localpart:server.abc/event/1234": (var req) => { + "content": { + "body": "This is an example text message", + "msgtype": "m.text", + "format": "org.matrix.custom.html", + "formatted_body": "This is an example text message" + }, + "type": "m.room.message", + "event_id": "143273582443PhrSn:example.org", + "room_id": "!localpart:server.abc", + "sender": "@example:example.org", + "origin_server_ts": 1432735824653, + "unsigned": {"age": 1234} + }, "/client/r0/rooms/!1234:example.com/messages": (var req) => { "start": "t47429-4392820_219380_26003_2265", "end": "t47409-4357353_219380_26003_2265", diff --git a/test/Room_test.dart b/test/Room_test.dart index 2b8557a6..02c3049e 100644 --- a/test/Room_test.dart +++ b/test/Room_test.dart @@ -24,6 +24,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:famedlysdk/src/Room.dart'; import 'package:famedlysdk/src/Client.dart'; +import 'package:famedlysdk/src/Event.dart'; import 'package:famedlysdk/src/User.dart'; import 'FakeMatrixApi.dart'; @@ -140,5 +141,10 @@ void main() { expect(user.avatarUrl.mxc, "mxc://example.org/SEsfnsuifSDFSSEF"); expect(user.room.id, "!localpart:server.abc"); }); + + test("getEventByID", () async { + final Event event = await room.getEventById("1234"); + expect(event.id, "143273582443PhrSn:example.org"); + }); }); }