Skip to content

Commit

Permalink
added functions to remove messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 committed Jun 14, 2024
1 parent 9e24aff commit 80147d4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Riot/Categories/MXRoomSummary.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright 2024 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

extension Notification.Name {
static let roomSummaryDidRemoveExpiredDataFromStore = Notification.Name(MXRoomSummary.roomSummaryDidRemoveExpiredDataFromStore)
}

@objc extension MXRoomSummary {
static let roomSummaryDidRemoveExpiredDataFromStore = "roomSummaryDidRemoveExpiredDataFromStore"

private enum Constants {
static let roomRetentionInDaysKey = "roomRetentionInDays"
}
/// Get the room messages retention period in days
func roomRetentionPeriodInDays() -> uint {
if let period = self.others[Constants.roomRetentionInDaysKey] as? uint {
return period
} else {
return 365
}
}

/// Get the timestamp below which the received messages must be removed from the store, and the display
func mininumTimestamp() -> UInt64 {
let periodInMs = Tools.durationInMs(fromDays: self.roomRetentionPeriodInDays())
let currentTs = (UInt64)(Date().timeIntervalSince1970 * 1000)
return (currentTs - periodInMs)
}

/// Remove the expired messages from the store.
/// If some data are removed, this operation posts the notification: roomSummaryDidRemoveExpiredDataFromStore.
/// 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)

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

View workflow job for this annotation

GitHub Actions / Build

value of type 'any MXStore' has no member 'removeAllMessagesSent'
if ret {
NotificationCenter.default.post(name: .roomSummaryDidRemoveExpiredDataFromStore, object: self)
}
return ret
}
}
15 changes: 15 additions & 0 deletions Riot/Categories/MXSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ extension MXSession {
displayName: user?.displayname)
}
}

@objc extension MXSession {

/// Clean the storage of a session by removing the expired contents.
func removeExpiredMessages() {
var hasStoreChanged = false
for room in self.rooms {
hasStoreChanged = hasStoreChanged || room.summary.removeExpiredRoomContentsFromStore()
}

if hasStoreChanged {
self.store.commit?()
}
}
}
13 changes: 13 additions & 0 deletions Riot/Utils/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,17 @@
*/
+ (NSURL*)fixURLWithSeveralHashKeys:(NSURL*)url;

#pragma mark - Time utilities

/**
* Convert a number of days to a duration in ms.
*/
+ (uint64_t)durationInMsFromDays:(uint)days;

/**
* Convert a duration in ms to a number of days.
*/
+ (uint)numberOfDaysFromDurationInMs:(uint64_t)duration;


@end
12 changes: 12 additions & 0 deletions Riot/Utils/Tools.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,16 @@ + (NSURL *)fixURLWithSeveralHashKeys:(NSURL *)url
return fixedURL;
}

#pragma mark - Time utilities

+ (uint64_t)durationInMsFromDays:(uint)days
{
return days * (uint64_t)(86400000);
}

+ (uint)numberOfDaysFromDurationInMs:(uint64_t)duration
{
return (uint)(duration / 86400000);
}

@end

0 comments on commit 80147d4

Please sign in to comment.