Skip to content

Commit

Permalink
added functions to listen to
Browse files Browse the repository at this point in the history
the event and redact the room accordingly
  • Loading branch information
Velin92 committed Jun 14, 2024
1 parent 25c77b6 commit ddd97be
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 10 deletions.
11 changes: 6 additions & 5 deletions Riot/Categories/MXRoomSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ extension Notification.Name {
static let roomSummaryDidRemoveExpiredDataFromStore = Notification.Name(MXRoomSummary.roomSummaryDidRemoveExpiredDataFromStore)
}

@objc extension MXRoomSummary {
static let roomSummaryDidRemoveExpiredDataFromStore = "roomSummaryDidRemoveExpiredDataFromStore"
extension MXRoomSummary {
@objc static let roomSummaryDidRemoveExpiredDataFromStore = "roomSummaryDidRemoveExpiredDataFromStore"
@objc static let roomRetentionStateEventType = "m.room.retention"

private enum Constants {
static let roomRetentionInDaysKey = "roomRetentionInDays"
Expand All @@ -36,7 +37,7 @@ extension Notification.Name {
}

/// Get the timestamp below which the received messages must be removed from the store, and the display
func mininumTimestamp() -> UInt64 {
@objc func minimumTimestamp() -> UInt64 {
let periodInMs = Tools.durationInMs(fromDays: self.roomRetentionPeriodInDays())
let currentTs = (UInt64)(Date().timeIntervalSince1970 * 1000)
return (currentTs - periodInMs)
Expand All @@ -47,8 +48,8 @@ extension Notification.Name {
/// This operation does not commit the potential change. We let the caller trigger the commit when this is the more suitable.
///
/// Provide a boolean telling whether some data have been removed.
func removeExpiredRoomContentsFromStore() -> Bool {
let ret = self.mxSession.store.removeAllMessagesSent(before: self.mininumTimestamp(), inRoom: roomId)
@objc func removeExpiredRoomContentsFromStore() -> Bool {
let ret = self.mxSession.store.removeAllMessagesSent(before: self.minimumTimestamp(), inRoom: roomId)

Check failure on line 52 in Riot/Categories/MXRoomSummary.swift

View workflow job for this annotation

GitHub Actions / Tests

value of type 'any MXStore' has no member 'removeAllMessagesSent'

Check failure on line 52 in Riot/Categories/MXRoomSummary.swift

View workflow job for this annotation

GitHub Actions / Tests

value of type 'any MXStore' has no member 'removeAllMessagesSent'
if ret {
NotificationCenter.default.post(name: .roomSummaryDidRemoveExpiredDataFromStore, object: self)
}
Expand Down
7 changes: 2 additions & 5 deletions Riot/Categories/MXSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ extension MXSession {
matrixItemId: userId,
displayName: user?.displayname)
}
}

@objc extension MXSession {


/// Clean the storage of a session by removing the expired contents.
func removeExpiredMessages() {
@objc func removeExpiredMessages() {
var hasStoreChanged = false
for room in self.rooms {
hasStoreChanged = hasStoreChanged || room.summary.removeExpiredRoomContentsFromStore()
Expand Down
3 changes: 3 additions & 0 deletions Riot/Modules/Application/LegacyAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,9 @@ - (void)initMatrixSessions
[self registerNewRequestNotificationForSession:mxSession];

[self.pushNotificationService checkPushKitPushersInSession:mxSession];

// Clean the storage by removing expired data
[mxSession removeExpiredMessages];
}
else if (mxSession.state == MXSessionStateRunning)
{
Expand Down
106 changes: 106 additions & 0 deletions Riot/Modules/Room/DataSources/RoomDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ @interface RoomDataSource() <RoomReactionsViewModelDelegate, URLPreviewViewDeleg
{
// Observe kThemeServiceDidChangeThemeNotification to handle user interface theme change.
id kThemeServiceDidChangeThemeNotificationObserver;

// The listener to the room retention changes.
id retentionListener;
}

// Observe key verification request changes
Expand Down Expand Up @@ -167,6 +170,31 @@ - (void)updateEventFormatter
self.eventFormatter.eventTypesFilterForMessages = [MXKAppSettings standardAppSettings].eventsFilterForMessages;
}

- (void)setDelegate:(id<MXKDataSourceDelegate>)delegate
{
[self unregisterRoomSummaryDidRemoveExpiredDataFromStoreNotifications];
[self removeRoomRetentionEventListener];

if (delegate && self.isLive)
{
if (self.room)
{
// Remove the potential expired messages from the store
if ([self.room.summary removeExpiredRoomContentsFromStore])
{
[self.mxSession.store commit];
}
[self addRoomRetentionEventListener];
}

// Observe room history flush (expired content data)
[self registerRoomSummaryDidRemoveExpiredDataFromStoreNotifications];
[self roomSummaryDidRemoveExpiredDataFromStore];
}

[super setDelegate:delegate];
}

- (void)destroy
{
if (kThemeServiceDidChangeThemeNotificationObserver)
Expand Down Expand Up @@ -197,6 +225,9 @@ - (void)destroy
[self.mxSession.aggregations.beaconAggregations removeListener:self.beaconInfoSummaryDeletionListener];
}

[self unregisterRoomSummaryDidRemoveExpiredDataFromStoreNotifications];
[self removeRoomRetentionEventListener];

[super destroy];
}

Expand Down Expand Up @@ -1242,4 +1273,79 @@ - (void)updateCurrentUserLocationSharingStatus
}
}

#pragma mark - roomSummaryDidRemoveExpiredDataFromStore notifications

- (void)registerRoomSummaryDidRemoveExpiredDataFromStoreNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(roomSummaryDidRemoveExpiredDataFromStore:) name:MXRoomSummary.roomSummaryDidRemoveExpiredDataFromStore object:nil];
}

- (void)unregisterRoomSummaryDidRemoveExpiredDataFromStoreNotifications
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MXRoomSummary.roomSummaryDidRemoveExpiredDataFromStore object:nil];
}

- (void)roomSummaryDidRemoveExpiredDataFromStore:(NSNotification*)notification
{
MXRoomSummary *roomSummary = notification.object;
if (self.mxSession == roomSummary.mxSession && [self.roomId isEqualToString:roomSummary.roomId])
{
[self roomSummaryDidRemoveExpiredDataFromStore];
}
}

- (void)roomSummaryDidRemoveExpiredDataFromStore
{
// Check whether the first cell data refers to an expired event (this may be a state event
MXEvent *firstMessageEvent;
for (id<MXKRoomBubbleCellDataStoring> cellData in bubbles)
{
for (MXEvent *event in cellData.events)
{
if (!event.isState) {
firstMessageEvent = event;
break;
}
}

if (firstMessageEvent)
{
break;
}
}

if (firstMessageEvent && firstMessageEvent.originServerTs < self.room.summary.minimumTimestamp)
{
[self reload];
}
}

#pragma mark - room retention event listener

- (void)addRoomRetentionEventListener
{
// Register a listener to handle the room retention in live timelines
retentionListener = [self.timeline listenToEventsOfTypes:@[MXRoomSummary.roomRetentionStateEventType] onEvent:^(MXEvent *redactionEvent, MXTimelineDirection direction, MXRoomState *roomState) {

// Consider only live events
if (direction == MXTimelineDirectionForwards)
{
// Remove the potential expired messages from the store
if ([self.room.summary removeExpiredRoomContentsFromStore])
{
[self.mxSession.store commit];
}
}
}];
}

- (void)removeRoomRetentionEventListener
{
if (retentionListener)
{
[self.timeline removeListener:retentionListener];
retentionListener = nil;
}
}

@end

0 comments on commit ddd97be

Please sign in to comment.