Improve dbus impl

This commit is contained in:
AsamK 2021-03-01 20:39:45 +01:00
parent 631f10c916
commit 253757cb31

View file

@ -8,8 +8,6 @@ import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl; import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.manager.storage.protocol.IdentityInfo;
import org.asamk.signal.manager.util.Utils;
import org.asamk.signal.util.ErrorUtils; import org.asamk.signal.util.ErrorUtils;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
@ -151,13 +149,11 @@ public class DbusSignalImpl implements Signal {
// the profile name // the profile name
@Override @Override
public String getContactName(final String number) { public String getContactName(final String number) {
String name = "";
try { try {
name = m.getContactOrProfileName(number); return m.getContactOrProfileName(number);
} catch (Exception e) { } catch (Exception e) {
throw new Error.InvalidNumber(e.getMessage()); throw new Error.InvalidNumber(e.getMessage());
} }
return name;
} }
@Override @Override
@ -299,8 +295,8 @@ public class DbusSignalImpl implements Signal {
@Override @Override
public List<String> getContactNumber(final String name) { public List<String> getContactNumber(final String name) {
// Contact names have precendence. // Contact names have precedence.
List<String> numbers = new ArrayList<>(); var numbers = new ArrayList<String>();
var contacts = m.getContacts(); var contacts = m.getContacts();
for (var c : contacts) { for (var c : contacts) {
if (c.name != null && c.name.equals(name)) { if (c.name != null && c.name.equals(name)) {
@ -308,20 +304,16 @@ public class DbusSignalImpl implements Signal {
} }
} }
// Try profiles if no contact name was found // Try profiles if no contact name was found
for (IdentityInfo identity : m.getIdentities()) { for (var identity : m.getIdentities()) {
String number = identity.getAddress().getNumber().orNull(); final var address = identity.getAddress();
var number = address.getNumber().orNull();
if (number != null) { if (number != null) {
var address = Utils.getSignalServiceAddressFromIdentifier(number);
var profile = m.getRecipientProfile(address); var profile = m.getRecipientProfile(address);
String profileName = profile.getDisplayName(); if (profile != null && profile.getDisplayName().equals(name)) {
if (profileName.equals(name)) {
numbers.add(number); numbers.add(number);
} }
} }
} }
if (numbers.size() == 0) {
throw new Error.Failure("Contact name not found");
}
return numbers; return numbers;
} }
@ -339,9 +331,8 @@ public class DbusSignalImpl implements Signal {
@Override @Override
public void joinGroup(final String groupLink) { public void joinGroup(final String groupLink) {
final GroupInviteLinkUrl linkUrl;
try { try {
linkUrl = GroupInviteLinkUrl.fromUri(groupLink); final var linkUrl = GroupInviteLinkUrl.fromUri(groupLink);
m.joinGroup(linkUrl); m.joinGroup(linkUrl);
} catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupLinkNotActiveException e) { } catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupLinkNotActiveException e) {
throw new Error.Failure("Group link is invalid: " + e.getMessage()); throw new Error.Failure("Group link is invalid: " + e.getMessage());
@ -382,5 +373,4 @@ public class DbusSignalImpl implements Signal {
return group.isMember(m.getSelfAddress()); return group.isMember(m.getSelfAddress());
} }
} }
} }