Implement editing of previous messages

This commit is contained in:
AsamK 2023-05-11 19:10:29 +02:00
parent 72390e595d
commit 8a31b7f2c1
13 changed files with 179 additions and 42 deletions

View file

@ -69,6 +69,10 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
var message = envelope.data().get();
printDataMessage(writer, message);
}
if (envelope.edit().isPresent()) {
var message = envelope.edit().get();
printEditMessage(writer, message);
}
if (envelope.story().isPresent()) {
var message = envelope.story().get();
printStoryMessage(writer.indentedWriter(), message);
@ -192,6 +196,13 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
}
}
private void printEditMessage(
PlainTextWriter writer, MessageEnvelope.Edit message
) {
writer.println("Edit: Target message timestamp: {}", DateUtils.formatTimestamp(message.targetSentTimestamp()));
printDataMessage(writer.indentedWriter(), message.dataMessage());
}
private void printStoryMessage(
PlainTextWriter writer, MessageEnvelope.Story message
) {

View file

@ -84,6 +84,9 @@ public class SendCommand implements JsonRpcLocalCommand {
.type(long.class)
.help("Specify the timestamp of a story to reply to.");
subparser.addArgument("--story-author").help("Specify the number of the author of the story.");
subparser.addArgument("--edit-timestamp")
.type(long.class)
.help("Specify the timestamp of a previous message with the recipient or group to send an edited message.");
}
@Override
@ -189,6 +192,8 @@ public class SendCommand implements JsonRpcLocalCommand {
"Sending empty message is not allowed, either a message, attachment or sticker must be given.");
}
final var editTimestamp = ns.getLong("edit-timestamp");
try {
final var message = new Message(messageText,
attachments,
@ -197,7 +202,9 @@ public class SendCommand implements JsonRpcLocalCommand {
Optional.ofNullable(sticker),
previews,
Optional.ofNullable((storyReply)));
var results = m.sendMessage(message, recipientIdentifiers);
var results = editTimestamp != null
? m.sendEditMessage(message, recipientIdentifiers, editTimestamp)
: m.sendMessage(message, recipientIdentifiers);
outputResult(outputWriter, results);
} catch (AttachmentInvalidException | IOException e) {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()

View file

@ -10,6 +10,7 @@ import org.asamk.signal.manager.api.Group;
import org.asamk.signal.manager.api.Identity;
import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.InvalidUsernameException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.MessageEnvelope;
@ -25,6 +26,7 @@ import org.asamk.signal.manager.api.StickerPack;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.api.UpdateGroup;
import org.asamk.signal.manager.api.UpdateProfile;
import org.asamk.signal.manager.api.UserStatus;
@ -369,6 +371,13 @@ public class DbusManagerImpl implements Manager {
groupId -> signal.sendGroupMessage(message.messageText(), message.attachments(), groupId));
}
@Override
public SendMessageResults sendEditMessage(
final Message message, final Set<RecipientIdentifier> recipients, final long editTargetTimestamp
) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException, InvalidStickerException {
throw new UnsupportedOperationException();
}
@Override
public SendMessageResults sendRemoteDeleteMessage(
final long targetSentTimestamp, final Set<RecipientIdentifier> recipients
@ -801,6 +810,7 @@ public class DbusManagerImpl implements Manager {
List.of())),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());
notifyMessageHandlers(envelope);
};
@ -827,6 +837,7 @@ public class DbusManagerImpl implements Manager {
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());
notifyMessageHandlers(envelope);
};
@ -844,6 +855,7 @@ public class DbusManagerImpl implements Manager {
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.of(new MessageEnvelope.Sync(Optional.of(new MessageEnvelope.Sync.Sent(syncReceived.getTimestamp(),
syncReceived.getTimestamp(),
syncReceived.getDestination().isEmpty()
@ -874,6 +886,7 @@ public class DbusManagerImpl implements Manager {
List.of(),
List.of(),
List.of())),
Optional.empty(),
Optional.empty())),
Optional.empty(),
List.of(),

View file

@ -0,0 +1,10 @@
package org.asamk.signal.json;
import org.asamk.signal.manager.api.MessageEnvelope;
record JsonEditMessage(long targetSentTimestamp, JsonDataMessage dataMessage) {
static JsonEditMessage from(MessageEnvelope.Edit editMessage) {
return new JsonEditMessage(editMessage.targetSentTimestamp(), JsonDataMessage.from(editMessage.dataMessage()));
}
}

View file

@ -18,6 +18,7 @@ public record JsonMessageEnvelope(
Integer sourceDevice,
long timestamp,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonDataMessage dataMessage,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonEditMessage editMessage,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonStoryMessage storyMessage,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonSyncMessage syncMessage,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonCallMessage callMessage,
@ -61,6 +62,7 @@ public record JsonMessageEnvelope(
final var typingMessage = envelope.typing().map(JsonTypingMessage::from).orElse(null);
final var dataMessage = envelope.data().map(JsonDataMessage::from).orElse(null);
final var editMessage = envelope.edit().map(JsonEditMessage::from).orElse(null);
final var storyMessage = envelope.story().map(JsonStoryMessage::from).orElse(null);
final var syncMessage = envelope.sync().map(JsonSyncMessage::from).orElse(null);
final var callMessage = envelope.call().map(JsonCallMessage::from).orElse(null);
@ -72,6 +74,7 @@ public record JsonMessageEnvelope(
sourceDevice,
timestamp,
dataMessage,
editMessage,
storyMessage,
syncMessage,
callMessage,

View file

@ -1,8 +1,10 @@
package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.RecipientAddress;
import java.util.UUID;
@ -10,22 +12,17 @@ record JsonSyncDataMessage(
@Deprecated String destination,
String destinationNumber,
String destinationUuid,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonEditMessage editMessage,
@JsonUnwrapped JsonDataMessage dataMessage
) {
static JsonSyncDataMessage from(MessageEnvelope.Sync.Sent transcriptMessage) {
if (transcriptMessage.destination().isPresent()) {
final var address = transcriptMessage.destination().get();
return new JsonSyncDataMessage(address.getLegacyIdentifier(),
address.number().orElse(null),
address.uuid().map(UUID::toString).orElse(null),
transcriptMessage.message().map(JsonDataMessage::from).orElse(null));
} else {
return new JsonSyncDataMessage(null,
null,
null,
transcriptMessage.message().map(JsonDataMessage::from).orElse(null));
}
return new JsonSyncDataMessage(transcriptMessage.destination()
.map(RecipientAddress::getLegacyIdentifier)
.orElse(null),
transcriptMessage.destination().flatMap(RecipientAddress::number).orElse(null),
transcriptMessage.destination().flatMap(address -> address.uuid().map(UUID::toString)).orElse(null),
transcriptMessage.editMessage().map(JsonEditMessage::from).orElse(null),
transcriptMessage.message().map(JsonDataMessage::from).orElse(null));
}
}