Skip to content

Commit

Permalink
updateMembers now is chain of membersNoSync + membersWithSync (#2324)
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 authored Jan 12, 2024
1 parent 21fa5d1 commit e51e2ac
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 47 deletions.
2 changes: 1 addition & 1 deletion ElementX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -6660,7 +6660,7 @@
repositoryURL = "https://github.com/matrix-org/matrix-rust-components-swift";
requirement = {
kind = exactVersion;
version = 1.1.31;
version = 1.1.32;
};
};
821C67C9A7F8CC3FD41B28B4 /* XCRemoteSwiftPackageReference "emojibase-bindings" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/matrix-org/matrix-rust-components-swift",
"state" : {
"revision" : "c9c6725af2c9fa93c19f710e307b0b25e0f1fa26",
"version" : "1.1.31"
"revision" : "07556e1b475291ec4794e22e9348a5a7db137595",
"version" : "1.1.32"
}
},
{
Expand Down
22 changes: 11 additions & 11 deletions ElementX/Sources/Mocks/Generated/GeneratedMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1614,21 +1614,21 @@ class NotificationSettingsProxyMock: NotificationSettingsProxyProtocol {
return getRoomsWithUserDefinedRulesReturnValue
}
}
//MARK: - canHomeserverPushEncryptedEventsToDevice
//MARK: - canPushEncryptedEventsToDevice

var canHomeserverPushEncryptedEventsToDeviceCallsCount = 0
var canHomeserverPushEncryptedEventsToDeviceCalled: Bool {
return canHomeserverPushEncryptedEventsToDeviceCallsCount > 0
var canPushEncryptedEventsToDeviceCallsCount = 0
var canPushEncryptedEventsToDeviceCalled: Bool {
return canPushEncryptedEventsToDeviceCallsCount > 0
}
var canHomeserverPushEncryptedEventsToDeviceReturnValue: Bool!
var canHomeserverPushEncryptedEventsToDeviceClosure: (() async -> Bool)?
var canPushEncryptedEventsToDeviceReturnValue: Bool!
var canPushEncryptedEventsToDeviceClosure: (() async -> Bool)?

func canHomeserverPushEncryptedEventsToDevice() async -> Bool {
canHomeserverPushEncryptedEventsToDeviceCallsCount += 1
if let canHomeserverPushEncryptedEventsToDeviceClosure = canHomeserverPushEncryptedEventsToDeviceClosure {
return await canHomeserverPushEncryptedEventsToDeviceClosure()
func canPushEncryptedEventsToDevice() async -> Bool {
canPushEncryptedEventsToDeviceCallsCount += 1
if let canPushEncryptedEventsToDeviceClosure = canPushEncryptedEventsToDeviceClosure {
return await canPushEncryptedEventsToDeviceClosure()
} else {
return canHomeserverPushEncryptedEventsToDeviceReturnValue
return canPushEncryptedEventsToDeviceReturnValue
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions ElementX/Sources/Mocks/NotificationSettingsProxyMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ struct NotificationSettingsProxyMockConfiguration {
var callback = PassthroughSubject<NotificationSettingsProxyCallback, Never>()
var defaultRoomMode: RoomNotificationModeProxy
var roomMode: RoomNotificationSettingsProxyMock
var canHomeserverPushEncryptedEvents = false
var canPushEncryptedEvents = false

init(defaultRoomMode: RoomNotificationModeProxy = .allMessages, roomMode: RoomNotificationModeProxy = .allMessages, canHomeserverPushEncryptedEvents: Bool = false) {
init(defaultRoomMode: RoomNotificationModeProxy = .allMessages, roomMode: RoomNotificationModeProxy = .allMessages, canPushEncryptedEvents: Bool = false) {
self.defaultRoomMode = defaultRoomMode
self.roomMode = RoomNotificationSettingsProxyMock(with: RoomNotificationSettingsProxyMockConfiguration(mode: roomMode, isDefault: defaultRoomMode == roomMode))
self.canHomeserverPushEncryptedEvents = canHomeserverPushEncryptedEvents
self.canPushEncryptedEvents = canPushEncryptedEvents
}
}

Expand Down Expand Up @@ -92,8 +92,8 @@ extension NotificationSettingsProxyMock {
}
}

canHomeserverPushEncryptedEventsToDeviceClosure = {
configuration.canHomeserverPushEncryptedEvents
canPushEncryptedEventsToDeviceClosure = {
configuration.canPushEncryptedEvents
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class RoomNotificationSettingsScreenViewModel: RoomNotificationSettingsScreenVie
}

private func fetchRoomNotificationSettings() async {
state.shouldDisplayMentionsOnlyDisclaimer = roomProxy.isEncrypted ? await !notificationSettingsProxy.canHomeserverPushEncryptedEventsToDevice() : false
state.shouldDisplayMentionsOnlyDisclaimer = roomProxy.isEncrypted ? await !notificationSettingsProxy.canPushEncryptedEventsToDevice() : false
do {
// `isOneToOne` here is not the same as `isDirect` on the room. From the point of view of the push rule, a one-to-one room is a room with exactly two active members.
let settings = try await notificationSettingsProxy.getNotificationSettings(roomId: roomProxy.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ struct NotificationSettingsEditScreenViewState: BindableState {
var defaultMode: NotificationSettingsEditScreenDefaultMode?
var pendingMode: NotificationSettingsEditScreenDefaultMode?
var roomsWithUserDefinedMode: [NotificationSettingsEditScreenRoom] = []
var canHomeServerPushEncryptedEvents = false
var canPushEncryptedEvents = false

func isSelected(mode: NotificationSettingsEditScreenDefaultMode) -> Bool {
pendingMode == nil && defaultMode == mode
}

func description(for mode: NotificationSettingsEditScreenDefaultMode) -> String? {
guard mode == .mentionsAndKeywordsOnly,
!canHomeServerPushEncryptedEvents else {
!canPushEncryptedEvents else {
return nil
}
return L10n.screenNotificationSettingsMentionsOnlyDisclaimer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class NotificationSettingsEditScreenViewModel: NotificationSettingsEditScreenVie
if encrypted_mode == unencrypted_mode {
mode = encrypted_mode
}
let canHomserverPushEncryptedEvents = await notificationSettingsProxy.canHomeserverPushEncryptedEventsToDevice()
let canPushEncryptedEvents = await notificationSettingsProxy.canPushEncryptedEventsToDevice()
guard !Task.isCancelled else { return }

switch mode {
Expand All @@ -104,7 +104,7 @@ class NotificationSettingsEditScreenViewModel: NotificationSettingsEditScreenVie
default:
state.defaultMode = nil
}
state.canHomeServerPushEncryptedEvents = canHomserverPushEncryptedEvents
state.canPushEncryptedEvents = canPushEncryptedEvents
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ struct NotificationSettingsEditScreen_Previews: PreviewProvider, TestablePreview
}()

static let viewModelGroupChatsWithouDisclaimer: NotificationSettingsEditScreenViewModel = {
let notificationSettingsProxy = NotificationSettingsProxyMock(with: .init(canHomeserverPushEncryptedEvents: true))
let notificationSettingsProxy = NotificationSettingsProxyMock(with: .init(canPushEncryptedEvents: true))
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReturnValue = .allMessages

notificationSettingsProxy.getRoomsWithUserDefinedRulesReturnValue = [RoomSummary].mockRooms.compactMap(\.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ final class NotificationSettingsProxy: NotificationSettingsProxyProtocol {
await notificationSettings.getRoomsWithUserDefinedRules(enabled: true)
}

func canHomeserverPushEncryptedEventsToDevice() async -> Bool {
await notificationSettings.canHomeserverPushEncryptedEventToDevice()
func canPushEncryptedEventsToDevice() async -> Bool {
await notificationSettings.canPushEncryptedEventToDevice()
}

// MARK: - Private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ protocol NotificationSettingsProxyProtocol {
func isInviteForMeEnabled() async throws -> Bool
func setInviteForMeEnabled(enabled: Bool) async throws
func getRoomsWithUserDefinedRules() async throws -> [String]
func canHomeserverPushEncryptedEventsToDevice() async -> Bool
func canPushEncryptedEventsToDevice() async -> Bool
}
34 changes: 20 additions & 14 deletions ElementX/Sources/Services/Room/RoomProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ class RoomProxy: RoomProxyProtocol {
pollHistoryTimeline = await TimelineProxy(timeline: room.pollHistory(), backgroundTaskService: backgroundTaskService)

Task {
// Force the timeline to load member details so it can populate sender profiles whenever we add a timeline listener
// This should become automatic on the RustSDK side at some point
await room.timeline().fetchMembers()

await updateMembers()
}
}
Expand Down Expand Up @@ -179,21 +175,31 @@ class RoomProxy: RoomProxyProtocol {
}
}
}

func updateMembers() async {
// We always update members first using the no sync API in case internet is not readily available
// To get the members stored on disk first, this API call is very fast.
do {
let membersIterator = try await room.members()
guard let members = membersIterator.nextChunk(chunkSize: membersIterator.len()) else {
return
let membersNoSyncIterator = try await room.membersNoSync()
if let members = membersNoSyncIterator.nextChunk(chunkSize: membersNoSyncIterator.len()) {
membersSubject.value = members.map {
RoomMemberProxy(member: $0, backgroundTaskService: self.backgroundTaskService)
}
}

let roomMembersProxies = members.map {
RoomMemberProxy(member: $0, backgroundTaskService: self.backgroundTaskService)
} catch {
MXLog.error("[RoomProxy] Failed to update members using no sync API: \(error)")
}

do {
// Then we update members using the sync API, this is slower but will get us the latest members
let membersIterator = try await room.members()
if let members = membersIterator.nextChunk(chunkSize: membersIterator.len()) {
membersSubject.value = members.map {
RoomMemberProxy(member: $0, backgroundTaskService: self.backgroundTaskService)
}
}

membersSubject.value = roomMembersProxies
} catch {
return
MXLog.error("[RoomProxy] Failed to update members using sync API: \(error)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class NotificationSettingsEditScreenViewModelTests: XCTestCase {

XCTAssertEqual(context.viewState.defaultMode, .mentionsAndKeywordsOnly)
XCTAssertNil(context.viewState.bindings.alertInfo)
XCTAssertFalse(context.viewState.canHomeServerPushEncryptedEvents)
XCTAssertFalse(context.viewState.canPushEncryptedEvents)
XCTAssertNotNil(context.viewState.description(for: .mentionsAndKeywordsOnly))
}

Expand All @@ -85,7 +85,7 @@ class NotificationSettingsEditScreenViewModelTests: XCTestCase {
return .mentionsAndKeywordsOnly
}
}
notificationSettingsProxy.canHomeserverPushEncryptedEventsToDeviceClosure = {
notificationSettingsProxy.canPushEncryptedEventsToDeviceClosure = {
true
}
viewModel = NotificationSettingsEditScreenViewModel(chatType: .groupChat,
Expand Down Expand Up @@ -113,7 +113,7 @@ class NotificationSettingsEditScreenViewModelTests: XCTestCase {

XCTAssertEqual(context.viewState.defaultMode, .mentionsAndKeywordsOnly)
XCTAssertNil(context.viewState.bindings.alertInfo)
XCTAssertTrue(context.viewState.canHomeServerPushEncryptedEvents)
XCTAssertTrue(context.viewState.canPushEncryptedEvents)
XCTAssertNil(context.viewState.description(for: .mentionsAndKeywordsOnly))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RoomNotificationSettingsScreenViewModelTests: XCTestCase {

func testInitialStateDefaultModeEncryptedRoomWithCanPushEncrypted() async throws {
let roomProxyMock = RoomProxyMock(with: .init(displayName: "Test", isEncrypted: true, joinedMembersCount: 0))
let notificationSettingsProxyMock = NotificationSettingsProxyMock(with: .init(canHomeserverPushEncryptedEvents: true))
let notificationSettingsProxyMock = NotificationSettingsProxyMock(with: .init(canPushEncryptedEvents: true))

notificationSettingsProxyMock.getNotificationSettingsRoomIdIsEncryptedIsOneToOneReturnValue = RoomNotificationSettingsProxyMock(with: .init(mode: .mentionsAndKeywordsOnly, isDefault: true))

Expand Down
1 change: 1 addition & 0 deletions changelog.d/2304.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix for read receipts loading slowly when opening a room.
2 changes: 1 addition & 1 deletion project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ packages:
# Element/Matrix dependencies
MatrixRustSDK:
url: https://github.com/matrix-org/matrix-rust-components-swift
exactVersion: 1.1.31
exactVersion: 1.1.32
# path: ../matrix-rust-sdk
Compound:
url: https://github.com/element-hq/compound-ios
Expand Down

0 comments on commit e51e2ac

Please sign in to comment.