mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 02:50:39 +00:00
Add RecipientIdentifier as external Manager interface
This commit is contained in:
parent
cd7172ee57
commit
467a48bac5
25 changed files with 958 additions and 595 deletions
99
src/main/java/org/asamk/signal/util/CommandUtil.java
Normal file
99
src/main/java/org/asamk/signal/util/CommandUtil.java
Normal file
|
@ -0,0 +1,99 @@
|
|||
package org.asamk.signal.util;
|
||||
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||
import org.asamk.signal.manager.groups.GroupId;
|
||||
import org.asamk.signal.manager.groups.GroupIdFormatException;
|
||||
import org.whispersystems.signalservice.api.util.InvalidNumberException;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class CommandUtil {
|
||||
|
||||
private CommandUtil() {
|
||||
}
|
||||
|
||||
public static Set<RecipientIdentifier> getRecipientIdentifiers(
|
||||
final Manager m,
|
||||
final boolean isNoteToSelf,
|
||||
final List<String> recipientStrings,
|
||||
final List<String> groupIdStrings
|
||||
) throws UserErrorException {
|
||||
final var recipientIdentifiers = new HashSet<RecipientIdentifier>();
|
||||
if (isNoteToSelf) {
|
||||
recipientIdentifiers.add(new RecipientIdentifier.NoteToSelf());
|
||||
}
|
||||
if (recipientStrings != null) {
|
||||
final var localNumber = m.getUsername();
|
||||
recipientIdentifiers.addAll(CommandUtil.getSingleRecipientIdentifiers(recipientStrings, localNumber));
|
||||
}
|
||||
if (groupIdStrings != null) {
|
||||
recipientIdentifiers.addAll(CommandUtil.getGroupIdentifiers(groupIdStrings));
|
||||
}
|
||||
|
||||
if (recipientIdentifiers.isEmpty()) {
|
||||
throw new UserErrorException("No recipients given");
|
||||
}
|
||||
return recipientIdentifiers;
|
||||
}
|
||||
|
||||
public static Set<RecipientIdentifier.Group> getGroupIdentifiers(Collection<String> groupIdStrings) throws UserErrorException {
|
||||
if (groupIdStrings == null) {
|
||||
return Set.of();
|
||||
}
|
||||
final var groupIds = new HashSet<RecipientIdentifier.Group>();
|
||||
for (final var groupIdString : groupIdStrings) {
|
||||
groupIds.add(new RecipientIdentifier.Group(getGroupId(groupIdString)));
|
||||
}
|
||||
return groupIds;
|
||||
}
|
||||
|
||||
public static Set<GroupId> getGroupIds(Collection<String> groupIdStrings) throws UserErrorException {
|
||||
if (groupIdStrings == null) {
|
||||
return Set.of();
|
||||
}
|
||||
final var groupIds = new HashSet<GroupId>();
|
||||
for (final var groupIdString : groupIdStrings) {
|
||||
groupIds.add(getGroupId(groupIdString));
|
||||
}
|
||||
return groupIds;
|
||||
}
|
||||
|
||||
public static GroupId getGroupId(String groupId) throws UserErrorException {
|
||||
if (groupId == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return GroupId.fromBase64(groupId);
|
||||
} catch (GroupIdFormatException e) {
|
||||
throw new UserErrorException("Invalid group id: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Set<RecipientIdentifier.Single> getSingleRecipientIdentifiers(
|
||||
final Collection<String> recipientStrings, final String localNumber
|
||||
) throws UserErrorException {
|
||||
if (recipientStrings == null) {
|
||||
return Set.of();
|
||||
}
|
||||
final var identifiers = new HashSet<RecipientIdentifier.Single>();
|
||||
for (var recipientString : recipientStrings) {
|
||||
identifiers.add(getSingleRecipientIdentifier(recipientString, localNumber));
|
||||
}
|
||||
return identifiers;
|
||||
}
|
||||
|
||||
public static RecipientIdentifier.Single getSingleRecipientIdentifier(
|
||||
final String recipientString, final String localNumber
|
||||
) throws UserErrorException {
|
||||
try {
|
||||
return RecipientIdentifier.Single.fromString(recipientString, localNumber);
|
||||
} catch (InvalidNumberException e) {
|
||||
throw new UserErrorException("Invalid phone number '" + recipientString + "': " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,13 +2,16 @@ package org.asamk.signal.util;
|
|||
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.signalservice.api.messages.SendMessageResult;
|
||||
import org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.asamk.signal.util.Util.getLegacyIdentifier;
|
||||
|
@ -21,13 +24,27 @@ public class ErrorUtils {
|
|||
}
|
||||
|
||||
public static void handleSendMessageResults(
|
||||
List<SendMessageResult> results
|
||||
Map<RecipientIdentifier, List<SendMessageResult>> mapResults
|
||||
) throws CommandException {
|
||||
List<String> errors = getErrorMessagesFromSendMessageResults(mapResults);
|
||||
handleSendMessageResultErrors(errors);
|
||||
}
|
||||
|
||||
public static void handleSendMessageResults(
|
||||
Collection<SendMessageResult> results
|
||||
) throws CommandException {
|
||||
var errors = getErrorMessagesFromSendMessageResults(results);
|
||||
handleSendMessageResultErrors(errors);
|
||||
}
|
||||
|
||||
public static List<String> getErrorMessagesFromSendMessageResults(List<SendMessageResult> results) {
|
||||
public static List<String> getErrorMessagesFromSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
|
||||
return mapResults.values()
|
||||
.stream()
|
||||
.flatMap(results -> getErrorMessagesFromSendMessageResults(results).stream())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static List<String> getErrorMessagesFromSendMessageResults(Collection<SendMessageResult> results) {
|
||||
var errors = new ArrayList<String>();
|
||||
for (var result : results) {
|
||||
var error = getErrorMessageFromSendMessageResult(result);
|
||||
|
|
|
@ -5,8 +5,6 @@ import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.asamk.signal.manager.groups.GroupId;
|
||||
import org.asamk.signal.manager.groups.GroupIdFormatException;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
|
@ -61,10 +59,6 @@ public class Util {
|
|||
return f.toString();
|
||||
}
|
||||
|
||||
public static GroupId decodeGroupId(String groupId) throws GroupIdFormatException {
|
||||
return GroupId.fromBase64(groupId);
|
||||
}
|
||||
|
||||
public static String getLegacyIdentifier(final SignalServiceAddress address) {
|
||||
return address.getNumber().or(() -> address.getUuid().get().toString());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue