From 366ccdf97dd912a99a0fd67a7e16c58397e8d16b Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 26 Jun 2020 12:33:47 +1000 Subject: [PATCH] Add more functions --- ts/session/utils/Attachments.ts | 82 ++++++++++++++++++++++++++++++++- ts/session/utils/index.ts | 1 + 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/ts/session/utils/Attachments.ts b/ts/session/utils/Attachments.ts index e9378f294..37b8aca43 100644 --- a/ts/session/utils/Attachments.ts +++ b/ts/session/utils/Attachments.ts @@ -1,7 +1,12 @@ import * as crypto from 'crypto'; import { Attachment } from '../../types/Attachment'; import { OpenGroup } from '../types'; -import { AttachmentPointer } from '../messages/outgoing'; +import { + AttachmentPointer, + Preview, + Quote, + QuotedAttachment, +} from '../messages/outgoing'; import { LokiAppDotNetServerInterface } from '../../../js/modules/loki_app_dot_net_api'; interface UploadParams { @@ -11,8 +16,27 @@ interface UploadParams { isRaw?: boolean; } +interface RawPreview { + url?: string; + title?: string; + image: Attachment; +} + +interface RawQuoteAttachment { + contentType?: string; + fileName?: string; + thumbnail?: Attachment; +} + +interface RawQuote { + id?: number; + author?: string; + text?: string; + attachments?: Array; +} + // tslint:disable-next-line: no-unnecessary-class -export class Attachments { +export class AttachmentUtils { private constructor() {} public static getDefaultServer(): LokiAppDotNetServerInterface { @@ -79,4 +103,58 @@ export class Attachments { return pointer; } + + public static async uploadAttachments( + attachments: Array, + openGroup?: OpenGroup + ): Promise> { + const promises = attachments.map(async attachment => + this.upload({ + attachment, + openGroup, + }) + ); + + return Promise.all(promises); + } + + public static async uploadLinkPreviews( + previews: Array, + openGroup?: OpenGroup + ): Promise> { + const promises = previews.map(async item => ({ + ...item, + image: await this.upload({ + attachment: item.image, + openGroup, + }), + })); + return Promise.all(promises); + } + + public static async uploadQuoteThumbnails( + quote: RawQuote, + openGroup?: OpenGroup + ): Promise { + const promises = (quote.attachments ?? []).map(async attachment => { + let thumbnail: AttachmentPointer | undefined; + if (attachment.thumbnail) { + thumbnail = await this.upload({ + attachment: attachment.thumbnail, + openGroup, + }); + } + return { + ...attachment, + thumbnail, + } as QuotedAttachment; + }); + + const attachments = await Promise.all(promises); + + return { + ...quote, + attachments, + }; + } } diff --git a/ts/session/utils/index.ts b/ts/session/utils/index.ts index dde73e928..4089c585c 100644 --- a/ts/session/utils/index.ts +++ b/ts/session/utils/index.ts @@ -4,6 +4,7 @@ import * as SyncMessageUtils from './SyncMessageUtils'; import * as StringUtils from './String'; import * as PromiseUtils from './Promise'; +export * from './Attachments'; export * from './TypedEmitter'; export * from './JobQueue';