mobile: show errors when joining group (#861)
* mobile: show errors when joining group * correct titles Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> * improvements Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
ce91dcde7f
commit
cc0a74fae4
@@ -579,19 +579,20 @@ func apiAddMember(groupId: Int64, contactId: Int64, memberRole: GroupMemberRole)
|
||||
throw r
|
||||
}
|
||||
|
||||
func joinGroup(_ groupId: Int64) async {
|
||||
do {
|
||||
let groupInfo = try await apiJoinGroup(groupId)
|
||||
DispatchQueue.main.async { ChatModel.shared.updateGroup(groupInfo) }
|
||||
} catch let error {
|
||||
logger.error("joinGroup error: \(responseError(error))")
|
||||
}
|
||||
enum JoinGroupResult {
|
||||
case joined(groupInfo: GroupInfo)
|
||||
case invitationRemoved
|
||||
case groupNotFound
|
||||
}
|
||||
|
||||
func apiJoinGroup(_ groupId: Int64) async throws -> GroupInfo {
|
||||
func apiJoinGroup(_ groupId: Int64) async throws -> JoinGroupResult {
|
||||
let r = await chatSendCmd(.apiJoinGroup(groupId: groupId))
|
||||
if case let .userAcceptedGroupSent(groupInfo) = r { return groupInfo }
|
||||
throw r
|
||||
switch r {
|
||||
case let .userAcceptedGroupSent(groupInfo): return .joined(groupInfo: groupInfo)
|
||||
case .chatCmdError(.errorAgent(.SMP(.AUTH))): return .invitationRemoved
|
||||
case .chatCmdError(.errorStore(.groupNotFound)): return .groupNotFound
|
||||
default: throw r
|
||||
}
|
||||
}
|
||||
|
||||
func apiRemoveMember(groupId: Int64, memberId: Int64) async throws -> GroupMember {
|
||||
|
||||
@@ -331,6 +331,36 @@ struct ChatListNavLink: View {
|
||||
}
|
||||
}
|
||||
|
||||
func joinGroup(_ groupId: Int64) async {
|
||||
do {
|
||||
let r = try await apiJoinGroup(groupId)
|
||||
switch r {
|
||||
case let .joined(groupInfo):
|
||||
await MainActor.run { ChatModel.shared.updateGroup(groupInfo) }
|
||||
case .invitationRemoved:
|
||||
AlertManager.shared.showAlertMsg(title: "Invitation expired!", message: "Group invitation is no longer valid, it was removed by sender.")
|
||||
await deleteGroup()
|
||||
case .groupNotFound:
|
||||
AlertManager.shared.showAlertMsg(title: "No group!", message: "This group no longer exists.")
|
||||
await deleteGroup()
|
||||
}
|
||||
} catch let error {
|
||||
let err = responseError(error)
|
||||
AlertManager.shared.showAlert(Alert(title: Text("Error joining group"), message: Text(err)))
|
||||
logger.error("apiJoinGroup error: \(err)")
|
||||
}
|
||||
|
||||
func deleteGroup() async {
|
||||
do {
|
||||
// TODO this API should update chat item with the invitation as well
|
||||
try await apiDeleteChat(type: .group, id: groupId)
|
||||
await MainActor.run { ChatModel.shared.removeChat("#\(groupId)") }
|
||||
} catch {
|
||||
logger.error("apiDeleteChat error: \(responseError(error))")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ChatListNavLink_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
@State var chatId: String? = "@1"
|
||||
|
||||
Reference in New Issue
Block a user