2020-05-11 16:19:26 +10:00
|
|
|
package org.thoughtcrime.securesms.loki.protocol
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import org.thoughtcrime.securesms.database.Address
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient
|
|
|
|
import org.thoughtcrime.securesms.sms.MessageSender
|
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
|
|
|
|
|
|
|
object ClosedGroupsProtocol {
|
|
|
|
|
|
|
|
fun leaveGroup(context: Context, recipient: Recipient): Boolean {
|
2020-05-11 16:54:31 +10:00
|
|
|
if (!recipient.address.isClosedGroup) { return true }
|
2020-05-11 16:19:26 +10:00
|
|
|
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient)
|
|
|
|
val message = GroupUtil.createGroupLeaveMessage(context, recipient)
|
|
|
|
if (threadID < 0 || !message.isPresent) { return false }
|
|
|
|
MessageSender.send(context, message.get(), threadID, false, null)
|
|
|
|
// Remove the *master* device from the group
|
2020-05-11 16:54:31 +10:00
|
|
|
val masterPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context)
|
|
|
|
val publicKeyToUse = masterPublicKey ?: TextSecurePreferences.getLocalNumber(context)
|
2020-05-11 16:19:26 +10:00
|
|
|
val groupDatabase = DatabaseFactory.getGroupDatabase(context)
|
|
|
|
val groupID = recipient.address.toGroupString()
|
|
|
|
groupDatabase.setActive(groupID, false)
|
2020-05-11 16:54:31 +10:00
|
|
|
groupDatabase.remove(groupID, Address.fromSerialized(publicKeyToUse))
|
2020-05-11 16:19:26 +10:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|