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

@ -65,6 +65,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.SignalSessionLock;
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
import org.whispersystems.signalservice.api.messages.SignalServicePreview;
import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
import org.whispersystems.signalservice.api.messages.SignalServiceTypingMessage;
import org.whispersystems.signalservice.api.push.ACI;
@ -551,9 +552,8 @@ class ManagerImpl implements Manager {
final SignalServiceDataMessage.Builder messageBuilder, final Message message
) throws AttachmentInvalidException, IOException, UnregisteredRecipientException, InvalidStickerException {
messageBuilder.withBody(message.messageText());
final var attachments = message.attachments();
if (attachments != null) {
messageBuilder.withAttachments(context.getAttachmentHelper().uploadAttachments(attachments));
if (message.attachments().size() > 0) {
messageBuilder.withAttachments(context.getAttachmentHelper().uploadAttachments(message.attachments()));
}
if (message.mentions().size() > 0) {
messageBuilder.withMentions(resolveMentions(message.mentions()));
@ -592,6 +592,19 @@ class ManagerImpl implements Manager {
manifestSticker.emoji(),
AttachmentUtils.createAttachmentStream(streamDetails, Optional.empty())));
}
if (message.previews().size() > 0) {
final var previews = new ArrayList<SignalServicePreview>(message.previews().size());
for (final var p : message.previews()) {
final var image = p.image().isPresent() ? context.getAttachmentHelper()
.uploadAttachment(p.image().get()) : null;
previews.add(new SignalServicePreview(p.url(),
p.title(),
p.description(),
0,
Optional.ofNullable(image)));
}
messageBuilder.withPreviews(previews);
}
}
private ArrayList<SignalServiceDataMessage.Mention> resolveMentions(final List<Message.Mention> mentionList) throws UnregisteredRecipientException {

View file

@ -8,7 +8,8 @@ public record Message(
List<String> attachments,
List<Mention> mentions,
Optional<Quote> quote,
Optional<Sticker> sticker
Optional<Sticker> sticker,
List<Preview> previews
) {
public record Mention(RecipientIdentifier.Single recipient, int start, int length) {}
@ -16,4 +17,6 @@ public record Message(
public record Quote(long timestamp, RecipientIdentifier.Single author, String message, List<Mention> mentions) {}
public record Sticker(byte[] packId, int stickerId) {}
public record Preview(String url, String title, String description, Optional<String> image) {}
}