If contact name is not set, print profile name instead

This commit is contained in:
AsamK 2021-01-18 20:28:25 +01:00
parent 804ad34a08
commit f4ed9a01b7
2 changed files with 27 additions and 15 deletions

View file

@ -3,7 +3,6 @@ package org.asamk.signal;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupUtils; import org.asamk.signal.manager.groups.GroupUtils;
import org.asamk.signal.manager.storage.contacts.ContactInfo;
import org.asamk.signal.manager.storage.groups.GroupInfo; import org.asamk.signal.manager.storage.groups.GroupInfo;
import org.asamk.signal.util.DateUtils; import org.asamk.signal.util.DateUtils;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
@ -53,9 +52,9 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) { public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
if (!envelope.isUnidentifiedSender() && envelope.hasSource()) { if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
SignalServiceAddress source = envelope.getSourceAddress(); SignalServiceAddress source = envelope.getSourceAddress();
ContactInfo sourceContact = m.getContact(source.getLegacyIdentifier()); String name = m.getContactOrProfileName(source.getLegacyIdentifier());
System.out.println(String.format("Envelope from: %s (device: %d)", System.out.println(String.format("Envelope from: %s (device: %d)",
(sourceContact == null ? "" : "" + sourceContact.name + "") + source.getLegacyIdentifier(), (name == null ? "" : "" + name + "") + source.getLegacyIdentifier(),
envelope.getSourceDevice())); envelope.getSourceDevice()));
if (source.getRelay().isPresent()) { if (source.getRelay().isPresent()) {
System.out.println("Relayed by: " + source.getRelay().get()); System.out.println("Relayed by: " + source.getRelay().get());
@ -98,10 +97,9 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
if (content == null) { if (content == null) {
System.out.println("Failed to decrypt message."); System.out.println("Failed to decrypt message.");
} else { } else {
ContactInfo sourceContact = m.getContact(content.getSender().getLegacyIdentifier()); String senderName = m.getContactOrProfileName(content.getSender().getLegacyIdentifier());
System.out.println(String.format("Sender: %s (device: %d)", System.out.println(String.format("Sender: %s (device: %d)",
(sourceContact == null ? "" : "" + sourceContact.name + "") + content.getSender() (senderName == null ? "" : "" + senderName + "") + content.getSender().getLegacyIdentifier(),
.getLegacyIdentifier(),
content.getSenderDevice())); content.getSenderDevice()));
if (content.getDataMessage().isPresent()) { if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get(); SignalServiceDataMessage message = content.getDataMessage().get();
@ -127,10 +125,11 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
if (syncMessage.getRead().isPresent()) { if (syncMessage.getRead().isPresent()) {
System.out.println("Received sync read messages list"); System.out.println("Received sync read messages list");
for (ReadMessage rm : syncMessage.getRead().get()) { for (ReadMessage rm : syncMessage.getRead().get()) {
ContactInfo fromContact = m.getContact(rm.getSender().getLegacyIdentifier()); String name = m.getContactOrProfileName(rm.getSender().getLegacyIdentifier());
System.out.println("From: " System.out.println("From: "
+ (fromContact == null ? "" : "" + fromContact.name + "") + (name == null ? "" : "" + name + "")
+ rm.getSender().getLegacyIdentifier() + rm.getSender()
.getLegacyIdentifier()
+ " Message timestamp: " + " Message timestamp: "
+ DateUtils.formatTimestamp(rm.getTimestamp())); + DateUtils.formatTimestamp(rm.getTimestamp()));
} }
@ -159,13 +158,13 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
String to; String to;
if (sentTranscriptMessage.getDestination().isPresent()) { if (sentTranscriptMessage.getDestination().isPresent()) {
String dest = sentTranscriptMessage.getDestination().get().getLegacyIdentifier(); String dest = sentTranscriptMessage.getDestination().get().getLegacyIdentifier();
ContactInfo destContact = m.getContact(dest); String name = m.getContactOrProfileName(dest);
to = (destContact == null ? "" : "" + destContact.name + "") + dest; to = (name == null ? "" : "" + name + "") + dest;
} else if (sentTranscriptMessage.getRecipients().size() > 0) { } else if (sentTranscriptMessage.getRecipients().size() > 0) {
StringBuilder toBuilder = new StringBuilder(); StringBuilder toBuilder = new StringBuilder();
for (SignalServiceAddress dest : sentTranscriptMessage.getRecipients()) { for (SignalServiceAddress dest : sentTranscriptMessage.getRecipients()) {
ContactInfo destContact = m.getContact(dest.getLegacyIdentifier()); String name = m.getContactOrProfileName(dest.getLegacyIdentifier());
toBuilder.append(destContact == null ? "" : "" + destContact.name + "") toBuilder.append(name == null ? "" : "" + name + "")
.append(dest.getLegacyIdentifier()) .append(dest.getLegacyIdentifier())
.append(" "); .append(" ");
} }

View file

@ -137,6 +137,7 @@ import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException; import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException;
import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider; import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider;
import org.whispersystems.signalservice.internal.util.Hex; import org.whispersystems.signalservice.internal.util.Hex;
import org.whispersystems.signalservice.internal.util.Util;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
@ -2304,8 +2305,20 @@ public class Manager implements Closeable {
return account.getContactStore().getContacts(); return account.getContactStore().getContacts();
} }
public ContactInfo getContact(String number) { public String getContactOrProfileName(String number) {
return account.getContactStore().getContact(Utils.getSignalServiceAddressFromIdentifier(number)); final SignalServiceAddress address = Utils.getSignalServiceAddressFromIdentifier(number);
final ContactInfo contact = account.getContactStore().getContact(address);
if (contact != null && !Util.isEmpty(contact.name)) {
return contact.name;
}
final SignalProfileEntry profileEntry = account.getProfileStore().getProfileEntry(address);
if (profileEntry != null && profileEntry.getProfile() != null) {
return profileEntry.getProfile().getName();
}
return null;
} }
public GroupInfo getGroup(GroupId groupId) { public GroupInfo getGroup(GroupId groupId) {