mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Improve internal group handling for receiving
This commit is contained in:
parent
d9b082a59e
commit
9427616906
3 changed files with 42 additions and 39 deletions
|
@ -25,11 +25,8 @@ public class JsonGroupStore {
|
|||
groups.put(Base64.encodeBytes(group.groupId), group);
|
||||
}
|
||||
|
||||
GroupInfo getGroup(byte[] groupId) throws GroupNotFoundException {
|
||||
GroupInfo getGroup(byte[] groupId) {
|
||||
GroupInfo g = groups.get(Base64.encodeBytes(groupId));
|
||||
if (g == null) {
|
||||
throw new GroupNotFoundException(groupId);
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
|
|
|
@ -665,7 +665,7 @@ public class Main {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, GroupInfo group) {
|
||||
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content) {
|
||||
SignalServiceAddress source = envelope.getSourceAddress();
|
||||
ContactInfo sourceContact = m.getContact(source.getNumber());
|
||||
System.out.println(String.format("Envelope from: %s (device: %d)", (sourceContact == null ? "" : "“" + sourceContact.name + "” ") + source.getNumber(), envelope.getSourceDevice()));
|
||||
|
@ -682,7 +682,7 @@ public class Main {
|
|||
} else {
|
||||
if (content.getDataMessage().isPresent()) {
|
||||
SignalServiceDataMessage message = content.getDataMessage().get();
|
||||
handleSignalServiceDataMessage(message, group);
|
||||
handleSignalServiceDataMessage(message);
|
||||
}
|
||||
if (content.getSyncMessage().isPresent()) {
|
||||
System.out.println("Received a sync message");
|
||||
|
@ -725,7 +725,7 @@ public class Main {
|
|||
}
|
||||
System.out.println("To: " + to + " , Message timestamp: " + sentTranscriptMessage.getTimestamp());
|
||||
SignalServiceDataMessage message = sentTranscriptMessage.getMessage();
|
||||
handleSignalServiceDataMessage(message, null);
|
||||
handleSignalServiceDataMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -735,8 +735,7 @@ public class Main {
|
|||
System.out.println();
|
||||
}
|
||||
|
||||
// TODO remove group parameter
|
||||
private void handleSignalServiceDataMessage(SignalServiceDataMessage message, GroupInfo group) {
|
||||
private void handleSignalServiceDataMessage(SignalServiceDataMessage message) {
|
||||
System.out.println("Message timestamp: " + message.getTimestamp());
|
||||
|
||||
if (message.getBody().isPresent()) {
|
||||
|
@ -748,10 +747,13 @@ public class Main {
|
|||
System.out.println(" Id: " + Base64.encodeBytes(groupInfo.getGroupId()));
|
||||
if (groupInfo.getName().isPresent()) {
|
||||
System.out.println(" Name: " + groupInfo.getName().get());
|
||||
} else if (group != null) {
|
||||
System.out.println(" Name: " + group.name);
|
||||
} else {
|
||||
System.out.println(" Name: <Unknown group>");
|
||||
GroupInfo group = m.getGroup(groupInfo.getGroupId());
|
||||
if (group != null) {
|
||||
System.out.println(" Name: " + group.name);
|
||||
} else {
|
||||
System.out.println(" Name: <Unknown group>");
|
||||
}
|
||||
}
|
||||
System.out.println(" Type: " + groupInfo.getType());
|
||||
if (groupInfo.getMembers().isPresent()) {
|
||||
|
@ -799,8 +801,8 @@ public class Main {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, GroupInfo group) {
|
||||
super.handleMessage(envelope, content, group);
|
||||
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content) {
|
||||
super.handleMessage(envelope, content);
|
||||
|
||||
if (!envelope.isReceipt() && content != null && content.getDataMessage().isPresent()) {
|
||||
SignalServiceDataMessage message = content.getDataMessage().get();
|
||||
|
|
|
@ -435,7 +435,11 @@ class Manager implements Signal {
|
|||
}
|
||||
SignalServiceDataMessage message = messageBuilder.build();
|
||||
|
||||
Set<String> members = groupStore.getGroup(groupId).members;
|
||||
GroupInfo g = groupStore.getGroup(groupId);
|
||||
if (g == null) {
|
||||
throw new GroupNotFoundException(groupId);
|
||||
}
|
||||
Set<String> members = g.members;
|
||||
members.remove(this.username);
|
||||
sendMessage(message, members);
|
||||
}
|
||||
|
@ -450,6 +454,9 @@ class Manager implements Signal {
|
|||
.build();
|
||||
|
||||
final GroupInfo g = groupStore.getGroup(groupId);
|
||||
if (g == null) {
|
||||
throw new GroupNotFoundException(groupId);
|
||||
}
|
||||
g.members.remove(this.username);
|
||||
groupStore.updateGroup(g);
|
||||
|
||||
|
@ -464,6 +471,9 @@ class Manager implements Signal {
|
|||
g.members.add(username);
|
||||
} else {
|
||||
g = groupStore.getGroup(groupId);
|
||||
if (g == null) {
|
||||
throw new GroupNotFoundException(groupId);
|
||||
}
|
||||
}
|
||||
|
||||
if (name != null) {
|
||||
|
@ -623,18 +633,17 @@ class Manager implements Signal {
|
|||
}
|
||||
|
||||
public interface ReceiveMessageHandler {
|
||||
void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, GroupInfo group);
|
||||
void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent);
|
||||
}
|
||||
|
||||
private GroupInfo handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, String source, String destination) {
|
||||
GroupInfo group = null;
|
||||
private void handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, String source, String destination) {
|
||||
if (message.getGroupInfo().isPresent()) {
|
||||
SignalServiceGroup groupInfo = message.getGroupInfo().get();
|
||||
switch (groupInfo.getType()) {
|
||||
case UPDATE:
|
||||
try {
|
||||
group = groupStore.getGroup(groupInfo.getGroupId());
|
||||
} catch (GroupNotFoundException e) {
|
||||
GroupInfo group;
|
||||
group = groupStore.getGroup(groupInfo.getGroupId());
|
||||
if (group == null) {
|
||||
group = new GroupInfo(groupInfo.getGroupId());
|
||||
}
|
||||
|
||||
|
@ -663,17 +672,12 @@ class Manager implements Signal {
|
|||
groupStore.updateGroup(group);
|
||||
break;
|
||||
case DELIVER:
|
||||
try {
|
||||
group = groupStore.getGroup(groupInfo.getGroupId());
|
||||
} catch (GroupNotFoundException e) {
|
||||
}
|
||||
break;
|
||||
case QUIT:
|
||||
try {
|
||||
group = groupStore.getGroup(groupInfo.getGroupId());
|
||||
group = groupStore.getGroup(groupInfo.getGroupId());
|
||||
if (group != null) {
|
||||
group.members.remove(source);
|
||||
groupStore.updateGroup(group);
|
||||
} catch (GroupNotFoundException e) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -692,7 +696,6 @@ class Manager implements Signal {
|
|||
}
|
||||
}
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
public void receiveMessages(int timeoutSeconds, boolean returnOnTimeout, ReceiveMessageHandler handler) throws IOException {
|
||||
|
@ -705,7 +708,6 @@ class Manager implements Signal {
|
|||
while (true) {
|
||||
SignalServiceEnvelope envelope;
|
||||
SignalServiceContent content = null;
|
||||
GroupInfo group = null;
|
||||
try {
|
||||
envelope = messagePipe.read(timeoutSeconds, TimeUnit.SECONDS);
|
||||
if (!envelope.isReceipt()) {
|
||||
|
@ -713,13 +715,13 @@ class Manager implements Signal {
|
|||
if (content != null) {
|
||||
if (content.getDataMessage().isPresent()) {
|
||||
SignalServiceDataMessage message = content.getDataMessage().get();
|
||||
group = handleSignalServiceDataMessage(message, false, envelope.getSource(), username);
|
||||
handleSignalServiceDataMessage(message, false, envelope.getSource(), username);
|
||||
}
|
||||
if (content.getSyncMessage().isPresent()) {
|
||||
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
|
||||
if (syncMessage.getSent().isPresent()) {
|
||||
SignalServiceDataMessage message = syncMessage.getSent().get().getMessage();
|
||||
group = handleSignalServiceDataMessage(message, true, envelope.getSource(), syncMessage.getSent().get().getDestination().get());
|
||||
handleSignalServiceDataMessage(message, true, envelope.getSource(), syncMessage.getSent().get().getDestination().get());
|
||||
}
|
||||
if (syncMessage.getRequest().isPresent()) {
|
||||
RequestMessage rm = syncMessage.getRequest().get();
|
||||
|
@ -747,10 +749,8 @@ class Manager implements Signal {
|
|||
DeviceGroupsInputStream s = new DeviceGroupsInputStream(retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer()));
|
||||
DeviceGroup g;
|
||||
while ((g = s.read()) != null) {
|
||||
GroupInfo syncGroup;
|
||||
try {
|
||||
syncGroup = groupStore.getGroup(g.getId());
|
||||
} catch (GroupNotFoundException e) {
|
||||
GroupInfo syncGroup = groupStore.getGroup(g.getId());
|
||||
if (syncGroup == null) {
|
||||
syncGroup = new GroupInfo(g.getId());
|
||||
}
|
||||
if (g.getName().isPresent()) {
|
||||
|
@ -796,7 +796,7 @@ class Manager implements Signal {
|
|||
}
|
||||
}
|
||||
save();
|
||||
handler.handleMessage(envelope, content, group);
|
||||
handler.handleMessage(envelope, content);
|
||||
} catch (TimeoutException e) {
|
||||
if (returnOnTimeout)
|
||||
return;
|
||||
|
@ -892,7 +892,7 @@ class Manager implements Signal {
|
|||
try {
|
||||
for (GroupInfo record : groupStore.getGroups()) {
|
||||
out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name),
|
||||
new ArrayList<>(record.members), Optional.<SignalServiceAttachmentStream>absent(), // TODO
|
||||
new ArrayList<>(record.members), Optional.<SignalServiceAttachmentStream>absent(), // TODO add avatar
|
||||
record.active));
|
||||
}
|
||||
} finally {
|
||||
|
@ -922,7 +922,7 @@ class Manager implements Signal {
|
|||
try {
|
||||
for (ContactInfo record : contactStore.getContacts()) {
|
||||
out.write(new DeviceContact(record.number, Optional.fromNullable(record.name),
|
||||
Optional.<SignalServiceAttachmentStream>absent())); // TODO
|
||||
Optional.<SignalServiceAttachmentStream>absent())); // TODO add avatar
|
||||
}
|
||||
} finally {
|
||||
out.close();
|
||||
|
@ -946,4 +946,8 @@ class Manager implements Signal {
|
|||
public ContactInfo getContact(String number) {
|
||||
return contactStore.getContact(number);
|
||||
}
|
||||
|
||||
public GroupInfo getGroup(byte[] groupId) {
|
||||
return groupStore.getGroup(groupId);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue