Implement JsonRpc command for updateGroup

Co-authored-by: technillogue <technillogue@gmail.com>

Closes #666
This commit is contained in:
AsamK 2021-08-10 14:33:18 +02:00
parent b09677a46c
commit 6826e3cb49
6 changed files with 84 additions and 90 deletions

View file

@ -1,35 +0,0 @@
package org.asamk.signal;
public enum GroupLinkState {
ENABLED {
@Override
public String toString() {
return "enabled";
}
},
ENABLED_WITH_APPROVAL {
@Override
public String toString() {
return "enabled-with-approval";
}
},
DISABLED {
@Override
public String toString() {
return "disabled";
}
};
public org.asamk.signal.manager.groups.GroupLinkState toLinkState() {
switch (this) {
case ENABLED:
return org.asamk.signal.manager.groups.GroupLinkState.ENABLED;
case ENABLED_WITH_APPROVAL:
return org.asamk.signal.manager.groups.GroupLinkState.ENABLED_WITH_APPROVAL;
case DISABLED:
return org.asamk.signal.manager.groups.GroupLinkState.DISABLED;
default:
throw new AssertionError();
}
}
}

View file

@ -1,27 +0,0 @@
package org.asamk.signal;
public enum GroupPermission {
EVERY_MEMBER {
@Override
public String toString() {
return "every-member";
}
},
ONLY_ADMINS {
@Override
public String toString() {
return "only-admins";
}
};
public org.asamk.signal.manager.groups.GroupPermission toManager() {
switch (this) {
case EVERY_MEMBER:
return org.asamk.signal.manager.groups.GroupPermission.EVERY_MEMBER;
case ONLY_ADMINS:
return org.asamk.signal.manager.groups.GroupPermission.ONLY_ADMINS;
default:
throw new AssertionError();
}
}
}

View file

@ -17,7 +17,7 @@ import org.whispersystems.signalservice.internal.push.exceptions.GroupPatchNotAc
import java.io.IOException; import java.io.IOException;
import static org.asamk.signal.util.ErrorUtils.handleTimestampAndSendMessageResults; import static org.asamk.signal.util.ErrorUtils.handleSendMessageResults;
public class JoinGroupCommand implements LocalCommand { public class JoinGroupCommand implements LocalCommand {
@ -58,7 +58,7 @@ public class JoinGroupCommand implements LocalCommand {
} else { } else {
writer.println("Joined group \"{}\"", newGroupId.toBase64()); writer.println("Joined group \"{}\"", newGroupId.toBase64());
} }
handleTimestampAndSendMessageResults(writer, 0, results.second()); handleSendMessageResults(results.second());
} catch (GroupPatchNotAcceptedException e) { } catch (GroupPatchNotAcceptedException e) {
throw new UserErrorException("Failed to join group, maybe already a member"); throw new UserErrorException("Failed to join group, maybe already a member");
} catch (IOException e) { } catch (IOException e) {

View file

@ -63,7 +63,9 @@ public class QuitGroupCommand implements LocalCommand {
try { try {
final var results = m.sendQuitGroupMessage(groupId, final var results = m.sendQuitGroupMessage(groupId,
groupAdmins == null ? Set.of() : new HashSet<>(groupAdmins)); groupAdmins == null ? Set.of() : new HashSet<>(groupAdmins));
handleTimestampAndSendMessageResults(writer, results.first(), results.second()); final var timestamp = results.first();
writer.println("{}", timestamp);
handleTimestampAndSendMessageResults(results.second());
} catch (NotAGroupMemberException e) { } catch (NotAGroupMemberException e) {
logger.info("User is not a group member"); logger.info("User is not a group member");
} }

View file

@ -5,8 +5,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.GroupLinkState; import org.asamk.signal.JsonWriter;
import org.asamk.signal.GroupPermission;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
@ -16,7 +15,9 @@ 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.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupLinkState;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.GroupPermission;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.util.ErrorUtils; import org.asamk.signal.util.ErrorUtils;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
@ -28,10 +29,10 @@ import org.whispersystems.signalservice.api.util.InvalidNumberException;
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.Base64; import java.util.HashMap;
import java.util.List; import java.util.List;
public class UpdateGroupCommand implements DbusCommand, LocalCommand { public class UpdateGroupCommand implements DbusCommand, JsonRpcLocalCommand {
private final static Logger logger = LoggerFactory.getLogger(UpdateGroupCommand.class); private final static Logger logger = LoggerFactory.getLogger(UpdateGroupCommand.class);
private final OutputWriter outputWriter; private final OutputWriter outputWriter;
@ -60,21 +61,53 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
.help("Reset group link and create new link password"); .help("Reset group link and create new link password");
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)); .choices("enabled", "enabled-with-approval", "disabled");
subparser.addArgument("--set-permission-add-member") subparser.addArgument("--set-permission-add-member")
.help("Set permission to add new group members") .help("Set permission to add new group members")
.type(Arguments.enumStringType(GroupPermission.class)); .choices("every-member", "only-admins");
subparser.addArgument("--set-permission-edit-details") subparser.addArgument("--set-permission-edit-details")
.help("Set permission to edit group details") .help("Set permission to edit group details")
.type(Arguments.enumStringType(GroupPermission.class)); .choices("every-member", "only-admins");
subparser.addArgument("-e", "--expiration").type(int.class).help("Set expiration time of messages (seconds)"); subparser.addArgument("-e", "--expiration").type(int.class).help("Set expiration time of messages (seconds)");
} }
GroupLinkState getGroupLinkState(String value) throws UserErrorException {
if (value == null) {
return null;
}
switch (value) {
case "enabled":
return GroupLinkState.ENABLED;
case "enabled-with-approval":
case "enabledWithApproval":
return GroupLinkState.ENABLED_WITH_APPROVAL;
case "disabled":
return GroupLinkState.DISABLED;
default:
throw new UserErrorException("Invalid group link state: " + value);
}
}
GroupPermission getGroupPermission(String value) throws UserErrorException {
if (value == null) {
return null;
}
switch (value) {
case "every-member":
case "everyMember":
return GroupPermission.EVERY_MEMBER;
case "only-admins":
case "onlyAdmins":
return GroupPermission.ONLY_ADMINS;
default:
throw new UserErrorException("Invalid group permission: " + value);
}
}
@Override @Override
public void handleCommand(final Namespace ns, final Manager m) throws CommandException { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final var writer = (PlainTextWriter) outputWriter;
GroupId groupId = null; GroupId groupId = null;
final var groupIdString = ns.getString("group-id"); final var groupIdString = ns.getString("group-id");
if (groupIdString != null) { if (groupIdString != null) {
@ -93,19 +126,20 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
var groupRemoveAdmins = ns.<String>getList("remove-admin"); var groupRemoveAdmins = ns.<String>getList("remove-admin");
var groupAvatar = ns.getString("avatar"); var groupAvatar = ns.getString("avatar");
var groupResetLink = ns.getBoolean("reset-link"); var groupResetLink = ns.getBoolean("reset-link");
var groupLinkState = ns.<GroupLinkState>get("link"); var groupLinkState = getGroupLinkState(ns.getString("link"));
var groupExpiration = ns.getInt("expiration"); var groupExpiration = ns.getInt("expiration");
var groupAddMemberPermission = ns.<GroupPermission>get("set-permission-add-member"); var groupAddMemberPermission = getGroupPermission(ns.getString("set-permission-add-member"));
var groupEditDetailsPermission = ns.<GroupPermission>get("set-permission-edit-details"); var groupEditDetailsPermission = getGroupPermission(ns.getString("set-permission-edit-details"));
try { try {
boolean isNewGroup = false;
if (groupId == null) { if (groupId == null) {
isNewGroup = true;
var results = m.createGroup(groupName, var results = m.createGroup(groupName,
groupMembers, groupMembers,
groupAvatar == null ? null : new File(groupAvatar)); groupAvatar == null ? null : new File(groupAvatar));
ErrorUtils.handleTimestampAndSendMessageResults(writer, 0, results.second()); ErrorUtils.handleSendMessageResults(results.second());
groupId = results.first(); groupId = results.first();
writer.println("Created new group: \"{}\"", groupId.toBase64());
groupName = null; groupName = null;
groupMembers = null; groupMembers = null;
groupAvatar = null; groupAvatar = null;
@ -119,14 +153,17 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
groupAdmins, groupAdmins,
groupRemoveAdmins, groupRemoveAdmins,
groupResetLink, groupResetLink,
groupLinkState != null ? groupLinkState.toLinkState() : null, groupLinkState,
groupAddMemberPermission != null ? groupAddMemberPermission.toManager() : null, groupAddMemberPermission,
groupEditDetailsPermission != null ? groupEditDetailsPermission.toManager() : null, groupEditDetailsPermission,
groupAvatar == null ? null : new File(groupAvatar), groupAvatar == null ? null : new File(groupAvatar),
groupExpiration); groupExpiration);
Long timestamp = null;
if (results != null) { if (results != null) {
ErrorUtils.handleTimestampAndSendMessageResults(writer, results.first(), results.second()); timestamp = results.first();
ErrorUtils.handleSendMessageResults(results.second());
} }
outputResult(timestamp, isNewGroup ? groupId : null);
} catch (AttachmentInvalidException e) { } catch (AttachmentInvalidException e) {
throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage()); throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage());
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
@ -142,7 +179,6 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
@Override @Override
public void handleCommand(final Namespace ns, final Signal signal) throws CommandException { public void handleCommand(final Namespace ns, final Signal signal) throws CommandException {
final var writer = (PlainTextWriter) outputWriter;
byte[] groupId = null; byte[] groupId = null;
if (ns.getString("group-id") != null) { if (ns.getString("group-id") != null) {
try { try {
@ -173,7 +209,7 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
try { try {
var newGroupId = signal.updateGroup(groupId, groupName, groupMembers, groupAvatar); var newGroupId = signal.updateGroup(groupId, groupName, groupMembers, groupAvatar);
if (groupId.length != newGroupId.length) { if (groupId.length != newGroupId.length) {
writer.println("Created new group: \"{}\"", Base64.getEncoder().encodeToString(newGroupId)); outputResult(null, GroupId.unknownVersion(newGroupId));
} }
} catch (Signal.Error.AttachmentInvalid e) { } catch (Signal.Error.AttachmentInvalid e) {
throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage()); throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage());
@ -181,4 +217,26 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage()); throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
} }
} }
private void outputResult(final Long timestamp, final GroupId groupId) {
if (outputWriter instanceof PlainTextWriter) {
final var writer = (PlainTextWriter) outputWriter;
if (groupId != null) {
writer.println("Created new group: \"{}\"", groupId.toBase64());
}
if (timestamp != null) {
writer.println("{}", timestamp);
}
} else {
final var writer = (JsonWriter) outputWriter;
final var result = new HashMap<>();
if (timestamp != null) {
result.put("timestamp", timestamp);
}
if (groupId != null) {
result.put("groupId", groupId.toBase64());
}
writer.write(result);
}
}
} }

View file

@ -1,6 +1,5 @@
package org.asamk.signal.util; package org.asamk.signal.util;
import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -21,12 +20,9 @@ public class ErrorUtils {
private ErrorUtils() { private ErrorUtils() {
} }
public static void handleTimestampAndSendMessageResults( public static void handleSendMessageResults(
PlainTextWriter writer, long timestamp, List<SendMessageResult> results List<SendMessageResult> results
) throws CommandException { ) throws CommandException {
if (timestamp != 0) {
writer.println("{}", timestamp);
}
var errors = getErrorMessagesFromSendMessageResults(results); var errors = getErrorMessagesFromSendMessageResults(results);
handleSendMessageResultErrors(errors); handleSendMessageResultErrors(errors);
} }