+
+ public class DbusSignalConfigurationImpl extends DbusProperties implements Signal.Configuration {
+
+ public DbusSignalConfigurationImpl() {
+ super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Configuration",
+ List.of(new DbusProperty<>("ReadReceipts", this::getReadReceipts, this::setReadReceipts),
+ new DbusProperty<>("UnidentifiedDeliveryIndicators",
+ this::getUnidentifiedDeliveryIndicators,
+ this::setUnidentifiedDeliveryIndicators),
+ new DbusProperty<>("TypingIndicators",
+ this::getTypingIndicators,
+ this::setTypingIndicators),
+ new DbusProperty<>("LinkPreviews", this::getLinkPreviews, this::setLinkPreviews))));
+
+ }
+
+ @Override
+ public String getObjectPath() {
+ return getConfigurationObjectPath(objectPath);
+ }
+
+ public void setReadReceipts(Boolean readReceipts) {
+ setConfiguration(readReceipts, null, null, null);
+ }
+
+ public void setUnidentifiedDeliveryIndicators(Boolean unidentifiedDeliveryIndicators) {
+ setConfiguration(null, unidentifiedDeliveryIndicators, null, null);
+ }
+
+ public void setTypingIndicators(Boolean typingIndicators) {
+ setConfiguration(null, null, typingIndicators, null);
+ }
+
+ public void setLinkPreviews(Boolean linkPreviews) {
+ setConfiguration(null, null, null, linkPreviews);
+ }
+
+ private void setConfiguration(
+ Boolean readReceipts,
+ Boolean unidentifiedDeliveryIndicators,
+ Boolean typingIndicators,
+ Boolean linkPreviews
+ ) {
+ try {
+ m.updateConfiguration(new org.asamk.signal.manager.api.Configuration(Optional.ofNullable(readReceipts),
+ Optional.ofNullable(unidentifiedDeliveryIndicators),
+ Optional.ofNullable(typingIndicators),
+ Optional.ofNullable(linkPreviews)));
+ } catch (NotPrimaryDeviceException e) {
+ throw new Error.Failure("This command doesn't work on linked devices.");
+ }
+ }
+
+ private boolean getReadReceipts() {
+ return m.getConfiguration().readReceipts().orElse(false);
+ }
+
+ private boolean getUnidentifiedDeliveryIndicators() {
+ return m.getConfiguration().unidentifiedDeliveryIndicators().orElse(false);
+ }
+
+ private boolean getTypingIndicators() {
+ return m.getConfiguration().typingIndicators().orElse(false);
+ }
+
+ private boolean getLinkPreviews() {
+ return m.getConfiguration().linkPreviews().orElse(false);
+ }
+ }
+
+ public class DbusSignalGroupImpl extends DbusProperties implements Signal.Group {
+
+ private final GroupId groupId;
+
+ public DbusSignalGroupImpl(final GroupId groupId) {
+ this.groupId = groupId;
+ super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Group",
+ List.of(new DbusProperty<>("Id", groupId::serialize),
+ new DbusProperty<>("Name", () -> emptyIfNull(getGroup().title()), this::setGroupName),
+ new DbusProperty<>("Description",
+ () -> emptyIfNull(getGroup().description()),
+ this::setGroupDescription),
+ new DbusProperty<>("Avatar", this::setGroupAvatar),
+ new DbusProperty<>("IsBlocked", () -> getGroup().isBlocked(), this::setIsBlocked),
+ new DbusProperty<>("IsMember", () -> getGroup().isMember()),
+ new DbusProperty<>("IsAdmin", () -> getGroup().isAdmin()),
+ new DbusProperty<>("MessageExpirationTimer",
+ () -> getGroup().messageExpirationTimer(),
+ this::setMessageExpirationTime),
+ new DbusProperty<>("Members",
+ () -> new Variant<>(getRecipientStrings(getGroup().members()), "as")),
+ new DbusProperty<>("PendingMembers",
+ () -> new Variant<>(getRecipientStrings(getGroup().pendingMembers()), "as")),
+ new DbusProperty<>("RequestingMembers",
+ () -> new Variant<>(getRecipientStrings(getGroup().requestingMembers()), "as")),
+ new DbusProperty<>("Admins",
+ () -> new Variant<>(getRecipientStrings(getGroup().adminMembers()), "as")),
+ new DbusProperty<>("Banned",
+ () -> new Variant<>(getRecipientStrings(getGroup().bannedMembers()), "as")),
+ new DbusProperty<>("PermissionAddMember",
+ () -> getGroup().permissionAddMember().name(),
+ this::setGroupPermissionAddMember),
+ new DbusProperty<>("PermissionEditDetails",
+ () -> getGroup().permissionEditDetails().name(),
+ this::setGroupPermissionEditDetails),
+ new DbusProperty<>("PermissionSendMessage",
+ () -> getGroup().permissionSendMessage().name(),
+ this::setGroupPermissionSendMessage),
+ new DbusProperty<>("GroupInviteLink", () -> {
+ final var groupInviteLinkUrl = getGroup().groupInviteLinkUrl();
+ return groupInviteLinkUrl == null ? "" : groupInviteLinkUrl.getUrl();
+ }))));
+ }
+
+ @Override
+ public String getObjectPath() {
+ return getGroupObjectPath(objectPath, groupId.serialize());
+ }
+
+ @Override
+ public void quitGroup() throws Error.Failure {
+ try {
+ m.quitGroup(groupId, Set.of());
+ } catch (GroupNotFoundException e) {
+ throw new Error.GroupNotFound(e.getMessage());
+ } catch (NotAGroupMemberException e) {
+ throw new Error.NotAGroupMember(e.getMessage());
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ } catch (LastGroupAdminException e) {
+ throw new Error.LastGroupAdmin(e.getMessage());
+ } catch (UnregisteredRecipientException e) {
+ throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered.");
+ }
+ }
+
+ @Override
+ public void deleteGroup() throws Error.Failure, Error.LastGroupAdmin {
+ try {
+ m.deleteGroup(groupId);
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ }
+ updateGroups();
+ }
+
+ @Override
+ public void addMembers(final List<String> recipients) throws Error.Failure {
+ final var memberIdentifiers = getSingleRecipientIdentifiers(recipients, m.getSelfNumber());
+ updateGroup(UpdateGroup.newBuilder().withMembers(memberIdentifiers).build());
+ }
+
+ @Override
+ public void removeMembers(final List<String> recipients) throws Error.Failure {
+ final var memberIdentifiers = getSingleRecipientIdentifiers(recipients, m.getSelfNumber());
+ updateGroup(UpdateGroup.newBuilder().withRemoveMembers(memberIdentifiers).build());
+ }
+
+ @Override
+ public void addAdmins(final List<String> recipients) throws Error.Failure {
+ final var memberIdentifiers = getSingleRecipientIdentifiers(recipients, m.getSelfNumber());
+ updateGroup(UpdateGroup.newBuilder().withAdmins(memberIdentifiers).build());
+ }
+
+ @Override
+ public void removeAdmins(final List<String> recipients) throws Error.Failure {
+ final var memberIdentifiers = getSingleRecipientIdentifiers(recipients, m.getSelfNumber());
+ updateGroup(UpdateGroup.newBuilder().withRemoveAdmins(memberIdentifiers).build());
+ }
+
+ @Override
+ public void resetLink() throws Error.Failure {
+ updateGroup(UpdateGroup.newBuilder().withResetGroupLink(true).build());
+ }
+
+ @Override
+ public void disableLink() throws Error.Failure {
+ updateGroup(UpdateGroup.newBuilder().withGroupLinkState(GroupLinkState.DISABLED).build());
+ }
+
+ @Override
+ public void enableLink(final boolean requiresApproval) throws Error.Failure {
+ updateGroup(UpdateGroup.newBuilder()
+ .withGroupLinkState(requiresApproval
+ ? GroupLinkState.ENABLED_WITH_APPROVAL
+ : GroupLinkState.ENABLED)
+ .build());
+ }
+
+ private org.asamk.signal.manager.api.Group getGroup() {
+ return m.getGroup(groupId);
+ }
+
+ private void setGroupName(final String name) {
+ updateGroup(UpdateGroup.newBuilder().withName(name).build());
+ }
+
+ private void setGroupDescription(final String description) {
+ updateGroup(UpdateGroup.newBuilder().withDescription(description).build());
+ }
+
+ private void setGroupAvatar(final String avatar) {
+ updateGroup(UpdateGroup.newBuilder().withAvatarFile(avatar).build());
+ }
+
+ private void setMessageExpirationTime(final int expirationTime) {
+ updateGroup(UpdateGroup.newBuilder().withExpirationTimer(expirationTime).build());
+ }
+
+ private void setGroupPermissionAddMember(final String permission) {
+ updateGroup(UpdateGroup.newBuilder().withAddMemberPermission(GroupPermission.valueOf(permission)).build());
+ }
+
+ private void setGroupPermissionEditDetails(final String permission) {
+ updateGroup(UpdateGroup.newBuilder()
+ .withEditDetailsPermission(GroupPermission.valueOf(permission))
+ .build());
+ }
+
+ private void setGroupPermissionSendMessage(final String permission) {
+ updateGroup(UpdateGroup.newBuilder()
+ .withIsAnnouncementGroup(GroupPermission.valueOf(permission) == GroupPermission.ONLY_ADMINS)
+ .build());
+ }
+
+ private void setIsBlocked(final boolean isBlocked) {
+ try {
+ m.setGroupsBlocked(List.of(groupId), isBlocked);
+ } catch (NotPrimaryDeviceException e) {
+ throw new Error.Failure("This command doesn't work on linked devices.");
+ } catch (GroupNotFoundException e) {
+ throw new Error.GroupNotFound(e.getMessage());
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ }
+ }
+
+ private void updateGroup(final UpdateGroup updateGroup) {
+ try {
+ m.updateGroup(groupId, updateGroup);
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
+ throw new Error.GroupNotFound(e.getMessage());
+ } catch (AttachmentInvalidException e) {
+ throw new Error.AttachmentInvalid(e.getMessage());
+ } catch (UnregisteredRecipientException e) {
+ throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered.");
+ }
+ }
+ }