From dce21bef812243f685375643a60bc26aa38de7e1 Mon Sep 17 00:00:00 2001 From: Giom Foret Date: Mon, 27 Jan 2020 10:18:25 +0100 Subject: [PATCH 1/5] MXStore: Add new API to remove expired messages --- .../Data/Store/MXFileStore/MXFileStore.m | 12 +++++++++ .../Store/MXMemoryStore/MXMemoryRoomStore.h | 10 +++++++ .../Store/MXMemoryStore/MXMemoryRoomStore.m | 27 +++++++++++++++++++ .../Data/Store/MXMemoryStore/MXMemoryStore.m | 6 +++++ MatrixSDK/Data/Store/MXNoStore/MXNoStore.m | 11 ++++++++ MatrixSDK/Data/Store/MXStore.h | 12 +++++++++ 6 files changed, 78 insertions(+) diff --git a/MatrixSDK/Data/Store/MXFileStore/MXFileStore.m b/MatrixSDK/Data/Store/MXFileStore/MXFileStore.m index 73ec69c859..8e6552f660 100644 --- a/MatrixSDK/Data/Store/MXFileStore/MXFileStore.m +++ b/MatrixSDK/Data/Store/MXFileStore/MXFileStore.m @@ -319,6 +319,18 @@ - (void)replaceEvent:(MXEvent*)event inRoom:(NSString*)roomId } } +- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs inRoom:(nonnull NSString *)roomId +{ + BOOL ret = [super removeAllMessagesSentBefore:limitTs inRoom:roomId]; + + if (ret && NSNotFound == [roomsToCommitForMessages indexOfObject:roomId]) + { + [roomsToCommitForMessages addObject:roomId]; + } + + return ret; +} + - (void)deleteAllMessagesInRoom:(NSString *)roomId { [super deleteAllMessagesInRoom:roomId]; diff --git a/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.h b/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.h index 7d78eec4a9..37df52c675 100644 --- a/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.h +++ b/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.h @@ -51,6 +51,16 @@ */ - (void)replaceEvent:(MXEvent*)event; +/** + Remove all the messages sent before a specific timestamp in a room. + The state events are not removed during this operation. We keep them in the timeline. + + @param limitTs the timestamp from which the messages are kept. + + @return YES if at least one event has been removed. + */ +- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs; + /** Get an event from this room. diff --git a/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m b/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m index e4653b4e9e..075fcb4f2f 100644 --- a/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m +++ b/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m @@ -76,6 +76,33 @@ - (void)replaceEvent:(MXEvent*)event } } +- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs +{ + NSUInteger index = 0; + BOOL didChange = NO; + while (index < messages.count) + { + MXEvent *anEvent = [messages objectAtIndex:index]; + if (anEvent.isState) + { + // Keep state event + index ++; + } + else if (anEvent.originServerTs < limitTs) + { + [messages removeObjectAtIndex:index]; + [messagesByEventIds removeObjectForKey:anEvent.eventId]; + didChange = YES; + } + else + { + // Break the loop, we've reached the first non-state event in the timeline which is not expired + break; + } + } + return didChange; +} + - (MXEvent *)eventWithEventId:(NSString *)eventId { return messagesByEventIds[eventId]; diff --git a/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.m b/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.m index 24aca78290..b6c396de5c 100644 --- a/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.m +++ b/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.m @@ -68,6 +68,12 @@ - (void)replaceEvent:(MXEvent *)event inRoom:(NSString *)roomId [roomStore replaceEvent:event]; } +- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs inRoom:(nonnull NSString *)roomId +{ + MXMemoryRoomStore *roomStore = [self getOrCreateRoomStore:roomId]; + return [roomStore removeAllMessagesSentBefore:limitTs]; +} + - (BOOL)eventExistsWithEventId:(NSString *)eventId inRoom:(NSString *)roomId { return (nil != [self eventWithEventId:eventId inRoom:roomId]); diff --git a/MatrixSDK/Data/Store/MXNoStore/MXNoStore.m b/MatrixSDK/Data/Store/MXNoStore/MXNoStore.m index 5f73842a8d..6b279ed3ef 100644 --- a/MatrixSDK/Data/Store/MXNoStore/MXNoStore.m +++ b/MatrixSDK/Data/Store/MXNoStore/MXNoStore.m @@ -105,6 +105,17 @@ - (void)replaceEvent:(MXEvent *)event inRoom:(NSString *)roomId } } +- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs inRoom:(nonnull NSString *)roomId +{ + // Only the last message is stored + MXEvent *lastMessage = lastMessages[roomId]; + if (!lastMessage.isState && lastMessage.originServerTs < limitTs) { + lastMessages[roomId] = nil; + return YES; + } + return NO; +} + - (BOOL)eventExistsWithEventId:(NSString *)eventId inRoom:(NSString *)roomId { // Events are not stored. So, we cannot find it. diff --git a/MatrixSDK/Data/Store/MXStore.h b/MatrixSDK/Data/Store/MXStore.h index 009304de4a..acf8cb1463 100644 --- a/MatrixSDK/Data/Store/MXStore.h +++ b/MatrixSDK/Data/Store/MXStore.h @@ -70,6 +70,18 @@ */ - (void)replaceEvent:(nonnull MXEvent*)event inRoom:(nonnull NSString*)roomId; +/** + Remove all the messages sent before a specific timestamp in a room. + The state events are not removed during this operation. We keep them in the timeline. + This operation doesn't change the pagination token, and the flag indicating that the SDK has reached the end of pagination. + + @param limitTs the timestamp from which the messages are kept. + @param roomId the id of the room. + + @return YES if at least one event has been removed. + */ +- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs inRoom:(nonnull NSString *)roomId; + /** Returns a Boolean value that indicates whether an event is already stored. From 5fa5d884f3665104c47cb9be33c51711ec3adaef Mon Sep 17 00:00:00 2001 From: Giom Foret Date: Mon, 13 Apr 2020 15:14:34 +0200 Subject: [PATCH 2/5] Update Changes --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index b9b75fc3d0..50989a02f2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -15,6 +15,7 @@ Improvements: * MXCrypto: the `setDeviceVerification` method now downloads all user's devices if the device is not yet known. * MXRoomSummary: Add the trust property to indicate trust in other users and devices in the room (vector-im/riot-ios/issues/2906). * MXStore: Add a method to get related events for a specific event. + * MXStore: Add a method to remove all the messages sent before a specific timestamp in a room. * MXPublicRoom: Add canonical alias property. * MXLogger: Add a parameter to indicate the number of log files. * MXDeviceList: Post `MXDeviceListDidUpdateUsersDevicesNotification` notification when users devices list are updated. From ec856c02cbd599904850d18374a07c59d5e4d60c Mon Sep 17 00:00:00 2001 From: giomfo Date: Tue, 12 May 2020 10:13:52 +0200 Subject: [PATCH 3/5] Update MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m Co-authored-by: manuroe --- MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m b/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m index 075fcb4f2f..8b04cf915f 100644 --- a/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m +++ b/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m @@ -82,7 +82,7 @@ - (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs BOOL didChange = NO; while (index < messages.count) { - MXEvent *anEvent = [messages objectAtIndex:index]; + MXEvent *anEvent = messages[index]; if (anEvent.isState) { // Keep state event From 5e06312030b5b9453370aab94cce33104991ac62 Mon Sep 17 00:00:00 2001 From: giomfo Date: Tue, 12 May 2020 10:14:04 +0200 Subject: [PATCH 4/5] Update MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m Co-authored-by: manuroe --- MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m b/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m index 8b04cf915f..0522a0e582 100644 --- a/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m +++ b/MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m @@ -86,7 +86,7 @@ - (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs if (anEvent.isState) { // Keep state event - index ++; + index++; } else if (anEvent.originServerTs < limitTs) { From 6cbc0c34ab29a0d4552a3ef4b26b51bf33481ae4 Mon Sep 17 00:00:00 2001 From: Giom Foret Date: Wed, 20 May 2020 10:14:15 +0200 Subject: [PATCH 5/5] Update CHANGES --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 10b8d58857..52409b4d37 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,7 @@ Changes in Matrix iOS SDK in 0.16.6 (2020-05-xx) ================================================ Improvements: + * MXStore: Add a method to remove all the messages sent before a specific timestamp in a room. Changes in Matrix iOS SDK in 0.16.5 (2020-05-18) ================================================ @@ -77,7 +78,6 @@ Improvements: * MXRoomSummary: Add the trust property to indicate trust in other users and devices in the room (vector-im/riot-ios/issues/2906). * Aggregations: Implement m.reference aggregations, aka thread ([MSC1849](https://github.com/matrix-org/matrix-doc/blob/matthew/msc1849/proposals/1849-aggregations.md)). * MXStore: Add a method to get related events for a specific event. - * MXStore: Add a method to remove all the messages sent before a specific timestamp in a room. * MXPublicRoom: Add canonical alias property. * MXLogger: Add a parameter to indicate the number of log files. * MXThrottler: Add this tool class to throttle actions.