updateMembers now is chain of membersNoSync + membersWithSync (#2324)

This commit is contained in:
Mauro
2024-01-12 09:46:51 +01:00
committed by GitHub
parent 21fa5d1f84
commit e51e2ac2af
15 changed files with 55 additions and 48 deletions
@@ -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
}
}
}
@@ -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
}
}
@@ -92,8 +92,8 @@ extension NotificationSettingsProxyMock {
}
}
canHomeserverPushEncryptedEventsToDeviceClosure = {
configuration.canHomeserverPushEncryptedEvents
canPushEncryptedEventsToDeviceClosure = {
configuration.canPushEncryptedEvents
}
}
}
@@ -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,
@@ -33,7 +33,7 @@ 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
@@ -41,7 +41,7 @@ struct NotificationSettingsEditScreenViewState: BindableState {
func description(for mode: NotificationSettingsEditScreenDefaultMode) -> String? {
guard mode == .mentionsAndKeywordsOnly,
!canHomeServerPushEncryptedEvents else {
!canPushEncryptedEvents else {
return nil
}
return L10n.screenNotificationSettingsMentionsOnlyDisclaimer
@@ -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 {
@@ -104,7 +104,7 @@ class NotificationSettingsEditScreenViewModel: NotificationSettingsEditScreenVie
default:
state.defaultMode = nil
}
state.canHomeServerPushEncryptedEvents = canHomserverPushEncryptedEvents
state.canPushEncryptedEvents = canPushEncryptedEvents
}
}
@@ -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)
@@ -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
@@ -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
}
+21 -15
View File
@@ -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()
}
}
@@ -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)
}
membersSubject.value = roomMembersProxies
} catch {
return
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)
}
}
} catch {
MXLog.error("[RoomProxy] Failed to update members using sync API: \(error)")
}
}