Implement setting expiration timer for groups

This commit is contained in:
AsamK 2021-05-15 14:16:48 +02:00
parent 03589f858b
commit 7170a68571
4 changed files with 48 additions and 17 deletions

View file

@ -836,7 +836,8 @@ public class Manager implements Closeable {
List<String> removeAdmins, List<String> removeAdmins,
boolean resetGroupLink, boolean resetGroupLink,
GroupLinkState groupLinkState, GroupLinkState groupLinkState,
File avatarFile File avatarFile,
Integer expirationTimer
) throws IOException, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException, NotAGroupMemberException { ) throws IOException, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException, NotAGroupMemberException {
return updateGroup(groupId, return updateGroup(groupId,
name, name,
@ -847,7 +848,8 @@ public class Manager implements Closeable {
removeAdmins == null ? null : getSignalServiceAddresses(removeAdmins), removeAdmins == null ? null : getSignalServiceAddresses(removeAdmins),
resetGroupLink, resetGroupLink,
groupLinkState, groupLinkState,
avatarFile); avatarFile,
expirationTimer);
} }
private Pair<Long, List<SendMessageResult>> updateGroup( private Pair<Long, List<SendMessageResult>> updateGroup(
@ -860,7 +862,8 @@ public class Manager implements Closeable {
final Set<RecipientId> removeAdmins, final Set<RecipientId> removeAdmins,
final boolean resetGroupLink, final boolean resetGroupLink,
final GroupLinkState groupLinkState, final GroupLinkState groupLinkState,
final File avatarFile final File avatarFile,
Integer expirationTimer
) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException { ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
var group = getGroupForUpdating(groupId); var group = getGroupForUpdating(groupId);
@ -874,10 +877,16 @@ public class Manager implements Closeable {
removeAdmins, removeAdmins,
resetGroupLink, resetGroupLink,
groupLinkState, groupLinkState,
avatarFile); avatarFile,
expirationTimer);
} }
return updateGroupV1((GroupInfoV1) group, name, members, avatarFile); final var gv1 = (GroupInfoV1) group;
final var result = updateGroupV1(gv1, name, members, avatarFile);
if (expirationTimer != null) {
setExpirationTimer(gv1, expirationTimer);
}
return result;
} }
private Pair<Long, List<SendMessageResult>> updateGroupV1( private Pair<Long, List<SendMessageResult>> updateGroupV1(
@ -939,7 +948,8 @@ public class Manager implements Closeable {
final Set<RecipientId> removeAdmins, final Set<RecipientId> removeAdmins,
final boolean resetGroupLink, final boolean resetGroupLink,
final GroupLinkState groupLinkState, final GroupLinkState groupLinkState,
final File avatarFile final File avatarFile,
Integer expirationTimer
) throws IOException { ) throws IOException {
Pair<Long, List<SendMessageResult>> result = null; Pair<Long, List<SendMessageResult>> result = null;
if (group.isPendingMember(account.getSelfRecipientId())) { if (group.isPendingMember(account.getSelfRecipientId())) {
@ -1010,6 +1020,11 @@ public class Manager implements Closeable {
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second()); result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
} }
if (expirationTimer != null) {
var groupGroupChangePair = groupV2Helper.setMessageExpirationTimer(group, expirationTimer);
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
}
if (result == null || name != null || description != null || avatarFile != null) { if (result == null || name != null || description != null || avatarFile != null) {
var groupGroupChangePair = groupV2Helper.updateGroup(group, name, description, avatarFile); var groupGroupChangePair = groupV2Helper.updateGroup(group, name, description, avatarFile);
if (avatarFile != null) { if (avatarFile != null) {
@ -1306,15 +1321,17 @@ public class Manager implements Closeable {
/** /**
* Change the expiration timer for a group * Change the expiration timer for a group
*/ */
public void setExpirationTimer(GroupId groupId, int messageExpirationTimer) { private void setExpirationTimer(
var g = getGroup(groupId); GroupInfoV1 groupInfoV1, int messageExpirationTimer
if (g instanceof GroupInfoV1) { ) throws NotAGroupMemberException, GroupNotFoundException, IOException {
var groupInfoV1 = (GroupInfoV1) g; groupInfoV1.messageExpirationTime = messageExpirationTimer;
groupInfoV1.messageExpirationTime = messageExpirationTimer; account.getGroupStore().updateGroup(groupInfoV1);
account.getGroupStore().updateGroup(groupInfoV1); sendExpirationTimerUpdate(groupInfoV1.getGroupId());
} else { }
throw new RuntimeException("TODO Not implemented!");
} private void sendExpirationTimerUpdate(GroupIdV1 groupId) throws IOException, NotAGroupMemberException, GroupNotFoundException {
final var messageBuilder = SignalServiceDataMessage.newBuilder().asExpirationUpdate();
sendGroupMessage(messageBuilder, groupId);
} }
/** /**

View file

@ -369,6 +369,14 @@ public class GroupV2Helper {
return commitChange(groupInfoV2, change); return commitChange(groupInfoV2, change);
} }
public Pair<DecryptedGroup, GroupChange> setMessageExpirationTimer(
GroupInfoV2 groupInfoV2, int messageExpirationTimer
) throws IOException {
final GroupsV2Operations.GroupOperations groupOperations = getGroupOperations(groupInfoV2);
final var change = groupOperations.createModifyGroupTimerChange(messageExpirationTimer);
return commitChange(groupInfoV2, change);
}
private AccessControl.AccessRequired toAccessControl(final GroupLinkState state) { private AccessControl.AccessRequired toAccessControl(final GroupLinkState state) {
switch (state) { switch (state) {
case DISABLED: case DISABLED:

View file

@ -54,6 +54,8 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
subparser.addArgument("--link") subparser.addArgument("--link")
.help("Set group link state, with or without admin approval") .help("Set group link state, with or without admin approval")
.type(Arguments.enumStringType(GroupLinkState.class)); .type(Arguments.enumStringType(GroupLinkState.class));
subparser.addArgument("-e", "--expiration").type(int.class).help("Set expiration time of messages (seconds)");
} }
@Override @Override
@ -87,6 +89,8 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
var groupLinkState = ns.<GroupLinkState>get("link"); var groupLinkState = ns.<GroupLinkState>get("link");
var groupExpiration = ns.getInt("expiration");
try { try {
if (groupId == null) { if (groupId == null) {
var results = m.createGroup(groupName, var results = m.createGroup(groupName,
@ -105,7 +109,8 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
groupRemoveAdmins, groupRemoveAdmins,
groupResetLink, groupResetLink,
groupLinkState != null ? groupLinkState.toLinkState() : null, groupLinkState != null ? groupLinkState.toLinkState() : null,
groupAvatar == null ? null : new File(groupAvatar)); groupAvatar == null ? null : new File(groupAvatar),
groupExpiration);
ErrorUtils.handleTimestampAndSendMessageResults(writer, results.first(), results.second()); ErrorUtils.handleTimestampAndSendMessageResults(writer, results.first(), results.second());
} }
} catch (AttachmentInvalidException e) { } catch (AttachmentInvalidException e) {

View file

@ -349,7 +349,8 @@ public class DbusSignalImpl implements Signal {
null, null,
false, false,
null, null,
avatar == null ? null : new File(avatar)); avatar == null ? null : new File(avatar),
null);
checkSendMessageResults(results.first(), results.second()); checkSendMessageResults(results.first(), results.second());
return groupId; return groupId;
} }