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

This commit is contained in:
jubb 2021-02-09 13:45:38 +11:00
parent 7c2b124ebc
commit 77eb460ba7
6 changed files with 46 additions and 29 deletions

View File

@ -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,

View File

@ -902,6 +902,17 @@ public class MmsDatabase extends MessagingDatabase {
return insertMessageInbox(retrieved, contentLocation, threadId, type, 0);
}
public Optional<InsertResult> 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<InsertResult> insertSecureDecryptedMessageInbox(IncomingMediaMessage retrieved, long threadId, long serverTimestamp)
throws MmsException
{

View File

@ -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) {

View File

@ -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<Attachment> 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> 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<DatabaseAttachment> 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);

View File

@ -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();

View File

@ -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.")