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:
Evgeny Poberezkin
2022-08-01 08:34:07 +01:00
committed by GitHub
parent ce91dcde7f
commit cc0a74fae4
7 changed files with 75 additions and 24 deletions

View File

@@ -577,11 +577,26 @@ open class ChatController(private val ctrl: ChatCtrl, val ntfManager: NtfManager
Log.e(TAG, "apiAddMember bad response: ${r.responseType} ${r.details}")
}
suspend fun apiJoinGroup(groupId: Long): GroupInfo? {
suspend fun apiJoinGroup(groupId: Long) {
val r = sendCmd(CC.ApiJoinGroup(groupId))
if (r is CR.UserAcceptedGroupSent) return r.groupInfo
Log.e(TAG, "apiJoinGroup bad response: ${r.responseType} ${r.details}")
return null
when (r) {
is CR.UserAcceptedGroupSent ->
chatModel.updateGroup(r.groupInfo)
is CR.ChatCmdError -> {
val e = r.chatError
suspend fun deleteGroup() { if (apiDeleteChat(ChatType.Group, groupId)) { chatModel.removeChat("#$groupId") } }
if (e is ChatError.ChatErrorAgent && e.agentError is AgentErrorType.SMP && e.agentError.smpErr is SMPErrorType.AUTH) {
deleteGroup()
AlertManager.shared.showAlertMsg(generalGetString(R.string.alert_title_group_invitation_expired), generalGetString(R.string.alert_message_group_invitation_expired))
} else if (e is ChatError.ChatErrorStore && e.storeError is StoreError.GroupNotFound) {
deleteGroup()
AlertManager.shared.showAlertMsg(generalGetString(R.string.alert_title_no_group), generalGetString(R.string.alert_message_no_group))
} else {
AlertManager.shared.showAlertMsg(generalGetString(R.string.alert_title_join_group_error), "$e")
}
}
else -> Log.e(TAG, "apiJoinGroup bad response: ${r.responseType} ${r.details}")
}
}
suspend fun apiRemoveMember(groupId: Long, memberId: Long): GroupMember? {
@@ -776,13 +791,6 @@ open class ChatController(private val ctrl: ChatCtrl, val ntfManager: NtfManager
}
}
suspend fun joinGroup(groupId: Long) {
val groupInfo = apiJoinGroup(groupId)
if (groupInfo != null) {
chatModel.updateGroup(groupInfo)
}
}
suspend fun leaveGroup(groupId: Long) {
val groupInfo = apiLeaveGroup(groupId)
if (groupInfo != null) {
@@ -1507,8 +1515,10 @@ sealed class ChatErrorType {
sealed class StoreError {
val string: String get() = when (this) {
is UserContactLinkNotFound -> "userContactLinkNotFound"
is GroupNotFound -> "groupNotFound"
}
@Serializable @SerialName("userContactLinkNotFound") class UserContactLinkNotFound: StoreError()
@Serializable @SerialName("groupNotFound") class GroupNotFound: StoreError()
}
@Serializable

View File

@@ -140,7 +140,7 @@ fun ChatView(chatModel: ChatModel) {
withApi { chatModel.controller.receiveFile(fileId) }
},
joinGroup = { groupId ->
withApi { chatModel.controller.joinGroup(groupId) }
withApi { chatModel.controller.apiJoinGroup(groupId) }
},
startCall = { media ->
val cInfo = chat.chatInfo

View File

@@ -193,7 +193,7 @@ fun JoinGroupAction(groupInfo: GroupInfo, chatModel: ChatModel, showMenu: Mutabl
stringResource(R.string.join_group_button),
Icons.Outlined.Login,
onClick = {
withApi { chatModel.controller.joinGroup(groupInfo.groupId) }
withApi { chatModel.controller.apiJoinGroup(groupInfo.groupId) }
showMenu.value = false
}
)
@@ -359,7 +359,7 @@ fun acceptGroupInvitationAlertDialog(groupInfo: GroupInfo, chatModel: ChatModel)
title = generalGetString(R.string.join_group_question),
text = generalGetString(R.string.you_are_invited_to_group_join_to_connect_with_group_members),
confirmText = generalGetString(R.string.join_group_button),
onConfirm = { withApi { chatModel.controller.joinGroup(groupInfo.groupId) } },
onConfirm = { withApi { chatModel.controller.apiJoinGroup(groupInfo.groupId) } },
dismissText = generalGetString(R.string.delete_verb),
onDismiss = { deleteGroup(groupInfo, chatModel) }
)

View File

@@ -511,6 +511,11 @@
<string name="you_will_stop_receiving_messages_from_this_group_chat_history_will_be_preserved">Вы перестанете получать сообщения от этой группы. История чата будет сохранена.</string>
<string name="icon_descr_add_members">Пригласить участников</string>
<string name="icon_descr_group_inactive">Группа неактивна</string>
<string name="alert_title_group_invitation_expired">Приглашение истекло!</string>
<string name="alert_message_group_invitation_expired">Приглашение в группу больше не действительно, оно было удалено отправителем.</string>
<string name="alert_title_no_group">Группа не найдена!</string>
<string name="alert_message_no_group">Эта группа больше не существует.</string>
<string name="alert_title_join_group_error">Ошибка приглашения</string>
<!-- CIGroupInvitationView.kt -->
<string name="you_sent_group_invitation">Вы отправили приглашение в группу</string>

View File

@@ -513,6 +513,11 @@
<string name="you_will_stop_receiving_messages_from_this_group_chat_history_will_be_preserved">You will stop receiving messages from this group. Chat history will be preserved.</string>
<string name="icon_descr_add_members">Invite members</string>
<string name="icon_descr_group_inactive">Group inactive</string>
<string name="alert_title_group_invitation_expired">Invitation expired!</string>
<string name="alert_message_group_invitation_expired">Group invitation is no longer valid, it was removed by sender.</string>
<string name="alert_title_no_group">Group not found!</string>
<string name="alert_message_no_group">This group no longer exists.</string>
<string name="alert_title_join_group_error">Error joining group</string>
<!-- CIGroupInvitationView.kt -->
<string name="you_sent_group_invitation">You sent group invitation</string>

View File

@@ -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 {

View File

@@ -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"