This commit is contained in:
AsamK 2021-03-01 20:33:12 +01:00
parent 1d2c7a479d
commit 631f10c916
2 changed files with 49 additions and 36 deletions

View file

@ -54,22 +54,22 @@ public interface Signal extends DBusInterface {
void updateProfile( void updateProfile(
String name, String about, String aboutEmoji, String avatarPath, boolean removeAvatar String name, String about, String aboutEmoji, String avatarPath, boolean removeAvatar
) throws Error.Failure; ) throws Error.Failure;
public String version(); public String version();
public List<String> listNumbers(); public List<String> listNumbers();
public List<String> getContactNumber(final String name) throws Error.Failure; public List<String> getContactNumber(final String name) throws Error.Failure;
public void quitGroup(final byte[] groupId) throws Error.GroupNotFound,Error.Failure; public void quitGroup(final byte[] groupId) throws Error.GroupNotFound, Error.Failure;
public boolean isContactBlocked(final String number); public boolean isContactBlocked(final String number);
public boolean isGroupBlocked(final byte[] groupId); public boolean isGroupBlocked(final byte[] groupId);
public boolean isMember(final byte[] groupId); public boolean isMember(final byte[] groupId);
public void joinGroup(final String groupLink) throws Error.Failure; public void joinGroup(final String groupLink) throws Error.Failure;
class MessageReceived extends DBusSignal { class MessageReceived extends DBusSignal {

View file

@ -1,30 +1,30 @@
package org.asamk.signal.dbus; package org.asamk.signal.dbus;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.BaseConfig;
import org.asamk.signal.manager.AttachmentInvalidException; import org.asamk.signal.manager.AttachmentInvalidException;
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.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.groups.GroupInviteLinkUrl;
import org.asamk.signal.manager.storage.protocol.IdentityInfo; import org.asamk.signal.manager.storage.protocol.IdentityInfo;
import org.asamk.signal.util.ErrorUtils;
import org.asamk.signal.manager.util.Utils; import org.asamk.signal.manager.util.Utils;
import org.asamk.signal.BaseConfig; 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;
import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
import org.whispersystems.signalservice.api.messages.SendMessageResult; import org.whispersystems.signalservice.api.messages.SendMessageResult;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.InvalidNumberException;
import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Stream;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DbusSignalImpl implements Signal { public class DbusSignalImpl implements Signal {
@ -67,8 +67,9 @@ public class DbusSignalImpl implements Signal {
} }
} }
private static void checkSendMessageResults(long timestamp, List<SendMessageResult> results) private static void checkSendMessageResults(
throws DBusExecutionException { long timestamp, List<SendMessageResult> results
) throws DBusExecutionException {
if (results.size() == 1) { if (results.size() == 1) {
checkSendMessageResult(timestamp, results.get(0)); checkSendMessageResult(timestamp, results.get(0));
return; return;
@ -105,8 +106,9 @@ public class DbusSignalImpl implements Signal {
} }
@Override @Override
public long sendNoteToSelfMessage(final String message, final List<String> attachments) public long sendNoteToSelfMessage(
throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity { final String message, final List<String> attachments
) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
try { try {
final var results = m.sendSelfMessage(message, attachments); final var results = m.sendSelfMessage(message, attachments);
checkSendMessageResult(results.first(), results.second()); checkSendMessageResult(results.first(), results.second());
@ -149,7 +151,7 @@ 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=""; String name = "";
try { try {
name = m.getContactOrProfileName(number); name = m.getContactOrProfileName(number);
} catch (Exception e) { } catch (Exception e) {
@ -211,8 +213,11 @@ public class DbusSignalImpl implements Signal {
if (group == null) { if (group == null) {
return List.of(); return List.of();
} else { } else {
return group.getMembers().stream().map(m::resolveSignalServiceAddress) return group.getMembers()
.map(SignalServiceAddress::getLegacyIdentifier).collect(Collectors.toList()); .stream()
.map(m::resolveSignalServiceAddress)
.map(SignalServiceAddress::getLegacyIdentifier)
.collect(Collectors.toList());
} }
} }
@ -231,7 +236,9 @@ public class DbusSignalImpl implements Signal {
if (avatar.isEmpty()) { if (avatar.isEmpty()) {
avatar = null; avatar = null;
} }
final var results = m.updateGroup(groupId == null ? null : GroupId.unknownVersion(groupId), name, members, final var results = m.updateGroup(groupId == null ? null : GroupId.unknownVersion(groupId),
name,
members,
avatar == null ? null : new File(avatar)); avatar == null ? null : new File(avatar));
checkSendMessageResults(0, results.second()); checkSendMessageResults(0, results.second());
return results.first().serialize(); return results.first().serialize();
@ -252,13 +259,19 @@ public class DbusSignalImpl implements Signal {
} }
@Override @Override
public void updateProfile(final String name, final String about, final String aboutEmoji, String avatarPath, public void updateProfile(
final boolean removeAvatar) { final String name,
final String about,
final String aboutEmoji,
String avatarPath,
final boolean removeAvatar
) {
try { try {
if (avatarPath.isEmpty()) { if (avatarPath.isEmpty()) {
avatarPath = null; avatarPath = null;
} }
Optional<File> avatarFile = removeAvatar ? Optional.absent() Optional<File> avatarFile = removeAvatar
? Optional.absent()
: avatarPath == null ? null : Optional.of(new File(avatarPath)); : avatarPath == null ? null : Optional.of(new File(avatarPath));
m.setProfile(name, about, aboutEmoji, avatarFile); m.setProfile(name, about, aboutEmoji, avatarFile);
} catch (IOException e) { } catch (IOException e) {
@ -277,7 +290,7 @@ public class DbusSignalImpl implements Signal {
// all numbers the system knows // all numbers the system knows
@Override @Override
public List<String> listNumbers() { public List<String> listNumbers() {
return Stream.concat(m.getIdentities().stream().map(i -> i.getAddress().getNumber().orNull()), return Stream.concat(m.getIdentities().stream().map(i -> i.getAddress().getNumber().orNull()),
m.getContacts().stream().map(c -> c.number)) m.getContacts().stream().map(c -> c.number))
.filter(Objects::nonNull) .filter(Objects::nonNull)
.distinct() .distinct()
@ -287,10 +300,10 @@ 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 precendence.
List<String> numbers=new ArrayList<>(); List<String> numbers = new ArrayList<>();
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)) {
numbers.add(c.number); numbers.add(c.number);
} }
} }
@ -306,7 +319,7 @@ public class DbusSignalImpl implements Signal {
} }
} }
} }
if (numbers.size()==0) { if (numbers.size() == 0) {
throw new Error.Failure("Contact name not found"); throw new Error.Failure("Contact name not found");
} }
return numbers; return numbers;