From c5287158c4b8db829c5aa63df7f305273f0c78d0 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 10 Jun 2021 12:27:03 +1000 Subject: [PATCH] merge protobuf attachment and preview types --- protos/SignalService.proto | 10 ++-- .../outgoing/visibleMessage/VisibleMessage.ts | 57 ++++++++++++------- ts/session/utils/Attachments.ts | 20 +++++-- ts/session/utils/AttachmentsV2.ts | 8 ++- ts/session/utils/syncUtils.ts | 6 +- .../session/unit/messages/ChatMessage_test.ts | 12 ++-- 6 files changed, 71 insertions(+), 42 deletions(-) diff --git a/protos/SignalService.proto b/protos/SignalService.proto index b85e6bde3..ac8536809 100644 --- a/protos/SignalService.proto +++ b/protos/SignalService.proto @@ -157,8 +157,10 @@ message ConfigurationMessage { } message Contact { - optional bytes publicKey = 1; - optional string name = 2; + // @required + required bytes publicKey = 1; + // @required + required string name = 2; optional string profilePicture = 3; optional bytes profileKey = 4; } @@ -179,7 +181,7 @@ message ReceiptMessage { } // @required - optional Type type = 1; + required Type type = 1; repeated uint64 timestamp = 2; } @@ -190,7 +192,7 @@ message AttachmentPointer { } // @required - optional fixed64 id = 1; + required fixed64 id = 1; optional string contentType = 2; optional bytes key = 3; optional uint32 size = 4; diff --git a/ts/session/messages/outgoing/visibleMessage/VisibleMessage.ts b/ts/session/messages/outgoing/visibleMessage/VisibleMessage.ts index e90fae4b4..21d3004ce 100644 --- a/ts/session/messages/outgoing/visibleMessage/VisibleMessage.ts +++ b/ts/session/messages/outgoing/visibleMessage/VisibleMessage.ts @@ -8,7 +8,6 @@ import { LokiProfile } from '../../../../types/Message'; import { MessageParams } from '../Message'; interface AttachmentPointerCommon { - id?: number; contentType?: string; key?: Uint8Array; size?: number; @@ -23,10 +22,12 @@ interface AttachmentPointerCommon { export interface AttachmentPointer extends AttachmentPointerCommon { url?: string; + id?: number; } export interface AttachmentPointerWithUrl extends AttachmentPointerCommon { url: string; + id: number; } export interface Preview { @@ -35,39 +36,53 @@ export interface Preview { image?: AttachmentPointer; } -export interface QuotedAttachment { +export interface PreviewWithAttachmentUrl { + url: string; + id: number; + title?: string; + image?: AttachmentPointerWithUrl; +} + +interface QuotedAttachmentCommon { contentType?: string; fileName?: string; +} + +export interface QuotedAttachment extends QuotedAttachmentCommon { thumbnail?: AttachmentPointer; } +export interface QuotedAttachmentWithUrl extends QuotedAttachmentCommon { + thumbnail?: AttachmentPointerWithUrl | QuotedAttachment; +} + export interface Quote { id: number; author: string; text?: string; - attachments?: Array; + attachments?: Array; } export interface VisibleMessageParams extends MessageParams { - attachments?: Array; + attachments?: Array; body?: string; quote?: Quote; expireTimer?: number; lokiProfile?: LokiProfile; - preview?: Array; + preview?: Array; syncTarget?: string; // null means it is not a synced message } export class VisibleMessage extends DataMessage { public readonly expireTimer?: number; - private readonly attachments?: Array; + private readonly attachments?: Array; private readonly body?: string; private readonly quote?: Quote; private readonly profileKey?: Uint8Array; private readonly displayName?: string; private readonly avatarPointer?: string; - private readonly preview?: Array; + private readonly preview?: Array; /// In the case of a sync message, the public key of the person the message was targeted at. /// - Note: `null or undefined` if this isn't a sync message. @@ -141,22 +156,20 @@ export class VisibleMessage extends DataMessage { dataMessage.quote.author = this.quote.author; dataMessage.quote.text = this.quote.text; if (this.quote.attachments) { - dataMessage.quote.attachments = this.quote.attachments.map( - (attachment: QuotedAttachment) => { - const quotedAttachment = new SignalService.DataMessage.Quote.QuotedAttachment(); - if (attachment.contentType) { - quotedAttachment.contentType = attachment.contentType; - } - if (attachment.fileName) { - quotedAttachment.fileName = attachment.fileName; - } - if (attachment.thumbnail) { - quotedAttachment.thumbnail = attachment.thumbnail; - } - - return quotedAttachment; + dataMessage.quote.attachments = this.quote.attachments.map(attachment => { + const quotedAttachment = new SignalService.DataMessage.Quote.QuotedAttachment(); + if (attachment.contentType) { + quotedAttachment.contentType = attachment.contentType; } - ); + if (attachment.fileName) { + quotedAttachment.fileName = attachment.fileName; + } + if (attachment.thumbnail && (attachment.thumbnail as any).id) { + quotedAttachment.thumbnail = attachment.thumbnail as any; // be sure to keep the typescript guard on id above + } + + return quotedAttachment; + }); } } diff --git a/ts/session/utils/Attachments.ts b/ts/session/utils/Attachments.ts index c9751ad0b..225d06536 100644 --- a/ts/session/utils/Attachments.ts +++ b/ts/session/utils/Attachments.ts @@ -5,8 +5,10 @@ import { AttachmentPointer, AttachmentPointerWithUrl, Preview, + PreviewWithAttachmentUrl, Quote, QuotedAttachment, + QuotedAttachmentWithUrl, } from '../messages/outgoing/visibleMessage/VisibleMessage'; import { FSv2 } from '../../fileserver'; import { addAttachmentPadding } from '../crypto/BufferPadding'; @@ -101,7 +103,7 @@ export class AttachmentFsV2Utils { public static async uploadAttachmentsToFsV2( attachments: Array - ): Promise> { + ): Promise> { const promises = (attachments || []).map(async attachment => this.uploadToFsV2({ attachment, @@ -114,7 +116,7 @@ export class AttachmentFsV2Utils { public static async uploadLinkPreviewsToFsV2( previews: Array - ): Promise> { + ): Promise> { const promises = (previews || []).map(async preview => { // some links does not have an image associated, and it makes the whole message fail to send if (!preview.image) { @@ -127,8 +129,9 @@ export class AttachmentFsV2Utils { return { ...preview, image, - url: preview.url || image.url, - } as Preview; + url: image.url, + id: image.id, + }; }); return _.compact(await Promise.all(promises)); } @@ -145,13 +148,18 @@ export class AttachmentFsV2Utils { attachment: attachment.thumbnail, }); } + if (!thumbnail) { + return attachment; + } return { ...attachment, thumbnail, - } as QuotedAttachment; + url: thumbnail.url, + id: thumbnail.id, + } as QuotedAttachmentWithUrl; }); - const attachments = await Promise.all(promises); + const attachments = _.compact(await Promise.all(promises)); return { ...quote, diff --git a/ts/session/utils/AttachmentsV2.ts b/ts/session/utils/AttachmentsV2.ts index abd13c9ce..d1783658c 100644 --- a/ts/session/utils/AttachmentsV2.ts +++ b/ts/session/utils/AttachmentsV2.ts @@ -5,6 +5,7 @@ import { AttachmentPointer, AttachmentPointerWithUrl, Preview, + PreviewWithAttachmentUrl, Quote, QuotedAttachment, } from '../messages/outgoing/visibleMessage/VisibleMessage'; @@ -59,7 +60,7 @@ export async function uploadV2(params: UploadParamsV2): Promise, openGroup: OpenGroupRequestCommonType -): Promise> { +): Promise> { const promises = (attachments || []).map(async attachment => exports.uploadV2({ attachment, @@ -73,7 +74,7 @@ export async function uploadAttachmentsV2( export async function uploadLinkPreviewsV2( previews: Array, openGroup: OpenGroupRequestCommonType -): Promise> { +): Promise> { const promises = (previews || []).map(async preview => { // some links does not have an image associated, and it makes the whole message fail to send if (!preview.image) { @@ -89,6 +90,7 @@ export async function uploadLinkPreviewsV2( ...preview, image, url: preview.url || (image.url as string), + id: image.id as number, }; }); return _.compact(await Promise.all(promises)); @@ -103,7 +105,7 @@ export async function uploadQuoteThumbnailsV2( } const promises = (quote.attachments ?? []).map(async attachment => { - let thumbnail: AttachmentPointer | undefined; + let thumbnail: PreviewWithAttachmentUrl | undefined; if (attachment.thumbnail) { thumbnail = await exports.uploadV2({ attachment: attachment.thumbnail, diff --git a/ts/session/utils/syncUtils.ts b/ts/session/utils/syncUtils.ts index 74c8c7d80..14b6daf93 100644 --- a/ts/session/utils/syncUtils.ts +++ b/ts/session/utils/syncUtils.ts @@ -20,7 +20,9 @@ import { SignalService } from '../../protobuf'; import _ from 'lodash'; import { AttachmentPointer, + AttachmentPointerWithUrl, Preview, + PreviewWithAttachmentUrl, Quote, VisibleMessage, } from '../messages/outgoing/visibleMessage/VisibleMessage'; @@ -237,9 +239,9 @@ const buildSyncVisibleMessage = ( key, digest, }; - }) as Array; + }) as Array; const quote = (dataMessage.quote as Quote) || undefined; - const preview = (dataMessage.preview as Array) || []; + const preview = (dataMessage.preview as Array) || []; const expireTimer = dataMessage.expireTimer; return new VisibleMessage({ diff --git a/ts/test/session/unit/messages/ChatMessage_test.ts b/ts/test/session/unit/messages/ChatMessage_test.ts index bd99449fa..e2b3c4b5e 100644 --- a/ts/test/session/unit/messages/ChatMessage_test.ts +++ b/ts/test/session/unit/messages/ChatMessage_test.ts @@ -6,7 +6,9 @@ import { toNumber } from 'lodash'; import { Constants } from '../../../../session'; import { AttachmentPointer, + AttachmentPointerWithUrl, Preview, + PreviewWithAttachmentUrl, Quote, VisibleMessage, } from '../../../../session/messages/outgoing/visibleMessage/VisibleMessage'; @@ -84,10 +86,10 @@ describe('VisibleMessage', () => { }); it('can create message with a preview', () => { - let preview: Preview; + let preview: PreviewWithAttachmentUrl; - preview = { url: 'url', title: 'title' }; - const previews = new Array(); + preview = { url: 'url', title: 'title', id: 1234 }; + const previews = new Array(); previews.push(preview); const message = new VisibleMessage({ @@ -106,10 +108,10 @@ describe('VisibleMessage', () => { }); it('can create message with an AttachmentPointer', () => { - let attachment: AttachmentPointer; + let attachment: AttachmentPointerWithUrl; attachment = { url: 'url', contentType: 'contentType', id: 1234 }; - const attachments = new Array(); + const attachments = new Array(); attachments.push(attachment); const message = new VisibleMessage({