Fix GroupDescriptions updating dynamically.

This shouldn't happen, rather it should have the snapshot of the group state per message.
This commit is contained in:
Mikunj 2020-04-09 13:17:00 +10:00
parent 4a36ed333d
commit f149065a30
2 changed files with 45 additions and 56 deletions

View File

@ -137,13 +137,25 @@ public class GroupMessageProcessor {
GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
String id = GroupUtil.getEncodedId(group);
// Only update group if admin sent the message
String masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context);
String ourHexEncodedPublicKey = masterHexEncodedPublicKey != null ? masterHexEncodedPublicKey : TextSecurePreferences.getLocalNumber(context);
if (group.getGroupType() == SignalServiceGroup.GroupType.SIGNAL) {
// Only update group if admin sent the message
String hexEncodedPublicKey = getMasterHexEncodedPublicKey(context, content.getSender());
if (!groupRecord.getAdmins().contains(Address.fromSerialized(hexEncodedPublicKey))) {
Log.d("Loki - Group Message", "Received a group update message from a non-admin user for " + id +". Ignoring.");
return null;
}
// We should only process update message if we were in the group
Address ourAddress = Address.fromSerialized(ourHexEncodedPublicKey);
if (!groupRecord.getMembers().contains(ourAddress) &&
!group.getMembers().or(Collections.emptyList()).contains(ourHexEncodedPublicKey)) {
Log.d("Loki - Group Message", "Received a group update message from a group we are not members in: " + id + " . Ignoring.");
database.setActive(id, false);
return null;
}
}
Set<Address> currentMembers = new HashSet<>(groupRecord.getMembers());
@ -169,23 +181,14 @@ public class GroupMessageProcessor {
database.updateMembers(id, new LinkedList<>(newMembers));
}
builder.clearMembers();
// We add any new or removed members to the group context
// This will allow us later to iterate over them to check if they left or were added for UI display
for (Address addedMember : addedMembers) {
builder.addMembers(addedMember.serialize());
builder.addNewMembers(addedMember.serialize());
}
for (Address removedMember : removedMembers) {
builder.addMembers(removedMember.serialize());
}
// If we were removed then we need to disable the chat
String masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context);
String ourHexEncodedPublicKey = masterHexEncodedPublicKey != null ? masterHexEncodedPublicKey : TextSecurePreferences.getLocalNumber(context);
if (removedMembers.contains(Address.fromSerialized(ourHexEncodedPublicKey))) {
database.setActive(id, false);
builder.addRemovedMembers(removedMember.serialize());
}
if (group.getName().isPresent() || group.getAvatar().isPresent()) {
@ -197,10 +200,15 @@ public class GroupMessageProcessor {
builder.clearName();
}
if (!groupRecord.isActive()) database.setActive(id, true);
// If we were removed then we need to disable the chat
if (removedMembers.contains(Address.fromSerialized(ourHexEncodedPublicKey))) {
database.setActive(id, false);
} else {
if (!groupRecord.isActive()) database.setActive(id, true);
if (group.getMembers().isPresent()) {
establishSessionsWithMembersIfNeeded(context, group.getMembers().get());
if (group.getMembers().isPresent()) {
establishSessionsWithMembersIfNeeded(context, group.getMembers().get());
}
}
return storeMessage(context, content, group, builder.build(), outgoing);

View File

@ -10,7 +10,6 @@ import com.google.protobuf.ByteString;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.GroupDatabase;
import org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage;
import org.thoughtcrime.securesms.recipients.Recipient;
@ -159,7 +158,7 @@ public class GroupUtil {
@NonNull private final Context context;
@Nullable private final GroupContext groupContext;
private final List<Recipient> members;
private final List<Recipient> newMembers;
private final List<Recipient> removedMembers;
private boolean ourDeviceWasRemoved;
@ -167,35 +166,35 @@ public class GroupUtil {
this.context = context.getApplicationContext();
this.groupContext = groupContext;
this.members = new LinkedList<>();
this.newMembers = new LinkedList<>();
this.removedMembers = new LinkedList<>();
this.ourDeviceWasRemoved = false;
if (groupContext != null && !groupContext.getMembersList().isEmpty()) {
List<String> memberList = groupContext.getMembersList();
List<Address> currentMembers = getCurrentGroupMembers();
if (groupContext != null) {
List<String> newMembers = groupContext.getNewMembersList();
for (String member : newMembers) {
this.newMembers.add(this.toRecipient(member));
}
// Add them to the member or removed members lists
for (String member : memberList) {
Address address = Address.fromSerialized(member);
Recipient recipient = Recipient.from(context, address, true);
if (currentMembers == null || currentMembers.contains(address)) {
this.members.add(recipient);
} else {
this.removedMembers.add(recipient);
}
List<String> removedMembers = groupContext.getRemovedMembersList();
for (String member : removedMembers) {
this.removedMembers.add(this.toRecipient(member));
}
// Check if our device was removed
if (!removedMembers.isEmpty()) {
String masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context);
String hexEncodedPublicKey = masterHexEncodedPublicKey != null ? masterHexEncodedPublicKey : TextSecurePreferences.getLocalNumber(context);
Recipient self = Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false);
ourDeviceWasRemoved = removedMembers.contains(self);
ourDeviceWasRemoved = removedMembers.contains(hexEncodedPublicKey);
}
}
}
private Recipient toRecipient(String hexEncodedPublicKey) {
Address address = Address.fromSerialized(hexEncodedPublicKey);
return Recipient.from(context, address, false);
}
public String toString(Recipient sender) {
// Show the local removed message
if (ourDeviceWasRemoved) {
@ -211,10 +210,10 @@ public class GroupUtil {
String title = groupContext.getName();
if (!members.isEmpty()) {
if (!newMembers.isEmpty()) {
description.append("\n");
description.append(context.getResources().getQuantityString(R.plurals.GroupUtil_joined_the_group,
members.size(), toString(members)));
newMembers.size(), toString(newMembers)));
}
if (!removedMembers.isEmpty()) {
@ -224,8 +223,8 @@ public class GroupUtil {
}
if (title != null && !title.trim().isEmpty()) {
if (!members.isEmpty()) description.append(" ");
else description.append("\n");
String separator = (!newMembers.isEmpty() || !removedMembers.isEmpty()) ? " " : "\n";
description.append(separator);
description.append(context.getString(R.string.GroupUtil_group_name_is_now, title));
}
@ -233,8 +232,8 @@ public class GroupUtil {
}
public void addListener(RecipientModifiedListener listener) {
if (!this.members.isEmpty()) {
for (Recipient member : this.members) {
if (!this.newMembers.isEmpty()) {
for (Recipient member : this.newMembers) {
member.addListener(listener);
}
}
@ -252,23 +251,5 @@ public class GroupUtil {
return result;
}
private List<Address> getCurrentGroupMembers() {
if (groupContext == null) { return null; }
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
byte[] decodedGroupId = groupContext.getId().toByteArray();
String signalGroupId = getEncodedId(decodedGroupId, false);
String publicChatId = getEncodedPublicChatId(decodedGroupId);
String rssFeedId = getEncodedRSSFeedId(decodedGroupId);
GroupRecord groupRecord = null;
if (!groupDatabase.isUnknownGroup(signalGroupId)) {
groupRecord = groupDatabase.getGroup(signalGroupId).orNull();
} else if (!groupDatabase.isUnknownGroup(publicChatId)) {
groupRecord = groupDatabase.getGroup(publicChatId).orNull();
} else if (!groupDatabase.isUnknownGroup(rssFeedId)) {
groupRecord = groupDatabase.getGroup(rssFeedId).orNull();
}
return (groupRecord != null) ? groupRecord.getMembers() : null;
}
}
}