Introduce a new RoomProxyType and treat rooms differently based on their membership state

This commit is contained in:
Stefan Ceriu
2024-08-20 16:13:27 +03:00
committed by Stefan Ceriu
parent e0ba9925e7
commit e281a3f242
97 changed files with 3755 additions and 3474 deletions
@@ -0,0 +1,111 @@
//
// Copyright 2024 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import MatrixRustSDK
import UIKit
class InvitedRoomProxy: InvitedRoomProxyProtocol {
private let roomListItem: RoomListItemProtocol
private let room: RoomProtocol
// A room identifier is constant and lazy stops it from being fetched
// multiple times over FFI
lazy var id: String = room.id()
var canonicalAlias: String? {
room.canonicalAlias()
}
var ownUserID: String {
room.ownUserId()
}
var name: String? {
roomListItem.displayName()
}
var topic: String? {
room.topic()
}
var avatarURL: URL? {
roomListItem.avatarUrl().flatMap(URL.init(string:))
}
var avatar: RoomAvatar {
if isDirect, avatarURL == nil {
let heroes = room.heroes()
if heroes.count == 1 {
return .heroes(heroes.map(UserProfileProxy.init))
}
}
return .room(id: id, name: name, avatarURL: avatarURL)
}
var isDirect: Bool {
room.isDirect()
}
var isPublic: Bool {
room.isPublic()
}
var isSpace: Bool {
room.isSpace()
}
var joinedMembersCount: Int {
Int(room.joinedMembersCount())
}
var activeMembersCount: Int {
Int(room.activeMembersCount())
}
var inviter: RoomMemberProxyProtocol? {
get async {
await (try? roomListItem.roomInfo().inviter).map(RoomMemberProxy.init)
}
}
init(roomListItem: RoomListItemProtocol,
room: RoomProtocol) {
self.roomListItem = roomListItem
self.room = room
}
func acceptInvitation() async -> Result<Void, RoomProxyError> {
do {
try await room.join()
return .success(())
} catch {
MXLog.error("Failed accepting invitation with error: \(error)")
return .failure(.sdkError(error))
}
}
func rejectInvitation() async -> Result<Void, RoomProxyError> {
do {
return try await .success(room.leave())
} catch {
MXLog.error("Failed rejecting invitiation with error: \(error)")
return .failure(.sdkError(error))
}
}
}
@@ -19,7 +19,7 @@ import Foundation
import MatrixRustSDK
import UIKit
class RoomProxy: RoomProxyProtocol {
class JoinedRoomProxy: JoinedRoomProxyProtocol {
private let roomListService: RoomListServiceProtocol
private let roomListItem: RoomListItemProtocol
private let room: RoomProtocol
@@ -77,13 +77,19 @@ class RoomProxy: RoomProxyProtocol {
typingMembersSubject.asCurrentValuePublisher()
}
private let actionsSubject = PassthroughSubject<RoomProxyAction, Never>()
var actionsPublisher: AnyPublisher<RoomProxyAction, Never> {
private let actionsSubject = PassthroughSubject<JoinedRoomProxyAction, Never>()
var actionsPublisher: AnyPublisher<JoinedRoomProxyAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
// A room identifier is constant and lazy stops it from being fetched
// multiple times over FFI
lazy var id: String = room.id()
var canonicalAlias: String? {
room.canonicalAlias()
}
var ownUserID: String {
room.ownUserId()
}
@@ -96,14 +102,20 @@ class RoomProxy: RoomProxyProtocol {
room.topic()
}
var membership: Membership {
room.membership()
var avatarURL: URL? {
roomListItem.avatarUrl().flatMap(URL.init(string:))
}
var inviter: RoomMemberProxyProtocol? {
get async {
await (try? roomListItem.roomInfo().inviter).map(RoomMemberProxy.init)
var avatar: RoomAvatar {
if isDirect, avatarURL == nil {
let heroes = room.heroes()
if heroes.count == 1 {
return .heroes(heroes.map(UserProfileProxy.init))
}
}
return .room(id: id, name: name, avatarURL: avatarURL)
}
var isDirect: Bool {
@@ -118,6 +130,14 @@ class RoomProxy: RoomProxyProtocol {
room.isSpace()
}
var joinedMembersCount: Int {
Int(room.joinedMembersCount())
}
var activeMembersCount: Int {
Int(room.activeMembersCount())
}
var isEncrypted: Bool {
(try? room.isEncrypted()) ?? false
}
@@ -145,34 +165,6 @@ class RoomProxy: RoomProxyProtocol {
room.activeRoomCallParticipants()
}
var canonicalAlias: String? {
room.canonicalAlias()
}
var avatarURL: URL? {
roomListItem.avatarUrl().flatMap(URL.init(string:))
}
var avatar: RoomAvatar {
if isDirect, avatarURL == nil {
let heroes = room.heroes()
if heroes.count == 1 {
return .heroes(heroes.map(UserProfileProxy.init))
}
}
return .room(id: id, name: name, avatarURL: avatarURL)
}
var joinedMembersCount: Int {
Int(room.joinedMembersCount())
}
var activeMembersCount: Int {
Int(room.activeMembersCount())
}
init?(roomListService: RoomListServiceProtocol,
roomListItem: RoomListItemProtocol,
room: RoomProtocol) async {
@@ -315,25 +307,6 @@ class RoomProxy: RoomProxyProtocol {
}
}
func rejectInvitation() async -> Result<Void, RoomProxyError> {
do {
return try await .success(room.leave())
} catch {
MXLog.error("Failed rejecting invitiation with error: \(error)")
return .failure(.sdkError(error))
}
}
func acceptInvitation() async -> Result<Void, RoomProxyError> {
do {
try await room.join()
return .success(())
} catch {
MXLog.error("Failed accepting invitation with error: \(error)")
return .failure(.sdkError(error))
}
}
func invite(userID: String) async -> Result<Void, RoomProxyError> {
do {
MXLog.info("Inviting user \(userID)")
@@ -26,46 +26,62 @@ enum RoomProxyError: Error {
case eventNotFound
}
enum RoomProxyAction {
case roomInfoUpdate
enum RoomProxyType {
case joined(JoinedRoomProxyProtocol)
case invited(InvitedRoomProxyProtocol)
case left
}
// sourcery: AutoMockable
protocol RoomProxyProtocol {
var id: String { get }
var isDirect: Bool { get }
var isPublic: Bool { get }
var isSpace: Bool { get }
var isEncrypted: Bool { get }
var isFavourite: Bool { get async }
var pinnedEventIDs: Set<String> { get async }
var membership: Membership { get }
var inviter: RoomMemberProxyProtocol? { get async }
var hasOngoingCall: Bool { get }
var activeRoomCallParticipants: [String] { get }
var canonicalAlias: String? { get }
var ownUserID: String { get }
var name: String? { get }
var topic: String? { get }
/// The room's avatar info for use in a ``RoomAvatarImage``.
var avatar: RoomAvatar { get }
/// The room's avatar URL. Use this for editing and favour ``avatar`` for display.
var avatarURL: URL? { get }
var isPublic: Bool { get }
var isDirect: Bool { get }
var isSpace: Bool { get }
var joinedMembersCount: Int { get }
var activeMembersCount: Int { get }
}
// sourcery: AutoMockable
protocol InvitedRoomProxyProtocol: RoomProxyProtocol {
var inviter: RoomMemberProxyProtocol? { get async }
func rejectInvitation() async -> Result<Void, RoomProxyError>
func acceptInvitation() async -> Result<Void, RoomProxyError>
}
enum JoinedRoomProxyAction {
case roomInfoUpdate
}
// sourcery: AutoMockable
protocol JoinedRoomProxyProtocol: RoomProxyProtocol {
var isEncrypted: Bool { get }
var isFavourite: Bool { get async }
var pinnedEventIDs: Set<String> { get async }
var hasOngoingCall: Bool { get }
var activeRoomCallParticipants: [String] { get }
var membersPublisher: CurrentValuePublisher<[RoomMemberProxyProtocol], Never> { get }
var typingMembersPublisher: CurrentValuePublisher<[String], Never> { get }
var joinedMembersCount: Int { get }
var activeMembersCount: Int { get }
var actionsPublisher: AnyPublisher<RoomProxyAction, Never> { get }
var actionsPublisher: AnyPublisher<JoinedRoomProxyAction, Never> { get }
var timeline: TimelineProxyProtocol { get }
@@ -87,10 +103,6 @@ protocol RoomProxyProtocol {
func getMember(userID: String) async -> Result<RoomMemberProxyProtocol, RoomProxyError>
func rejectInvitation() async -> Result<Void, RoomProxyError>
func acceptInvitation() async -> Result<Void, RoomProxyError>
func invite(userID: String) async -> Result<Void, RoomProxyError>
func setName(_ name: String) async -> Result<Void, RoomProxyError>
@@ -155,7 +167,7 @@ protocol RoomProxyProtocol {
func clearDraft() async -> Result<Void, RoomProxyError>
}
extension RoomProxyProtocol {
extension JoinedRoomProxyProtocol {
var details: RoomDetails {
RoomDetails(id: id,
name: name,