Add missing implementation to delete member's message on swarm (#815)

* Add missing implementation to delete member's message on swarm

* Add comment
pull/1706/head
SessionHero01 5 months ago committed by GitHub
parent 07bb5d7a22
commit 18efb1b1f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -189,6 +189,18 @@ class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper)
return messageDB.getMessageIDs(serverIds, threadId) return messageDB.getMessageIDs(serverIds, threadId)
} }
override fun getUserMessageHashes(threadId: Long, userPubKey: String): List<String> {
val component = DatabaseComponent.get(context)
val messages = component.mmsSmsDatabase().getUserMessages(threadId, userPubKey)
val messageDatabase = component.lokiMessageDatabase()
return messages.mapNotNull {
messageDatabase.getMessageServerHash(
messageID = it.id,
mms = it.isMms
)
}
}
override fun deleteMessage(messageID: Long, isSms: Boolean) { override fun deleteMessage(messageID: Long, isSms: Boolean) {
val messagingDatabase: MessagingDatabase = if (isSms) DatabaseComponent.get(context).smsDatabase() val messagingDatabase: MessagingDatabase = if (isSms) DatabaseComponent.get(context).smsDatabase()
else DatabaseComponent.get(context).mmsDatabase() else DatabaseComponent.get(context).mmsDatabase()

@ -1012,15 +1012,18 @@ class GroupManagerV2Impl @Inject constructor(
} }
} }
// Delete from swarm if we are admin
val adminKey = configFactory.getGroup(groupId)?.adminKey val adminKey = configFactory.getGroup(groupId)?.adminKey
if (!senderIsVerifiedAdmin && adminKey != null && hashes.isNotEmpty()) { if (adminKey != null) {
// If the deletion request comes from a non-admin, and we as an admin, will also delete
// the content from the swarm, provided that the messages are actually sent by that user // If hashes are given, these are the messages to delete. To be able to delete these
if (storage.ensureMessageHashesAreSender( // messages from the swarm, the deletion request must be sent by an admin, or the messages
hashes.toSet(), // belong to the requester.
sender.hexString, if (hashes.isNotEmpty() && (
groupId.hexString senderIsVerifiedAdmin || storage.ensureMessageHashesAreSender(
) hashes = hashes.toSet(),
sender = sender.hexString,
closedGroupId = groupId.hexString))
) { ) {
SnodeAPI.deleteMessage( SnodeAPI.deleteMessage(
groupId.hexString, groupId.hexString,
@ -1029,8 +1032,21 @@ class GroupManagerV2Impl @Inject constructor(
) )
} }
// The non-admin user shouldn't be able to delete other user's messages so we will // If memberIds are given, all messages belong to these members will be deleted on the
// ignore the memberIds in the message // swarm. These requests must be sent by an admin.
if (memberIds.isNotEmpty() && senderIsVerifiedAdmin) {
val userMessageHashes = memberIds.flatMap { memberId ->
messageDataProvider.getUserMessageHashes(threadId, memberId)
}
if (userMessageHashes.isNotEmpty()) {
SnodeAPI.deleteMessage(
groupId.hexString,
OwnedSwarmAuth.ofClosedGroup(groupId, adminKey),
userMessageHashes
)
}
}
} }
} }

@ -22,6 +22,7 @@ interface MessageDataProvider {
*/ */
fun getMessageID(serverId: Long, threadId: Long): Pair<Long, Boolean>? fun getMessageID(serverId: Long, threadId: Long): Pair<Long, Boolean>?
fun getMessageIDs(serverIDs: List<Long>, threadID: Long): Pair<List<Long>, List<Long>> fun getMessageIDs(serverIDs: List<Long>, threadID: Long): Pair<List<Long>, List<Long>>
fun getUserMessageHashes(threadId: Long, userPubKey: String): List<String>
fun deleteMessage(messageID: Long, isSms: Boolean) fun deleteMessage(messageID: Long, isSms: Boolean)
fun deleteMessages(messageIDs: List<Long>, threadId: Long, isSms: Boolean) fun deleteMessages(messageIDs: List<Long>, threadId: Long, isSms: Boolean)
fun markMessageAsDeleted(timestamp: Long, author: String, displayedMessage: String) fun markMessageAsDeleted(timestamp: Long, author: String, displayedMessage: String)

Loading…
Cancel
Save