+
+ @Override
+ public void updateProfile(
+ final String name,
+ final String about,
+ final String aboutEmoji,
+ String avatarPath,
+ final boolean removeAvatar
+ ) {
+ try {
+ if (avatarPath.isEmpty()) {
+ avatarPath = null;
+ }
+ Optional<File> avatarFile = removeAvatar
+ ? Optional.absent()
+ : avatarPath == null ? null : Optional.of(new File(avatarPath));
+ m.setProfile(name, about, aboutEmoji, avatarFile);
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ }
+ }
+
+ // Provide option to query a version string in order to react on potential
+ // future interface changes
+ @Override
+ public String version() {
+ return BaseConfig.PROJECT_VERSION;
+ }
+
+ // Create a unique list of Numbers from Identities and Contacts to really get
+ // all numbers the system knows
+ @Override
+ public List<String> listNumbers() {
+ return Stream.concat(m.getIdentities().stream().map(i -> i.getAddress().getNumber().orNull()),
+ m.getContacts().stream().map(c -> c.number))
+ .filter(Objects::nonNull)
+ .distinct()
+ .collect(Collectors.toList());
+ }
+
+ @Override
+ public List<String> getContactNumber(final String name) {
+ // Contact names have precedence.
+ var numbers = new ArrayList<String>();
+ var contacts = m.getContacts();
+ for (var c : contacts) {
+ if (c.name != null && c.name.equals(name)) {
+ numbers.add(c.number);
+ }
+ }
+ // Try profiles if no contact name was found
+ for (var identity : m.getIdentities()) {
+ final var address = identity.getAddress();
+ var number = address.getNumber().orNull();
+ if (number != null) {
+ var profile = m.getRecipientProfile(address);
+ if (profile != null && profile.getDisplayName().equals(name)) {
+ numbers.add(number);
+ }
+ }
+ }
+ return numbers;
+ }
+
+ @Override
+ public void quitGroup(final byte[] groupId) {
+ var group = GroupId.unknownVersion(groupId);
+ try {
+ m.sendQuitGroupMessage(group);
+ } catch (GroupNotFoundException | NotAGroupMemberException e) {
+ throw new Error.GroupNotFound(e.getMessage());
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ }
+ }
+
+ @Override
+ public void joinGroup(final String groupLink) {
+ try {
+ final var linkUrl = GroupInviteLinkUrl.fromUri(groupLink);
+ if (linkUrl == null) {
+ throw new Error.Failure("Group link is invalid:");
+ }
+ m.joinGroup(linkUrl);
+ } catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupLinkNotActiveException e) {
+ throw new Error.Failure("Group link is invalid: " + e.getMessage());
+ } catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) {
+ throw new Error.Failure("Group link was created with an incompatible version: " + e.getMessage());
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ }
+ }
+
+ @Override
+ public boolean isContactBlocked(final String number) {
+ var contacts = m.getContacts();
+ for (var c : contacts) {
+ if (c.number.equals(number)) {
+ return c.blocked;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public boolean isGroupBlocked(final byte[] groupId) {
+ var group = m.getGroup(GroupId.unknownVersion(groupId));
+ if (group == null) {
+ return false;
+ } else {
+ return group.isBlocked();
+ }
+ }
+
+ @Override
+ public boolean isMember(final byte[] groupId) {
+ var group = m.getGroup(GroupId.unknownVersion(groupId));
+ if (group == null) {
+ return false;
+ } else {
+ return group.isMember(m.getSelfAddress());
+ }
+ }