Delete conversation on home screen

This commit is contained in:
SessionHero01 2024-09-12 16:58:40 +10:00
parent dc1075e0c7
commit 32f95337d5
No known key found for this signature in database
3 changed files with 75 additions and 65 deletions

View File

@ -1212,7 +1212,14 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
return false return false
} }
return viewModel.recipient?.let { recipient -> return viewModel.recipient?.let { recipient ->
ConversationMenuHelper.onOptionItemSelected(this, item, recipient, configFactory, storage) ConversationMenuHelper.onOptionItemSelected(
context = this,
item = item,
thread = recipient,
threadID = threadId,
factory = configFactory,
storage = storage
)
} ?: false } ?: false
} }

View File

@ -152,6 +152,7 @@ object ConversationMenuHelper {
context: Context, context: Context,
item: MenuItem, item: MenuItem,
thread: Recipient, thread: Recipient,
threadID: Long,
factory: ConfigFactory, factory: ConfigFactory,
storage: StorageProtocol storage: StorageProtocol
): Boolean { ): Boolean {
@ -166,7 +167,7 @@ object ConversationMenuHelper {
R.id.menu_copy_account_id -> { copyAccountID(context, thread) } R.id.menu_copy_account_id -> { copyAccountID(context, thread) }
R.id.menu_copy_open_group_url -> { copyOpenGroupUrl(context, thread) } R.id.menu_copy_open_group_url -> { copyOpenGroupUrl(context, thread) }
R.id.menu_edit_group -> { editClosedGroup(context, thread) } R.id.menu_edit_group -> { editClosedGroup(context, thread) }
R.id.menu_leave_group -> { leaveClosedGroup(context, thread, factory, storage) } R.id.menu_leave_group -> { leaveClosedGroup(context, thread, threadID, factory, storage) }
R.id.menu_invite_to_open_group -> { inviteContacts(context, thread) } R.id.menu_invite_to_open_group -> { inviteContacts(context, thread) }
R.id.menu_unmute_notifications -> { unmute(context, thread) } R.id.menu_unmute_notifications -> { unmute(context, thread) }
R.id.menu_mute_notifications -> { mute(context, thread) } R.id.menu_mute_notifications -> { mute(context, thread) }
@ -305,9 +306,10 @@ object ConversationMenuHelper {
} }
} }
private fun leaveClosedGroup( fun leaveClosedGroup(
context: Context, context: Context,
thread: Recipient, thread: Recipient,
threadID: Long,
configFactory: ConfigFactory, configFactory: ConfigFactory,
storage: StorageProtocol storage: StorageProtocol
) { ) {
@ -318,14 +320,21 @@ object ConversationMenuHelper {
val accountID = TextSecurePreferences.getLocalNumber(context) val accountID = TextSecurePreferences.getLocalNumber(context)
val isCurrentUserAdmin = admins.any { it.toString() == accountID } val isCurrentUserAdmin = admins.any { it.toString() == accountID }
confirmAndLeaveClosedGroup(context, group.title, isCurrentUserAdmin, doLeave = { confirmAndLeaveClosedGroup(
val groupPublicKey = doubleDecodeGroupID(thread.address.toString()).toHexString() context = context,
groupName = group.title,
isAdmin = isCurrentUserAdmin,
threadID = threadID,
storage = storage,
doLeave = {
val groupPublicKey = doubleDecodeGroupID(thread.address.toString()).toHexString()
check(DatabaseComponent.get(context).lokiAPIDatabase().isClosedGroup(groupPublicKey)) { check(DatabaseComponent.get(context).lokiAPIDatabase().isClosedGroup(groupPublicKey)) {
"Invalid group public key" "Invalid group public key"
}
MessageSender.leave(groupPublicKey, notifyUser = false)
} }
MessageSender.leave(groupPublicKey, notifyUser = false) )
})
} }
thread.isClosedGroupV2Recipient -> { thread.isClosedGroupV2Recipient -> {
@ -339,6 +348,8 @@ object ConversationMenuHelper {
context = context, context = context,
groupName = name, groupName = name,
isAdmin = isAdmin, isAdmin = isAdmin,
threadID = threadID,
storage = storage,
doLeave = { doLeave = {
check(storage.leaveGroup(accountId.hexString, true)) check(storage.leaveGroup(accountId.hexString, true))
} }
@ -351,6 +362,8 @@ object ConversationMenuHelper {
context: Context, context: Context,
groupName: String, groupName: String,
isAdmin: Boolean, isAdmin: Boolean,
threadID: Long,
storage: StorageProtocol,
doLeave: () -> Unit, doLeave: () -> Unit,
) { ) {
val message = if (isAdmin) { val message = if (isAdmin) {
@ -375,6 +388,9 @@ object ConversationMenuHelper {
text(message) text(message)
dangerButton(R.string.leave) { dangerButton(R.string.leave) {
try { try {
// Cancel any outstanding jobs
storage.cancelPendingMessageSendJobs(threadID)
doLeave() doLeave()
} catch (e: Exception) { } catch (e: Exception) {
onLeaveFailed() onLeaveFailed()

View File

@ -73,6 +73,7 @@ import org.thoughtcrime.securesms.home.search.GlobalSearchViewModel
import org.thoughtcrime.securesms.messagerequests.MessageRequestsActivity import org.thoughtcrime.securesms.messagerequests.MessageRequestsActivity
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager import com.bumptech.glide.RequestManager
import org.thoughtcrime.securesms.conversation.v2.menus.ConversationMenuHelper
import org.thoughtcrime.securesms.permissions.Permissions import org.thoughtcrime.securesms.permissions.Permissions
import org.thoughtcrime.securesms.preferences.SettingsActivity import org.thoughtcrime.securesms.preferences.SettingsActivity
import org.thoughtcrime.securesms.recoverypassword.RecoveryPasswordActivity import org.thoughtcrime.securesms.recoverypassword.RecoveryPasswordActivity
@ -592,49 +593,46 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
private fun deleteConversation(thread: ThreadRecord) { private fun deleteConversation(thread: ThreadRecord) {
val threadID = thread.threadId val threadID = thread.threadId
val recipient = thread.recipient val recipient = thread.recipient
if (recipient.isClosedGroupV2Recipient || recipient.isLegacyClosedGroupRecipient) {
ConversationMenuHelper.leaveClosedGroup(
context = this,
thread = recipient,
threadID = threadID,
configFactory = configFactory,
storage = storage
)
return
}
val title: String val title: String
val message: CharSequence val message: CharSequence
var positiveButtonId: Int = R.string.yes var positiveButtonId: Int = R.string.yes
var negativeButtonId: Int = R.string.no var negativeButtonId: Int = R.string.no
if (recipient.isGroupRecipient) { if (recipient.isCommunityRecipient) {
val group = groupDatabase.getGroup(recipient.address.toString()).orNull() title =
if (recipient.isCommunityRecipient) getString(R.string.communityLeave) else getString(
// If you are an admin of this group you can delete it R.string.groupLeave
if (group != null && group.admins.map { it.toString() } )
.contains(textSecurePreferences.getLocalNumber())) { message = Phrase.from(this.applicationContext, R.string.groupLeaveDescription)
title = getString(R.string.groupLeave) .put(GROUP_NAME_KEY, recipient.name.orEmpty())
message = Phrase.from(this.applicationContext, R.string.groupDeleteDescription) .format()
.put(GROUP_NAME_KEY, group.title) }
.format() // If this is a 1-on-1 conversation
} else { else if (recipient.name != null) {
// Otherwise this is either a community, or it's a group you're not an admin of title = getString(R.string.conversationsDelete)
title = message = Phrase.from(this.applicationContext, R.string.conversationsDeleteDescription)
if (recipient.isCommunityRecipient) getString(R.string.communityLeave) else getString( .put(NAME_KEY, recipient.name)
R.string.groupLeave .format()
) }
message = Phrase.from(this.applicationContext, R.string.groupLeaveDescription) else {
.put(GROUP_NAME_KEY, group.title) // If not group-related and we don't have a recipient name then this must be our Note to Self conversation
.format() title = getString(R.string.clearMessages)
} message = getString(R.string.clearMessagesNoteToSelfDescription)
positiveButtonId = R.string.clear
positiveButtonId = R.string.leave
negativeButtonId = R.string.cancel negativeButtonId = R.string.cancel
} else {
// If this is a 1-on-1 conversation
if (recipient.name != null) {
title = getString(R.string.conversationsDelete)
message = Phrase.from(this.applicationContext, R.string.conversationsDeleteDescription)
.put(NAME_KEY, recipient.name)
.format()
}
else {
// If not group-related and we don't have a recipient name then this must be our Note to Self conversation
title = getString(R.string.clearMessages)
message = getString(R.string.clearMessagesNoteToSelfDescription)
positiveButtonId = R.string.clear
negativeButtonId = R.string.cancel
}
} }
showSessionDialog { showSessionDialog {
@ -646,23 +644,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
// Cancel any outstanding jobs // Cancel any outstanding jobs
DatabaseComponent.get(context).sessionJobDatabase() DatabaseComponent.get(context).sessionJobDatabase()
.cancelPendingMessageSendJobs(threadID) .cancelPendingMessageSendJobs(threadID)
// Send a leave group message if this is an active closed group
if (recipient.address.isLegacyClosedGroup && DatabaseComponent.get(context)
.groupDatabase().isActive(recipient.address.toGroupString())
) {
try {
GroupUtil.doubleDecodeGroupID(recipient.address.toString())
.toHexString()
.takeIf(DatabaseComponent.get(context).lokiAPIDatabase()::isClosedGroup)
?.let { MessageSender.explicitLeave(it, true, deleteThread = true) }
} catch (ioe: IOException) {
Log.w(TAG, "Got an IOException while sending leave group message", ioe)
}
}
if (recipient.address.isClosedGroupV2) {
val groupLeave = LibSessionGroupLeavingJob(AccountId(recipient.address.serialize()), true)
JobQueue.shared.add(groupLeave)
}
// Delete the conversation // Delete the conversation
val v2OpenGroup = DatabaseComponent.get(context).lokiThreadDatabase() val v2OpenGroup = DatabaseComponent.get(context).lokiThreadDatabase()
.getOpenGroupChat(threadID) .getOpenGroupChat(threadID)
@ -672,7 +654,12 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
v2OpenGroup.room, v2OpenGroup.room,
context context
) )
} else {
lifecycleScope.launch(Dispatchers.IO) {
threadDb.deleteConversation(threadID)
}
} }
// Update the badge count // Update the badge count
ApplicationContext.getInstance(context).messageNotifier.updateNotification(context) ApplicationContext.getInstance(context).messageNotifier.updateNotification(context)