diff --git a/js/modules/signal.js b/js/modules/signal.js index 5939c54c4..47450703b 100644 --- a/js/modules/signal.js +++ b/js/modules/signal.js @@ -129,9 +129,6 @@ const { QualifiedAddress } = require('../../ts/types/QualifiedAddress'); // Views const Initialization = require('./views/initialization'); -// Workflow -const MessageDataMigrator = require('./messages_data_migrator'); - // Processes / Services const { initializeGroupCredentialFetcher, @@ -412,10 +409,6 @@ exports.setup = (options = {}) => { Initialization, }; - const Workflow = { - MessageDataMigrator, - }; - return { Backbone, Components, @@ -436,6 +429,5 @@ exports.setup = (options = {}) => { Types, Util, Views, - Workflow, }; }; diff --git a/ts/background.ts b/ts/background.ts index 5879097c3..87bfd698d 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -50,6 +50,7 @@ import { isMoreRecentThan, isOlderThan, toDayMillis } from './util/timestamp'; import { isValidReactionEmoji } from './reactions/isValidReactionEmoji'; import type { ConversationModel } from './models/conversations'; import { getContact } from './messages/helpers'; +import { migrateMessageData } from './messages/migrateMessageData'; import { createBatcher } from './util/batcher'; import { updateConversationsWithUuidLookup } from './updateConversationsWithUuidLookup'; import { initializeAllJobQueues } from './jobs/initializeAllJobQueues'; @@ -493,7 +494,6 @@ export async function startApp(): Promise { // of preload.js processing window.setImmediate = window.nodeSetImmediate; - const { MessageDataMigrator } = window.Signal.Workflow; const { removeDatabase: removeIndexedDB, doesDatabaseExist } = window.Signal.IndexedDB; const { Message } = window.Signal.Types; @@ -827,8 +827,7 @@ export async function startApp(): Promise { const NUM_MESSAGES_PER_BATCH = 1; if (!isMigrationWithIndexComplete) { - const batchWithIndex = await MessageDataMigrator.processNext({ - BackboneMessageCollection: window.Whisper.MessageCollection, + const batchWithIndex = await migrateMessageData({ numMessagesPerBatch: NUM_MESSAGES_PER_BATCH, upgradeMessageSchema, getMessagesNeedingUpgrade: diff --git a/js/modules/messages_data_migrator.js b/ts/messages/migrateMessageData.ts similarity index 60% rename from js/modules/messages_data_migrator.js rename to ts/messages/migrateMessageData.ts index c63336887..51e9c81e4 100644 --- a/js/modules/messages_data_migrator.js +++ b/ts/messages/migrateMessageData.ts @@ -1,29 +1,49 @@ -// Copyright 2018-2020 Signal Messenger, LLC +// Copyright 2018-2022 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only -// Ensures that messages in database are at the right schema. +import { isFunction, isNumber } from 'lodash'; +import * as Message from '../../js/modules/types/message'; +import type { MessageAttributesType } from '../model-types.d'; +import type { UUIDStringType } from '../types/UUID'; -/* global window */ - -const { isFunction, isNumber } = require('lodash'); - -const Message = require('./types/message'); - -exports.processNext = async ({ - BackboneMessageCollection, +/** + * Ensures that messages in database are at the right schema. + */ +export async function migrateMessageData({ numMessagesPerBatch, upgradeMessageSchema, getMessagesNeedingUpgrade, saveMessage, maxVersion = Message.CURRENT_SCHEMA_VERSION, -} = {}) => { - if (!isFunction(BackboneMessageCollection)) { - throw new TypeError( - "'BackboneMessageCollection' (Whisper.MessageCollection)" + - ' constructor is required' - ); - } - +}: Readonly<{ + numMessagesPerBatch: number; + upgradeMessageSchema: ( + message: MessageAttributesType, + options: { maxVersion: number } + ) => Promise; + getMessagesNeedingUpgrade: ( + limit: number, + options: { maxVersion: number } + ) => Promise>; + saveMessage: ( + data: MessageAttributesType, + options: { ourUuid: UUIDStringType } + ) => Promise; + maxVersion?: number; +}>): Promise< + | { + done: true; + numProcessed: 0; + } + | { + done: boolean; + numProcessed: number; + fetchDuration: number; + upgradeDuration: number; + saveDuration: number; + totalDuration: number; + } +> { if (!isNumber(numMessagesPerBatch)) { throw new TypeError("'numMessagesPerBatch' is required"); } @@ -39,10 +59,7 @@ exports.processNext = async ({ try { messagesRequiringSchemaUpgrade = await getMessagesNeedingUpgrade( numMessagesPerBatch, - { - maxVersion, - MessageCollection: BackboneMessageCollection, - } + { maxVersion } ); } catch (error) { window.SignalContext.log.error( @@ -85,4 +102,4 @@ exports.processNext = async ({ saveDuration, totalDuration, }; -}; +} diff --git a/ts/window.d.ts b/ts/window.d.ts index befef2992..7cb3f96a3 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -401,9 +401,6 @@ declare global { WhatsNewLink: typeof WhatsNewLink; }; OS: typeof OS; - Workflow: { - MessageDataMigrator: WhatIsThis; - }; IndexedDB: { removeDatabase: WhatIsThis; doesDatabaseExist: WhatIsThis;