Implement configuring of group link

This commit is contained in:
AsamK 2021-05-15 13:14:12 +02:00
parent 3de30e166f
commit 03589f858b
7 changed files with 121 additions and 5 deletions

View file

@ -23,6 +23,7 @@ import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupIdV1;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
import org.asamk.signal.manager.groups.GroupLinkState;
import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.GroupUtils;
import org.asamk.signal.manager.groups.NotAGroupMemberException;
@ -833,6 +834,8 @@ public class Manager implements Closeable {
List<String> removeMembers,
List<String> admins,
List<String> removeAdmins,
boolean resetGroupLink,
GroupLinkState groupLinkState,
File avatarFile
) throws IOException, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException, NotAGroupMemberException {
return updateGroup(groupId,
@ -842,6 +845,8 @@ public class Manager implements Closeable {
removeMembers == null ? null : getSignalServiceAddresses(removeMembers),
admins == null ? null : getSignalServiceAddresses(admins),
removeAdmins == null ? null : getSignalServiceAddresses(removeAdmins),
resetGroupLink,
groupLinkState,
avatarFile);
}
@ -853,6 +858,8 @@ public class Manager implements Closeable {
final Set<RecipientId> removeMembers,
final Set<RecipientId> admins,
final Set<RecipientId> removeAdmins,
final boolean resetGroupLink,
final GroupLinkState groupLinkState,
final File avatarFile
) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
var group = getGroupForUpdating(groupId);
@ -865,6 +872,8 @@ public class Manager implements Closeable {
removeMembers,
admins,
removeAdmins,
resetGroupLink,
groupLinkState,
avatarFile);
}
@ -928,6 +937,8 @@ public class Manager implements Closeable {
final Set<RecipientId> removeMembers,
final Set<RecipientId> admins,
final Set<RecipientId> removeAdmins,
final boolean resetGroupLink,
final GroupLinkState groupLinkState,
final File avatarFile
) throws IOException {
Pair<Long, List<SendMessageResult>> result = null;
@ -989,6 +1000,16 @@ public class Manager implements Closeable {
}
}
if (resetGroupLink) {
var groupGroupChangePair = groupV2Helper.resetGroupLinkPassword(group);
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
}
if (groupLinkState != null) {
var groupGroupChangePair = groupV2Helper.setGroupLinkState(group, groupLinkState);
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
}
if (result == null || name != null || description != null || avatarFile != null) {
var groupGroupChangePair = groupV2Helper.updateGroup(group, name, description, avatarFile);
if (avatarFile != null) {

View file

@ -0,0 +1,7 @@
package org.asamk.signal.manager.groups;
public enum GroupLinkState {
ENABLED,
ENABLED_WITH_APPROVAL,
DISABLED,
}

View file

@ -3,6 +3,7 @@ package org.asamk.signal.manager.helper;
import com.google.protobuf.InvalidProtocolBufferException;
import org.asamk.signal.manager.groups.GroupLinkPassword;
import org.asamk.signal.manager.groups.GroupLinkState;
import org.asamk.signal.manager.groups.GroupUtils;
import org.asamk.signal.manager.storage.groups.GroupInfoV2;
import org.asamk.signal.manager.storage.recipients.Profile;
@ -290,6 +291,29 @@ public class GroupV2Helper {
return revokeInvites(groupInfoV2, memberUuids);
}
public Pair<DecryptedGroup, GroupChange> resetGroupLinkPassword(GroupInfoV2 groupInfoV2) throws IOException {
final GroupsV2Operations.GroupOperations groupOperations = getGroupOperations(groupInfoV2);
final var newGroupLinkPassword = GroupLinkPassword.createNew().serialize();
final var change = groupOperations.createModifyGroupLinkPasswordChange(newGroupLinkPassword);
return commitChange(groupInfoV2, change);
}
public Pair<DecryptedGroup, GroupChange> setGroupLinkState(
GroupInfoV2 groupInfoV2, GroupLinkState state
) throws IOException {
final GroupsV2Operations.GroupOperations groupOperations = getGroupOperations(groupInfoV2);
final var accessRequired = toAccessControl(state);
final var requiresNewPassword = state != GroupLinkState.DISABLED && groupInfoV2.getGroup()
.getInviteLinkPassword()
.isEmpty();
final var change = requiresNewPassword ? groupOperations.createModifyGroupLinkPasswordAndRightsChange(
GroupLinkPassword.createNew().serialize(),
accessRequired) : groupOperations.createChangeJoinByLinkRights(accessRequired);
return commitChange(groupInfoV2, change);
}
public GroupChange joinGroup(
GroupMasterKey groupMasterKey,
GroupLinkPassword groupLinkPassword,
@ -345,6 +369,19 @@ public class GroupV2Helper {
return commitChange(groupInfoV2, change);
}
private AccessControl.AccessRequired toAccessControl(final GroupLinkState state) {
switch (state) {
case DISABLED:
return AccessControl.AccessRequired.UNSATISFIABLE;
case ENABLED:
return AccessControl.AccessRequired.ANY;
case ENABLED_WITH_APPROVAL:
return AccessControl.AccessRequired.ADMINISTRATOR;
default:
throw new AssertionError();
}
}
private GroupsV2Operations.GroupOperations getGroupOperations(final GroupInfoV2 groupInfoV2) {
final var groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupInfoV2.getMasterKey());
return groupsV2Operations.forGroup(groupSecretParams);

View file

@ -70,7 +70,7 @@ public class GroupInfoV2 extends GroupInfo {
@Override
public GroupInviteLinkUrl getGroupInviteLink() {
if (this.group == null || this.group.getInviteLinkPassword() == null || (
if (this.group == null || this.group.getInviteLinkPassword().isEmpty() || (
this.group.getAccessControl().getAddFromInviteLink() != AccessControl.AccessRequired.ANY
&& this.group.getAccessControl().getAddFromInviteLink()
!= AccessControl.AccessRequired.ADMINISTRATOR