Merge branch 'room-enhance-getEventByID' into 'master'

[Room] Add method to get an event by the ID.

See merge request famedly/famedlysdk!24
This commit is contained in:
Marcel 2019-06-28 08:59:00 +00:00
commit 3f72cf2c05
4 changed files with 41 additions and 0 deletions

View file

@ -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<Event> 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);
}
}

View file

@ -597,6 +597,14 @@ class Store {
return;
}
/// Searches for the event in the store.
Future<Event> getEventById(String eventID, Room room) async {
List<Map<String, dynamic>> 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, ' +

View file

@ -62,6 +62,20 @@ class FakeMatrixApi extends MockClient {
static final Map<String, Map<String, dynamic>> 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": "<b>This is an example text message</b>"
},
"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",

View file

@ -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");
});
});
}