Skip to content

Commit

Permalink
Merge pull request #1231 from matrix-org/ismail/4384_room_summary_store
Browse files Browse the repository at this point in the history
Lazy Load Room Summaries: Milestone 0
  • Loading branch information
ismailgulek committed Oct 19, 2021
2 parents cbe894f + a83328c commit 44b86c5
Show file tree
Hide file tree
Showing 56 changed files with 3,311 additions and 360 deletions.
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

0 comments on commit 44b86c5

Please sign in to comment.