Merge branch 'timeline-enhance-add-getevent-method' into 'master'

[Timeline] Add get event by id method

See merge request famedly/famedlysdk!127
This commit is contained in:
Christian Pauly 2019-11-29 11:12:04 +00:00
commit 2a17d67f1b
2 changed files with 17 additions and 6 deletions

View file

@ -758,13 +758,8 @@ class Room {
return user; return user;
} }
/// Searches for the event in the store. If it isn't found, try to request it /// Searches for the event on the server. Returns null if not found.
/// from the server. Returns null if not found.
Future<Event> getEventById(String eventID) async { 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( final dynamic resp = await client.connection.jsonRequest(
type: HTTPType.GET, action: "/client/r0/rooms/$id/event/$eventID"); type: HTTPType.GET, action: "/client/r0/rooms/$id/event/$eventID");
if (resp is ErrorResponse) return null; if (resp is ErrorResponse) return null;

View file

@ -44,6 +44,22 @@ class Timeline {
StreamSubscription<EventUpdate> sub; StreamSubscription<EventUpdate> sub;
bool _requestingHistoryLock = false; bool _requestingHistoryLock = false;
Map<String, Event> _eventCache = {};
/// Searches for the event in this timeline. If not
/// found, requests from the server. Requested events
/// are cached.
Future<Event> getEventById(String id) async {
for (int i = 0; i < events.length; i++) {
if (events[i].eventId == id) return events[i];
}
if (_eventCache.containsKey(id)) return _eventCache[id];
final Event requestedEvent = await room.getEventById(id);
if (requestedEvent == null) return null;
_eventCache[id] = requestedEvent;
return _eventCache[id];
}
Future<void> requestHistory( Future<void> requestHistory(
{int historyCount = Room.DefaultHistoryCount}) async { {int historyCount = Room.DefaultHistoryCount}) async {
if (!_requestingHistoryLock) { if (!_requestingHistoryLock) {