mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Flescio/create room action (#925)
* fix invites users screen * add create room action * add a task runner with timeout, add fetch room summary after creation * rename Runner, add Unit Test, separate UserProfile form client proxy * change to all room summary publisher
This commit is contained in:
@@ -192,9 +192,9 @@ class ClientProxy: ClientProxyProtocol {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func createDirectRoom(with userID: String) async -> Result<String, ClientProxyError> {
|
||||
await Task.dispatch(on: clientQueue) {
|
||||
|
||||
func createDirectRoom(with userID: String, expectedRoomName: String?) async -> Result<String, ClientProxyError> {
|
||||
let result: Result<String, ClientProxyError> = await Task.dispatch(on: clientQueue) {
|
||||
do {
|
||||
let parameters = CreateRoomParameters(name: nil, topic: nil, isEncrypted: true, isDirect: true, visibility: .private, preset: .trustedPrivateChat, invite: [userID], avatar: nil)
|
||||
let result = try self.client.createRoom(request: parameters)
|
||||
@@ -203,6 +203,52 @@ class ClientProxy: ClientProxyProtocol {
|
||||
return .failure(.failedCreatingRoom)
|
||||
}
|
||||
}
|
||||
|
||||
return await waitForRoomSummary(with: result, name: expectedRoomName)
|
||||
}
|
||||
|
||||
func createRoom(with parameters: CreateRoomFlowParameters, userIDs: [String]) async -> Result<String, ClientProxyError> {
|
||||
let result: Result<String, ClientProxyError> = await Task.dispatch(on: clientQueue) {
|
||||
do {
|
||||
let parameters = CreateRoomParameters(name: parameters.name,
|
||||
topic: parameters.topic,
|
||||
isEncrypted: parameters.isRoomPrivate,
|
||||
isDirect: false,
|
||||
visibility: parameters.isRoomPrivate ? .private : .public,
|
||||
preset: parameters.isRoomPrivate ? .privateChat : .publicChat,
|
||||
invite: userIDs,
|
||||
avatar: nil)
|
||||
let roomId = try self.client.createRoom(request: parameters)
|
||||
return .success(roomId)
|
||||
} catch {
|
||||
return .failure(.failedCreatingRoom)
|
||||
}
|
||||
}
|
||||
|
||||
return await waitForRoomSummary(with: result, name: parameters.name)
|
||||
}
|
||||
|
||||
/// Await the room to be available in the room summary list
|
||||
/// - Parameter result: the result of a room creation Task with the roomId
|
||||
private func waitForRoomSummary(with result: Result<String, ClientProxyError>, name: String?) async -> Result<String, ClientProxyError> {
|
||||
guard case .success(let roomId) = result else { return result }
|
||||
let runner = ExpiringTaskRunner { [weak self] in
|
||||
guard let roomLists = self?.allRoomsSummaryProvider?.roomListPublisher.values else {
|
||||
return
|
||||
}
|
||||
// for every list of summaries, we check if we have a room summary with matching ID and name (if present)
|
||||
for await roomList in roomLists {
|
||||
guard let summary = roomList.first(where: { $0.id == roomId }) else { continue }
|
||||
guard let name else { break }
|
||||
if summary.name == name {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// we want to ignore the timeout error, and return the .success case because the room it was properly created already, we are only waiting for it to appear
|
||||
try? await runner.run(timeout: .seconds(10))
|
||||
return result
|
||||
}
|
||||
|
||||
func roomForIdentifier(_ identifier: String) async -> RoomProxyProtocol? {
|
||||
|
||||
@@ -94,7 +94,9 @@ protocol ClientProxyProtocol: AnyObject, MediaLoaderProtocol {
|
||||
|
||||
func directRoomForUserID(_ userID: String) async -> Result<String?, ClientProxyError>
|
||||
|
||||
func createDirectRoom(with userID: String) async -> Result<String, ClientProxyError>
|
||||
func createDirectRoom(with userID: String, expectedRoomName: String?) async -> Result<String, ClientProxyError>
|
||||
|
||||
func createRoom(with parameters: CreateRoomFlowParameters, userIDs: [String]) async -> Result<String, ClientProxyError>
|
||||
|
||||
func roomForIdentifier(_ identifier: String) async -> RoomProxyProtocol?
|
||||
|
||||
|
||||
@@ -53,7 +53,11 @@ class MockClientProxy: ClientProxyProtocol {
|
||||
.failure(.failedRetrievingDirectRoom)
|
||||
}
|
||||
|
||||
func createDirectRoom(with userID: String) async -> Result<String, ClientProxyError> {
|
||||
func createDirectRoom(with userID: String, expectedRoomName: String?) async -> Result<String, ClientProxyError> {
|
||||
.failure(.failedCreatingRoom)
|
||||
}
|
||||
|
||||
func createRoom(with parameters: CreateRoomFlowParameters, userIDs: [String]) async -> Result<String, ClientProxyError> {
|
||||
.failure(.failedCreatingRoom)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user