From 073be6696b29dc43e3cc3483b4c88111a36ef23c Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Tue, 24 Nov 2015 16:01:21 -0800 Subject: [PATCH] Mark threads as read when we get an outgoing sync message // FREEBIE --- .../groups/GroupMessageProcessor.java | 70 +++++++++++-------- .../securesms/jobs/PushDecryptJob.java | 21 ++++-- 2 files changed, 56 insertions(+), 35 deletions(-) diff --git a/src/org/thoughtcrime/securesms/groups/GroupMessageProcessor.java b/src/org/thoughtcrime/securesms/groups/GroupMessageProcessor.java index 19c241e071..1387604b4e 100644 --- a/src/org/thoughtcrime/securesms/groups/GroupMessageProcessor.java +++ b/src/org/thoughtcrime/securesms/groups/GroupMessageProcessor.java @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.groups; import android.content.Context; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.util.Log; import android.util.Pair; @@ -44,15 +45,15 @@ public class GroupMessageProcessor { private static final String TAG = GroupMessageProcessor.class.getSimpleName(); - public static void process(@NonNull Context context, - @NonNull MasterSecretUnion masterSecret, - @NonNull TextSecureEnvelope envelope, - @NonNull TextSecureDataMessage message, - boolean outgoing) + public static @Nullable Long process(@NonNull Context context, + @NonNull MasterSecretUnion masterSecret, + @NonNull TextSecureEnvelope envelope, + @NonNull TextSecureDataMessage message, + boolean outgoing) { if (!message.getGroupInfo().isPresent() || message.getGroupInfo().get().getGroupId() == null) { Log.w(TAG, "Received group message with no id! Ignoring..."); - return; + return null; } GroupDatabase database = DatabaseFactory.getGroupDatabase(context); @@ -61,21 +62,22 @@ public class GroupMessageProcessor { GroupRecord record = database.getGroup(id); if (record != null && group.getType() == TextSecureGroup.Type.UPDATE) { - handleGroupUpdate(context, masterSecret, envelope, group, record, outgoing); + return handleGroupUpdate(context, masterSecret, envelope, group, record, outgoing); } else if (record == null && group.getType() == TextSecureGroup.Type.UPDATE) { - handleGroupCreate(context, masterSecret, envelope, group, outgoing); + return handleGroupCreate(context, masterSecret, envelope, group, outgoing); } else if (record != null && group.getType() == TextSecureGroup.Type.QUIT) { - handleGroupLeave(context, masterSecret, envelope, group, record, outgoing); + return handleGroupLeave(context, masterSecret, envelope, group, record, outgoing); } else { Log.w(TAG, "Received unknown type, ignoring..."); + return null; } } - private static void handleGroupCreate(@NonNull Context context, - @NonNull MasterSecretUnion masterSecret, - @NonNull TextSecureEnvelope envelope, - @NonNull TextSecureGroup group, - boolean outgoing) + private static @Nullable Long handleGroupCreate(@NonNull Context context, + @NonNull MasterSecretUnion masterSecret, + @NonNull TextSecureEnvelope envelope, + @NonNull TextSecureGroup group, + boolean outgoing) { GroupDatabase database = DatabaseFactory.getGroupDatabase(context); byte[] id = group.getGroupId(); @@ -88,15 +90,15 @@ public class GroupMessageProcessor { avatar != null && avatar.isPointer() ? avatar.asPointer() : null, envelope.getRelay()); - storeMessage(context, masterSecret, envelope, group, builder.build(), outgoing); + return storeMessage(context, masterSecret, envelope, group, builder.build(), outgoing); } - private static void handleGroupUpdate(@NonNull Context context, - @NonNull MasterSecretUnion masterSecret, - @NonNull TextSecureEnvelope envelope, - @NonNull TextSecureGroup group, - @NonNull GroupRecord groupRecord, - boolean outgoing) + private static @Nullable Long handleGroupUpdate(@NonNull Context context, + @NonNull MasterSecretUnion masterSecret, + @NonNull TextSecureEnvelope envelope, + @NonNull TextSecureGroup group, + @NonNull GroupRecord groupRecord, + boolean outgoing) { GroupDatabase database = DatabaseFactory.getGroupDatabase(context); @@ -139,10 +141,10 @@ public class GroupMessageProcessor { if (!groupRecord.isActive()) database.setActive(id, true); - storeMessage(context, masterSecret, envelope, group, builder.build(), outgoing); + return storeMessage(context, masterSecret, envelope, group, builder.build(), outgoing); } - private static void handleGroupLeave(@NonNull Context context, + private static Long handleGroupLeave(@NonNull Context context, @NonNull MasterSecretUnion masterSecret, @NonNull TextSecureEnvelope envelope, @NonNull TextSecureGroup group, @@ -160,17 +162,19 @@ public class GroupMessageProcessor { database.remove(id, envelope.getSource()); if (outgoing) database.setActive(id, false); - storeMessage(context, masterSecret, envelope, group, builder.build(), outgoing); + return storeMessage(context, masterSecret, envelope, group, builder.build(), outgoing); } + + return null; } - private static void storeMessage(@NonNull Context context, - @NonNull MasterSecretUnion masterSecret, - @NonNull TextSecureEnvelope envelope, - @NonNull TextSecureGroup group, - @NonNull GroupContext storage, - boolean outgoing) + private static @Nullable Long storeMessage(@NonNull Context context, + @NonNull MasterSecretUnion masterSecret, + @NonNull TextSecureEnvelope envelope, + @NonNull TextSecureGroup group, + @NonNull GroupContext storage, + boolean outgoing) { if (group.getAvatar().isPresent()) { ApplicationContext.getInstance(context).getJobManager() @@ -186,6 +190,8 @@ public class GroupMessageProcessor { long messageId = mmsDatabase.insertMessageOutbox(masterSecret, outgoingMessage, threadId, false); mmsDatabase.markAsSent(messageId); + + return threadId; } else { EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context); String body = Base64.encodeBytes(storage.toByteArray()); @@ -194,10 +200,14 @@ public class GroupMessageProcessor { Pair messageAndThreadId = smsDatabase.insertMessageInbox(masterSecret, groupMessage); MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second); + + return messageAndThreadId.second; } } catch (MmsException e) { Log.w(TAG, e); } + + return null; } private static GroupContext.Builder createGroupContext(TextSecureGroup group) { diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index e4b382f3bb..bd5981d6af 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -220,12 +220,19 @@ public class PushDecryptJob extends ContextJob { @NonNull Optional smsMessageId) throws MmsException { + Long threadId; + if (message.getMessage().isGroupUpdate()) { - GroupMessageProcessor.process(context, masterSecret, envelope, message.getMessage(), true); + threadId = GroupMessageProcessor.process(context, masterSecret, envelope, message.getMessage(), true); } else if (message.getMessage().getAttachments().isPresent()) { - handleSynchronizeSentMediaMessage(masterSecret, message, smsMessageId); + threadId = handleSynchronizeSentMediaMessage(masterSecret, message, smsMessageId); } else { - handleSynchronizeSentTextMessage(masterSecret, message, smsMessageId); + threadId = handleSynchronizeSentTextMessage(masterSecret, message, smsMessageId); + } + + if (threadId != null) { + DatabaseFactory.getThreadDatabase(getContext()).setRead(threadId); + MessageNotifier.updateNotification(getContext(), masterSecret.getMasterSecret().orNull()); } } @@ -277,7 +284,7 @@ public class PushDecryptJob extends ContextJob { MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second); } - private void handleSynchronizeSentMediaMessage(@NonNull MasterSecretUnion masterSecret, + private long handleSynchronizeSentMediaMessage(@NonNull MasterSecretUnion masterSecret, @NonNull SentTranscriptMessage message, @NonNull Optional smsMessageId) throws MmsException @@ -305,6 +312,8 @@ public class PushDecryptJob extends ContextJob { if (smsMessageId.isPresent()) { DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get()); } + + return threadId; } private void handleTextMessage(@NonNull MasterSecretUnion masterSecret, @@ -332,7 +341,7 @@ public class PushDecryptJob extends ContextJob { MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second); } - private void handleSynchronizeSentTextMessage(@NonNull MasterSecretUnion masterSecret, + private long handleSynchronizeSentTextMessage(@NonNull MasterSecretUnion masterSecret, @NonNull SentTranscriptMessage message, @NonNull Optional smsMessageId) { @@ -351,6 +360,8 @@ public class PushDecryptJob extends ContextJob { if (smsMessageId.isPresent()) { database.deleteMessage(smsMessageId.get()); } + + return threadId; } private void handleInvalidVersionMessage(@NonNull MasterSecretUnion masterSecret,