From 77eb460ba74bbc7ad0096857870be86a166730f6 Mon Sep 17 00:00:00 2001 From: jubb Date: Tue, 9 Feb 2021 13:45:38 +1100 Subject: [PATCH] feat: add image handling across device self-send. close an unclosed resource. remove unnecessary checks and SmsDatabase way of checking for existing message from ourselves --- app/build.gradle | 4 +-- .../securesms/database/MmsDatabase.java | 11 ++++++++ .../securesms/database/SmsDatabase.java | 9 ------- .../securesms/jobs/PushDecryptJob.java | 24 ++++++++++++++---- .../SingleRecipientNotificationBuilder.java | 2 +- .../loki/utilities/DownloadUtilities.kt | 25 ++++++++++--------- 6 files changed, 46 insertions(+), 29 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 64b4078941..b83c5ae9c8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -158,8 +158,8 @@ dependencies { testImplementation 'org.robolectric:shadows-multidex:4.2' } -def canonicalVersionCode = 121 -def canonicalVersionName = "1.6.4" +def canonicalVersionCode = 135 +def canonicalVersionName = "1.6.12" def postFixSize = 10 def abiPostFix = ['armeabi-v7a' : 1, diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.java index 7b84e9975d..2e6e2a9b3a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.java @@ -902,6 +902,17 @@ public class MmsDatabase extends MessagingDatabase { return insertMessageInbox(retrieved, contentLocation, threadId, type, 0); } + public Optional insertSecureDecryptedMessageOutbox(OutgoingMediaMessage retrieved, long threadId, long serverTimestamp) + throws MmsException + { + long messageId = insertMessageOutbox(retrieved, threadId, false, null, serverTimestamp); + if (messageId == -1) { + return Optional.absent(); + } + markAsSent(messageId, true); + return Optional.fromNullable(new InsertResult(messageId, threadId)); + } + public Optional insertSecureDecryptedMessageInbox(IncomingMediaMessage retrieved, long threadId, long serverTimestamp) throws MmsException { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java index aaa14151d2..0234dfae92 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -725,15 +725,6 @@ public class SmsDatabase extends MessagingDatabase { contentValues.put(DELIVERY_RECEIPT_COUNT, Stream.of(earlyDeliveryReceipts.values()).mapToLong(Long::longValue).sum()); contentValues.put(READ_RECEIPT_COUNT, Stream.of(earlyReadReceipts.values()).mapToLong(Long::longValue).sum()); - SQLiteDatabase readDb = databaseHelper.getReadableDatabase(); - Cursor existingRecord = readDb.query(TABLE_NAME, null, String.format("%s = ? AND %s = ? AND %s = ?",ADDRESS, THREAD_ID, DATE_SENT), - new String[] { address.serialize(), Long.toString(threadId), Long.toString(date) }, null, null, null); - int existingRecordCount = existingRecord.getCount(); - if (existingRecordCount > 0) { - // return -1 because record exists from Address to ThreadID with the same date sent (probably sent from us) - return -1; - } - SQLiteDatabase db = databaseHelper.getWritableDatabase(); long messageId = db.insert(TABLE_NAME, ADDRESS, contentValues); if (insertListener != null) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 7d3b72a97a..1047085422 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -83,6 +83,7 @@ import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocolV2; import org.thoughtcrime.securesms.loki.protocol.SessionManagementProtocol; import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol; import org.thoughtcrime.securesms.loki.protocol.SessionResetImplementation; +import org.thoughtcrime.securesms.loki.utilities.DatabaseUtilitiesKt; import org.thoughtcrime.securesms.loki.utilities.MentionManagerUtilities; import org.thoughtcrime.securesms.mms.IncomingMediaMessage; import org.thoughtcrime.securesms.mms.MmsException; @@ -584,9 +585,12 @@ public class PushDecryptJob extends BaseJob implements InjectableType { masterAddress = getMessageMasterDestination(content.getSender()).getAddress(); } + // Handle sync message from ourselves if (syncTarget != null && !syncTarget.isEmpty()) { List attachments = PointerAttachment.forPointers(message.getAttachments()); + Address targetAddress = Address.fromSerialized(syncTarget); + OutgoingMediaMessage mediaMessage = new OutgoingMediaMessage(masterRecipient, message.getBody().orNull(), attachments, message.getTimestamp(), -1, @@ -596,6 +600,11 @@ public class PushDecryptJob extends BaseJob implements InjectableType { linkPreviews.or(Collections.emptyList()), Collections.emptyList(), Collections.emptyList()); + if (DatabaseFactory.getMmsSmsDatabase(context).getMessageFor(message.getTimestamp(), targetAddress) != null) { + Log.d("Loki","Message already exists, don't insert again"); + return; + } + MmsDatabase database = DatabaseFactory.getMmsDatabase(context); database.beginTransaction(); @@ -607,11 +616,11 @@ public class PushDecryptJob extends BaseJob implements InjectableType { Optional insertResult; try { - if (message.isGroupMessage()) { - insertResult = database.insertSecureDecryptedMessageOutbox(mediaMessage, -1, content.getTimestamp()); - } else { - insertResult = database.insertSecureDecryptedMessageOutbox(mediaMessage, -1); - } + + // Check if we have the thread already + long threadID = DatabaseFactory.getLokiThreadDatabase(context).getThreadID(syncTarget); + + insertResult = database.insertSecureDecryptedMessageOutbox(mediaMessage, threadID, content.getTimestamp()); if (insertResult.isPresent()) { List allAttachments = DatabaseFactory.getAttachmentDatabase(context).getAttachmentsForMessage(insertResult.get().getMessageId()); @@ -837,6 +846,11 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } else if (syncTarget != null && !syncTarget.isEmpty()) { Address targetAddress = Address.fromSerialized(syncTarget); + if (DatabaseFactory.getMmsSmsDatabase(context).getMessageFor(message.getTimestamp(), targetAddress) != null) { + Log.d("Loki","Message already exists, don't insert again"); + return; + } + OutgoingTextMessage tm = new OutgoingTextMessage(Recipient.from(context, targetAddress, false), body, message.getExpiresInSeconds(), -1); diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java index 62bfcc8b5e..1ea22350a6 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java @@ -326,7 +326,7 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil private static Drawable getPlaceholderDrawable(Context context, Recipient recipient) { String publicKey = recipient.getAddress().serialize(); - String hepk = (recipient.isLocalNumber() && publicKey != null) + String hepk = (recipient.isLocalNumber() && publicKey == null) ? TextSecurePreferences.getMasterHexEncodedPublicKey(context) : publicKey; String displayName = recipient.getName(); diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/utilities/DownloadUtilities.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/utilities/DownloadUtilities.kt index 1986d2c2db..a582c0fa31 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/utilities/DownloadUtilities.kt +++ b/libsignal/src/main/java/org/session/libsignal/service/loki/utilities/DownloadUtilities.kt @@ -65,19 +65,20 @@ object DownloadUtilities { Log.d("Loki", "Attachment size limit exceeded.") throw PushNetworkException("Max response size exceeded.") } - val input = body.inputStream() - val buffer = ByteArray(32768) - var count = 0 - var bytes = input.read(buffer) - while (bytes >= 0) { - outputStream.write(buffer, 0, bytes) - count += bytes - if (count > maxSize) { - Log.d("Loki", "Attachment size limit exceeded.") - throw PushNetworkException("Max response size exceeded.") + body.inputStream().use { input -> + val buffer = ByteArray(32768) + var count = 0 + var bytes = input.read(buffer) + while (bytes >= 0) { + outputStream.write(buffer, 0, bytes) + count += bytes + if (count > maxSize) { + Log.d("Loki", "Attachment size limit exceeded.") + throw PushNetworkException("Max response size exceeded.") + } + listener?.onAttachmentProgress(body.size.toLong(), count.toLong()) + bytes = input.read(buffer) } - listener?.onAttachmentProgress(body.size.toLong(), count.toLong()) - bytes = input.read(buffer) } } catch (e: Exception) { Log.d("Loki", "Couldn't download attachment due to error: $e.")