From 4bf0990ef9be1c29dc0c23b2e90fd7f08fc2da4b Mon Sep 17 00:00:00 2001 From: jubb Date: Mon, 26 Apr 2021 14:30:51 +1000 Subject: [PATCH] feat: open group attachment and message processing --- .../securesms/database/Storage.kt | 7 +++++++ .../securesms/loki/api/PublicChatManager.kt | 2 +- .../libsession/messaging/StorageProtocol.kt | 1 + .../messaging/jobs/AttachmentDownloadJob.kt | 20 +++++++++++++------ .../messaging/opengroups/OpenGroupAPIV2.kt | 4 ++++ 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt index 6e4a26cdef..059d94e175 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -576,6 +576,13 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context, return if (threadID < 0) null else threadID } + override fun getThreadIdForMms(mmsId: Long): Long { + val mmsDb = DatabaseFactory.getMmsDatabase(context) + val cursor = mmsDb.getMessage(mmsId) + val reader = mmsDb.readerFor(cursor) + return reader.current.threadId + } + override fun getSessionRequestSentTimestamp(publicKey: String): Long? { return DatabaseFactory.getLokiAPIDatabase(context).getSessionRequestSentTimestamp(publicKey) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/api/PublicChatManager.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/api/PublicChatManager.kt index 97a9b25028..129b8d83b6 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/api/PublicChatManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/api/PublicChatManager.kt @@ -110,7 +110,7 @@ class PublicChatManager(private val context: Context) { fun addChat(server: String, room: String, info: OpenGroupAPIV2.Info, publicKey: String): OpenGroupV2 { val chat = OpenGroupV2(server, room, info.name, publicKey) var threadID = GroupManager.getOpenGroupThreadID(chat.id, context) - var profilePicture: Bitmap? = null + val profilePicture: Bitmap? if (threadID < 0) { val profilePictureAsByteArray = try { OpenGroupAPIV2.downloadOpenGroupProfilePicture(info.id,server).get() diff --git a/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt b/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt index c15e62ad3e..5e64633136 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt @@ -136,6 +136,7 @@ interface StorageProtocol { fun getOrCreateThreadIdFor(address: Address): Long fun getOrCreateThreadIdFor(publicKey: String, groupPublicKey: String?, openGroupID: String?): Long fun getThreadIdFor(address: Address): Long? + fun getThreadIdForMms(messageId: Long): Long // Session Request fun getSessionRequestSentTimestamp(publicKey: String): Long? diff --git a/libsession/src/main/java/org/session/libsession/messaging/jobs/AttachmentDownloadJob.kt b/libsession/src/main/java/org/session/libsession/messaging/jobs/AttachmentDownloadJob.kt index 1a0b9f9ef3..4c48b44977 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/jobs/AttachmentDownloadJob.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/jobs/AttachmentDownloadJob.kt @@ -53,15 +53,23 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long) messageDataProvider.setAttachmentState(AttachmentState.STARTED, attachmentID, this.databaseMessageID) val tempFile = createTempFile() - FileServerAPI.shared.downloadFile(tempFile, attachment.url, MAX_ATTACHMENT_SIZE, null) + val threadId = MessagingConfiguration.shared.storage.getThreadIdForMms(databaseMessageID) + val openGroupV2 = MessagingConfiguration.shared.storage.getV2OpenGroup(threadId.toString()) - // DECRYPTION + val isOpenGroupV2 = false + if (!isOpenGroupV2) { + FileServerAPI.shared.downloadFile(tempFile, attachment.url, MAX_ATTACHMENT_SIZE, null) - // Assume we're retrieving an attachment for an open group server if the digest is not set - val stream = if (attachment.digest?.size ?: 0 == 0 || attachment.key.isNullOrEmpty()) FileInputStream(tempFile) - else AttachmentCipherInputStream.createForAttachment(tempFile, attachment.size, Base64.decode(attachment.key), attachment.digest) + // DECRYPTION - messageDataProvider.insertAttachment(databaseMessageID, attachment.attachmentId, stream) + // Assume we're retrieving an attachment for an open group server if the digest is not set + val stream = if (attachment.digest?.size ?: 0 == 0 || attachment.key.isNullOrEmpty()) FileInputStream(tempFile) + else AttachmentCipherInputStream.createForAttachment(tempFile, attachment.size, Base64.decode(attachment.key), attachment.digest) + + messageDataProvider.insertAttachment(databaseMessageID, attachment.attachmentId, stream) + } else { +// val bytes = OpenGroupAPIV2.download() + } tempFile.delete() handleSuccess() diff --git a/libsession/src/main/java/org/session/libsession/messaging/opengroups/OpenGroupAPIV2.kt b/libsession/src/main/java/org/session/libsession/messaging/opengroups/OpenGroupAPIV2.kt index 11868d4ced..23117b1ac8 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/opengroups/OpenGroupAPIV2.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/opengroups/OpenGroupAPIV2.kt @@ -350,6 +350,10 @@ object OpenGroupAPIV2 { val storage = MessagingConfiguration.shared.storage storage.setOpenGroupPublicKey(DEFAULT_SERVER, DEFAULT_SERVER_PUBLIC_KEY) return getAllRooms(DEFAULT_SERVER).map { groups -> + val earlyGroups = groups.map { group -> + DefaultGroup(group.id, group.name, null) + } + defaultRooms.tryEmit(earlyGroups) // TODO: take into account cached w/ images groups val images = groups.map { group -> group.id to downloadOpenGroupProfilePicture(group.id, DEFAULT_SERVER) }.toMap()