Mark threads as read when we get an outgoing sync message

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-11-24 16:01:21 -08:00
parent 54b30c47f5
commit 073be6696b
2 changed files with 56 additions and 35 deletions

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.groups;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
@ -44,15 +45,15 @@ public class GroupMessageProcessor {
private static final String TAG = GroupMessageProcessor.class.getSimpleName(); private static final String TAG = GroupMessageProcessor.class.getSimpleName();
public static void process(@NonNull Context context, public static @Nullable Long process(@NonNull Context context,
@NonNull MasterSecretUnion masterSecret, @NonNull MasterSecretUnion masterSecret,
@NonNull TextSecureEnvelope envelope, @NonNull TextSecureEnvelope envelope,
@NonNull TextSecureDataMessage message, @NonNull TextSecureDataMessage message,
boolean outgoing) boolean outgoing)
{ {
if (!message.getGroupInfo().isPresent() || message.getGroupInfo().get().getGroupId() == null) { if (!message.getGroupInfo().isPresent() || message.getGroupInfo().get().getGroupId() == null) {
Log.w(TAG, "Received group message with no id! Ignoring..."); Log.w(TAG, "Received group message with no id! Ignoring...");
return; return null;
} }
GroupDatabase database = DatabaseFactory.getGroupDatabase(context); GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
@ -61,21 +62,22 @@ public class GroupMessageProcessor {
GroupRecord record = database.getGroup(id); GroupRecord record = database.getGroup(id);
if (record != null && group.getType() == TextSecureGroup.Type.UPDATE) { 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) { } 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) { } 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 { } else {
Log.w(TAG, "Received unknown type, ignoring..."); Log.w(TAG, "Received unknown type, ignoring...");
return null;
} }
} }
private static void handleGroupCreate(@NonNull Context context, private static @Nullable Long handleGroupCreate(@NonNull Context context,
@NonNull MasterSecretUnion masterSecret, @NonNull MasterSecretUnion masterSecret,
@NonNull TextSecureEnvelope envelope, @NonNull TextSecureEnvelope envelope,
@NonNull TextSecureGroup group, @NonNull TextSecureGroup group,
boolean outgoing) boolean outgoing)
{ {
GroupDatabase database = DatabaseFactory.getGroupDatabase(context); GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
byte[] id = group.getGroupId(); byte[] id = group.getGroupId();
@ -88,15 +90,15 @@ public class GroupMessageProcessor {
avatar != null && avatar.isPointer() ? avatar.asPointer() : null, avatar != null && avatar.isPointer() ? avatar.asPointer() : null,
envelope.getRelay()); 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, private static @Nullable Long handleGroupUpdate(@NonNull Context context,
@NonNull MasterSecretUnion masterSecret, @NonNull MasterSecretUnion masterSecret,
@NonNull TextSecureEnvelope envelope, @NonNull TextSecureEnvelope envelope,
@NonNull TextSecureGroup group, @NonNull TextSecureGroup group,
@NonNull GroupRecord groupRecord, @NonNull GroupRecord groupRecord,
boolean outgoing) boolean outgoing)
{ {
GroupDatabase database = DatabaseFactory.getGroupDatabase(context); GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
@ -139,10 +141,10 @@ public class GroupMessageProcessor {
if (!groupRecord.isActive()) database.setActive(id, true); 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 MasterSecretUnion masterSecret,
@NonNull TextSecureEnvelope envelope, @NonNull TextSecureEnvelope envelope,
@NonNull TextSecureGroup group, @NonNull TextSecureGroup group,
@ -160,17 +162,19 @@ public class GroupMessageProcessor {
database.remove(id, envelope.getSource()); database.remove(id, envelope.getSource());
if (outgoing) database.setActive(id, false); 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, private static @Nullable Long storeMessage(@NonNull Context context,
@NonNull MasterSecretUnion masterSecret, @NonNull MasterSecretUnion masterSecret,
@NonNull TextSecureEnvelope envelope, @NonNull TextSecureEnvelope envelope,
@NonNull TextSecureGroup group, @NonNull TextSecureGroup group,
@NonNull GroupContext storage, @NonNull GroupContext storage,
boolean outgoing) boolean outgoing)
{ {
if (group.getAvatar().isPresent()) { if (group.getAvatar().isPresent()) {
ApplicationContext.getInstance(context).getJobManager() ApplicationContext.getInstance(context).getJobManager()
@ -186,6 +190,8 @@ public class GroupMessageProcessor {
long messageId = mmsDatabase.insertMessageOutbox(masterSecret, outgoingMessage, threadId, false); long messageId = mmsDatabase.insertMessageOutbox(masterSecret, outgoingMessage, threadId, false);
mmsDatabase.markAsSent(messageId); mmsDatabase.markAsSent(messageId);
return threadId;
} else { } else {
EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context); EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
String body = Base64.encodeBytes(storage.toByteArray()); String body = Base64.encodeBytes(storage.toByteArray());
@ -194,10 +200,14 @@ public class GroupMessageProcessor {
Pair<Long, Long> messageAndThreadId = smsDatabase.insertMessageInbox(masterSecret, groupMessage); Pair<Long, Long> messageAndThreadId = smsDatabase.insertMessageInbox(masterSecret, groupMessage);
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second); MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second);
return messageAndThreadId.second;
} }
} catch (MmsException e) { } catch (MmsException e) {
Log.w(TAG, e); Log.w(TAG, e);
} }
return null;
} }
private static GroupContext.Builder createGroupContext(TextSecureGroup group) { private static GroupContext.Builder createGroupContext(TextSecureGroup group) {

View File

@ -220,12 +220,19 @@ public class PushDecryptJob extends ContextJob {
@NonNull Optional<Long> smsMessageId) @NonNull Optional<Long> smsMessageId)
throws MmsException throws MmsException
{ {
Long threadId;
if (message.getMessage().isGroupUpdate()) { 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()) { } else if (message.getMessage().getAttachments().isPresent()) {
handleSynchronizeSentMediaMessage(masterSecret, message, smsMessageId); threadId = handleSynchronizeSentMediaMessage(masterSecret, message, smsMessageId);
} else { } 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); MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second);
} }
private void handleSynchronizeSentMediaMessage(@NonNull MasterSecretUnion masterSecret, private long handleSynchronizeSentMediaMessage(@NonNull MasterSecretUnion masterSecret,
@NonNull SentTranscriptMessage message, @NonNull SentTranscriptMessage message,
@NonNull Optional<Long> smsMessageId) @NonNull Optional<Long> smsMessageId)
throws MmsException throws MmsException
@ -305,6 +312,8 @@ public class PushDecryptJob extends ContextJob {
if (smsMessageId.isPresent()) { if (smsMessageId.isPresent()) {
DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get()); DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
} }
return threadId;
} }
private void handleTextMessage(@NonNull MasterSecretUnion masterSecret, private void handleTextMessage(@NonNull MasterSecretUnion masterSecret,
@ -332,7 +341,7 @@ public class PushDecryptJob extends ContextJob {
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second); MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second);
} }
private void handleSynchronizeSentTextMessage(@NonNull MasterSecretUnion masterSecret, private long handleSynchronizeSentTextMessage(@NonNull MasterSecretUnion masterSecret,
@NonNull SentTranscriptMessage message, @NonNull SentTranscriptMessage message,
@NonNull Optional<Long> smsMessageId) @NonNull Optional<Long> smsMessageId)
{ {
@ -351,6 +360,8 @@ public class PushDecryptJob extends ContextJob {
if (smsMessageId.isPresent()) { if (smsMessageId.isPresent()) {
database.deleteMessage(smsMessageId.get()); database.deleteMessage(smsMessageId.get());
} }
return threadId;
} }
private void handleInvalidVersionMessage(@NonNull MasterSecretUnion masterSecret, private void handleInvalidVersionMessage(@NonNull MasterSecretUnion masterSecret,