From 825980fbd13b08ee108cb440a31fe2a089a5b3b3 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Fri, 13 Apr 2018 22:16:35 -0400 Subject: [PATCH] Add `Collection.fetchVisualMediaAttachments` --- ts/backbone/Conversation.ts | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ts/backbone/Conversation.ts diff --git a/ts/backbone/Conversation.ts b/ts/backbone/Conversation.ts new file mode 100644 index 000000000..2d652029a --- /dev/null +++ b/ts/backbone/Conversation.ts @@ -0,0 +1,44 @@ +/** + * @prettier + */ +import is from '@sindresorhus/is'; + +import { deferredToPromise } from '../../js/modules/deferred_to_promise'; +import { Collection as BackboneCollection } from '../types/backbone/Collection'; +import { Message } from '../types/Message'; + +export const fetchVisualMediaAttachments = async ({ + conversationId, + WhisperMessageCollection, +}: { + conversationId: string; + WhisperMessageCollection: BackboneCollection; +}): Promise> => { + if (!is.string(conversationId)) { + throw new TypeError("'conversationId' is required"); + } + + if (!is.object(WhisperMessageCollection)) { + throw new TypeError("'WhisperMessageCollection' is required"); + } + + const collection = new WhisperMessageCollection(); + const lowerReceivedAt = 0; + const upperReceivedAt = Number.MAX_VALUE; + const hasVisualMediaAttachments = 1; + await deferredToPromise( + collection.fetch({ + index: { + // 'hasVisualMediaAttachments' index on + // [conversationId, hasVisualMediaAttachments, received_at] + name: 'hasVisualMediaAttachments', + lower: [conversationId, hasVisualMediaAttachments, lowerReceivedAt], + upper: [conversationId, hasVisualMediaAttachments, upperReceivedAt], + order: 'desc', + }, + limit: 10, + }) + ); + + return collection.models.map(model => model.toJSON()); +};