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

Unread rooms: Move the storage file to a better location #1730

Merged
merged 1 commit into from
Mar 3, 2023
Merged
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
26 changes: 22 additions & 4 deletions MatrixSDK/Data/Store/MXFileStore/MXFileStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#import "MatrixSDKSwiftHeader.h"
#import "MXFileRoomSummaryStore.h"

static NSUInteger const kMXFileVersion = 82;
static NSUInteger const kMXFileVersion = 82; // Check getUnreadRoomFromStore if you update this value. Delete this comment after

static NSString *const kMXFileStoreFolder = @"MXFileStore";
static NSString *const kMXFileStoreMedaDataFile = @"MXFileStore";
Expand Down Expand Up @@ -1004,7 +1004,7 @@ -(void)saveUnreadRooms
{

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

Expand All @@ -1015,8 +1015,19 @@ -(void)loadUnreadRooms

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

// TODO: Remove this migration code on the next kMXFileVersion update (>82)
// The `unreadFileForRoomsForBackup()` method should be removed.
// Likewise for the associated test `testUnreadRoomsFileMigration()`
NSString *oldUnreadRoomsFile = [self unreadFileForRoomsForBackup:NO];
if ([[NSFileManager defaultManager] fileExistsAtPath:oldUnreadRoomsFile])
{
MXLogDebug(@"[MXFileStore] getUnreadRoomFromStore: Migrate the unread rooms file")
[[NSFileManager defaultManager] moveItemAtPath:oldUnreadRoomsFile toPath:unreadRoomsFile error:nil];
}

NSArray *result = [self loadUnreadRoomsStoreFromFileAt:unreadRoomsFile];
return result;
}

Expand Down Expand Up @@ -1187,6 +1198,13 @@ - (NSString*)readReceiptsFileForRoom:(NSString*)roomId forBackup:(BOOL)backup
{
return [[self folderForRoom:roomId forBackup:backup] stringByAppendingPathComponent:kMXFileStoreRoomReadReceiptsFile];
}

- (NSString*)unreadRoomsFile
{
return [storePath stringByAppendingPathComponent:kMXFileStoreRoomUnreadRoomsFile];
}

// TODO: To delete when kMXFileVersion > 82
- (NSString*)unreadFileForRoomsForBackup:(BOOL)backup
{
return [[self folderForRoomsforBackup:backup] stringByAppendingPathComponent:kMXFileStoreRoomUnreadRoomsFile];
Expand Down
47 changes: 47 additions & 0 deletions MatrixSDKTests/MXRoomTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"

@interface MXFileStore ()
- (NSString*)unreadRoomsFile;
- (NSString*)unreadFileForRoomsForBackup:(BOOL)backup;
@end


@interface MXRoomTests : XCTestCase
{
MatrixSDKTestsData *matrixSDKTestsData;
Expand Down Expand Up @@ -975,6 +981,47 @@ - (void)testMarkUnreadWithMessage
}];
}

// Test migration of the MXFileStore unread rooms file that was at a bad location.
// - Have a Bob session with an unread room
// - Close that session
// - Reopen a Bob session (it simulates an app restart)
// -> The room should be still unread, meaning the migration went as expected
// TODO: Delete this test when removing the unreadFileForRoomsForBackup() method
- (void)testUnreadRoomsFileMigration
{
// - Have a Bob session with an unread room
MXFileStore *store = [MXFileStore new];
[matrixSDKTestsData doMXSessionTestWithBobAndARoom:self andStore:store readyToTest:^(MXSession *bobSession, MXRoom *room, XCTestExpectation *expectation) {
[room setUnread];

// - Close that session
NSString *roomId = room.roomId;
MXRestClient *bobRestClient = bobSession.matrixRestClient;
NSString *wrongFile = [store unreadFileForRoomsForBackup:NO];
NSString *goodFile = [store unreadRoomsFile];
[bobSession close];

// - Move the unread rooms file to the previous wrong location
[[NSFileManager defaultManager] moveItemAtPath:goodFile toPath:wrongFile error:nil];

// - Reopen a Bob session (it simulates an app restart)
MXSession *bobSession2 = [[MXSession alloc] initWithMatrixRestClient:bobRestClient];
[self->matrixSDKTestsData retain:bobSession2];
[bobSession2 setStore:[[MXFileStore alloc] init] success:^{

// -> The room should be still unread, meaning the migration went as expected
MXRoom *room = [bobSession2 roomWithRoomId:roomId];
XCTAssert([room isMarkedAsUnread], @"the room should still be marked as unread");

[expectation fulfill];

} failure:^(NSError *error) {
XCTFail(@"Cannot set up intial test conditions - error: %@", error);
[expectation fulfill];
}];
}];
}

@end

#pragma clang diagnostic pop
1 change: 1 addition & 0 deletions changelog.d/pr-1730.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unread rooms: Move the storage file to a better location.