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

Lazy Load Room Summaries: Milestone 0 #1231

Merged
merged 77 commits into from Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
8668fd5
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Sep 9, 2021
ca32d08
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Sep 21, 2021
4b33558
Introduce `MXRoomSummaryStore`
ismailgulek Sep 21, 2021
7f1cd38
Remove unnecessary Swift bridges
ismailgulek Sep 21, 2021
ac48eac
Introduce `MXRoomSummaryProtocol`
ismailgulek Sep 21, 2021
5e00e5c
Create MXMulticastDelegate
ismailgulek Sep 21, 2021
2279047
Create room list data manager protocols
ismailgulek Sep 21, 2021
83d8a46
Introduce `MXSDKOptions.roomListDataManagerClass`
ismailgulek Sep 21, 2021
7766dfb
Introduce `MXSession.roomListDataManager`
ismailgulek Sep 21, 2021
86d9cf7
Room list data manager implementation based on `MXStore`
ismailgulek Sep 21, 2021
10d8618
Make outgoing event method non-optional
ismailgulek Sep 21, 2021
3bc3842
Introduce `sentStatus` on room summary protocol
ismailgulek Sep 21, 2021
ccd8143
Add initializer with summary model
ismailgulek Sep 23, 2021
c1896df
Store changes
ismailgulek Sep 23, 2021
6a1a1c5
Make session use MXRoomSummary instances again
ismailgulek Sep 23, 2021
522761b
Make properties mutable
ismailgulek Sep 23, 2021
214e3b0
Expose functions
ismailgulek Sep 23, 2021
45e77b4
Update summary model
ismailgulek Sep 23, 2021
158713d
Delay refreshing outgoing messages to the next run loop
ismailgulek Sep 23, 2021
1ac2d23
Add new properties and refactor
ismailgulek Sep 23, 2021
7851f0d
Refactor background sync service
ismailgulek Sep 23, 2021
5a7f491
Add missing headers
ismailgulek Sep 23, 2021
1930138
Move counts to a dedicated class
ismailgulek Sep 24, 2021
4694808
Add favorite tag order to summary model
ismailgulek Sep 24, 2021
f49a52e
Refresh fetcher only options changed
ismailgulek Sep 24, 2021
c47f855
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Sep 27, 2021
878cd4a
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Sep 28, 2021
53c4bec
Add spaces option
ismailgulek Sep 29, 2021
63eeb94
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Sep 30, 2021
51dd0b7
Add space filter
ismailgulek Sep 30, 2021
79cd88b
Add suggested rooms sorting
ismailgulek Sep 30, 2021
7e17777
Add summary initializer for suggested rooms
ismailgulek Oct 1, 2021
8b45f15
Make MXSpace equatable
ismailgulek Oct 1, 2021
40734de
Remove suggested data type
ismailgulek Oct 1, 2021
5246c71
Introduce pagination options enum
ismailgulek Oct 1, 2021
dd6cc83
Make suggested sorting enabled by default
ismailgulek Oct 1, 2021
8dbcea2
Add onlySuggested case for filter options
ismailgulek Oct 1, 2021
9ba36ca
Make counts class internal
ismailgulek Oct 1, 2021
4d8aa61
Implement suggested rooms fetcher
ismailgulek Oct 1, 2021
220aeac
Return suggested rooms fetcher if onlySuggested
ismailgulek Oct 1, 2021
7b38fd6
Add clearing cache
ismailgulek Oct 4, 2021
2a764f5
Update some classes for mocking
ismailgulek Oct 5, 2021
29f3d64
Fix cache logic
ismailgulek Oct 5, 2021
549bc33
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Oct 5, 2021
e864865
Refactor class and move to Utils
ismailgulek Oct 5, 2021
88bad18
Add some comments
ismailgulek Oct 5, 2021
9d7716c
Implement MXRoomSummaryStore in MXMemoryStore
ismailgulek Oct 5, 2021
54a0868
Add some unit and integration tests
ismailgulek Oct 5, 2021
887a09a
Trigger Realm migrations only if an old db found
ismailgulek Oct 6, 2021
5d6e189
Clear Realm store on every test to speed up tests
ismailgulek Oct 6, 2021
694a4dd
Update MatrixSDKTests/MXRoomListDataManagerTests.swift
ismailgulek Oct 7, 2021
d772903
Remove tearDown
ismailgulek Oct 7, 2021
6e867aa
Remove obsolete code
ismailgulek Oct 7, 2021
44d0459
Add log for non-existent room case
ismailgulek Oct 7, 2021
0decfa7
Remove calculate data types logging
ismailgulek Oct 7, 2021
8bf2e6b
Add NS_ASSUME_NONNULL_BEGIN to MXRoomSummaryProtocol
ismailgulek Oct 7, 2021
2c8af1b
Add NS_ASSUME_NONNULL_BEGIN to MXRoomSummaryStore
ismailgulek Oct 7, 2021
b55bb05
Update counts
ismailgulek Oct 8, 2021
8ff40ac
Update doc for currentPage property
ismailgulek Oct 8, 2021
de7e714
Add an explanation for dispatch_async
ismailgulek Oct 8, 2021
84d7778
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Oct 8, 2021
999a76b
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Oct 8, 2021
28651fc
Fix unit tests after deviceId requirement
ismailgulek Oct 8, 2021
e83ae31
Clear Realm cache for non e2e integration tests
ismailgulek Oct 11, 2021
bc85a9f
Add comments
ismailgulek Oct 11, 2021
a07c46a
Weakify self in saveRoomsSummaries method
ismailgulek Oct 11, 2021
1af8eeb
Add comments for MXRoomSummaryStore
ismailgulek Oct 11, 2021
ab13600
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Oct 12, 2021
23537c0
Fix property
ismailgulek Oct 12, 2021
d7480ec
Improve in-memory counting performance
ismailgulek Oct 12, 2021
4b4909c
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Oct 13, 2021
a2d1d36
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Oct 15, 2021
9581c58
Introduce `MXCryptoStore.deleteAllStores` and implement for `MXRealmC…
ismailgulek Oct 18, 2021
f078799
Add changelog
ismailgulek Oct 18, 2021
d5387f7
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Oct 19, 2021
76f762b
Implement showAllRoomsInHomeSpace
ismailgulek Oct 19, 2021
a83328c
Merge branch 'develop' into ismail/4384_room_summary_store
ismailgulek Oct 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
263 changes: 239 additions & 24 deletions MatrixSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions MatrixSDK/Background/MXBackgroundCryptoStore.m
Expand Up @@ -312,6 +312,11 @@ + (void)deleteStoreWithCredentials:(MXCredentials*)credentials
NSAssert(NO, @"This method should be useless in the context of MXBackgroundCryptoStore");
}

+ (void)deleteAllStores
{
NSAssert(NO, @"This method should be useless in the context of MXBackgroundCryptoStore");
}

+ (void)deleteReadonlyStoreWithCredentials:(MXCredentials*)credentials
{
NSAssert(NO, @"This method should be useless in the context of MXBackgroundCryptoStore");
Expand Down
36 changes: 31 additions & 5 deletions MatrixSDK/Background/MXBackgroundStore.swift
Expand Up @@ -71,11 +71,6 @@ class MXBackgroundStore: NSObject, MXStore {
fileStore.state(ofRoom: roomId, success: success, failure: failure)
}

// Fetch real soom summary
func summary(ofRoom roomId: String) -> MXRoomSummary? {
return fileStore.summary(ofRoom: roomId)
}

// Fetch real room account data
func accountData(ofRoom roomId: String) -> MXRoomAccountData? {
return fileStore.accountData(ofRoom: roomId)
Expand Down Expand Up @@ -244,4 +239,35 @@ class MXBackgroundStore: NSObject, MXStore {
}
}

func storeOutgoingMessage(forRoom roomId: String, outgoingMessage: MXEvent) {

}

func removeAllOutgoingMessages(fromRoom roomId: String) {

}

func removeOutgoingMessage(fromRoom roomId: String, outgoingMessage outgoingMessageEventId: String) {

}

func outgoingMessages(inRoom roomId: String) -> [MXEvent]? {
return []
}

// MARK: - MXRoomSummaryStore

var rooms: [String] {
return []
}

func storeSummary(forRoom roomId: String, summary: MXRoomSummaryProtocol) {

}

// Fetch real soom summary
func summary(ofRoom roomId: String) -> MXRoomSummaryProtocol? {
return fileStore.summary(ofRoom: roomId)
}

}
5 changes: 3 additions & 2 deletions MatrixSDK/Background/MXBackgroundSyncService.swift
Expand Up @@ -178,8 +178,9 @@ public enum MXBackgroundSyncServiceError: Error {
/// Fetch the summary for the given room identifier.
/// - Parameter roomId: The room identifier to fetch.
/// - Returns: Summary of room.
public func roomSummary(forRoomId roomId: String) -> MXRoomSummary? {
return syncResponseStoreManager.roomSummary(forRoomId: roomId, using: store.summary?(ofRoom: roomId))
public func roomSummary(forRoomId roomId: String) -> MXRoomSummaryProtocol? {
let summary = store.summary(ofRoom: roomId)
return syncResponseStoreManager.roomSummary(forRoomId: roomId, using: summary)
}

/// Fetch push rule matching an event.
Expand Down
33 changes: 17 additions & 16 deletions MatrixSDK/Background/Store/MXSyncResponseStoreManager.swift
Expand Up @@ -227,29 +227,30 @@ public class MXSyncResponseStoreManager: NSObject {

/// Fetch room summary for an invited room. Just uses the data in syncResponse to guess the room display name
/// - Parameter roomId: Room identifier to be fetched
/// - Parameter summary: A room summary (if exists) which user had before a sync response
public func roomSummary(forRoomId roomId: String, using summary: MXRoomSummary?) -> MXRoomSummary? {
guard let summary = summary ?? MXRoomSummary(roomId: roomId, andMatrixSession: nil) else {
/// - Parameter model: A room summary model (if exists) which user had before a sync response
public func roomSummary(forRoomId roomId: String, using model: MXRoomSummaryProtocol?) -> MXRoomSummaryProtocol? {
let summary: MXRoomSummary?

if let model = model {
summary = MXRoomSummary(summaryModel: model)
} else {
summary = MXRoomSummary(roomId: roomId, andMatrixSession: nil)
}

guard var result = summary else {
return nil
}

// update summary with each sync response
for id in syncResponseStore.syncResponseIds.reversed() {
let summary = autoreleasepool { () -> MXRoomSummary? in
guard let response = try? syncResponseStore.syncResponse(withId: id) else {
return nil
autoreleasepool {
if let response = try? syncResponseStore.syncResponse(withId: id) {
result = roomSummary(forRoomId: roomId, using: result, inSyncResponse: response)
}

return roomSummary(forRoomId: roomId, using: summary, inSyncResponse: response)
}

if let summary = summary {
return summary
}
}

MXLog.debug("[MXSyncResponseStoreManager] roomSummary: Not found for room \(roomId)")

return nil
return result
}

// MARK: - Private
Expand Down Expand Up @@ -306,7 +307,7 @@ public class MXSyncResponseStoreManager: NSObject {
return MXSyncResponse(fromJSON: dictionary as? [AnyHashable : Any])
}

private func roomSummary(forRoomId roomId: String, using summary: MXRoomSummary, inSyncResponse response: MXCachedSyncResponse) -> MXRoomSummary? {
private func roomSummary(forRoomId roomId: String, using summary: MXRoomSummary, inSyncResponse response: MXCachedSyncResponse) -> MXRoomSummary {
var eventsToProcess: [MXEvent] = []

if let invitedRoomSync = response.syncResponse.rooms?.invite?[roomId] {
Expand Down
30 changes: 0 additions & 30 deletions MatrixSDK/Contrib/Swift/Data/MXRoomMember.swift

This file was deleted.

9 changes: 0 additions & 9 deletions MatrixSDK/Contrib/Swift/Data/MXRoomState.swift
Expand Up @@ -34,15 +34,6 @@ extension MXRoomState {
return MXRoomGuestAccess(identifier: self.__guestAccess)
}


/// The membership state of the logged in user for this room
///
/// If the membership is `invite`, the room state contains few information.
/// Join the room with [MXRoom join] to get full information about the room.
public var membership: MXMembership {
return MXMembership(identifier: self.__membership)
}

/**
Return the state events with the given type.
Expand Down
28 changes: 0 additions & 28 deletions MatrixSDK/Contrib/Swift/Data/MXRoomSummary.swift

This file was deleted.

21 changes: 0 additions & 21 deletions MatrixSDK/Contrib/Swift/JSONModels/MXEvent.swift
Expand Up @@ -163,24 +163,3 @@ public enum MXMessageType: Equatable, Hashable {
self = messages.first(where: { $0.identifier == identifier }) ?? .custom(identifier)
}
}


/// Membership definitions
public enum MXMembership: Equatable, Hashable {
case unknown, invite, join, leave, ban

public var identifier: __MXMembership {
switch self {
case .unknown: return __MXMembershipUnknown
case .invite: return __MXMembershipInvite
case .join: return __MXMembershipJoin
case .leave: return __MXMembershipLeave
case .ban: return __MXMembershipBan
}
}

public init(identifier: __MXMembership) {
let possibilities: [MXMembership] = [.unknown, .invite, .join, .leave, .ban]
self = possibilities.first(where: { $0.identifier == identifier }) ?? .unknown
}
}
5 changes: 5 additions & 0 deletions MatrixSDK/Crypto/Data/Store/MXCryptoStore.h
Expand Up @@ -62,6 +62,11 @@
*/
+ (void)deleteStoreWithCredentials:(MXCredentials*)credentials;

/**
Delete crypto stores for all users. Implementations should also attempt to delete read-only stores.
*/
+ (void)deleteAllStores;

/**
Delete the read-only crypto store for the passed credentials.
Expand Down
51 changes: 26 additions & 25 deletions MatrixSDK/Crypto/Data/Store/MXRealmCryptoStore/MXRealmCryptoStore.m
Expand Up @@ -370,6 +370,11 @@ + (void)deleteStoreWithCredentials:(MXCredentials*)credentials
[self _deleteStoreWithCredentials:credentials readOnly:YES];
}

+ (void)deleteAllStores
{
[[NSFileManager defaultManager] removeItemAtURL:[self storeFolderURL] error:nil];
}

+ (void)deleteReadonlyStoreWithCredentials:(MXCredentials *)credentials
{
[self _deleteStoreWithCredentials:credentials readOnly:YES];
Expand Down Expand Up @@ -1633,7 +1638,14 @@ -(void)setCryptoVersion:(MXCryptoVersion)cryptoVersion
*/
+ (nullable RLMRealm*)realmForUser:(NSString*)userId andDevice:(NSString*)deviceId readOnly:(BOOL)readOnly
{
if (readOnly)
// Each user has its own db file.
// Else, it can lead to issue with primary keys.
// Ex: if 2 users are is the same encrypted room, [self storeAlgorithmForRoom]
// will be called twice for the same room id which breaks the uniqueness of the
// primary key (roomId) for this table.
NSURL *realmFileURL = [self realmFileURLForUserWithUserId:userId andDevice:deviceId];

if (readOnly && [[NSFileManager defaultManager] fileExistsAtPath:realmFileURL.path])
{
// just open Realm once in writable mode to trigger migrations
static dispatch_once_t onceToken;
Expand All @@ -1645,13 +1657,6 @@ + (nullable RLMRealm*)realmForUser:(NSString*)userId andDevice:(NSString*)device
}

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];

// Each user has its own db file.
// Else, it can lead to issue with primary keys.
// Ex: if 2 users are is the same encrypted room, [self storeAlgorithmForRoom]
// will be called twice for the same room id which breaks the uniqueness of the
// primary key (roomId) for this table.
NSURL *realmFileURL = [self realmFileURLForUserWithUserId:userId andDevice:deviceId];
[self ensurePathExistenceForFileAtFileURL:realmFileURL];
config.fileURL = realmFileURL;

Expand Down Expand Up @@ -1775,36 +1780,32 @@ + (nullable RLMRealm*)realmForUser:(NSString*)userId andDevice:(NSString*)device
return realm;
}

// Return the realm db file to use for a given user and device
+ (NSURL*)realmFileURLForUserWithUserId:(NSString*)userId andDevice:(NSString*)deviceId
+ (NSURL*)storeFolderURL
{
NSURL *realmFileURL;

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];

NSURL *defaultRealmPathURL = config.fileURL.URLByDeletingLastPathComponent;

// Default db file URL: use the default directory, but replace the filename with the userId.
NSString *realmFile = [self realmFileNameWithUserId:userId deviceId:deviceId];
NSURL *defaultRealmFileURL = [[defaultRealmPathURL URLByAppendingPathComponent:realmFile]
URLByAppendingPathExtension:@"realm"];

// Check for a potential application group id.
NSString *applicationGroupIdentifier = [MXSDKOptions sharedInstance].applicationGroupIdentifier;
if (applicationGroupIdentifier)
{
// Use the shared db file URL.
NSURL *sharedContainerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:applicationGroupIdentifier];
NSURL *realmFileFolderURL = [sharedContainerURL URLByAppendingPathComponent:kMXRealmCryptoStoreFolder];
realmFileURL = [[realmFileFolderURL URLByAppendingPathComponent:realmFile] URLByAppendingPathExtension:@"realm"];
return [sharedContainerURL URLByAppendingPathComponent:kMXRealmCryptoStoreFolder];
}
else
{
// Use the default URL
realmFileURL = defaultRealmFileURL;
NSURL *defaultRealmPathURL = [RLMRealmConfiguration defaultConfiguration].fileURL.URLByDeletingLastPathComponent;
return [defaultRealmPathURL URLByAppendingPathComponent:kMXRealmCryptoStoreFolder];
}
}

// Return the realm db file to use for a given user and device
+ (NSURL*)realmFileURLForUserWithUserId:(NSString*)userId andDevice:(NSString*)deviceId
{
// Default db file URL: use the default directory, but replace the filename with the userId.
NSString *fileName = [self realmFileNameWithUserId:userId
deviceId:deviceId];

return realmFileURL;
return [[[self storeFolderURL] URLByAppendingPathComponent:fileName] URLByAppendingPathExtension:@"realm"];
}

/**
Expand Down