Skip to content

Commit ded18b3

Browse files
✨Add multiroom to MXStore
1 parent bc0286c commit ded18b3

File tree

10 files changed

+242
-4
lines changed

10 files changed

+242
-4
lines changed

MatrixSDK/Background/MXBackgroundStore.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,14 @@ class MXBackgroundStore: NSObject, MXStore {
186186
func user(withUserId userId: String) -> MXUser? {
187187
return nil
188188
}
189-
189+
190+
func storeLocations(_ newLocations: [String : MXMultiroomSync]) {
191+
}
192+
193+
func locations() -> [String : MXMultiroomSync] {
194+
return [:]
195+
}
196+
190197
func store(_ group: MXGroup) {
191198
}
192199

MatrixSDK/Data/Store/MXFileStore/MXFileStore.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ typedef NS_OPTIONS(NSInteger, MXFileStorePreloadOptions)
7171
L usersGroup #1
7272
L usersGroup #2
7373
L ...
74+
+ multiroom
75+
L userLocation #1
76+
L userLocation #2
77+
L ...
7478
+ groups:
7579
L groupA
7680
L groupB

MatrixSDK/Data/Store/MXFileStore/MXFileStore.m

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
static NSString *const kMXFileStoreMedaDataFile = @"MXFileStore";
3636
static NSString *const kMXFileStoreFiltersFile = @"filters";
3737
static NSString *const kMXFileStoreUsersFolder = @"users";
38+
static NSString *const kMXFileStoreMultiroomFolder = @"multiroom";
3839
static NSString *const kMXFileStoreGroupsFolder = @"groups";
3940
static 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 %.0fms", 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 %.0fms", [[NSDate date] timeIntervalSinceDate:startDate] * 1000);
2138+
#endif
2139+
});
2140+
}
2141+
}
2142+
20182143
#pragma mark - Matrix groups
20192144
/**
20202145
Preload all groups.

MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ typedef NSMutableDictionary<NSString*, RoomReceiptsStore*> RoomThreadedReceiptsS
4242
// All matrix users known by the user
4343
// The keys are user ids.
4444
NSMutableDictionary <NSString*, MXUser*> *users;
45+
46+
// All matrix multiroom locations known by the user
47+
// The keys are user ids.
48+
NSMutableDictionary <NSString*, MXMultiroomSync*> *locations;
4549

4650
// All matrix groups known by the user
4751
// The keys are groups ids.

MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ - (instancetype)init
5151
roomOutgoingMessagesStores = [NSMutableDictionary dictionary];
5252
roomThreadedReceiptsStores = [NSMutableDictionary dictionary];
5353
users = [NSMutableDictionary dictionary];
54+
// locations = [NSMutableDictionary dictionary];
5455
groups = [NSMutableDictionary dictionary];
5556
roomUnreaded = [[NSMutableSet alloc] init];
5657
roomSummaryStore = [[MXMemoryRoomSummaryStore alloc] init];
@@ -446,6 +447,17 @@ - (MXUser *)userWithUserId:(NSString *)userId
446447
return users[userId];
447448
}
448449

450+
#pragma mark - Multiroom locations
451+
452+
- (void)storeLocations:(nonnull NSDictionary<NSString*, MXMultiroomSync*>*)newLocations {
453+
[locations addEntriesFromDictionary: newLocations];
454+
}
455+
456+
- (NSDictionary<NSString*, MXMultiroomSync*>*)locations
457+
{
458+
return locations;
459+
}
460+
449461
#pragma mark - Matrix groups
450462
- (void)storeGroup:(MXGroup *)group
451463
{

MatrixSDK/Data/Store/MXStore.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import "MXRoomAccountData.h"
2727
#import "MXGroup.h"
2828
#import "MXFilterJSONModel.h"
29+
#import "MXMultiroomSync.h"
2930

3031
#import "MXEventsEnumerator.h"
3132
#import "MXRoomSummaryStore.h"
@@ -218,6 +219,19 @@
218219
*/
219220
- (MXUser* _Nullable)userWithUserId:(nonnull NSString*)userId;
220221

222+
#pragma mark - Multiroom locations
223+
/**
224+
Store a multiroom locations.
225+
*/
226+
- (void)storeLocations:(nonnull NSDictionary<NSString*, MXMultiroomSync*>*)newLocations;
227+
228+
/**
229+
Get the list of all stored multiroom locations.
230+
231+
@return an array of MXUser.
232+
*/
233+
- (NSDictionary<NSString*, MXMultiroomSync*>*)locations;
234+
221235
#pragma mark - groups
222236
/**
223237
Store a matrix group.

MatrixSDK/JSONModels/Sync/Multiroom/MXMultiroomSync.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
// Created by Artem Krachulov on 04.04.2023.
66
//
77

8-
#import <MatrixSDK/MatrixSDK.h>
8+
#import <Foundation/Foundation.h>
99

10+
#import "MXJSONModels.h"
11+
//#import "MXEnumConstants.h"
12+
13+
@class MXSession;
1014
@class MXMultiroomSyncLocation;
1115

1216
NS_ASSUME_NONNULL_BEGIN
1317

14-
@interface MXMultiroomSync : MXJSONModel
18+
@interface MXMultiroomSync : MXJSONModel <NSCoding, NSCopying>
1519

1620
/**
1721
The location posted via /_matrix/client/r0/multiroom/connect.multiroom.location request.

MatrixSDK/JSONModels/Sync/Multiroom/MXMultiroomSync.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,38 @@ - (NSDictionary *)JSONDictionary
3838
return JSONDictionary;
3939
}
4040

41+
#pragma mark - NSCoding
42+
43+
- (instancetype)initWithCoder:(NSCoder *)aDecoder
44+
{
45+
self = [super init];
46+
if (self)
47+
{
48+
_location = [aDecoder decodeObjectForKey:@"location"];
49+
_panic = [aDecoder decodeObjectForKey:@"panic"];
50+
}
51+
return self;
52+
}
53+
54+
- (void)encodeWithCoder:(NSCoder *)aCoder
55+
{
56+
if(_location) {
57+
[aCoder encodeObject:_location forKey:@"location"];
58+
}
59+
if (_panic) {
60+
[aCoder encodeObject:_panic forKey:@"panic"];
61+
}
62+
}
63+
64+
#pragma mark - NSCopying
65+
66+
- (id)copyWithZone:(NSZone *)zone
67+
{
68+
MXMultiroomSync *multiroomSync = [[[self class] allocWithZone:zone] init];
69+
70+
multiroomSync->_location = [_location copyWithZone:zone];
71+
multiroomSync->_panic = [_panic copyWithZone:zone];
72+
return multiroomSync;
73+
}
74+
4175
@end

MatrixSDK/JSONModels/Sync/Multiroom/MXMultiroomSyncLocation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
1616
by this model.
1717
*/
1818

19-
@interface MXMultiroomSyncLocation : MXJSONModel
19+
@interface MXMultiroomSyncLocation : MXJSONModel<NSCopying>
2020

2121
/**
2222
The event (possibly encrypted) content.

0 commit comments

Comments
 (0)