Implement `Conversation.fetchFileAttachments`

pull/1/head
Daniel Gasienica 7 years ago
parent 3a8582ee16
commit 96c44094e3

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

Loading…
Cancel
Save