This commit is contained in:
Niels Andriesse 2021-05-14 13:13:02 +10:00
parent ed9d1c7471
commit 5eed7a3cdd

View File

@ -24,7 +24,6 @@ import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import network.loki.messenger.R import network.loki.messenger.R
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.Subscribe
@ -315,25 +314,24 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
val threadID = thread.threadId val threadID = thread.threadId
val recipient = thread.recipient val recipient = thread.recipient
val threadDB = DatabaseFactory.getThreadDatabase(this) val threadDB = DatabaseFactory.getThreadDatabase(this)
val dialogMessage: String val message: String
if (recipient.isGroupRecipient) { if (recipient.isGroupRecipient) {
val group = DatabaseFactory.getGroupDatabase(this).getGroup(recipient.address.toString()).orNull() val group = DatabaseFactory.getGroupDatabase(this).getGroup(recipient.address.toString()).orNull()
if (group != null && group.admins.map { it.toString() }.contains(TextSecurePreferences.getLocalNumber(this))) { if (group != null && group.admins.map { it.toString() }.contains(TextSecurePreferences.getLocalNumber(this))) {
dialogMessage = "Because you are the creator of this group it will be deleted for everyone. This cannot be undone." message = "Because you are the creator of this group it will be deleted for everyone. This cannot be undone."
} else { } else {
dialogMessage = resources.getString(R.string.activity_home_leave_group_dialog_message) message = resources.getString(R.string.activity_home_leave_group_dialog_message)
} }
} else { } else {
dialogMessage = resources.getString(R.string.activity_home_delete_conversation_dialog_message) message = resources.getString(R.string.activity_home_delete_conversation_dialog_message)
} }
val dialog = AlertDialog.Builder(this) val dialog = AlertDialog.Builder(this)
dialog.setMessage(dialogMessage) dialog.setMessage(message)
dialog.setPositiveButton(R.string.yes) { _, _ -> dialog.setPositiveButton(R.string.yes) { _, _ ->
lifecycleScope.launch(Dispatchers.Main) { lifecycleScope.launch(Dispatchers.Main) {
val context = this@HomeActivity as Context val context = this@HomeActivity as Context
// Cancel any outstanding jobs
DatabaseFactory.getSessionJobDatabase(this@HomeActivity).cancelPendingMessageSendJobs(threadID) DatabaseFactory.getSessionJobDatabase(context).cancelPendingMessageSendJobs(threadID)
// Send a leave group message if this is an active closed group // Send a leave group message if this is an active closed group
if (recipient.address.isClosedGroup && DatabaseFactory.getGroupDatabase(context).isActive(recipient.address.toGroupString())) { if (recipient.address.isClosedGroup && DatabaseFactory.getGroupDatabase(context).isActive(recipient.address.toGroupString())) {
var isClosedGroup: Boolean var isClosedGroup: Boolean
@ -352,34 +350,28 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
return@launch return@launch
} }
} }
// Delete the conversation
withContext(Dispatchers.IO) { val v1OpenGroup = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID)
val publicChat = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID) val v2OpenGroup = DatabaseFactory.getLokiThreadDatabase(context).getOpenGroupChat(threadID)
val openGroupV2 = DatabaseFactory.getLokiThreadDatabase(context).getOpenGroupChat(threadID) if (v1OpenGroup != null) {
//TODO Move open group related logic to OpenGroupUtilities / PublicChatManager / GroupManager val apiDB = DatabaseFactory.getLokiAPIDatabase(context)
if (publicChat != null) { apiDB.removeLastMessageServerID(v1OpenGroup.channel, v1OpenGroup.server)
val apiDB = DatabaseFactory.getLokiAPIDatabase(context) apiDB.removeLastDeletionServerID(v1OpenGroup.channel, v1OpenGroup.server)
apiDB.removeLastMessageServerID(publicChat.channel, publicChat.server) apiDB.clearOpenGroupProfilePictureURL(v1OpenGroup.channel, v1OpenGroup.server)
apiDB.removeLastDeletionServerID(publicChat.channel, publicChat.server) OpenGroupAPI.leave(v1OpenGroup.channel, v1OpenGroup.server)
apiDB.clearOpenGroupProfilePictureURL(publicChat.channel, publicChat.server) ApplicationContext.getInstance(context).publicChatManager
.removeChat(v1OpenGroup.server, v1OpenGroup.channel)
OpenGroupAPI.leave(publicChat.channel, publicChat.server) } else if (v2OpenGroup != null) {
val apiDB = DatabaseFactory.getLokiAPIDatabase(context)
ApplicationContext.getInstance(context).publicChatManager apiDB.removeLastMessageServerID(v2OpenGroup.room, v2OpenGroup.server)
.removeChat(publicChat.server, publicChat.channel) apiDB.removeLastDeletionServerID(v2OpenGroup.room, v2OpenGroup.server)
} else if (openGroupV2 != null) { ApplicationContext.getInstance(context).publicChatManager
val apiDB = DatabaseFactory.getLokiAPIDatabase(context) .removeChat(v2OpenGroup.server, v2OpenGroup.room)
apiDB.removeLastMessageServerID(openGroupV2.room, openGroupV2.server) } else {
apiDB.removeLastDeletionServerID(openGroupV2.room, openGroupV2.server) threadDB.deleteConversation(threadID)
ApplicationContext.getInstance(context).publicChatManager
.removeChat(openGroupV2.server, openGroupV2.room)
} else {
threadDB.deleteConversation(threadID)
}
ApplicationContext.getInstance(context).messageNotifier.updateNotification(context)
} }
// Update the badge count
ApplicationContext.getInstance(context).messageNotifier.updateNotification(context)
// Notify the user // Notify the user
val toastMessage = if (recipient.isGroupRecipient) R.string.MessageRecord_left_group else R.string.activity_home_conversation_deleted_message val toastMessage = if (recipient.isGroupRecipient) R.string.MessageRecord_left_group else R.string.activity_home_conversation_deleted_message
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show() Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show()