Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MXStore: Add a method to remove all the messages sent before a specific timestamp in a room. #819

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* MXCrypto: Only create one olm session at a time per device (vector-im/riot-ios/issues/2331).
* MXCrossSigning: Add the bootstrapWithAuthParams method.
* MXRecoveryService: Create this service to manage keys we want to store in SSSS.
Expand Down
12 changes: 12 additions & 0 deletions MatrixSDK/Data/Store/MXFileStore/MXFileStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
10 changes: 10 additions & 0 deletions MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
27 changes: 27 additions & 0 deletions MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -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[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];
Expand Down
6 changes: 6 additions & 0 deletions MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
11 changes: 11 additions & 0 deletions MatrixSDK/Data/Store/MXNoStore/MXNoStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions MatrixSDK/Data/Store/MXStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down