diff --git a/ts/backbone/Conversation.ts b/ts/backbone/Conversation.ts index 20dc2acfe8..09209410ac 100644 --- a/ts/backbone/Conversation.ts +++ b/ts/backbone/Conversation.ts @@ -5,14 +5,49 @@ import is from '@sindresorhus/is'; import { Collection as BackboneCollection } from '../types/backbone/Collection'; import { deferredToPromise } from '../../js/modules/deferred_to_promise'; +import { IndexableBoolean } from '../types/IndexedDB'; import { Message } from '../types/Message'; +const DEFAULT_FETCH_COUNT = 50; + export const fetchVisualMediaAttachments = async ({ conversationId, WhisperMessageCollection, }: { conversationId: string; WhisperMessageCollection: BackboneCollection; +}): Promise> => + fetchFromAttachmentsIndex({ + name: 'hasVisualMediaAttachments', + conversationId, + WhisperMessageCollection, + count: DEFAULT_FETCH_COUNT, + }); + +export const fetchFileAttachments = async ({ + conversationId, + WhisperMessageCollection, +}: { + conversationId: string; + WhisperMessageCollection: BackboneCollection; +}): Promise> => + fetchFromAttachmentsIndex({ + name: 'hasFileAttachments', + conversationId, + WhisperMessageCollection, + count: DEFAULT_FETCH_COUNT, + }); + +const fetchFromAttachmentsIndex = async ({ + name, + conversationId, + WhisperMessageCollection, + count, +}: { + name: 'hasVisualMediaAttachments' | 'hasFileAttachments'; + conversationId: string; + WhisperMessageCollection: BackboneCollection; + count: number; }): Promise> => { if (!is.string(conversationId)) { throw new TypeError("'conversationId' is required"); @@ -25,16 +60,16 @@ export const fetchVisualMediaAttachments = async ({ const collection = new WhisperMessageCollection(); const lowerReceivedAt = 0; const upperReceivedAt = Number.MAX_VALUE; - const hasVisualMediaAttachments = 1; + const condition: IndexableBoolean = 1; await deferredToPromise( collection.fetch({ index: { - name: 'hasVisualMediaAttachments', - lower: [conversationId, lowerReceivedAt, hasVisualMediaAttachments], - upper: [conversationId, upperReceivedAt, hasVisualMediaAttachments], + name, + lower: [conversationId, lowerReceivedAt, condition], + upper: [conversationId, upperReceivedAt, condition], order: 'desc', }, - limit: 50, + limit: count, }) );