From b00b1636d1ee751dc4ff499d9534e55c4b83211b Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 7 Jun 2022 15:07:15 +0200 Subject: [PATCH] fix: Do not show seen events in push notification --- lib/src/client.dart | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/src/client.dart b/lib/src/client.dart index 278b34d5..cea34fb8 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1036,10 +1036,13 @@ class Client extends MatrixApi { /// initalized, it will only fetch the necessary parts of the database. This /// should make it possible to run this parallel to another client with the /// same client name. + /// This also checks if the given event has a readmarker and returns null + /// in this case. Future getEventByPushNotification( PushNotification notification, { bool storeInDatabase = true, Duration timeoutForServerRequests = const Duration(seconds: 8), + bool returnNullIfSeen = true, }) async { // Get access token if necessary: final database = _database ??= await databaseBuilder?.call(this); @@ -1111,6 +1114,7 @@ class Client extends MatrixApi { matrixEvent ??= await database ?.getEventById(eventId, room) .timeout(timeoutForServerRequests); + try { matrixEvent ??= await getOneRoomEvent(roomId, eventId) .timeout(timeoutForServerRequests); @@ -1128,6 +1132,28 @@ class Client extends MatrixApi { throw Exception('Unable to find event for this push notification!'); } + // If the event was already in database, check if it has a read marker + // before displaying it. + if (returnNullIfSeen) { + if (room.fullyRead == matrixEvent.eventId) { + return null; + } + final readMarkerEvent = await database + ?.getEventById(room.fullyRead, room) + .timeout(timeoutForServerRequests); + if (readMarkerEvent != null && + readMarkerEvent.originServerTs.isAfter( + matrixEvent.originServerTs + // As origin server timestamps are not always correct data in + // a federated environment, we add 10 minutes to the calculation + // to reduce the possibility that an event is marked as read which + // isn't. + ..add(Duration(minutes: 10)), + )) { + return null; + } + } + // Load the sender of this event try { await room