diff --git a/js/modules/migrations/18/index.js b/js/modules/migrations/18/index.js index c717474618..9ffeffb216 100644 --- a/js/modules/migrations/18/index.js +++ b/js/modules/migrations/18/index.js @@ -1,15 +1,18 @@ exports.run = (transaction) => { const messagesStore = transaction.objectStore('messages'); - [ + console.log("Create message attachment metadata index: 'hasAttachments'"); + messagesStore.createIndex( 'hasAttachments', - 'hasVisualMediaAttachments', - 'hasFileAttachments', - ].forEach((name) => { + ['conversationId', 'hasAttachments', 'received_at'], + { unique: false } + ); + + ['hasVisualMediaAttachments', 'hasFileAttachments'].forEach((name) => { console.log(`Create message attachment metadata index: '${name}'`); messagesStore.createIndex( name, - ['conversationId', name, 'received_at'], + ['conversationId', 'received_at', name], { unique: false } ); }); diff --git a/js/modules/types/message.js b/js/modules/types/message.js index 0f4d331f9d..166252c145 100644 --- a/js/modules/types/message.js +++ b/js/modules/types/message.js @@ -25,9 +25,8 @@ const PRIVATE = 'private'; // Version 5 // - Attachments: Track number and kind of attachments for media gallery // - `hasAttachments?: 1 | 0` -// - `hasVisualMediaAttachments?: 1 | 0` (for media gallery ‘Media’ view) -// - `hasFileAttachments?: 1 | 0` (for media gallery ‘Documents’ view) - +// - `hasVisualMediaAttachments?: 1 | undefined` (for media gallery ‘Media’ view) +// - `hasFileAttachments?: 1 | undefined` (for media gallery ‘Documents’ view) const INITIAL_SCHEMA_VERSION = 0; diff --git a/test/backup_test.js b/test/backup_test.js index 1169a12748..96c8a7ef22 100644 --- a/test/backup_test.js +++ b/test/backup_test.js @@ -350,7 +350,7 @@ describe('Backup', () => { ]).buffer, }], hasAttachments: 1, - hasFileAttachments: 0, + hasFileAttachments: undefined, hasVisualMediaAttachments: 1, quote: { text: "Isn't it cute?", diff --git a/test/modules/types/message_test.js b/test/modules/types/message_test.js index 6af2758c3a..15f285813f 100644 --- a/test/modules/types/message_test.js +++ b/test/modules/types/message_test.js @@ -182,7 +182,7 @@ describe('Message', () => { size: 1111, }], hasAttachments: 1, - hasVisualMediaAttachments: 0, + hasVisualMediaAttachments: undefined, hasFileAttachments: 1, schemaVersion: Message.CURRENT_SCHEMA_VERSION, }; diff --git a/ts/backbone/Conversation.ts b/ts/backbone/Conversation.ts index 84348bde9d..20dc2acfe8 100644 --- a/ts/backbone/Conversation.ts +++ b/ts/backbone/Conversation.ts @@ -30,8 +30,8 @@ export const fetchVisualMediaAttachments = async ({ collection.fetch({ index: { name: 'hasVisualMediaAttachments', - lower: [conversationId, hasVisualMediaAttachments, lowerReceivedAt], - upper: [conversationId, hasVisualMediaAttachments, upperReceivedAt], + lower: [conversationId, lowerReceivedAt, hasVisualMediaAttachments], + upper: [conversationId, upperReceivedAt, hasVisualMediaAttachments], order: 'desc', }, limit: 50, diff --git a/ts/test/types/message/initializeAttachmentMetadata_test.ts b/ts/test/types/message/initializeAttachmentMetadata_test.ts index 10c0d06758..2979f13f9f 100644 --- a/ts/test/types/message/initializeAttachmentMetadata_test.ts +++ b/ts/test/types/message/initializeAttachmentMetadata_test.ts @@ -40,7 +40,7 @@ describe('Message', () => { }], hasAttachments: 1, hasVisualMediaAttachments: 1, - hasFileAttachments: 0, + hasFileAttachments: undefined, }; const actual = await Message.initializeAttachmentMetadata(input); diff --git a/ts/types/IndexedDB.ts b/ts/types/IndexedDB.ts index 150ad64316..fa89f986ae 100644 --- a/ts/types/IndexedDB.ts +++ b/ts/types/IndexedDB.ts @@ -3,15 +3,21 @@ */ // IndexedDB doesn’t support boolean indexes so we map `true` to 1 and `false` -// to `0`. +// to `0`, i.e. `IndexableBoolean`. // N.B. Using `undefined` allows excluding an entry from an index. Useful -// when index size is a consideration or one only needs to query for `true`. +// when index size is a consideration or one only needs to query for `true`, +// i.e. `IndexablePresence`. export type IndexableBoolean = IndexableFalse | IndexableTrue; -type IndexableTrue = 1; +export type IndexablePresence = undefined | IndexableTrue; + type IndexableFalse = 0; +type IndexableTrue = 1; export const INDEXABLE_FALSE: IndexableFalse = 0; export const INDEXABLE_TRUE: IndexableTrue = 1; export const toIndexableBoolean = (value: boolean): IndexableBoolean => value ? INDEXABLE_TRUE : INDEXABLE_FALSE; + +export const toIndexablePresence = (value: boolean): IndexablePresence => + value ? INDEXABLE_TRUE : undefined; diff --git a/ts/types/Message.ts b/ts/types/Message.ts index cf60f5530e..441bd3b13b 100644 --- a/ts/types/Message.ts +++ b/ts/types/Message.ts @@ -2,7 +2,7 @@ * @prettier */ import { Attachment } from './Attachment'; -import { IndexableBoolean } from './IndexedDB'; +import { IndexableBoolean, IndexablePresence } from './IndexedDB'; export type Message = UserMessage | VerifiedChangeMessage; export type UserMessage = IncomingMessage | OutgoingMessage; @@ -23,7 +23,7 @@ export type IncomingMessage = Readonly< source?: string; sourceDevice?: number; } & SharedMessageProperties & - MessageSchemaVersion4 & + MessageSchemaVersion5 & ExpirationTimerUpdate >; @@ -49,7 +49,7 @@ export type OutgoingMessage = Readonly< recipients?: Array; // Array synced: boolean; } & SharedMessageProperties & - MessageSchemaVersion4 & + MessageSchemaVersion5 & ExpirationTimerUpdate >; @@ -57,7 +57,7 @@ export type VerifiedChangeMessage = Readonly< { type: 'verified-change'; } & SharedMessageProperties & - MessageSchemaVersion4 & + MessageSchemaVersion5 & ExpirationTimerUpdate >; @@ -77,10 +77,10 @@ type ExpirationTimerUpdate = Partial< }> >; -type MessageSchemaVersion4 = Partial< +type MessageSchemaVersion5 = Partial< Readonly<{ hasAttachments: IndexableBoolean; - hasVisualMediaAttachments: IndexableBoolean; - hasFileAttachments: IndexableBoolean; + hasVisualMediaAttachments: IndexablePresence; + hasFileAttachments: IndexablePresence; }> >; diff --git a/ts/types/message/initializeAttachmentMetadata.ts b/ts/types/message/initializeAttachmentMetadata.ts index 251529695c..55d172ae47 100644 --- a/ts/types/message/initializeAttachmentMetadata.ts +++ b/ts/types/message/initializeAttachmentMetadata.ts @@ -22,7 +22,7 @@ export const initializeAttachmentMetadata = async ( Attachment.isVisualMedia ) .map(attachments => attachments.length > 0) - .map(IndexedDB.toIndexableBoolean); + .map(IndexedDB.toIndexablePresence); return { ...message,