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

Adapt New Sync API #1100

Merged
merged 8 commits into from
May 26, 2021
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Changes to be released in next version
* MXCrypto: eventDeviceInfo: Do not synchronise anymore the operation with the decryption queue.
* MXRoomSummary: Improve reset resetLastMessage to avoid pagination loop and to limit number of decryptions.
* MXSession: Limit the number of decryptions when processing an initial sync (vector-im/element-ios/issues/4307).
* Adapt sync response models to new sync API (vector-im/element-ios/issues/4309).

🐛 Bugfix
* MXRoomSummary: Fix decryption of the last message when it is edited (vector-im/element-ios/issues/4322).
Expand Down
228 changes: 228 additions & 0 deletions MatrixSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions MatrixSDK/Background/MXBackgroundSyncService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,19 +488,21 @@ public enum MXBackgroundSyncServiceError: Error {

private func handleSyncResponse(_ syncResponse: MXSyncResponse, syncToken: String) {
NSLog("[MXBackgroundSyncService] handleSyncResponse: Received %tu joined rooms, %tu invited rooms, %tu left rooms, %tu toDevice events.",
syncResponse.rooms.join.count,
syncResponse.rooms.invite.count,
syncResponse.rooms.leave.count,
syncResponse.toDevice.events?.count ?? 0)
syncResponse.rooms?.join?.count ?? 0,
syncResponse.rooms?.invite?.count ?? 0,
syncResponse.rooms?.leave?.count ?? 0,
syncResponse.toDevice?.events.count ?? 0)

pushRulesManager.handleAccountData(syncResponse.accountData)
if let accountData = syncResponse.accountData {
pushRulesManager.handleAccountData(accountData)
}
syncResponseStoreManager.updateStore(with: syncResponse, syncToken: syncToken)

for event in syncResponse.toDevice?.events ?? [] {
handleToDeviceEvent(event)
}

NSLog("[MXBackgroundSyncService] handleSyncResponse: Next sync token: \(syncResponse.nextBatch ?? "nil")")
NSLog("[MXBackgroundSyncService] handleSyncResponse: Next sync token: \(syncResponse.nextBatch)")
}

private func handleToDeviceEvent(_ event: MXEvent) {
Expand Down
73 changes: 32 additions & 41 deletions MatrixSDK/Background/Store/MXSyncResponseStoreManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,34 @@ public class MXSyncResponseStoreManager: NSObject {
NSLog("[MXSyncResponseStoreManager] updateStore: Merge new sync response to the previous one")

// handle new limited timelines
newSyncResponse.rooms.join.filter({ $1.timeline?.limited == true }).forEach { (roomId, _) in
if let joinedRoomSync = cachedSyncResponse.syncResponse.rooms.join[roomId] {
newSyncResponse.rooms?.join?.filter({ $1.timeline.limited == true }).forEach { (roomId, _) in
if let joinedRoomSync = cachedSyncResponse.syncResponse.rooms?.join?[roomId] {
// remove old events
joinedRoomSync.timeline?.events = []
joinedRoomSync.timeline.events = []
// mark old timeline as limited too
joinedRoomSync.timeline?.limited = true
joinedRoomSync.timeline.limited = true
}
}
newSyncResponse.rooms.leave.filter({ $1.timeline?.limited == true }).forEach { (roomId, _) in
if let leftRoomSync = cachedSyncResponse.syncResponse.rooms.leave[roomId] {
newSyncResponse.rooms?.leave?.filter({ $1.timeline.limited == true }).forEach { (roomId, _) in
if let leftRoomSync = cachedSyncResponse.syncResponse.rooms?.leave?[roomId] {
// remove old events
leftRoomSync.timeline?.events = []
leftRoomSync.timeline.events = []
// mark old timeline as limited too
leftRoomSync.timeline?.limited = true
leftRoomSync.timeline.limited = true
}
}

// handle old limited timelines
cachedSyncResponse.syncResponse.rooms.join.filter({ $1.timeline?.limited == true }).forEach { (roomId, _) in
if let joinedRoomSync = newSyncResponse.rooms.join[roomId] {
cachedSyncResponse.syncResponse.rooms?.join?.filter({ $1.timeline.limited == true }).forEach { (roomId, _) in
if let joinedRoomSync = newSyncResponse.rooms?.join?[roomId] {
// mark new timeline as limited too, to avoid losing value of limited
joinedRoomSync.timeline?.limited = true
joinedRoomSync.timeline.limited = true
}
}
cachedSyncResponse.syncResponse.rooms.leave.filter({ $1.timeline?.limited == true }).forEach { (roomId, _) in
if let leftRoomSync = newSyncResponse.rooms.leave[roomId] {
cachedSyncResponse.syncResponse.rooms?.leave?.filter({ $1.timeline.limited == true }).forEach { (roomId, _) in
if let leftRoomSync = newSyncResponse.rooms?.leave?[roomId] {
// mark new timeline as limited too, to avoid losing value of limited
leftRoomSync.timeline?.limited = true
leftRoomSync.timeline.limited = true
}
}

Expand Down Expand Up @@ -191,18 +191,18 @@ public class MXSyncResponseStoreManager: NSObject {

private func event(withEventId eventId: String, inRoom roomId: String, inSyncResponse response: MXCachedSyncResponse) -> MXEvent? {
var allEvents: [MXEvent] = []
if let joinedRoomSync = response.syncResponse.rooms.join[roomId] {
allEvents.appendIfNotNil(contentsOf: joinedRoomSync.state?.events)
allEvents.appendIfNotNil(contentsOf: joinedRoomSync.timeline?.events)
allEvents.appendIfNotNil(contentsOf: joinedRoomSync.accountData?.events)
if let joinedRoomSync = response.syncResponse.rooms?.join?[roomId] {
allEvents.appendIfNotNil(contentsOf: joinedRoomSync.state.events)
allEvents.appendIfNotNil(contentsOf: joinedRoomSync.timeline.events)
allEvents.appendIfNotNil(contentsOf: joinedRoomSync.accountData.events)
}
if let invitedRoomSync = response.syncResponse.rooms.invite[roomId] {
allEvents.appendIfNotNil(contentsOf: invitedRoomSync.inviteState?.events)
if let invitedRoomSync = response.syncResponse.rooms?.invite?[roomId] {
allEvents.appendIfNotNil(contentsOf: invitedRoomSync.inviteState.events)
}
if let leftRoomSync = response.syncResponse.rooms.leave[roomId] {
allEvents.appendIfNotNil(contentsOf: leftRoomSync.state?.events)
allEvents.appendIfNotNil(contentsOf: leftRoomSync.timeline?.events)
allEvents.appendIfNotNil(contentsOf: leftRoomSync.accountData?.events)
if let leftRoomSync = response.syncResponse.rooms?.leave?[roomId] {
allEvents.appendIfNotNil(contentsOf: leftRoomSync.state.events)
allEvents.appendIfNotNil(contentsOf: leftRoomSync.timeline.events)
allEvents.appendIfNotNil(contentsOf: leftRoomSync.accountData.events)
}

let result = allEvents.first(where: { eventId == $0.eventId })
Expand Down Expand Up @@ -243,27 +243,18 @@ public class MXSyncResponseStoreManager: NSObject {
private func roomSummary(forRoomId roomId: String, using summary: MXRoomSummary, inSyncResponse response: MXCachedSyncResponse) -> MXRoomSummary? {
var eventsToProcess: [MXEvent] = []

if let invitedRoomSync = response.syncResponse.rooms.invite[roomId],
let stateEvents = invitedRoomSync.inviteState?.events {
eventsToProcess.append(contentsOf: stateEvents)
if let invitedRoomSync = response.syncResponse.rooms?.invite?[roomId] {
eventsToProcess.append(contentsOf: invitedRoomSync.inviteState.events)
}

if let joinedRoomSync = response.syncResponse.rooms.join[roomId] {
if let stateEvents = joinedRoomSync.state?.events {
eventsToProcess.append(contentsOf: stateEvents)
}
if let timelineEvents = joinedRoomSync.timeline?.events {
eventsToProcess.append(contentsOf: timelineEvents)
}
if let joinedRoomSync = response.syncResponse.rooms?.join?[roomId] {
eventsToProcess.append(contentsOf: joinedRoomSync.state.events)
eventsToProcess.append(contentsOf: joinedRoomSync.timeline.events)
}

if let leftRoomSync = response.syncResponse.rooms.leave[roomId] {
if let stateEvents = leftRoomSync.state?.events {
eventsToProcess.append(contentsOf: stateEvents)
}
if let timelineEvents = leftRoomSync.timeline?.events {
eventsToProcess.append(contentsOf: timelineEvents)
}
if let leftRoomSync = response.syncResponse.rooms?.leave?[roomId] {
eventsToProcess.append(contentsOf: leftRoomSync.state.events)
eventsToProcess.append(contentsOf: leftRoomSync.timeline.events)
}

for event in eventsToProcess {
Expand Down
2 changes: 1 addition & 1 deletion MatrixSDK/Background/Store/Model/MXCachedSyncResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//

#import "MXCachedSyncResponse.h"
#import "MXJSONModels.h"
#import "MXSyncResponse.h"

@implementation MXCachedSyncResponse

Expand Down
2 changes: 2 additions & 0 deletions MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

#import "NSArray+MatrixSDK.h"

#import "MXDeviceListResponse.h"

/**
The store to use for crypto.
*/
Expand Down
2 changes: 2 additions & 0 deletions MatrixSDK/Data/MXEventTimeline.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

#import "MXEventsEnumeratorOnArray.h"

#import "MXRoomSync.h"

NSString *const kMXRoomInviteStateEventIdPrefix = @"invite-";

@interface MXEventTimeline ()
Expand Down
2 changes: 2 additions & 0 deletions MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#import "MXError.h"

#import "MXRoomSync.h"

NSString *const kMXRoomDidFlushDataNotification = @"kMXRoomDidFlushDataNotification";
NSString *const kMXRoomInitialSyncNotification = @"kMXRoomInitialSyncNotification";

Expand Down
3 changes: 3 additions & 0 deletions MatrixSDK/Data/MXRoomSummary.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#import "MXRoomType.h"

@class MXSession, MXRoom, MXRoomState, MXEvent;
@class MXRoomSync;
@class MXInvitedRoomSync;
@class MXRoomSyncSummary;
@protocol MXStore;


Expand Down
2 changes: 2 additions & 0 deletions MatrixSDK/Data/MXRoomSummary.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#import "MXEventRelations.h"
#import "MXEventReplace.h"

#import "MXRoomSync.h"

#import <Security/Security.h>
#import <CommonCrypto/CommonCryptor.h>

Expand Down
Loading