Correctly display when user was kicked or added

This commit is contained in:
Mikunj 2020-04-09 09:41:01 +10:00
parent 547a8bd000
commit 3ec5974210

View File

@ -146,40 +146,40 @@ public class GroupMessageProcessor {
} }
} }
Set<Address> recordMembers = new HashSet<>(groupRecord.getMembers()); Set<Address> currentMembers = new HashSet<>(groupRecord.getMembers());
Set<Address> messageMembers = new HashSet<>(); Set<Address> newMembers = new HashSet<>();
for (String messageMember : group.getMembers().get()) { for (String messageMember : group.getMembers().get()) {
messageMembers.add(Address.fromExternal(context, messageMember)); newMembers.add(Address.fromExternal(context, messageMember));
} }
Set<Address> addedMembers = new HashSet<>(messageMembers); // Added members are the members who are present in newMembers but not in currentMembers
addedMembers.removeAll(recordMembers); Set<Address> addedMembers = new HashSet<>(newMembers);
addedMembers.removeAll(currentMembers);
Set<Address> missingMembers = new HashSet<>(recordMembers); // Kicked members are members who are present in currentMembers but not in newMembers
missingMembers.removeAll(messageMembers); Set<Address> removedMembers = new HashSet<>(currentMembers);
removedMembers.removeAll(newMembers);
GroupContext.Builder builder = createGroupContext(group); GroupContext.Builder builder = createGroupContext(group);
builder.setType(GroupContext.Type.UPDATE); builder.setType(GroupContext.Type.UPDATE);
if (addedMembers.size() > 0) { // Update our group members if they're different
Set<Address> unionMembers = new HashSet<>(recordMembers); if (!currentMembers.equals(newMembers)) {
unionMembers.addAll(messageMembers); database.updateMembers(id, new LinkedList<>(newMembers));
database.updateMembers(id, new LinkedList<>(unionMembers));
builder.clearMembers();
for (Address addedMember : addedMembers) {
builder.addMembers(addedMember.serialize());
}
} else {
builder.clearMembers();
} }
if (missingMembers.size() > 0) { builder.clearMembers();
for (Address removedMember : missingMembers) {
builder.addMembers(removedMember.serialize()); // 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());
}
for (Address removedMember : removedMembers) {
builder.addMembers(removedMember.serialize());
} }
if (group.getName().isPresent() || group.getAvatar().isPresent()) { if (group.getName().isPresent() || group.getAvatar().isPresent()) {