Skip to content

Commit

Permalink
Handle absent change during invite.
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-signal authored and greyson-signal committed May 29, 2020
1 parent 4712833 commit 297a7d0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
Expand Up @@ -9,9 +9,11 @@

import org.signal.storageservice.protos.groups.local.DecryptedGroup;
import org.signal.storageservice.protos.groups.local.DecryptedGroupChange;
import org.signal.storageservice.protos.groups.local.DecryptedPendingMember;
import org.signal.zkgroup.VerificationFailedException;
import org.signal.zkgroup.groups.GroupMasterKey;
import org.signal.zkgroup.groups.GroupSecretParams;
import org.signal.zkgroup.util.UUIDUtil;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.GroupDatabase;
import org.thoughtcrime.securesms.database.MmsDatabase;
Expand Down Expand Up @@ -279,7 +281,10 @@ private void persistLearnedProfileKeys(@NonNull GlobalGroupState globalGroupStat
final ProfileKeySet profileKeys = new ProfileKeySet();

for (GroupLogEntry entry : globalGroupState.getHistory()) {
profileKeys.addKeysFromGroupState(entry.getGroup(), DecryptedGroupUtil.editorUuid(entry.getChange()));
Optional<UUID> editor = DecryptedGroupUtil.editorUuid(entry.getChange());
if (editor.isPresent()) {
profileKeys.addKeysFromGroupState(entry.getGroup(), editor.get());
}
}

Collection<RecipientId> updated = recipientDatabase.persistProfileKeySet(profileKeys);
Expand Down Expand Up @@ -335,8 +340,14 @@ private List<GroupLogEntry> getFullMemberHistory(@NonNull UUID selfUuid, int log
}

private void storeMessage(@NonNull DecryptedGroupV2Context decryptedGroupV2Context, long timestamp) {
UUID editor = DecryptedGroupUtil.editorUuid(decryptedGroupV2Context.getChange());
boolean outgoing = Recipient.self().getUuid().get().equals(editor);
Optional<UUID> editor = getEditor(decryptedGroupV2Context);

if (!editor.isPresent() || UuidUtil.UNKNOWN_UUID.equals(editor.get())) {
Log.w(TAG, "Cannot determine editor of change, can't insert message");
return;
}

boolean outgoing = Recipient.self().requireUuid().equals(editor.get());

if (outgoing) {
try {
Expand All @@ -353,12 +364,26 @@ private void storeMessage(@NonNull DecryptedGroupV2Context decryptedGroupV2Conte
}
} else {
SmsDatabase smsDatabase = DatabaseFactory.getSmsDatabase(context);
RecipientId sender = Recipient.externalPush(context, editor, null).getId();
RecipientId sender = RecipientId.from(editor.get(), null);
IncomingTextMessage incoming = new IncomingTextMessage(sender, -1, timestamp, timestamp, "", Optional.of(groupId), 0, false);
IncomingGroupUpdateMessage groupMessage = new IncomingGroupUpdateMessage(incoming, decryptedGroupV2Context);

smsDatabase.insertMessageInbox(groupMessage);
}
}

private Optional<UUID> getEditor(@NonNull DecryptedGroupV2Context decryptedGroupV2Context) {
DecryptedGroupChange change = decryptedGroupV2Context.getChange();
Optional<UUID> changeEditor = DecryptedGroupUtil.editorUuid(change);
if (changeEditor.isPresent()) {
return changeEditor;
} else {
Optional<DecryptedPendingMember> pendingByUuid = DecryptedGroupUtil.findPendingByUuid(decryptedGroupV2Context.getGroupState().getPendingMembersList(), Recipient.self().requireUuid());
if (pendingByUuid.isPresent()) {
return Optional.fromNullable(UuidUtil.fromByteStringOrNull(pendingByUuid.get().getAddedByUuid()));
}
}
return Optional.absent();
}
}
}
Expand Up @@ -88,8 +88,8 @@ private static UUID toUuid(ByteString member) {
/**
* The UUID of the member that made the change.
*/
public static UUID editorUuid(DecryptedGroupChange change) {
return change != null ? UuidUtil.fromByteStringOrUnknown(change.getEditor()) : UuidUtil.UNKNOWN_UUID;
public static Optional<UUID> editorUuid(DecryptedGroupChange change) {
return Optional.fromNullable(change != null ? UuidUtil.fromByteStringOrNull(change.getEditor()) : null);
}

public static Optional<DecryptedMember> findMemberByUuid(Collection<DecryptedMember> members, UUID uuid) {
Expand Down
Expand Up @@ -63,11 +63,6 @@ public static UUID fromByteStringOrNull(ByteString bytes) {
return parseOrNull(bytes.toByteArray());
}

public static UUID fromByteStringOrUnknown(ByteString bytes) {
UUID uuid = parseOrNull(bytes.toByteArray());
return uuid != null ? uuid : UNKNOWN_UUID;
}

private static UUID parseOrNull(byte[] byteArray) {
return byteArray != null && byteArray.length == 16 ? parseOrThrow(byteArray) : null;
}
Expand Down
Expand Up @@ -34,7 +34,7 @@ public void can_extract_editor_uuid_from_decrypted_group_change() {
.setEditor(editor)
.build();

UUID parsed = DecryptedGroupUtil.editorUuid(groupChange);
UUID parsed = DecryptedGroupUtil.editorUuid(groupChange).get();

assertEquals(uuid, parsed);
}
Expand Down

0 comments on commit 297a7d0

Please sign in to comment.