2020-12-07 05:22:02 +01:00
|
|
|
package org.session.libsession.messaging.sending_receiving
|
|
|
|
|
2021-01-11 23:58:38 +01:00
|
|
|
import android.text.TextUtils
|
2021-04-26 03:14:45 +02:00
|
|
|
import org.session.libsession.messaging.MessagingModuleConfiguration
|
2020-12-18 06:48:45 +01:00
|
|
|
import org.session.libsession.messaging.jobs.AttachmentDownloadJob
|
2021-01-08 01:13:05 +01:00
|
|
|
import org.session.libsession.messaging.jobs.JobQueue
|
2020-12-07 05:22:02 +01:00
|
|
|
import org.session.libsession.messaging.messages.Message
|
2021-02-09 01:45:38 +01:00
|
|
|
import org.session.libsession.messaging.messages.control.*
|
2020-12-18 06:48:45 +01:00
|
|
|
import org.session.libsession.messaging.messages.visible.Attachment
|
2020-12-07 05:22:02 +01:00
|
|
|
import org.session.libsession.messaging.messages.visible.VisibleMessage
|
2021-01-11 23:58:38 +01:00
|
|
|
import org.session.libsession.messaging.sending_receiving.attachments.PointerAttachment
|
2021-04-26 03:23:09 +02:00
|
|
|
import org.session.libsession.messaging.sending_receiving.data_extraction.DataExtractionNotificationInfoMessage
|
|
|
|
import org.session.libsession.messaging.sending_receiving.link_preview.LinkPreview
|
2020-12-07 05:22:02 +01:00
|
|
|
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
2021-01-11 23:58:38 +01:00
|
|
|
import org.session.libsession.messaging.sending_receiving.quotes.QuoteModel
|
2020-12-10 05:33:57 +01:00
|
|
|
import org.session.libsession.messaging.threads.Address
|
2021-02-09 01:45:38 +01:00
|
|
|
import org.session.libsession.messaging.threads.GroupRecord
|
2021-01-08 01:13:05 +01:00
|
|
|
import org.session.libsession.messaging.threads.recipients.Recipient
|
2020-12-10 05:33:57 +01:00
|
|
|
import org.session.libsession.utilities.GroupUtil
|
2021-01-08 01:13:05 +01:00
|
|
|
import org.session.libsession.utilities.SSKEnvironment
|
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
2021-03-30 07:23:12 +02:00
|
|
|
import org.session.libsession.utilities.preferences.ProfileKeyUtil
|
2021-05-18 01:21:56 +02:00
|
|
|
import org.session.libsignal.crypto.ecc.DjbECPrivateKey
|
|
|
|
import org.session.libsignal.crypto.ecc.DjbECPublicKey
|
|
|
|
import org.session.libsignal.crypto.ecc.ECKeyPair
|
2021-05-18 01:26:08 +02:00
|
|
|
import org.session.libsignal.utilities.guava.Optional
|
2021-05-18 01:50:16 +02:00
|
|
|
import org.session.libsignal.messages.SignalServiceGroup
|
2021-05-18 01:44:06 +02:00
|
|
|
import org.session.libsignal.protos.SignalServiceProtos
|
2021-05-18 01:34:45 +02:00
|
|
|
import org.session.libsignal.utilities.removing05PrefixIfNeeded
|
|
|
|
import org.session.libsignal.utilities.toHexString
|
2021-03-30 07:23:12 +02:00
|
|
|
import org.session.libsignal.utilities.Base64
|
2021-05-18 01:12:33 +02:00
|
|
|
import org.session.libsignal.utilities.Log
|
2021-01-08 01:13:05 +01:00
|
|
|
import java.security.MessageDigest
|
2020-12-07 05:22:02 +01:00
|
|
|
import java.util.*
|
2020-12-18 06:48:45 +01:00
|
|
|
import kotlin.collections.ArrayList
|
2020-12-07 05:22:02 +01:00
|
|
|
|
2021-05-13 07:27:08 +02:00
|
|
|
internal fun MessageReceiver.isBlocked(publicKey: String): Boolean {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
2021-01-13 07:11:30 +01:00
|
|
|
val recipient = Recipient.from(context, Address.fromSerialized(publicKey), false)
|
|
|
|
return recipient.isBlocked
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun MessageReceiver.handle(message: Message, proto: SignalServiceProtos.Content, openGroupID: String?) {
|
|
|
|
when (message) {
|
|
|
|
is ReadReceipt -> handleReadReceipt(message)
|
|
|
|
is TypingIndicator -> handleTypingIndicator(message)
|
2021-02-09 01:45:38 +01:00
|
|
|
is ClosedGroupControlMessage -> handleClosedGroupControlMessage(message)
|
2021-03-25 04:55:23 +01:00
|
|
|
is ExpirationTimerUpdate -> handleExpirationTimerUpdate(message)
|
2021-03-16 04:56:47 +01:00
|
|
|
is DataExtractionNotification -> handleDataExtractionNotification(message)
|
2021-02-09 01:45:38 +01:00
|
|
|
is ConfigurationMessage -> handleConfigurationMessage(message)
|
2020-12-07 05:22:02 +01:00
|
|
|
is VisibleMessage -> handleVisibleMessage(message, proto, openGroupID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 07:18:41 +02:00
|
|
|
// region Control Messages
|
2020-12-07 05:22:02 +01:00
|
|
|
private fun MessageReceiver.handleReadReceipt(message: ReadReceipt) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
2021-03-05 00:17:34 +01:00
|
|
|
SSKEnvironment.shared.readReceiptManager.processReadReceipts(context, message.sender!!, message.timestamps!!, message.receivedTimestamp!!)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun MessageReceiver.handleTypingIndicator(message: TypingIndicator) {
|
|
|
|
when (message.kind!!) {
|
|
|
|
TypingIndicator.Kind.STARTED -> showTypingIndicatorIfNeeded(message.sender!!)
|
|
|
|
TypingIndicator.Kind.STOPPED -> hideTypingIndicatorIfNeeded(message.sender!!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun MessageReceiver.showTypingIndicatorIfNeeded(senderPublicKey: String) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
2021-01-08 01:13:05 +01:00
|
|
|
val address = Address.fromSerialized(senderPublicKey)
|
2021-04-26 03:14:45 +02:00
|
|
|
val threadID = MessagingModuleConfiguration.shared.storage.getThreadIdFor(address) ?: return
|
2021-01-20 06:29:52 +01:00
|
|
|
SSKEnvironment.shared.typingIndicators.didReceiveTypingStartedMessage(context, threadID, address, 1)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun MessageReceiver.hideTypingIndicatorIfNeeded(senderPublicKey: String) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
2021-01-08 01:13:05 +01:00
|
|
|
val address = Address.fromSerialized(senderPublicKey)
|
2021-04-26 03:14:45 +02:00
|
|
|
val threadID = MessagingModuleConfiguration.shared.storage.getThreadIdFor(address) ?: return
|
2021-01-20 06:29:52 +01:00
|
|
|
SSKEnvironment.shared.typingIndicators.didReceiveTypingStoppedMessage(context, threadID, address, 1, false)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun MessageReceiver.cancelTypingIndicatorsIfNeeded(senderPublicKey: String) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
2021-01-08 01:13:05 +01:00
|
|
|
val address = Address.fromSerialized(senderPublicKey)
|
2021-04-26 03:14:45 +02:00
|
|
|
val threadID = MessagingModuleConfiguration.shared.storage.getThreadIdFor(address) ?: return
|
2021-01-20 06:29:52 +01:00
|
|
|
SSKEnvironment.shared.typingIndicators.didReceiveIncomingMessage(context, threadID, address, 1)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 04:55:23 +01:00
|
|
|
private fun MessageReceiver.handleExpirationTimerUpdate(message: ExpirationTimerUpdate) {
|
2020-12-07 05:22:02 +01:00
|
|
|
if (message.duration!! > 0) {
|
2021-03-25 04:55:23 +01:00
|
|
|
SSKEnvironment.shared.messageExpirationManager.setExpirationTimer(message)
|
2020-12-07 05:22:02 +01:00
|
|
|
} else {
|
2021-03-25 04:55:23 +01:00
|
|
|
SSKEnvironment.shared.messageExpirationManager.disableExpirationTimer(message)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 04:56:47 +01:00
|
|
|
private fun MessageReceiver.handleDataExtractionNotification(message: DataExtractionNotification) {
|
2021-05-13 07:42:14 +02:00
|
|
|
// We don't handle data extraction messages for groups (they shouldn't be sent, but just in case we filter them here too)
|
2021-04-09 08:06:12 +02:00
|
|
|
if (message.groupPublicKey != null) return
|
2021-04-26 03:14:45 +02:00
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
2021-03-16 04:56:47 +01:00
|
|
|
val senderPublicKey = message.sender!!
|
|
|
|
val notification: DataExtractionNotificationInfoMessage = when(message.kind) {
|
|
|
|
is DataExtractionNotification.Kind.Screenshot -> DataExtractionNotificationInfoMessage(DataExtractionNotificationInfoMessage.Kind.SCREENSHOT)
|
2021-04-20 09:07:21 +02:00
|
|
|
is DataExtractionNotification.Kind.MediaSaved -> DataExtractionNotificationInfoMessage(DataExtractionNotificationInfoMessage.Kind.MEDIA_SAVED)
|
2021-03-16 04:56:47 +01:00
|
|
|
else -> return
|
|
|
|
}
|
2021-04-09 08:06:12 +02:00
|
|
|
storage.insertDataExtractionNotificationMessage(senderPublicKey, notification, message.sentTimestamp!!)
|
2021-03-16 04:56:47 +01:00
|
|
|
}
|
|
|
|
|
2021-04-26 07:18:12 +02:00
|
|
|
private fun handleConfigurationMessage(message: ConfigurationMessage) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
2021-05-13 07:42:14 +02:00
|
|
|
if (TextSecurePreferences.getConfigurationMessageSynced(context)
|
|
|
|
&& !TextSecurePreferences.shouldUpdateProfile(context, message.sentTimestamp!!)) return
|
2021-03-30 07:23:12 +02:00
|
|
|
val userPublicKey = storage.getUserPublicKey()
|
|
|
|
if (userPublicKey == null || message.sender != storage.getUserPublicKey()) return
|
|
|
|
TextSecurePreferences.setConfigurationMessageSynced(context, true)
|
|
|
|
TextSecurePreferences.setLastProfileUpdateTime(context, message.sentTimestamp!!)
|
2021-02-09 01:45:38 +01:00
|
|
|
val allClosedGroupPublicKeys = storage.getAllClosedGroupPublicKeys()
|
2021-05-13 07:42:14 +02:00
|
|
|
for (closedGroup in message.closedGroups) {
|
|
|
|
if (allClosedGroupPublicKeys.contains(closedGroup.publicKey)) continue
|
|
|
|
handleNewClosedGroup(message.sender!!, message.sentTimestamp!!, closedGroup.publicKey, closedGroup.name,
|
|
|
|
closedGroup.encryptionKeyPair!!, closedGroup.members, closedGroup.admins, message.sentTimestamp!!)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
val allOpenGroups = storage.getAllOpenGroups().map { it.value.server }
|
2021-05-12 06:48:13 +02:00
|
|
|
val allV2OpenGroups = storage.getAllV2OpenGroups().map { it.value.joinURL }
|
2021-02-09 01:45:38 +01:00
|
|
|
for (openGroup in message.openGroups) {
|
2021-05-05 09:29:27 +02:00
|
|
|
if (allOpenGroups.contains(openGroup) || allV2OpenGroups.contains(openGroup)) continue
|
2021-02-09 03:16:33 +01:00
|
|
|
storage.addOpenGroup(openGroup, 1)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
2021-03-30 07:23:12 +02:00
|
|
|
if (message.displayName.isNotEmpty()) {
|
|
|
|
TextSecurePreferences.setProfileName(context, message.displayName)
|
|
|
|
storage.setDisplayName(userPublicKey, message.displayName)
|
|
|
|
}
|
2021-05-13 07:42:14 +02:00
|
|
|
if (message.profileKey.isNotEmpty() && !message.profilePicture.isNullOrEmpty()
|
|
|
|
&& TextSecurePreferences.getProfilePictureURL(context) != message.profilePicture) {
|
2021-03-30 07:23:12 +02:00
|
|
|
val profileKey = Base64.encodeBytes(message.profileKey)
|
|
|
|
ProfileKeyUtil.setEncodedProfileKey(context, profileKey)
|
|
|
|
storage.setProfileKeyForRecipient(userPublicKey, message.profileKey)
|
2021-05-13 07:42:14 +02:00
|
|
|
storage.setUserProfilePictureUrl(message.profilePicture!!)
|
2021-03-30 07:23:12 +02:00
|
|
|
}
|
|
|
|
storage.addContacts(message.contacts)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
2021-04-30 08:09:11 +02:00
|
|
|
//endregion
|
|
|
|
|
2021-05-17 07:18:41 +02:00
|
|
|
// region Visible Messages
|
2020-12-07 05:22:02 +01:00
|
|
|
fun MessageReceiver.handleVisibleMessage(message: VisibleMessage, proto: SignalServiceProtos.Content, openGroupID: String?) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
2021-04-27 01:38:07 +02:00
|
|
|
val userPublicKey = storage.getUserPublicKey()
|
2021-04-29 09:13:42 +02:00
|
|
|
// Get or create thread
|
2021-05-13 07:42:14 +02:00
|
|
|
// FIXME: In case this is an open group this actually * doesn't * create the thread if it doesn't yet
|
|
|
|
// exist. This is intentional, but it's very non-obvious.
|
2021-04-29 09:13:42 +02:00
|
|
|
val threadID = storage.getOrCreateThreadIdFor(message.syncTarget
|
2021-05-13 01:38:39 +02:00
|
|
|
?: message.sender!!, message.groupPublicKey, openGroupID)
|
2021-05-12 08:48:18 +02:00
|
|
|
if (threadID < 0) {
|
2021-05-13 07:42:14 +02:00
|
|
|
// Thread doesn't exist; should only be reached in a case where we are processing open group messages for a no longer existent thread
|
2021-05-12 08:48:18 +02:00
|
|
|
throw MessageReceiver.Error.NoThread
|
|
|
|
}
|
2021-04-29 09:13:42 +02:00
|
|
|
val openGroup = threadID.let {
|
|
|
|
storage.getOpenGroup(it.toString())
|
|
|
|
}
|
2020-12-18 06:48:45 +01:00
|
|
|
// Update profile if needed
|
2021-05-13 07:42:14 +02:00
|
|
|
val profile = message.profile
|
|
|
|
if (profile != null && userPublicKey != message.sender && openGroup == null) { // Don't do this in V1 open groups
|
2021-01-08 01:13:05 +01:00
|
|
|
val profileManager = SSKEnvironment.shared.profileManager
|
|
|
|
val recipient = Recipient.from(context, Address.fromSerialized(message.sender!!), false)
|
2021-05-13 07:42:14 +02:00
|
|
|
val displayName = profile.displayName!!
|
2021-04-27 01:38:07 +02:00
|
|
|
if (displayName.isNotEmpty()) {
|
2021-04-26 07:44:31 +02:00
|
|
|
profileManager.setDisplayName(context, recipient, displayName)
|
2021-01-08 01:13:05 +01:00
|
|
|
}
|
2021-05-13 07:42:14 +02:00
|
|
|
if (profile.profileKey?.isNotEmpty() == true && profile.profilePictureURL?.isNotEmpty() == true
|
|
|
|
&& (recipient.profileKey == null || !MessageDigest.isEqual(recipient.profileKey, profile.profileKey))) {
|
|
|
|
profileManager.setProfileKey(context, recipient, profile.profileKey!!)
|
2021-01-13 07:11:30 +01:00
|
|
|
profileManager.setUnidentifiedAccessMode(context, recipient, Recipient.UnidentifiedAccessMode.UNKNOWN)
|
2021-05-13 07:42:14 +02:00
|
|
|
profileManager.setProfilePictureURL(context, recipient, profile.profilePictureURL!!)
|
2021-01-08 01:13:05 +01:00
|
|
|
}
|
2020-12-18 06:48:45 +01:00
|
|
|
}
|
|
|
|
// Parse quote if needed
|
2021-01-11 23:58:38 +01:00
|
|
|
var quoteModel: QuoteModel? = null
|
2020-12-18 06:48:45 +01:00
|
|
|
if (message.quote != null && proto.dataMessage.hasQuote()) {
|
2021-01-11 23:58:38 +01:00
|
|
|
val quote = proto.dataMessage.quote
|
|
|
|
val author = Address.fromSerialized(quote.author)
|
2021-05-13 07:42:14 +02:00
|
|
|
val messageDataProvider = MessagingModuleConfiguration.shared.messageDataProvider
|
|
|
|
val messageInfo = messageDataProvider.getMessageForQuote(quote.id, author)
|
2021-03-30 04:09:40 +02:00
|
|
|
if (messageInfo != null) {
|
2021-05-13 07:42:14 +02:00
|
|
|
val attachments = if (messageInfo.second) messageDataProvider.getAttachmentsAndLinkPreviewFor(messageInfo.first) else ArrayList()
|
|
|
|
quoteModel = QuoteModel(quote.id, author, messageDataProvider.getMessageBodyFor(quote.id, quote.author), false, attachments)
|
2021-01-11 23:58:38 +01:00
|
|
|
} else {
|
|
|
|
quoteModel = QuoteModel(quote.id, author, quote.text, true, PointerAttachment.forPointers(proto.dataMessage.quote.attachmentsList))
|
|
|
|
}
|
2020-12-18 06:48:45 +01:00
|
|
|
}
|
|
|
|
// Parse link preview if needed
|
2021-01-11 23:58:38 +01:00
|
|
|
val linkPreviews: MutableList<LinkPreview?> = mutableListOf()
|
2020-12-18 06:48:45 +01:00
|
|
|
if (message.linkPreview != null && proto.dataMessage.previewCount > 0) {
|
2021-01-11 23:58:38 +01:00
|
|
|
for (preview in proto.dataMessage.previewList) {
|
|
|
|
val thumbnail = PointerAttachment.forPointer(preview.image)
|
|
|
|
val url = Optional.fromNullable(preview.url)
|
|
|
|
val title = Optional.fromNullable(preview.title)
|
|
|
|
val hasContent = !TextUtils.isEmpty(title.or("")) || thumbnail.isPresent
|
|
|
|
if (hasContent) {
|
|
|
|
val linkPreview = LinkPreview(url.get(), title.or(""), thumbnail)
|
|
|
|
linkPreviews.add(linkPreview)
|
|
|
|
} else {
|
|
|
|
Log.w("Loki", "Discarding an invalid link preview. hasContent: $hasContent")
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 06:48:45 +01:00
|
|
|
}
|
2021-05-13 07:42:14 +02:00
|
|
|
// Parse attachments if needed
|
2021-03-15 03:35:05 +01:00
|
|
|
val attachments = proto.dataMessage.attachmentsList.mapNotNull { proto ->
|
|
|
|
val attachment = Attachment.fromProto(proto)
|
|
|
|
if (!attachment.isValid()) {
|
|
|
|
return@mapNotNull null
|
|
|
|
} else {
|
|
|
|
return@mapNotNull attachment
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 06:48:45 +01:00
|
|
|
// Persist the message
|
|
|
|
message.threadID = threadID
|
2021-04-30 08:53:53 +02:00
|
|
|
val messageID = storage.persist(message, quoteModel, linkPreviews, message.groupPublicKey, openGroupID, attachments) ?: throw MessageReceiver.Error.DuplicateMessage
|
2021-03-15 03:35:05 +01:00
|
|
|
// Parse & persist attachments
|
2020-12-18 06:48:45 +01:00
|
|
|
// Start attachment downloads if needed
|
2021-03-15 03:35:05 +01:00
|
|
|
storage.getAttachmentsForMessage(messageID).forEach { attachment ->
|
|
|
|
attachment.attachmentId?.let { id ->
|
|
|
|
val downloadJob = AttachmentDownloadJob(id.rowId, messageID)
|
|
|
|
JobQueue.shared.add(downloadJob)
|
|
|
|
}
|
2020-12-18 06:48:45 +01:00
|
|
|
}
|
2021-04-29 09:13:42 +02:00
|
|
|
val openGroupServerID = message.openGroupServerMessageID
|
|
|
|
if (openGroupServerID != null) {
|
2021-05-14 07:55:38 +02:00
|
|
|
val isSms = !(message.isMediaMessage() || attachments.isNotEmpty())
|
|
|
|
storage.setOpenGroupServerMessageID(messageID, openGroupServerID, threadID, isSms)
|
2021-04-29 09:13:42 +02:00
|
|
|
}
|
2021-01-11 23:58:38 +01:00
|
|
|
// Cancel any typing indicators if needed
|
|
|
|
cancelTypingIndicatorsIfNeeded(message.sender!!)
|
|
|
|
//Notify the user if needed
|
|
|
|
SSKEnvironment.shared.notificationManager.updateNotification(context, threadID)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
2021-04-30 08:09:11 +02:00
|
|
|
//endregion
|
|
|
|
|
2021-05-17 07:18:41 +02:00
|
|
|
// region Closed Groups
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleClosedGroupControlMessage(message: ClosedGroupControlMessage) {
|
2020-12-07 05:22:02 +01:00
|
|
|
when (message.kind!!) {
|
2021-02-09 01:45:38 +01:00
|
|
|
is ClosedGroupControlMessage.Kind.New -> handleNewClosedGroup(message)
|
|
|
|
is ClosedGroupControlMessage.Kind.EncryptionKeyPair -> handleClosedGroupEncryptionKeyPair(message)
|
|
|
|
is ClosedGroupControlMessage.Kind.NameChange -> handleClosedGroupNameChanged(message)
|
|
|
|
is ClosedGroupControlMessage.Kind.MembersAdded -> handleClosedGroupMembersAdded(message)
|
|
|
|
is ClosedGroupControlMessage.Kind.MembersRemoved -> handleClosedGroupMembersRemoved(message)
|
2021-03-04 05:16:47 +01:00
|
|
|
is ClosedGroupControlMessage.Kind.MemberLeft -> handleClosedGroupMemberLeft(message)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleNewClosedGroup(message: ClosedGroupControlMessage) {
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.New ?: return
|
|
|
|
val groupPublicKey = kind.publicKey.toByteArray().toHexString()
|
|
|
|
val members = kind.members.map { it.toByteArray().toHexString() }
|
|
|
|
val admins = kind.admins.map { it.toByteArray().toHexString() }
|
2021-03-15 05:43:05 +01:00
|
|
|
handleNewClosedGroup(message.sender!!, message.sentTimestamp!!, groupPublicKey, kind.name, kind.encryptionKeyPair!!, members, admins, message.sentTimestamp!!)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 05:31:14 +01:00
|
|
|
private fun handleNewClosedGroup(sender: String, sentTimestamp: Long, groupPublicKey: String, name: String, encryptionKeyPair: ECKeyPair, members: List<String>, admins: List<String>, formationTimestamp: Long) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
2020-12-07 05:22:02 +01:00
|
|
|
// Create the group
|
2021-02-09 01:45:38 +01:00
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
2020-12-10 05:33:57 +01:00
|
|
|
if (storage.getGroup(groupID) != null) {
|
2020-12-07 05:22:02 +01:00
|
|
|
// Update the group
|
2020-12-10 05:33:57 +01:00
|
|
|
storage.updateTitle(groupID, name)
|
|
|
|
storage.updateMembers(groupID, members.map { Address.fromSerialized(it) })
|
2020-12-07 05:22:02 +01:00
|
|
|
} else {
|
2020-12-10 05:33:57 +01:00
|
|
|
storage.createGroup(groupID, name, LinkedList(members.map { Address.fromSerialized(it) }),
|
2021-05-13 07:42:14 +02:00
|
|
|
null, null, LinkedList(admins.map { Address.fromSerialized(it) }), formationTimestamp)
|
2021-04-14 08:37:04 +02:00
|
|
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
|
2021-02-10 06:48:03 +01:00
|
|
|
// Notify the user
|
2021-04-14 08:37:04 +02:00
|
|
|
if (userPublicKey == sender) {
|
|
|
|
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
|
|
|
storage.insertOutgoingInfoMessage(context, groupID, SignalServiceGroup.Type.CREATION, name, members, admins, threadID, sentTimestamp)
|
|
|
|
} else {
|
|
|
|
storage.insertIncomingInfoMessage(context, sender, groupID, SignalServiceGroup.Type.CREATION, name, members, admins, sentTimestamp)
|
|
|
|
}
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
2020-12-10 05:33:57 +01:00
|
|
|
storage.setProfileSharing(Address.fromSerialized(groupID), true)
|
2020-12-07 05:22:02 +01:00
|
|
|
// Add the group to the user's set of public keys to poll for
|
2021-02-09 01:45:38 +01:00
|
|
|
storage.addClosedGroupPublicKey(groupPublicKey)
|
|
|
|
// Store the encryption key pair
|
|
|
|
storage.addClosedGroupEncryptionKeyPair(encryptionKeyPair, groupPublicKey)
|
|
|
|
// Notify the PN server
|
|
|
|
PushNotificationAPI.performOperation(PushNotificationAPI.ClosedGroupOperation.Subscribe, groupPublicKey, storage.getUserPublicKey()!!)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleClosedGroupEncryptionKeyPair(message: ClosedGroupControlMessage) {
|
|
|
|
// Prepare
|
2021-04-26 03:14:45 +02:00
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
2021-02-09 01:45:38 +01:00
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.EncryptionKeyPair ?: return
|
2021-04-23 08:35:24 +02:00
|
|
|
var groupPublicKey = kind.publicKey?.toByteArray()?.toHexString()
|
|
|
|
if (groupPublicKey.isNullOrEmpty()) groupPublicKey = message.groupPublicKey ?: return
|
2021-01-08 01:13:05 +01:00
|
|
|
val userPublicKey = storage.getUserPublicKey()!!
|
2021-02-09 01:45:38 +01:00
|
|
|
val userKeyPair = storage.getUserX25519KeyPair()
|
|
|
|
// Unwrap the message
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group encryption key pair for nonexistent group.")
|
2021-02-09 01:45:38 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group encryption key pair for inactive group.")
|
2021-03-23 00:00:51 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-26 05:46:37 +01:00
|
|
|
if (!group.admins.map { it.toString() }.contains(senderPublicKey)) {
|
2021-04-23 06:07:10 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group encryption key pair from non-admin.")
|
2021-02-09 01:45:38 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Find our wrapper and decrypt it if possible
|
2021-03-26 05:46:37 +01:00
|
|
|
val wrapper = kind.wrappers.firstOrNull { it.publicKey!! == userPublicKey } ?: return
|
2021-02-09 01:45:38 +01:00
|
|
|
val encryptedKeyPair = wrapper.encryptedKeyPair!!.toByteArray()
|
2021-05-13 07:27:08 +02:00
|
|
|
val plaintext = MessageDecrypter.decrypt(encryptedKeyPair, userKeyPair).first
|
2021-02-09 01:45:38 +01:00
|
|
|
// Parse it
|
|
|
|
val proto = SignalServiceProtos.KeyPair.parseFrom(plaintext)
|
|
|
|
val keyPair = ECKeyPair(DjbECPublicKey(proto.publicKey.toByteArray().removing05PrefixIfNeeded()), DjbECPrivateKey(proto.privateKey.toByteArray()))
|
2021-02-10 06:48:03 +01:00
|
|
|
// Store it if needed
|
|
|
|
val closedGroupEncryptionKeyPairs = storage.getClosedGroupEncryptionKeyPairs(groupPublicKey)
|
|
|
|
if (closedGroupEncryptionKeyPairs.contains(keyPair)) {
|
|
|
|
Log.d("Loki", "Ignoring duplicate closed group encryption key pair.")
|
|
|
|
return
|
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
storage.addClosedGroupEncryptionKeyPair(keyPair, groupPublicKey)
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Received a new closed group encryption key pair.")
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun MessageReceiver.handleClosedGroupNameChanged(message: ClosedGroupControlMessage) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
2021-03-26 05:46:37 +01:00
|
|
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
|
2021-02-09 01:45:38 +01:00
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.NameChange ?: return
|
|
|
|
val groupPublicKey = message.groupPublicKey ?: return
|
|
|
|
// Check that the sender is a member of the group (before the update)
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group update for nonexistent group.")
|
2021-02-09 01:45:38 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group update for inactive group.")
|
2021-03-23 00:00:51 +01:00
|
|
|
return
|
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
// Check common group update logic
|
|
|
|
if (!isValidGroupUpdate(group, message.sentTimestamp!!, senderPublicKey)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val members = group.members.map { it.serialize() }
|
|
|
|
val admins = group.admins.map { it.serialize() }
|
|
|
|
val name = kind.name
|
|
|
|
storage.updateTitle(groupID, name)
|
2021-03-26 05:46:37 +01:00
|
|
|
// Notify the user
|
|
|
|
if (userPublicKey == senderPublicKey) {
|
|
|
|
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
2021-04-14 08:37:04 +02:00
|
|
|
storage.insertOutgoingInfoMessage(context, groupID, SignalServiceGroup.Type.NAME_CHANGE, name, members, admins, threadID, message.sentTimestamp!!)
|
2021-03-26 05:46:37 +01:00
|
|
|
} else {
|
2021-04-14 08:37:04 +02:00
|
|
|
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, SignalServiceGroup.Type.NAME_CHANGE, name, members, admins, message.sentTimestamp!!)
|
2021-03-26 05:46:37 +01:00
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun MessageReceiver.handleClosedGroupMembersAdded(message: ClosedGroupControlMessage) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
2021-03-12 00:21:09 +01:00
|
|
|
val userPublicKey = storage.getUserPublicKey()!!
|
2021-02-09 01:45:38 +01:00
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.MembersAdded ?: return
|
|
|
|
val groupPublicKey = message.groupPublicKey ?: return
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group update for nonexistent group.")
|
2021-01-08 01:13:05 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group update for inactive group.")
|
2021-03-23 00:00:51 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-12 00:21:09 +01:00
|
|
|
if (!isValidGroupUpdate(group, message.sentTimestamp!!, senderPublicKey)) { return }
|
2021-02-09 01:45:38 +01:00
|
|
|
val name = group.title
|
|
|
|
// Check common group update logic
|
|
|
|
val members = group.members.map { it.serialize() }
|
|
|
|
val admins = group.admins.map { it.serialize() }
|
|
|
|
|
|
|
|
val updateMembers = kind.members.map { it.toByteArray().toHexString() }
|
|
|
|
val newMembers = members + updateMembers
|
|
|
|
storage.updateMembers(groupID, newMembers.map { Address.fromSerialized(it) })
|
2021-03-26 05:46:37 +01:00
|
|
|
// Notify the user
|
2021-03-12 00:21:09 +01:00
|
|
|
if (userPublicKey == senderPublicKey) {
|
|
|
|
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
2021-04-14 08:37:04 +02:00
|
|
|
storage.insertOutgoingInfoMessage(context, groupID, SignalServiceGroup.Type.MEMBER_ADDED, name, updateMembers, admins, threadID, message.sentTimestamp!!)
|
2021-03-12 00:21:09 +01:00
|
|
|
} else {
|
2021-04-14 08:37:04 +02:00
|
|
|
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, SignalServiceGroup.Type.MEMBER_ADDED, name, updateMembers, admins, message.sentTimestamp!!)
|
2021-03-12 00:21:09 +01:00
|
|
|
}
|
|
|
|
if (userPublicKey in admins) {
|
2021-05-13 07:42:14 +02:00
|
|
|
// Send the latest encryption key pair to the added members if the current user is the admin of the group
|
|
|
|
//
|
|
|
|
// This fixes a race condition where:
|
|
|
|
// • A member removes another member.
|
|
|
|
// • A member adds someone to the group and sends them the latest group key pair.
|
|
|
|
// • The admin is offline during all of this.
|
|
|
|
// • When the admin comes back online they see the member removed message and generate + distribute a new key pair,
|
|
|
|
// but they don't know about the added member yet.
|
|
|
|
// • Now they see the member added message.
|
|
|
|
//
|
|
|
|
// Without the code below, the added member(s) would never get the key pair that was generated by the admin when they saw
|
|
|
|
// the member removed message.
|
2021-05-13 08:14:54 +02:00
|
|
|
val encryptionKeyPair = pendingKeyPairs[groupPublicKey]?.orNull()
|
2021-05-13 07:42:14 +02:00
|
|
|
?: storage.getLatestClosedGroupEncryptionKeyPair(groupPublicKey)
|
2021-03-12 00:21:09 +01:00
|
|
|
if (encryptionKeyPair == null) {
|
|
|
|
android.util.Log.d("Loki", "Couldn't get encryption key pair for closed group.")
|
|
|
|
} else {
|
|
|
|
for (user in updateMembers) {
|
|
|
|
MessageSender.sendEncryptionKeyPair(groupPublicKey, encryptionKeyPair, setOf(user), targetUser = user, force = false)
|
|
|
|
}
|
2021-02-11 06:57:43 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
2021-04-23 06:07:10 +02:00
|
|
|
/// Removes the given members from the group IF
|
|
|
|
/// • it wasn't the admin that was removed (that should happen through a `MEMBER_LEFT` message).
|
|
|
|
/// • the admin sent the message (only the admin can truly remove members).
|
|
|
|
/// If we're among the users that were removed, delete all encryption key pairs and the group public key, unsubscribe
|
|
|
|
/// from push notifications for this closed group, and remove the given members from the zombie list for this group.
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleClosedGroupMembersRemoved(message: ClosedGroupControlMessage) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
2021-02-09 01:45:38 +01:00
|
|
|
val userPublicKey = storage.getUserPublicKey()!!
|
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.MembersRemoved ?: return
|
|
|
|
val groupPublicKey = message.groupPublicKey ?: return
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group update for nonexistent group.")
|
2021-01-08 01:13:05 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group update for inactive group.")
|
2021-03-23 00:00:51 +01:00
|
|
|
return
|
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
val name = group.title
|
|
|
|
// Check common group update logic
|
|
|
|
val members = group.members.map { it.serialize() }
|
|
|
|
val admins = group.admins.map { it.toString() }
|
2021-05-13 07:42:14 +02:00
|
|
|
val removedMembers = kind.members.map { it.toByteArray().toHexString() }
|
2021-04-23 06:07:10 +02:00
|
|
|
// Check that the admin wasn't removed
|
2021-05-13 07:42:14 +02:00
|
|
|
if (removedMembers.contains(admins.first())) {
|
2021-04-23 06:07:10 +02:00
|
|
|
Log.d("Loki", "Ignoring invalid closed group update.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Check that the message was sent by the group admin
|
|
|
|
if (!admins.contains(senderPublicKey)) {
|
|
|
|
Log.d("Loki", "Ignoring invalid closed group update.")
|
|
|
|
return
|
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
if (!isValidGroupUpdate(group, message.sentTimestamp!!, senderPublicKey)) { return }
|
2021-05-13 07:42:14 +02:00
|
|
|
// If the admin leaves the group is disbanded
|
|
|
|
val didAdminLeave = admins.any { it in removedMembers }
|
|
|
|
val newMembers = members - removedMembers
|
|
|
|
// A user should be posting a MEMBERS_LEFT in case they leave, so this shouldn't be encountered
|
|
|
|
val senderLeft = senderPublicKey in removedMembers
|
2021-02-09 01:45:38 +01:00
|
|
|
if (senderLeft) {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Received a MEMBERS_REMOVED instead of a MEMBERS_LEFT from sender: $senderPublicKey.")
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
2021-05-13 07:42:14 +02:00
|
|
|
val wasCurrentUserRemoved = userPublicKey in removedMembers
|
|
|
|
// Admin should send a MEMBERS_LEFT message but handled here just in case
|
2021-02-09 01:45:38 +01:00
|
|
|
if (didAdminLeave || wasCurrentUserRemoved) {
|
|
|
|
disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey)
|
|
|
|
} else {
|
|
|
|
storage.updateMembers(groupID, newMembers.map { Address.fromSerialized(it) })
|
|
|
|
}
|
2021-05-13 07:42:14 +02:00
|
|
|
// Update zombie members
|
2021-04-23 06:07:10 +02:00
|
|
|
val zombies = storage.getZombieMember(groupID)
|
2021-05-13 07:42:14 +02:00
|
|
|
storage.updateZombieMembers(groupID, zombies.minus(removedMembers).map { Address.fromSerialized(it) })
|
|
|
|
val type = if (senderLeft) SignalServiceGroup.Type.QUIT else SignalServiceGroup.Type.MEMBER_REMOVED
|
2021-03-26 05:46:37 +01:00
|
|
|
// Notify the user
|
2021-05-13 07:42:14 +02:00
|
|
|
// We don't display zombie members in the notification as users have already been notified when those members left
|
|
|
|
val notificationMembers = removedMembers.minus(zombies)
|
2021-04-26 08:33:54 +02:00
|
|
|
if (notificationMembers.isNotEmpty()) {
|
2021-05-13 07:42:14 +02:00
|
|
|
// No notification to display when only zombies have been removed
|
2021-04-26 08:33:54 +02:00
|
|
|
if (userPublicKey == senderPublicKey) {
|
|
|
|
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
|
|
|
storage.insertOutgoingInfoMessage(context, groupID, type, name, notificationMembers, admins, threadID, message.sentTimestamp!!)
|
|
|
|
} else {
|
|
|
|
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, type, name, notificationMembers, admins, message.sentTimestamp!!)
|
|
|
|
}
|
2021-03-26 05:46:37 +01:00
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
2021-04-23 06:07:10 +02:00
|
|
|
/// If a regular member left:
|
|
|
|
/// • Mark them as a zombie (to be removed by the admin later).
|
|
|
|
/// If the admin left:
|
|
|
|
/// • Unsubscribe from PNs, delete the group public key, etc. as the group will be disbanded.
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleClosedGroupMemberLeft(message: ClosedGroupControlMessage) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
2021-02-09 01:45:38 +01:00
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val userPublicKey = storage.getUserPublicKey()!!
|
2021-02-10 06:48:03 +01:00
|
|
|
if (message.kind!! !is ClosedGroupControlMessage.Kind.MemberLeft) return
|
2021-02-09 01:45:38 +01:00
|
|
|
val groupPublicKey = message.groupPublicKey ?: return
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group update for nonexistent group.")
|
2021-02-09 01:45:38 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
2021-05-13 07:42:14 +02:00
|
|
|
Log.d("Loki", "Ignoring closed group update for inactive group.")
|
2021-03-23 00:00:51 +01:00
|
|
|
return
|
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
val name = group.title
|
|
|
|
// Check common group update logic
|
|
|
|
val members = group.members.map { it.serialize() }
|
|
|
|
val admins = group.admins.map { it.toString() }
|
|
|
|
if (!isValidGroupUpdate(group, message.sentTimestamp!!, senderPublicKey)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If admin leaves the group is disbanded
|
|
|
|
val didAdminLeave = admins.contains(senderPublicKey)
|
|
|
|
val updatedMemberList = members - senderPublicKey
|
2021-03-26 05:46:37 +01:00
|
|
|
val userLeft = (userPublicKey == senderPublicKey)
|
|
|
|
if (didAdminLeave || userLeft) {
|
2021-02-09 01:45:38 +01:00
|
|
|
disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey)
|
|
|
|
} else {
|
|
|
|
storage.updateMembers(groupID, updatedMemberList.map { Address.fromSerialized(it) })
|
2021-05-13 07:42:14 +02:00
|
|
|
// Update zombie members
|
2021-04-23 06:07:10 +02:00
|
|
|
val zombies = storage.getZombieMember(groupID)
|
|
|
|
storage.updateZombieMembers(groupID, zombies.plus(senderPublicKey).map { Address.fromSerialized(it) })
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
2021-03-26 05:46:37 +01:00
|
|
|
// Notify the user
|
|
|
|
if (userLeft) {
|
|
|
|
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
2021-04-14 08:37:04 +02:00
|
|
|
storage.insertOutgoingInfoMessage(context, groupID, SignalServiceGroup.Type.QUIT, name, members, admins, threadID, message.sentTimestamp!!)
|
2021-03-26 05:46:37 +01:00
|
|
|
} else {
|
2021-04-14 08:37:04 +02:00
|
|
|
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, SignalServiceGroup.Type.QUIT, name, members, admins, message.sentTimestamp!!)
|
2021-03-26 05:46:37 +01:00
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
2021-05-13 07:42:14 +02:00
|
|
|
private fun isValidGroupUpdate(group: GroupRecord, sentTimestamp: Long, senderPublicKey: String): Boolean {
|
2021-02-09 01:45:38 +01:00
|
|
|
val oldMembers = group.members.map { it.serialize() }
|
|
|
|
// Check that the message isn't from before the group was created
|
2021-02-16 01:14:27 +01:00
|
|
|
if (group.formationTimestamp > sentTimestamp) {
|
2021-02-09 01:45:38 +01:00
|
|
|
android.util.Log.d("Loki", "Ignoring closed group update from before thread was created.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Check that the sender is a member of the group (before the update)
|
|
|
|
if (senderPublicKey !in oldMembers) {
|
|
|
|
android.util.Log.d("Loki", "Ignoring closed group info message from non-member.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-09 04:45:22 +01:00
|
|
|
fun MessageReceiver.disableLocalGroupAndUnsubscribe(groupPublicKey: String, groupID: String, userPublicKey: String) {
|
2021-04-26 03:14:45 +02:00
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
2021-02-09 01:45:38 +01:00
|
|
|
storage.removeClosedGroupPublicKey(groupPublicKey)
|
|
|
|
// Remove the key pairs
|
|
|
|
storage.removeAllClosedGroupEncryptionKeyPairs(groupPublicKey)
|
|
|
|
// Mark the group as inactive
|
|
|
|
storage.setActive(groupID, false)
|
|
|
|
storage.removeMember(groupID, Address.fromSerialized(userPublicKey))
|
|
|
|
// Notify the PN server
|
|
|
|
PushNotificationAPI.performOperation(PushNotificationAPI.ClosedGroupOperation.Unsubscribe, groupPublicKey, userPublicKey)
|
2021-04-30 08:09:11 +02:00
|
|
|
}
|
2021-05-17 07:18:41 +02:00
|
|
|
// endregion
|