Skip to content

Commit b9bee86

Browse files
committed
added some functions to remove messages
before a certain timestamp
1 parent 8ae5269 commit b9bee86

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@
4848
*/
4949
- (void)replaceEvent:(MXEvent*)event;
5050

51+
/**
52+
Remove all the messages sent before a specific timestamp in a room.
53+
The state events are not removed during this operation. We keep them in the timeline.
54+
55+
@param limitTs the timestamp from which the messages are kept.
56+
57+
@return YES if at least one event has been removed.
58+
*/
59+
- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs;
60+
5161
/**
5262
Get an event from this room.
5363

MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,31 @@ - (NSString *)description
197197
return [NSString stringWithFormat:@"%tu messages - paginationToken: %@ - hasReachedHomeServerPaginationEnd: %@ - hasLoadedAllRoomMembersForRoom: %@", messages.count, _paginationToken, @(_hasReachedHomeServerPaginationEnd), @(_hasLoadedAllRoomMembersForRoom)];
198198
}
199199

200+
- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs
201+
{
202+
NSUInteger index = 0;
203+
BOOL didChange = NO;
204+
while (index < messages.count)
205+
{
206+
MXEvent *anEvent = [messages objectAtIndex:index];
207+
if (anEvent.isState)
208+
{
209+
// Keep state event
210+
index ++;
211+
}
212+
else if (anEvent.originServerTs < limitTs)
213+
{
214+
[messages removeObjectAtIndex:index];
215+
[messagesByEventIds removeObjectForKey:anEvent.eventId];
216+
didChange = YES;
217+
}
218+
else
219+
{
220+
// Break the loop, we've reached the first non-state event in the timeline which is not expired
221+
break;
222+
}
223+
}
224+
return didChange;
225+
}
226+
200227
@end

MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ - (void)replaceEvent:(MXEvent *)event inRoom:(NSString *)roomId
8383
[roomStore replaceEvent:event];
8484
}
8585

86+
- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs inRoom:(nonnull NSString *)roomId
87+
{
88+
MXMemoryRoomStore *roomStore = [self getOrCreateRoomStore:roomId];
89+
return [roomStore removeAllMessagesSentBefore:limitTs];
90+
}
91+
8692
- (BOOL)eventExistsWithEventId:(NSString *)eventId inRoom:(NSString *)roomId
8793
{
8894
return (nil != [self eventWithEventId:eventId inRoom:roomId]);

MatrixSDK/Data/Store/MXNoStore/MXNoStore.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ - (BOOL)eventExistsWithEventId:(NSString *)eventId inRoom:(NSString *)roomId
124124
return NO;
125125
}
126126

127+
- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs inRoom:(nonnull NSString *)roomId
128+
{
129+
// Only the last message is stored
130+
MXEvent *lastMessage = lastMessages[roomId];
131+
if (!lastMessage.isState && lastMessage.originServerTs < limitTs) {
132+
lastMessages[roomId] = nil;
133+
return YES;
134+
}
135+
return NO;
136+
}
137+
127138
- (MXEvent *)eventWithEventId:(NSString *)eventId inRoom:(NSString *)roomId
128139
{
129140
// Events are not stored. So, we cannot find it.

MatrixSDK/Data/Store/MXStore.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,5 +593,17 @@
593593
success:(nonnull void (^)(NSString * _Nullable filterId))success
594594
failure:(nullable void (^)(NSError * _Nullable error))failure;
595595

596+
/**
597+
Remove all the messages sent before a specific timestamp in a room.
598+
The state events are not removed during this operation. We keep them in the timeline.
599+
This operation doesn't change the pagination token, and the flag indicating that the SDK has reached the end of pagination.
600+
601+
@param limitTs the timestamp from which the messages are kept.
602+
@param roomId the id of the room.
603+
604+
@return YES if at least one event has been removed.
605+
*/
606+
- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs inRoom:(nonnull NSString *)roomId;
607+
596608

597609
@end

0 commit comments

Comments
 (0)