Implement sending link previews

Fixes #276
This commit is contained in:
AsamK 2022-05-25 23:23:33 +02:00
parent 0f701df91f
commit b178c7c67a
8 changed files with 95 additions and 9 deletions

View file

@ -72,6 +72,11 @@ public class SendCommand implements JsonRpcLocalCommand {
.nargs("*")
.help("Quote with mention of another group member (syntax: start:length:recipientNumber)");
subparser.addArgument("--sticker").help("Send a sticker (syntax: stickerPackId:stickerId)");
subparser.addArgument("--preview-url")
.help("Specify the url for the link preview (the same url must also appear in the message body).");
subparser.addArgument("--preview-title").help("Specify the title for the link preview (mandatory).");
subparser.addArgument("--preview-description").help("Specify the description for the link preview (optional).");
subparser.addArgument("--preview-image").help("Specify the image file for the link preview (optional).");
}
@Override
@ -146,12 +151,27 @@ public class SendCommand implements JsonRpcLocalCommand {
quote = null;
}
final List<Message.Preview> previews;
String previewUrl = ns.getString("preview-url");
if (previewUrl != null) {
String previewTitle = ns.getString("preview-title");
String previewDescription = ns.getString("preview-description");
String previewImage = ns.getString("preview-image");
previews = List.of(new Message.Preview(previewUrl,
Optional.ofNullable(previewTitle).orElse(""),
Optional.ofNullable(previewDescription).orElse(""),
Optional.ofNullable(previewImage)));
} else {
previews = List.of();
}
try {
var results = m.sendMessage(new Message(messageText == null ? "" : messageText,
attachments,
mentions,
Optional.ofNullable(quote),
Optional.ofNullable(sticker)), recipientIdentifiers);
Optional.ofNullable(sticker),
previews), recipientIdentifiers);
outputResult(outputWriter, results);
} catch (AttachmentInvalidException | IOException e) {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()

View file

@ -216,7 +216,8 @@ public class DbusSignalImpl implements Signal {
attachments,
List.of(),
Optional.empty(),
Optional.empty()),
Optional.empty(),
List.of()),
getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
.map(RecipientIdentifier.class::cast)
.collect(Collectors.toSet()));
@ -367,7 +368,8 @@ public class DbusSignalImpl implements Signal {
attachments,
List.of(),
Optional.empty(),
Optional.empty()), Set.of(RecipientIdentifier.NoteToSelf.INSTANCE));
Optional.empty(),
List.of()), Set.of(RecipientIdentifier.NoteToSelf.INSTANCE));
checkSendMessageResults(results);
return results.timestamp();
} catch (AttachmentInvalidException e) {
@ -408,7 +410,8 @@ public class DbusSignalImpl implements Signal {
attachments,
List.of(),
Optional.empty(),
Optional.empty()), Set.of(getGroupRecipientIdentifier(groupId)));
Optional.empty(),
List.of()), Set.of(getGroupRecipientIdentifier(groupId)));
checkSendMessageResults(results);
return results.timestamp();
} catch (IOException | InvalidStickerException e) {

View file

@ -15,6 +15,7 @@ record JsonDataMessage(
@JsonInclude(JsonInclude.Include.NON_NULL) JsonQuote quote,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonPayment payment,
@JsonInclude(JsonInclude.Include.NON_NULL) List<JsonMention> mentions,
@JsonInclude(JsonInclude.Include.NON_NULL) List<JsonPreview> previews,
@JsonInclude(JsonInclude.Include.NON_NULL) List<JsonAttachment> attachments,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonSticker sticker,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonRemoteDelete remoteDelete,
@ -36,6 +37,10 @@ record JsonDataMessage(
.stream()
.map(JsonMention::from)
.toList() : null;
final var previews = dataMessage.previews().size() > 0 ? dataMessage.previews()
.stream()
.map(JsonPreview::from)
.toList() : null;
final var remoteDelete = dataMessage.remoteDeleteId().isPresent()
? new JsonRemoteDelete(dataMessage.remoteDeleteId().get())
: null;
@ -57,6 +62,7 @@ record JsonDataMessage(
quote,
payment,
mentions,
previews,
attachments,
sticker,
remoteDelete,

View file

@ -0,0 +1,13 @@
package org.asamk.signal.json;
import org.asamk.signal.manager.api.MessageEnvelope;
public record JsonPreview(String url, String title, String description, JsonAttachment image) {
static JsonPreview from(MessageEnvelope.Data.Preview preview) {
return new JsonPreview(preview.url(),
preview.title(),
preview.description(),
preview.image().map(JsonAttachment::from).orElse(null));
}
}