Wrap ignoreAttachments option in a ReceiveConfig record

This commit is contained in:
AsamK 2022-05-26 17:53:14 +02:00
parent 8828b60288
commit fd92a96e1a
9 changed files with 35 additions and 24 deletions

View file

@ -12,6 +12,7 @@ import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.MessageEnvelope; import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.NotPrimaryDeviceException; import org.asamk.signal.manager.api.NotPrimaryDeviceException;
import org.asamk.signal.manager.api.Pair; import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.ReceiveConfig;
import org.asamk.signal.manager.api.RecipientIdentifier; import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendGroupMessageResults; import org.asamk.signal.manager.api.SendGroupMessageResults;
import org.asamk.signal.manager.api.SendMessageResults; import org.asamk.signal.manager.api.SendMessageResults;
@ -206,7 +207,7 @@ public interface Manager extends Closeable {
*/ */
void receiveMessages(ReceiveMessageHandler handler) throws IOException; void receiveMessages(ReceiveMessageHandler handler) throws IOException;
void setIgnoreAttachments(boolean ignoreAttachments); void setReceiveConfig(ReceiveConfig receiveConfig);
boolean hasCaughtUpWithOldMessages(); boolean hasCaughtUpWithOldMessages();

View file

@ -27,6 +27,7 @@ import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.Message; import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.NotPrimaryDeviceException; import org.asamk.signal.manager.api.NotPrimaryDeviceException;
import org.asamk.signal.manager.api.Pair; import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.ReceiveConfig;
import org.asamk.signal.manager.api.RecipientIdentifier; import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendGroupMessageResults; import org.asamk.signal.manager.api.SendGroupMessageResults;
import org.asamk.signal.manager.api.SendMessageResult; import org.asamk.signal.manager.api.SendMessageResult;
@ -947,8 +948,8 @@ class ManagerImpl implements Manager {
} }
@Override @Override
public void setIgnoreAttachments(final boolean ignoreAttachments) { public void setReceiveConfig(final ReceiveConfig receiveConfig) {
context.getReceiveHelper().setIgnoreAttachments(ignoreAttachments); context.getReceiveHelper().setReceiveConfig(receiveConfig);
} }
@Override @Override

View file

@ -0,0 +1,3 @@
package org.asamk.signal.manager.api;
public record ReceiveConfig(boolean ignoreAttachments) {}

View file

@ -22,6 +22,7 @@ import org.asamk.signal.manager.actions.SendSyncKeysAction;
import org.asamk.signal.manager.actions.UpdateAccountAttributesAction; import org.asamk.signal.manager.actions.UpdateAccountAttributesAction;
import org.asamk.signal.manager.api.MessageEnvelope; import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.Pair; import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.ReceiveConfig;
import org.asamk.signal.manager.api.StickerPackId; import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.manager.api.TrustLevel; import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.api.UntrustedIdentityException; import org.asamk.signal.manager.api.UntrustedIdentityException;
@ -76,7 +77,7 @@ public final class IncomingMessageHandler {
public Pair<List<HandleAction>, Exception> handleRetryEnvelope( public Pair<List<HandleAction>, Exception> handleRetryEnvelope(
final SignalServiceEnvelope envelope, final SignalServiceEnvelope envelope,
final boolean ignoreAttachments, final ReceiveConfig receiveConfig,
final Manager.ReceiveMessageHandler handler final Manager.ReceiveMessageHandler handler
) { ) {
final List<HandleAction> actions = new ArrayList<>(); final List<HandleAction> actions = new ArrayList<>();
@ -100,13 +101,13 @@ public final class IncomingMessageHandler {
account.getIdentityKeyStore().setRetryingDecryption(false); account.getIdentityKeyStore().setRetryingDecryption(false);
} }
} }
actions.addAll(checkAndHandleMessage(envelope, content, ignoreAttachments, handler, null)); actions.addAll(checkAndHandleMessage(envelope, content, receiveConfig, handler, null));
return new Pair<>(actions, null); return new Pair<>(actions, null);
} }
public Pair<List<HandleAction>, Exception> handleEnvelope( public Pair<List<HandleAction>, Exception> handleEnvelope(
final SignalServiceEnvelope envelope, final SignalServiceEnvelope envelope,
final boolean ignoreAttachments, final ReceiveConfig receiveConfig,
final Manager.ReceiveMessageHandler handler final Manager.ReceiveMessageHandler handler
) { ) {
final var actions = new ArrayList<HandleAction>(); final var actions = new ArrayList<HandleAction>();
@ -156,14 +157,14 @@ public final class IncomingMessageHandler {
} }
} }
actions.addAll(checkAndHandleMessage(envelope, content, ignoreAttachments, handler, exception)); actions.addAll(checkAndHandleMessage(envelope, content, receiveConfig, handler, exception));
return new Pair<>(actions, exception); return new Pair<>(actions, exception);
} }
private List<HandleAction> checkAndHandleMessage( private List<HandleAction> checkAndHandleMessage(
final SignalServiceEnvelope envelope, final SignalServiceEnvelope envelope,
final SignalServiceContent content, final SignalServiceContent content,
final boolean ignoreAttachments, final ReceiveConfig receiveConfig,
final Manager.ReceiveMessageHandler handler, final Manager.ReceiveMessageHandler handler,
final Exception exception final Exception exception
) { ) {
@ -190,7 +191,7 @@ public final class IncomingMessageHandler {
} else { } else {
List<HandleAction> actions; List<HandleAction> actions;
if (content != null) { if (content != null) {
actions = handleMessage(envelope, content, ignoreAttachments); actions = handleMessage(envelope, content, receiveConfig);
} else { } else {
actions = List.of(); actions = List.of();
} }
@ -205,7 +206,7 @@ public final class IncomingMessageHandler {
} }
public List<HandleAction> handleMessage( public List<HandleAction> handleMessage(
SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments SignalServiceEnvelope envelope, SignalServiceContent content, ReceiveConfig receiveConfig
) { ) {
var actions = new ArrayList<HandleAction>(); var actions = new ArrayList<HandleAction>();
final var senderPair = getSender(envelope, content); final var senderPair = getSender(envelope, content);
@ -264,12 +265,12 @@ public final class IncomingMessageHandler {
false, false,
sender, sender,
account.getSelfRecipientId(), account.getSelfRecipientId(),
ignoreAttachments)); receiveConfig.ignoreAttachments()));
} }
if (content.getSyncMessage().isPresent()) { if (content.getSyncMessage().isPresent()) {
var syncMessage = content.getSyncMessage().get(); var syncMessage = content.getSyncMessage().get();
actions.addAll(handleSyncMessage(syncMessage, sender, ignoreAttachments)); actions.addAll(handleSyncMessage(syncMessage, sender, receiveConfig.ignoreAttachments()));
} }
return actions; return actions;

View file

@ -3,6 +3,7 @@ package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.SignalDependencies; import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.actions.HandleAction; import org.asamk.signal.manager.actions.HandleAction;
import org.asamk.signal.manager.api.ReceiveConfig;
import org.asamk.signal.manager.api.UntrustedIdentityException; import org.asamk.signal.manager.api.UntrustedIdentityException;
import org.asamk.signal.manager.storage.SignalAccount; import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.messageCache.CachedMessage; import org.asamk.signal.manager.storage.messageCache.CachedMessage;
@ -34,7 +35,7 @@ public class ReceiveHelper {
private final SignalDependencies dependencies; private final SignalDependencies dependencies;
private final Context context; private final Context context;
private boolean ignoreAttachments = false; private ReceiveConfig receiveConfig = new ReceiveConfig(false);
private boolean needsToRetryFailedMessages = false; private boolean needsToRetryFailedMessages = false;
private boolean hasCaughtUpWithOldMessages = false; private boolean hasCaughtUpWithOldMessages = false;
private boolean isWaitingForMessage = false; private boolean isWaitingForMessage = false;
@ -48,8 +49,8 @@ public class ReceiveHelper {
this.context = context; this.context = context;
} }
public void setIgnoreAttachments(final boolean ignoreAttachments) { public void setReceiveConfig(final ReceiveConfig receiveConfig) {
this.ignoreAttachments = ignoreAttachments; this.receiveConfig = receiveConfig;
} }
public void setNeedsToRetryFailedMessages(final boolean needsToRetryFailedMessages) { public void setNeedsToRetryFailedMessages(final boolean needsToRetryFailedMessages) {
@ -192,7 +193,7 @@ public class ReceiveHelper {
continue; continue;
} }
final var result = context.getIncomingMessageHandler().handleEnvelope(envelope, ignoreAttachments, handler); final var result = context.getIncomingMessageHandler().handleEnvelope(envelope, receiveConfig, handler);
for (final var h : result.first()) { for (final var h : result.first()) {
final var existingAction = queuedActions.get(h); final var existingAction = queuedActions.get(h);
if (existingAction == null) { if (existingAction == null) {
@ -247,8 +248,7 @@ public class ReceiveHelper {
return null; return null;
} }
final var result = context.getIncomingMessageHandler() final var result = context.getIncomingMessageHandler().handleRetryEnvelope(envelope, receiveConfig, handler);
.handleRetryEnvelope(envelope, ignoreAttachments, handler);
final var actions = result.first(); final var actions = result.first();
final var exception = result.second(); final var exception = result.second();

View file

@ -17,6 +17,7 @@ import org.asamk.signal.json.JsonReceiveMessageHandler;
import org.asamk.signal.jsonrpc.SignalJsonRpcDispatcherHandler; import org.asamk.signal.jsonrpc.SignalJsonRpcDispatcherHandler;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.MultiAccountManager; import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.manager.api.ReceiveConfig;
import org.asamk.signal.output.JsonWriter; import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.JsonWriterImpl; import org.asamk.signal.output.JsonWriterImpl;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
@ -94,7 +95,7 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
final var receiveMode = ns.<ReceiveMode>get("receive-mode"); final var receiveMode = ns.<ReceiveMode>get("receive-mode");
final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
m.setIgnoreAttachments(ignoreAttachments); m.setReceiveConfig(new ReceiveConfig(ignoreAttachments));
addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START); addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START);
final Channel inheritedChannel; final Channel inheritedChannel;
@ -156,12 +157,13 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
final var receiveMode = ns.<ReceiveMode>get("receive-mode"); final var receiveMode = ns.<ReceiveMode>get("receive-mode");
final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
final var receiveConfig = new ReceiveConfig(ignoreAttachments);
c.getManagers().forEach(m -> { c.getManagers().forEach(m -> {
m.setIgnoreAttachments(ignoreAttachments); m.setReceiveConfig(receiveConfig);
addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START); addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START);
}); });
c.addOnManagerAddedHandler(m -> { c.addOnManagerAddedHandler(m -> {
m.setIgnoreAttachments(ignoreAttachments); m.setReceiveConfig(receiveConfig);
addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START); addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START);
}); });

View file

@ -8,6 +8,7 @@ import org.asamk.signal.OutputType;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.jsonrpc.SignalJsonRpcDispatcherHandler; import org.asamk.signal.jsonrpc.SignalJsonRpcDispatcherHandler;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.ReceiveConfig;
import org.asamk.signal.output.JsonWriter; import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.util.IOUtils; import org.asamk.signal.util.IOUtils;
@ -45,7 +46,7 @@ public class JsonRpcDispatcherCommand implements LocalCommand {
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
final boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); final boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
m.setIgnoreAttachments(ignoreAttachments); m.setReceiveConfig(new ReceiveConfig(ignoreAttachments));
final var jsonOutputWriter = (JsonWriter) outputWriter; final var jsonOutputWriter = (JsonWriter) outputWriter;
final Supplier<String> lineSupplier = IOUtils.getLineSupplier(new InputStreamReader(System.in, final Supplier<String> lineSupplier = IOUtils.getLineSupplier(new InputStreamReader(System.in,

View file

@ -10,6 +10,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.json.JsonReceiveMessageHandler; import org.asamk.signal.json.JsonReceiveMessageHandler;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.ReceiveConfig;
import org.asamk.signal.output.JsonWriter; import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter; import org.asamk.signal.output.PlainTextWriter;
@ -52,7 +53,7 @@ public class ReceiveCommand implements LocalCommand {
) throws CommandException { ) throws CommandException {
double timeout = ns.getDouble("timeout"); double timeout = ns.getDouble("timeout");
boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
m.setIgnoreAttachments(ignoreAttachments); m.setReceiveConfig(new ReceiveConfig(ignoreAttachments));
try { try {
final var handler = outputWriter instanceof JsonWriter ? new JsonReceiveMessageHandler(m, final var handler = outputWriter instanceof JsonWriter ? new JsonReceiveMessageHandler(m,
(JsonWriter) outputWriter) : new ReceiveMessageHandler(m, (PlainTextWriter) outputWriter); (JsonWriter) outputWriter) : new ReceiveMessageHandler(m, (PlainTextWriter) outputWriter);

View file

@ -14,6 +14,7 @@ import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.MessageEnvelope; import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.NotPrimaryDeviceException; import org.asamk.signal.manager.api.NotPrimaryDeviceException;
import org.asamk.signal.manager.api.Pair; import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.ReceiveConfig;
import org.asamk.signal.manager.api.RecipientIdentifier; import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendGroupMessageResults; import org.asamk.signal.manager.api.SendGroupMessageResults;
import org.asamk.signal.manager.api.SendMessageResults; import org.asamk.signal.manager.api.SendMessageResults;
@ -531,7 +532,7 @@ public class DbusManagerImpl implements Manager {
} }
@Override @Override
public void setIgnoreAttachments(final boolean ignoreAttachments) { public void setReceiveConfig(final ReceiveConfig receiveConfig) {
} }
@Override @Override