Implement textStyles for sending and receiving

Fixes #1250
This commit is contained in:
AsamK 2023-05-20 12:47:35 +02:00
parent 145a2f1179
commit 91700ce995
13 changed files with 176 additions and 39 deletions

View file

@ -40,6 +40,7 @@ import org.asamk.signal.manager.api.SendMessageResults;
import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TextStyle;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.api.UpdateGroup;
@ -618,6 +619,9 @@ class ManagerImpl implements Manager {
if (message.mentions().size() > 0) {
messageBuilder.withMentions(resolveMentions(message.mentions()));
}
if (message.textStyles().size() > 0) {
messageBuilder.withBodyRanges(message.textStyles().stream().map(TextStyle::toBodyRange).toList());
}
if (message.quote().isPresent()) {
final var quote = message.quote().get();
messageBuilder.withQuote(new SignalServiceDataMessage.Quote(quote.timestamp(),
@ -628,7 +632,7 @@ class ManagerImpl implements Manager {
List.of(),
resolveMentions(quote.mentions()),
SignalServiceDataMessage.Quote.Type.NORMAL,
List.of()));
quote.textStyles().stream().map(TextStyle::toBodyRange).toList()));
}
if (message.sticker().isPresent()) {
final var sticker = message.sticker().get();

View file

@ -10,12 +10,19 @@ public record Message(
Optional<Quote> quote,
Optional<Sticker> sticker,
List<Preview> previews,
Optional<StoryReply> storyReply
Optional<StoryReply> storyReply,
List<TextStyle> textStyles
) {
public record Mention(RecipientIdentifier.Single recipient, int start, int length) {}
public record Quote(long timestamp, RecipientIdentifier.Single author, String message, List<Mention> mentions) {}
public record Quote(
long timestamp,
RecipientIdentifier.Single author,
String message,
List<Mention> mentions,
List<TextStyle> textStyles
) {}
public record Sticker(byte[] packId, int stickerId) {}

View file

@ -515,32 +515,6 @@ public record MessageEnvelope(
}
}
public record TextStyle(Style style, int start, int length) {
public enum Style {
NONE,
BOLD,
ITALIC,
SPOILER,
STRIKETHROUGH,
MONOSPACE;
static Style from(BodyRange.Style style) {
return switch (style) {
case NONE -> NONE;
case BOLD -> BOLD;
case ITALIC -> ITALIC;
case SPOILER -> SPOILER;
case STRIKETHROUGH -> STRIKETHROUGH;
case MONOSPACE -> MONOSPACE;
};
}
}
static TextStyle from(BodyRange bodyRange) {
return new TextStyle(Style.from(bodyRange.getStyle()), bodyRange.getStart(), bodyRange.getLength());
}
}
}
public record Edit(long targetSentTimestamp, Data dataMessage) {

View file

@ -0,0 +1,61 @@
package org.asamk.signal.manager.api;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
public record TextStyle(Style style, int start, int length) {
public enum Style {
NONE,
BOLD,
ITALIC,
SPOILER,
STRIKETHROUGH,
MONOSPACE;
static Style fromInternal(SignalServiceProtos.BodyRange.Style style) {
return switch (style) {
case NONE -> NONE;
case BOLD -> BOLD;
case ITALIC -> ITALIC;
case SPOILER -> SPOILER;
case STRIKETHROUGH -> STRIKETHROUGH;
case MONOSPACE -> MONOSPACE;
};
}
public static Style from(String style) {
return switch (style) {
case "NONE" -> NONE;
case "BOLD" -> BOLD;
case "ITALIC" -> ITALIC;
case "SPOILER" -> SPOILER;
case "STRIKETHROUGH" -> STRIKETHROUGH;
case "MONOSPACE" -> MONOSPACE;
default -> null;
};
}
SignalServiceProtos.BodyRange.Style toBodyRangeStyle() {
return switch (this) {
case NONE -> SignalServiceProtos.BodyRange.Style.NONE;
case BOLD -> SignalServiceProtos.BodyRange.Style.BOLD;
case ITALIC -> SignalServiceProtos.BodyRange.Style.ITALIC;
case SPOILER -> SignalServiceProtos.BodyRange.Style.SPOILER;
case STRIKETHROUGH -> SignalServiceProtos.BodyRange.Style.STRIKETHROUGH;
case MONOSPACE -> SignalServiceProtos.BodyRange.Style.MONOSPACE;
};
}
}
static TextStyle from(SignalServiceProtos.BodyRange bodyRange) {
return new TextStyle(Style.fromInternal(bodyRange.getStyle()), bodyRange.getStart(), bodyRange.getLength());
}
public SignalServiceProtos.BodyRange toBodyRange() {
return SignalServiceProtos.BodyRange.newBuilder()
.setStart(this.start())
.setLength(this.length())
.setStyle(this.style().toBodyRangeStyle())
.build();
}
}