3535static NSString *const kMXFileStoreMedaDataFile = @" MXFileStore" ;
3636static NSString *const kMXFileStoreFiltersFile = @" filters" ;
3737static NSString *const kMXFileStoreUsersFolder = @" users" ;
38+ static NSString *const kMXFileStoreMultiroomFolder = @" multiroom" ;
3839static NSString *const kMXFileStoreGroupsFolder = @" groups" ;
3940static NSString *const kMXFileStoreBackupFolder = @" backup" ;
4041
@@ -71,6 +72,8 @@ @interface MXFileStore ()
7172 NSMutableArray *roomsToCommitForDeletion;
7273
7374 NSMutableDictionary *usersToCommit;
75+
76+ NSDictionary *multiroomToCommit;
7477
7578 NSMutableDictionary *groupsToCommit;
7679 NSMutableArray *groupsToCommitForDeletion;
@@ -86,6 +89,9 @@ @interface MXFileStore ()
8689
8790 // The path of the rooms folder
8891 NSString *storeUsersPath;
92+
93+ // The path of the multiroom folder
94+ NSString *storeMultiroomPath;
8995
9096 // The path of the groups folder
9197 NSString *storeGroupsPath;
@@ -150,6 +156,7 @@ - (instancetype)init;
150156 roomsToCommitForReceipts = [NSMutableArray array ];
151157 roomsToCommitForDeletion = [NSMutableArray array ];
152158 usersToCommit = [NSMutableDictionary dictionary ];
159+ multiroomToCommit = [NSDictionary dictionary ];
153160 groupsToCommit = [NSMutableDictionary dictionary ];
154161 groupsToCommitForDeletion = [NSMutableArray array ];
155162 preloadedRoomsStates = [NSMutableDictionary dictionary ];
@@ -260,6 +267,7 @@ - (void)openWithCredentials:(MXCredentials*)someCredentials onComplete:(void (^)
260267 [self preloadRoomReceipts ];
261268 }
262269 [self loadUsers ];
270+ [self loadMultiroom ];
263271 [self loadGroups ];
264272 [self loadUnreadRooms ];
265273 taskProfile.units = self.roomSummaryStore .countOfRooms ;
@@ -412,6 +420,7 @@ - (void)deleteAllData
412420 [[NSFileManager defaultManager ] createDirectoryExcludedFromBackupAtPath: storePath error: nil ];
413421 [[NSFileManager defaultManager ] createDirectoryExcludedFromBackupAtPath: storeRoomsPath error: nil ];
414422 [[NSFileManager defaultManager ] createDirectoryExcludedFromBackupAtPath: storeUsersPath error: nil ];
423+ [[NSFileManager defaultManager ] createDirectoryExcludedFromBackupAtPath: storeMultiroomPath error: nil ];
415424 [[NSFileManager defaultManager ] createDirectoryExcludedFromBackupAtPath: storeGroupsPath error: nil ];
416425
417426 [roomSummaryStore removeAllSummaries ];
@@ -620,6 +629,13 @@ - (void)storeUser:(MXUser *)user
620629 usersToCommit[user.userId] = user;
621630}
622631
632+ - (void )storeLocations : (nonnull NSDictionary <NSString*, MXMultiroomSync*>*)newLocations {
633+
634+ [super storeLocations: newLocations];
635+
636+ multiroomToCommit = newLocations;
637+ }
638+
623639- (void )storeGroup : (MXGroup *)group
624640{
625641 [super storeGroup: group];
@@ -840,6 +856,7 @@ - (void)saveDataToFiles
840856 [self saveRoomsAccountData ];
841857 [self saveReceipts ];
842858 [self saveUsers ];
859+ [self saveMultiroom ];
843860 [self saveGroupsDeletion ];
844861 [self saveGroups ];
845862 [self saveUnreadRooms ];
@@ -1123,6 +1140,7 @@ - (void)setUpStoragePaths
11231140 storePath = [[cachePath stringByAppendingPathComponent: kMXFileStoreFolder ] stringByAppendingPathComponent: credentials.userId];
11241141 storeRoomsPath = [storePath stringByAppendingPathComponent: kMXFileStoreRoomsFolder ];
11251142 storeUsersPath = [storePath stringByAppendingPathComponent: kMXFileStoreUsersFolder ];
1143+ storeMultiroomPath = [storePath stringByAppendingPathComponent: kMXFileStoreMultiroomFolder ];
11261144 storeGroupsPath = [storePath stringByAppendingPathComponent: kMXFileStoreGroupsFolder ];
11271145
11281146 storeBackupPath = [storePath stringByAppendingPathComponent: kMXFileStoreBackupFolder ];
@@ -1257,6 +1275,31 @@ - (NSString*)usersFileForUser:(NSString*)userId forBackup:(BOOL)backup
12571275 }
12581276}
12591277
1278+ - (NSString *)multiroomFileForGroup : (NSString *)multiroomId forBackup : (BOOL )backup
1279+ {
1280+ if (!backup)
1281+ {
1282+ return [storeMultiroomPath stringByAppendingPathComponent: multiroomId];
1283+ }
1284+ else
1285+ {
1286+ if (backupEventStreamToken)
1287+ {
1288+ NSString *multiroomBackupFolder = [[storeBackupPath stringByAppendingPathComponent: backupEventStreamToken] stringByAppendingPathComponent: kMXFileStoreMultiroomFolder ];
1289+ if (![NSFileManager .defaultManager fileExistsAtPath: multiroomBackupFolder])
1290+ {
1291+ [[NSFileManager defaultManager ] createDirectoryExcludedFromBackupAtPath: multiroomBackupFolder error: nil ];
1292+ }
1293+
1294+ return [multiroomBackupFolder stringByAppendingPathComponent: multiroomId];
1295+ }
1296+ else
1297+ {
1298+ return nil ;
1299+ }
1300+ }
1301+ }
1302+
12601303- (NSString *)groupFileForGroup : (NSString *)groupId forBackup : (BOOL )backup
12611304{
12621305 if (!backup)
@@ -2015,6 +2058,88 @@ - (void)saveUsers
20152058 }
20162059}
20172060
2061+ #pragma mark - Multiroom locations
2062+
2063+ /* *
2064+ Preload all multiroom locations.
2065+
2066+ This operation must be called on the `dispatchQueue` thread to avoid blocking the main thread.
2067+ */
2068+ - (void )loadMultiroom
2069+ {
2070+ NSDate *startDate = [NSDate date ];
2071+
2072+ // Load all multiroom files
2073+ NSArray *multiroomIds = [[NSFileManager defaultManager ] contentsOfDirectoryAtPath: storeMultiroomPath error: nil ];
2074+
2075+ for (NSString *multiroomId in multiroomIds)
2076+ {
2077+ NSString *multiroomFile = [storeMultiroomPath stringByAppendingPathComponent: multiroomId];
2078+
2079+ // Load stored locations
2080+
2081+ @try
2082+ {
2083+ MXMultiroomSync *multiroom = [NSKeyedUnarchiver unarchiveObjectWithFile: multiroomFile];
2084+ if (multiroom) {
2085+ // Append them
2086+ [locations setObject: multiroom forKey: multiroomId];
2087+ }
2088+ }
2089+ @catch (NSException *exception)
2090+ {
2091+ MXLogDebug (@" [MXFileStore] Warning: MXFileRoomStore file for multiroom %@ has been corrupted" , locations);
2092+ }
2093+ }
2094+
2095+ MXLogDebug (@" [MXFileStore] Loaded %tu MXMultiroomSync in %.0f ms" , locations.count , [[NSDate date ] timeIntervalSinceDate: startDate] * 1000 );
2096+ }
2097+
2098+ - (void )saveMultiroom
2099+ {
2100+ // Save only in case of change
2101+ if (multiroomToCommit.count )
2102+ {
2103+
2104+ // Check if folder exists
2105+ if (![NSFileManager .defaultManager fileExistsAtPath: storeMultiroomPath])
2106+ {
2107+ [[NSFileManager defaultManager ] createDirectoryExcludedFromBackupAtPath: storeMultiroomPath error: nil ];
2108+ }
2109+
2110+ // Take a snapshot of groups to store them on the other thread
2111+ NSMutableDictionary *theMultiroomToCommit = [[NSMutableDictionary alloc ] initWithDictionary: multiroomToCommit copyItems: YES ];
2112+ multiroomToCommit = [NSDictionary dictionary ];
2113+ #if DEBUG
2114+ MXLogDebug (@" [MXFileStore commit] queuing saveMultiroom" );
2115+ #endif
2116+ dispatch_async (dispatchQueue, ^(void ){
2117+
2118+ #if DEBUG
2119+ NSDate *startDate = [NSDate date ];
2120+ #endif
2121+ for (NSString *multiroomId in theMultiroomToCommit)
2122+ {
2123+ MXMultiroomSync *multiroom = theMultiroomToCommit[multiroomId];
2124+
2125+ NSString *file = [self multiroomFileForGroup: multiroomId forBackup: NO ];
2126+
2127+ // Backup the file for this location
2128+ NSString *backupFile = [self multiroomFileForGroup: multiroomId forBackup: YES ];
2129+ if (backupFile && [[NSFileManager defaultManager ] fileExistsAtPath: file])
2130+ {
2131+ [[NSFileManager defaultManager ] moveItemAtPath: file toPath: backupFile error: nil ];
2132+ }
2133+
2134+ [NSKeyedArchiver archiveRootObject: multiroom toFile: file];
2135+ }
2136+ #if DEBUG
2137+ MXLogDebug (@" [MXFileStore] saveMultiroom in %.0f ms" , [[NSDate date ] timeIntervalSinceDate: startDate] * 1000 );
2138+ #endif
2139+ });
2140+ }
2141+ }
2142+
20182143#pragma mark - Matrix groups
20192144/* *
20202145 Preload all groups.
0 commit comments