Implement receive handling for story messages

This commit is contained in:
AsamK 2022-08-08 18:12:30 +02:00
parent 81e36d4f9b
commit a593051512
12 changed files with 443 additions and 47 deletions

View file

@ -20,13 +20,17 @@ record JsonDataMessage(
@JsonInclude(JsonInclude.Include.NON_NULL) JsonSticker sticker,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonRemoteDelete remoteDelete,
@JsonInclude(JsonInclude.Include.NON_NULL) List<JsonSharedContact> contacts,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonGroupInfo groupInfo
@JsonInclude(JsonInclude.Include.NON_NULL) JsonGroupInfo groupInfo,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonStoryContext storyContext
) {
static JsonDataMessage from(MessageEnvelope.Data dataMessage) {
final var timestamp = dataMessage.timestamp();
final var groupInfo = dataMessage.groupContext().isPresent() ? JsonGroupInfo.from(dataMessage.groupContext()
.get()) : null;
final var storyContext = dataMessage.storyContext().isPresent()
? JsonStoryContext.from(dataMessage.storyContext().get())
: null;
final var message = dataMessage.body().orElse(null);
final var expiresInSeconds = dataMessage.expiresInSeconds();
final var viewOnce = dataMessage.isViewOnce();
@ -67,6 +71,7 @@ record JsonDataMessage(
sticker,
remoteDelete,
contacts,
groupInfo);
groupInfo,
storyContext);
}
}

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) JsonStoryMessage storyMessage,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonSyncMessage syncMessage,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonCallMessage callMessage,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonReceiptMessage receiptMessage,
@ -60,6 +61,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 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);
@ -70,6 +72,7 @@ public record JsonMessageEnvelope(
sourceDevice,
timestamp,
dataMessage,
storyMessage,
syncMessage,
callMessage,
receiptMessage,

View file

@ -0,0 +1,16 @@
package org.asamk.signal.json;
import org.asamk.signal.manager.api.MessageEnvelope;
import java.util.UUID;
record JsonStoryContext(
String authorNumber, String authorUuid, long sentTimestamp
) {
static JsonStoryContext from(MessageEnvelope.Data.StoryContext storyContext) {
return new JsonStoryContext(storyContext.author().number().orElse(null),
storyContext.author().uuid().map(UUID::toString).orElse(null),
storyContext.sentTimestamp());
}
}

View file

@ -0,0 +1,52 @@
package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.asamk.signal.manager.api.Color;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.groups.GroupId;
record JsonStoryMessage(
boolean allowsReplies,
@JsonInclude(JsonInclude.Include.NON_NULL) String groupId,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonAttachment fileAttachment,
@JsonInclude(JsonInclude.Include.NON_NULL) TextAttachment textAttachment
) {
static JsonStoryMessage from(MessageEnvelope.Story storyMessage) {
return new JsonStoryMessage(storyMessage.allowsReplies(),
storyMessage.groupId().map(GroupId::toBase64).orElse(null),
storyMessage.fileAttachment().map(JsonAttachment::from).orElse(null),
storyMessage.textAttachment().map(TextAttachment::from).orElse(null));
}
public record TextAttachment(
String text,
@JsonInclude(JsonInclude.Include.NON_NULL) String style,
@JsonInclude(JsonInclude.Include.NON_NULL) String textForegroundColor,
@JsonInclude(JsonInclude.Include.NON_NULL) String textBackgroundColor,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonPreview preview,
@JsonInclude(JsonInclude.Include.NON_NULL) Gradient backgroundGradient,
@JsonInclude(JsonInclude.Include.NON_NULL) String backgroundColor
) {
static TextAttachment from(MessageEnvelope.Story.TextAttachment textAttachment) {
return new TextAttachment(textAttachment.text().orElse(null),
textAttachment.style().map(MessageEnvelope.Story.TextAttachment.Style::name).orElse(null),
textAttachment.textForegroundColor().map(Color::toHexColor).orElse(null),
textAttachment.textBackgroundColor().map(Color::toHexColor).orElse(null),
textAttachment.preview().map(JsonPreview::from).orElse(null),
textAttachment.backgroundGradient().map(Gradient::from).orElse(null),
textAttachment.backgroundColor().map(Color::toHexColor).orElse(null));
}
public record Gradient(String startColor, String endColor, Integer angle) {
static Gradient from(MessageEnvelope.Story.TextAttachment.Gradient gradient) {
return new Gradient(gradient.startColor().map(Color::toHexColor).orElse(null),
gradient.endColor().map(Color::toHexColor).orElse(null),
gradient.angle().orElse(null));
}
}
}
}

View file

@ -16,30 +16,20 @@ enum JsonSyncMessageType {
record JsonSyncMessage(
@JsonInclude(JsonInclude.Include.NON_NULL) JsonSyncDataMessage sentMessage,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonSyncStoryMessage sentStoryMessage,
@JsonInclude(JsonInclude.Include.NON_NULL) List<String> blockedNumbers,
@JsonInclude(JsonInclude.Include.NON_NULL) List<String> blockedGroupIds,
@JsonInclude(JsonInclude.Include.NON_NULL) List<JsonSyncReadMessage> readMessages,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonSyncMessageType type
) {
JsonSyncMessage(
final JsonSyncDataMessage sentMessage,
final List<String> blockedNumbers,
final List<String> blockedGroupIds,
final List<JsonSyncReadMessage> readMessages,
final JsonSyncMessageType type
) {
this.sentMessage = sentMessage;
this.blockedNumbers = blockedNumbers;
this.blockedGroupIds = blockedGroupIds;
this.readMessages = readMessages;
this.type = type;
}
static JsonSyncMessage from(MessageEnvelope.Sync syncMessage) {
final var sentMessage = syncMessage.sent().isPresent()
final var sentMessage = syncMessage.sent().isPresent() && syncMessage.sent().get().story().isEmpty()
? JsonSyncDataMessage.from(syncMessage.sent().get())
: null;
final var sentStoryMessage = syncMessage.sent().isPresent() && syncMessage.sent().get().story().isPresent()
? JsonSyncStoryMessage.from(syncMessage.sent().get())
: null;
final List<String> blockedNumbers;
final List<String> blockedGroupIds;
if (syncMessage.blocked().isPresent()) {
@ -68,6 +58,6 @@ record JsonSyncMessage(
} else {
type = null;
}
return new JsonSyncMessage(sentMessage, blockedNumbers, blockedGroupIds, readMessages, type);
return new JsonSyncMessage(sentMessage, sentStoryMessage, blockedNumbers, blockedGroupIds, readMessages, type);
}
}

View file

@ -0,0 +1,26 @@
package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import org.asamk.signal.manager.api.MessageEnvelope;
import java.util.UUID;
record JsonSyncStoryMessage(
String destinationNumber, String destinationUuid, @JsonUnwrapped JsonStoryMessage dataMessage
) {
static JsonSyncStoryMessage from(MessageEnvelope.Sync.Sent transcriptMessage) {
if (transcriptMessage.destination().isPresent()) {
final var address = transcriptMessage.destination().get();
return new JsonSyncStoryMessage(address.number().orElse(null),
address.uuid().map(UUID::toString).orElse(null),
transcriptMessage.story().map(JsonStoryMessage::from).orElse(null));
} else {
return new JsonSyncStoryMessage(null,
null,
transcriptMessage.story().map(JsonStoryMessage::from).orElse(null));
}
}
}