Skip to content

Commit

Permalink
Merge pull request #1696 from matrix-org/flescio/7253-Add_mar_kas_unr…
Browse files Browse the repository at this point in the history
…ead_option_for_rooms

Flescio/7253 add mar kas unread option for rooms
  • Loading branch information
Flescio authored Jan 31, 2023
2 parents 9e36625 + 3142e9a commit b1b6be3
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 3 deletions.
11 changes: 11 additions & 0 deletions MatrixSDK/Background/MXBackgroundStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,17 @@ class MXBackgroundStore: NSObject, MXStore {
return []
}

func setUnreadForRoom(_ roomId: String) {
// no-op
}

func resetUnread(forRoom roomId: String) {
// no-op
}

func isRoomMarked(asUnread roomId: String) -> Bool {
return false
}
}

// MARK: - MXRoomSummaryStore
Expand Down
17 changes: 17 additions & 0 deletions MatrixSDK/Data/MXRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,23 @@ Remove a tag applied on an event of the room
*/
- (void)acknowledgeEvent:(MXEvent*)event andUpdateReadMarker:(BOOL)updateReadMarker;

/**
It will set the entire room as unread.
This is only local since it's not possible to remove from the server the read events.
*/
-(void)setUnread;

/**
It will unset the entire room from unread list.
This is only a local list.
*/
-(void)resetUnread;

/**
it will return if the room is marked unread by the user
*/
@property (nonatomic, readonly) BOOL isMarkedAsUnread;

/**
Move the read marker to the latest event.
Update the read receipt by acknowledging the latest event of type defined in MXSession.acknowledgableEventTypes.
Expand Down
24 changes: 24 additions & 0 deletions MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -3348,8 +3348,32 @@ - (void)acknowledgeEvent:(MXEvent*)event andUpdateReadMarker:(BOOL)updateReadMar
}
}

- (void)setUnread
{
[mxSession.store setUnreadForRoom:self.roomId];
if ([mxSession.store respondsToSelector:@selector(commit)])
{
[mxSession.store commit];
}
}

- (void)resetUnread
{
[mxSession.store resetUnreadForRoom:self.roomId];
if ([mxSession.store respondsToSelector:@selector(commit)])
{
[mxSession.store commit];
}
}

- (BOOL)isMarkedAsUnread
{
return [mxSession.store isRoomMarkedAsUnread:self.roomId];
}

- (void)markAllAsRead
{
[self resetUnread];
NSString *readMarkerEventId = nil;
MXReceiptData *updatedReceiptData = nil;

Expand Down
10 changes: 10 additions & 0 deletions MatrixSDK/Data/MXRoomSummary.m
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ - (void)updateWith:(id<MXRoomSummaryProtocol>)summary
{
return;
}
// if there is a new LastMessage then it's better to unmark the room as unread
if (nil != _lastMessage && ![_lastMessage.eventId isEqualToString:summary.lastMessage.eventId])
{
[self.room resetUnread];
}

_roomTypeString = summary.roomTypeString;
_roomType = summary.roomType;
Expand Down Expand Up @@ -299,6 +304,11 @@ - (void)resetRoomStateData

- (void)updateLastMessage:(MXRoomLastMessage *)message
{
// if there is a new LastMessage then it's better to unmark the room as unread
if (![_lastMessage.eventId isEqualToString:message.eventId])
{
[self.room resetUnread];
}
_lastMessage = message;
}

Expand Down
72 changes: 70 additions & 2 deletions MatrixSDK/Data/Store/MXFileStore/MXFileStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
static NSString *const kMXFileStoreRoomStateFile = @"state";
static NSString *const kMXFileStoreRoomAccountDataFile = @"accountData";
static NSString *const kMXFileStoreRoomReadReceiptsFile = @"readReceipts";
static NSString *const kMXFileStoreRoomUnreadRoomsFile = @"unreadRooms";
static NSString *const kMXFileStoreRoomThreadedReadReceiptsFile = @"threadedReadReceipts";

static NSUInteger preloadOptions;
Expand Down Expand Up @@ -260,7 +261,7 @@ - (void)openWithCredentials:(MXCredentials*)someCredentials onComplete:(void (^)
}
[self loadUsers];
[self loadGroups];

[self loadUnreadRooms];
taskProfile.units = self.roomSummaryStore.countOfRooms;
[MXSDKOptions.sharedInstance.profiler stopMeasuringTaskWithProfile:taskProfile];
MXLogDebug(@"[MXFileStore] Data loaded from files in %.0fms", taskProfile.duration * 1000);
Expand Down Expand Up @@ -841,6 +842,7 @@ - (void)saveDataToFiles
[self saveUsers];
[self saveGroupsDeletion];
[self saveGroups];
[self saveUnreadRooms];
[self saveFilters];
[self saveMetaData];
}
Expand Down Expand Up @@ -998,6 +1000,26 @@ - (RoomThreadedReceiptsStore*)getOrCreateRoomThreadedReceiptsStore:(NSString*)ro
return threadedStore;
}

-(void)saveUnreadRooms
{

NSArray<NSString*>* rooms = [roomUnreaded allObjects];
NSString *roomsFile = [self unreadFileForRoomsForBackup:NO];
[self saveObject:rooms toFile:roomsFile];
}

-(void)loadUnreadRooms
{
roomUnreaded = [NSMutableSet setWithArray:[self getUnreadRoomFromStore]];
}

- (NSArray*)getUnreadRoomFromStore
{
NSString *roomsFile = [self unreadFileForRoomsForBackup:NO];
NSArray *result = [self loadUnreadRoomsStoreFromFileAt:roomsFile];
return result;
}

- (NSMutableDictionary*)loadReceiptsStoreFromFileAt:(NSString*)filePath forRoomWithId:(NSString*)roomId
{
NSMutableDictionary *store;
Expand Down Expand Up @@ -1032,7 +1054,37 @@ - (NSMutableDictionary*)loadReceiptsStoreFromFileAt:(NSString*)filePath forRoomW

return store;
}

- (NSArray*)loadUnreadRoomsStoreFromFileAt:(NSString*)filePath
{
NSArray *store = [NSArray new];

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
@try
{
NSDate *startDate = [NSDate date];
NSData *dataOfFile = [NSData dataWithContentsOfFile:filePath];
NSError* error;
NSArray *result = [NSKeyedUnarchiver unarchivedArrayOfObjectsOfClass:[NSString class] fromData:dataOfFile error:&error];
if (nil != result) {
store = result;
}
if ([NSThread isMainThread])
{
MXLogWarning(@"[MXFileStore] Loaded unread rooms: %.0fms, in main thread", [[NSDate date] timeIntervalSinceDate:startDate] * 1000);
}
}
@catch (NSException *exception)
{
NSDictionary *logDetails = @{
@"exception": exception
};
MXLogErrorDetails(@"[MXFileStore] Warning: loadReceipts file for room as been corrupted", logDetails);
}
}

return store;
}
#pragma mark - File paths
- (void)setUpStoragePaths
{
Expand Down Expand Up @@ -1077,6 +1129,18 @@ - (NSString*)folderForRoom:(NSString*)roomId forBackup:(BOOL)backup
}
}

- (NSString*)folderForRoomsforBackup:(BOOL)backup
{
if (!backup)
{
return storeRoomsPath;
}
else
{
return self.storeBackupRoomsPath;
}
}

- (NSString*)storeBackupRoomsPath
{
NSString *storeBackupRoomsPath;
Expand Down Expand Up @@ -1123,6 +1187,10 @@ - (NSString*)readReceiptsFileForRoom:(NSString*)roomId forBackup:(BOOL)backup
{
return [[self folderForRoom:roomId forBackup:backup] stringByAppendingPathComponent:kMXFileStoreRoomReadReceiptsFile];
}
- (NSString*)unreadFileForRoomsForBackup:(BOOL)backup
{
return [[self folderForRoomsforBackup:backup] stringByAppendingPathComponent:kMXFileStoreRoomUnreadRoomsFile];
}

- (NSString*)threadedReadReceiptsFileForRoom:(NSString*)roomId forBackup:(BOOL)backup
{
Expand Down
5 changes: 4 additions & 1 deletion MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ typedef NSMutableDictionary<NSString*, RoomReceiptsStore*> RoomThreadedReceiptsS
// The keys are room ids.
NSMutableDictionary <NSString*, RoomThreadedReceiptsStore*> *roomThreadedReceiptsStores;

// Set of unreaded rooms
// The elements are room ids.
NSMutableSet <NSString*> *roomUnreaded;

// Matrix filters
// FilterId -> Filter JSON string
NSMutableDictionary<NSString*, NSString*> *filters;
Expand Down Expand Up @@ -93,5 +97,4 @@ typedef NSMutableDictionary<NSString*, RoomReceiptsStore*> RoomThreadedReceiptsS
@return receipts dictionary by thread id.
*/
- (RoomReceiptsStore*)getOrCreateReceiptsStoreForRoomWithId:(NSString*)roomId threadId:(NSString* _Nullable)threadId;

@end
16 changes: 16 additions & 0 deletions MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ - (instancetype)init
roomThreadedReceiptsStores = [NSMutableDictionary dictionary];
users = [NSMutableDictionary dictionary];
groups = [NSMutableDictionary dictionary];
roomUnreaded = [[NSMutableSet alloc] init];
roomSummaryStore = [[MXMemoryRoomSummaryStore alloc] init];
maxUploadSize = -1;
areAllIdentityServerTermsAgreed = NO;
Expand Down Expand Up @@ -311,6 +312,21 @@ - (MXReceiptData *)getReceiptInRoom:(NSString *)roomId threadId:(NSString *)thre
return receiptsData;
}

- (void)setUnreadForRoom:(nonnull NSString*)roomId;
{
[roomUnreaded addObject:roomId];
}

- (void)resetUnreadForRoom:(nonnull NSString*)roomId;
{
[roomUnreaded removeObject:roomId];
}

- (BOOL)isRoomMarkedAsUnread:(nonnull NSString*)roomId
{
return [roomUnreaded containsObject:roomId];
}

- (NSUInteger)localUnreadEventCount:(NSString*)roomId threadId:(NSString *)threadId withTypeIn:(NSArray*)types
{
NSArray<MXEvent*> *newEvents = [self newIncomingEventsInRoom:roomId threadId:threadId withTypeIn:types];
Expand Down
13 changes: 13 additions & 0 deletions MatrixSDK/Data/Store/MXNoStore/MXNoStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,19 @@ - (void)storeMaxUploadSize:(NSInteger)maxUploadSize
{
}

- (void)markRoomAsUnread:(nonnull NSString*)roomId;
{
}

- (void)unmarkRoomAsUnread:(nonnull NSString*)roomId;
{
}

- (BOOL)isRoomMarkedAsUnread:(nonnull NSString*)roomId
{
return NO;
}

- (void)close
{
[paginationTokens removeAllObjects];
Expand Down
20 changes: 20 additions & 0 deletions MatrixSDK/Data/Store/MXStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@
*/
- (NSArray<MXEvent*>* _Nonnull)relationsForEvent:(nonnull NSString*)eventId inRoom:(nonnull NSString*)roomId relationType:(nonnull NSString*)relationType;

/**
Set the room as unread, add the room to the unread list
@param roomId the id of the room.
*/
- (void)setUnreadForRoom:(nonnull NSString*)roomId;

/**
Remove the room from unread list
@param roomId the id of the room.
*/
- (void)resetUnreadForRoom:(nonnull NSString*)roomId;

/**
Set the room as unread
@param roomId the id of the room.
*/
- (BOOL)isRoomMarkedAsUnread:(nonnull NSString*)roomId;

#pragma mark - Matrix users
/**
Expand Down
5 changes: 5 additions & 0 deletions MatrixSDK/MXSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,11 @@ typedef void (^MXOnBackgroundSyncFail)(NSError *error);
success:(void (^)(BOOL canEnableE2E))success
failure:(void (^)(NSError *error))failure;

/**
it will return if the room is marked unread by the user
*/
- (BOOL)isRoomMarkedAsUnread:(NSString*)roomId;

#pragma mark - The user's rooms
/**
Check if the user is in a room
Expand Down
5 changes: 5 additions & 0 deletions MatrixSDK/MXSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,11 @@ - (MXHTTPOperation*)canEnableE2EByDefaultInNewRoomWithUsers:(NSArray<NSString*>*
} failure:failure];
}

- (BOOL) isRoomMarkedAsUnread:(NSString*)roomId
{
return [[self roomWithRoomId:roomId] isMarkedAsUnread];
}

- (void)joinPendingRoomInvites
{
NSArray<NSString *> *roomIds = [[self.invitedRooms valueForKey:@"roomId"] copy];
Expand Down
Loading

0 comments on commit b1b6be3

Please sign in to comment.