Move to centralized message/cache data layer

Also, ensure that conversation.messageCollection has nothing in it
unless it has an associated ConversationView.
This commit is contained in:
Scott Nonnenberg 2018-07-25 15:02:27 -07:00
parent 34231168a7
commit f39a96bc76
21 changed files with 1119 additions and 993 deletions

View file

@ -1,76 +0,0 @@
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';
export const fetchVisualMediaAttachments = async ({
conversationId,
count,
WhisperMessageCollection,
}: {
conversationId: string;
count: number;
WhisperMessageCollection: BackboneCollection<Message>;
}): Promise<Array<Message>> =>
fetchFromAttachmentsIndex({
name: 'hasVisualMediaAttachments',
conversationId,
WhisperMessageCollection,
count,
});
export const fetchFileAttachments = async ({
conversationId,
count,
WhisperMessageCollection,
}: {
conversationId: string;
count: number;
WhisperMessageCollection: BackboneCollection<Message>;
}): Promise<Array<Message>> =>
fetchFromAttachmentsIndex({
name: 'hasFileAttachments',
conversationId,
WhisperMessageCollection,
count,
});
const fetchFromAttachmentsIndex = async ({
name,
conversationId,
WhisperMessageCollection,
count,
}: {
name: 'hasVisualMediaAttachments' | 'hasFileAttachments';
conversationId: string;
WhisperMessageCollection: BackboneCollection<Message>;
count: number;
}): Promise<Array<Message>> => {
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 condition: IndexableBoolean = 1;
await deferredToPromise(
collection.fetch({
index: {
name,
lower: [conversationId, lowerReceivedAt, condition],
upper: [conversationId, upperReceivedAt, condition],
order: 'desc',
},
limit: count,
})
);
return collection.models.map(model => model.toJSON());
};

View file

@ -1,4 +1,3 @@
import * as Conversation from './Conversation';
import * as Views from './views';
export { Conversation, Views };
export { Views };