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