Skip to content

Commit fcfbb67

Browse files
authored
Merge pull request #1861 from matrix-org/mauroromito/add_m.room.retention
Add a new state event type: "m.room.retention"
2 parents 82f93fb + d3f4ec0 commit fcfbb67

File tree

10 files changed

+79
-4
lines changed

10 files changed

+79
-4
lines changed

MatrixSDK/Background/MXBackgroundStore.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ class MXBackgroundStore: NSObject, MXStore {
298298
func isRoomMarked(asUnread roomId: String) -> Bool {
299299
return false
300300
}
301+
302+
func removeAllMessagesSent(before limitTs: UInt64, inRoom roomId: String) -> Bool {
303+
// Not sure if this needs to be implemented
304+
false
305+
}
301306
}
302307

303308
// MARK: - MXRoomSummaryStore
@@ -334,5 +339,4 @@ extension MXBackgroundStore: MXRoomSummaryStore {
334339
completion([])
335340
}
336341
}
337-
338342
}

MatrixSDK/Contrib/Swift/JSONModels/MXEvent.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public enum MXEventType: Equatable, Hashable {
8686

8787
case beaconInfo
8888
case beacon
89+
case roomRetention
8990

9091
case custom(String)
9192

@@ -141,6 +142,7 @@ public enum MXEventType: Equatable, Hashable {
141142
case .taggedEvents: return kMXEventTypeStringTaggedEvents
142143
case .spaceChild: return kMXEventTypeStringSpaceChild
143144
case .spaceOrder: return kMXEventTypeStringSpaceOrderMSC3230
145+
case .roomRetention: return kMXEventTypeStringRoomRetention
144146

145147
case .pollStart: return kMXEventTypeStringPollStartMSC3381
146148
case .pollResponse: return kMXEventTypeStringPollResponseMSC3381
@@ -157,7 +159,7 @@ public enum MXEventType: Equatable, Hashable {
157159
}
158160

159161
public init(identifier: String) {
160-
let events: [MXEventType] = [.roomName, .roomTopic, .roomAvatar, .roomMember, .roomCreate, .roomJoinRules, .roomPowerLevels, .roomAliases, .roomCanonicalAlias, .roomEncrypted, .roomEncryption, .roomGuestAccess, .roomHistoryVisibility, .roomKey, .roomForwardedKey, .roomKeyRequest, .roomMessage, .roomMessageFeedback, .roomRedaction, .roomThirdPartyInvite, .roomTag, .presence, .typing, .callInvite, .callCandidates, .callAnswer, .callSelectAnswer, .callHangup, .callReject, .callNegotiate, .callReplaces, .callRejectReplacement, .callAssertedIdentity, .callAssertedIdentityUnstable, .reaction, .receipt, .roomTombStone, .keyVerificationStart, .keyVerificationAccept, .keyVerificationKey, .keyVerificationMac, .keyVerificationCancel, .keyVerificationDone, .secretRequest, .secretSend, .secretStorageDefaultKey, .taggedEvents, .spaceChild, .spaceOrder, .pollStart, .pollResponse, .pollEnd, .beaconInfo, .beacon]
162+
let events: [MXEventType] = [.roomName, .roomTopic, .roomAvatar, .roomMember, .roomCreate, .roomJoinRules, .roomPowerLevels, .roomAliases, .roomCanonicalAlias, .roomEncrypted, .roomEncryption, .roomGuestAccess, .roomHistoryVisibility, .roomKey, .roomForwardedKey, .roomKeyRequest, .roomMessage, .roomMessageFeedback, .roomRedaction, .roomThirdPartyInvite, .roomTag, .presence, .typing, .callInvite, .callCandidates, .callAnswer, .callSelectAnswer, .callHangup, .callReject, .callNegotiate, .callReplaces, .callRejectReplacement, .callAssertedIdentity, .callAssertedIdentityUnstable, .reaction, .receipt, .roomTombStone, .keyVerificationStart, .keyVerificationAccept, .keyVerificationKey, .keyVerificationMac, .keyVerificationCancel, .keyVerificationDone, .secretRequest, .secretSend, .secretStorageDefaultKey, .taggedEvents, .spaceChild, .spaceOrder, .pollStart, .pollResponse, .pollEnd, .beaconInfo, .beacon, .roomRetention]
161163

162164
if let type = events.first(where: { $0.identifier == identifier }) {
163165
self = type

MatrixSDK/Data/Store/MXMemoryStore/MXMemoryRoomStore.h

+10
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

+27
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

+6
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

+11
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

+12-1
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,18 @@
426426
*/
427427
- (void)storeOutgoingMessageForRoom:(nonnull NSString*)roomId outgoingMessage:(nonnull MXEvent*)outgoingMessage;
428428

429+
/**
430+
Remove all the messages sent before a specific timestamp in a room.
431+
The state events are not removed during this operation. We keep them in the timeline.
432+
This operation doesn't change the pagination token, and the flag indicating that the SDK has reached the end of pagination.
433+
434+
@param limitTs the timestamp from which the messages are kept.
435+
@param roomId the id of the room.
436+
437+
@return YES if at least one event has been removed.
438+
*/
439+
- (BOOL)removeAllMessagesSentBefore:(uint64_t)limitTs inRoom:(nonnull NSString *)roomId;
440+
429441
/**
430442
Remove all outgoing messages from a room.
431443
@@ -593,5 +605,4 @@
593605
success:(nonnull void (^)(NSString * _Nullable filterId))success
594606
failure:(nullable void (^)(NSError * _Nullable error))failure;
595607

596-
597608
@end

MatrixSDK/JSONModels/MXEvent.h

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ typedef NS_ENUM(NSInteger, MXEventType)
103103
MXEventTypeSpaceOrder,
104104
MXEventTypeBeaconInfo,
105105
MXEventTypeBeacon,
106+
MXEventTypeRoomRetention,
106107

107108
// The event is a custom event. Refer to its `MXEventTypeString` version
108109
MXEventTypeCustom = 1000
@@ -163,6 +164,7 @@ FOUNDATION_EXPORT NSString *const kMXEventTypeStringSpaceChild;
163164
FOUNDATION_EXPORT NSString *const kMXEventTypeStringSpaceOrder;
164165
FOUNDATION_EXPORT NSString *const kMXEventTypeStringSpaceOrderMSC3230;
165166
FOUNDATION_EXPORT NSString *const kMXEventTypeStringSpaceOrderKey;
167+
FOUNDATION_EXPORT NSString *const kMXEventTypeStringRoomRetention;
166168

167169
// Interactive key verification
168170
FOUNDATION_EXPORT NSString *const kMXEventTypeStringKeyVerificationRequest;

MatrixSDK/JSONModels/MXEvent.m

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
NSString *const kMXMessageTypeLocation = @"m.location";
116116
NSString *const kMXMessageTypeFile = @"m.file";
117117
NSString *const kMXMessageTypeServerNotice = @"m.server_notice";
118+
NSString *const kMXEventTypeStringRoomRetention = @"m.room.retention";
118119
NSString *const kMXMessageTypeKeyVerificationRequest = @"m.key.verification.request";
119120

120121
NSString *const kMXMessageBodyKey = @"body";

MatrixSDK/Utils/MXTools.m

+2-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ + (void)initialize
215215
kMXEventTypeStringBeaconInfoMSC3672 : @(MXEventTypeBeaconInfo),
216216
kMXEventTypeStringBeaconInfo : @(MXEventTypeBeaconInfo),
217217
kMXEventTypeStringBeaconMSC3672 : @(MXEventTypeBeacon),
218-
kMXEventTypeStringBeacon : @(MXEventTypeBeacon)
218+
kMXEventTypeStringBeacon : @(MXEventTypeBeacon),
219+
kMXEventTypeStringRoomRetention: @(MXEventTypeRoomRetention),
219220
};
220221

221222
isEmailAddressRegex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^%@$", kMXToolsRegexStringForEmailAddress]

0 commit comments

Comments
 (0)