Wire up Message / Attachment migration functions on startup

Makes `migrationContext` obsolete.
This commit is contained in:
Daniel Gasienica 2018-03-26 10:17:45 -04:00
parent 867f73b80a
commit 0c06fff47b
5 changed files with 25 additions and 26 deletions

View file

@ -15,7 +15,7 @@
'use strict';
const { Errors, Message } = window.Signal.Types;
const { context: migrationsContext } = window.Signal.Migrations;
const { upgradeMessageSchema } = window.Signal.Migrations;
// Implicitly used in `indexeddb-backbonejs-adapter`:
// https://github.com/signalapp/Signal-Desktop/blob/4033a9f8137e62ed286170ed5d4941982b1d3a64/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js#L569
@ -574,8 +574,7 @@
return event.confirm();
}
const upgradedMessage =
await Message.upgradeSchema(data.message, migrationsContext);
const upgradedMessage = await upgradeMessageSchema(data.message);
await ConversationController.getOrCreateAndWait(
messageDescriptor.id,
messageDescriptor.type

View file

@ -10,7 +10,7 @@
window.Whisper = window.Whisper || {};
const { Attachment, Message } = window.Signal.Types;
const { context: migrationContext } = window.Signal.Migrations;
const { upgradeMessageSchema, loadAttachmentData } = window.Signal.Migrations;
// TODO: Factor out private and group subclasses of Conversation
@ -618,7 +618,7 @@
now
);
const messageWithSchema = await Message.upgradeSchema({
const messageWithSchema = await upgradeMessageSchema({
type: 'outgoing',
body,
conversationId: this.id,
@ -627,7 +627,7 @@
received_at: now,
expireTimer: this.get('expireTimer'),
recipients: this.getRecipients(),
}, migrationContext);
});
const message = this.messageCollection.add(messageWithSchema);
if (this.isPrivate()) {
message.set({ destination: this.id });
@ -657,9 +657,8 @@
profileKey = storage.get('profileKey');
}
const loadData = Attachment.loadData(migrationContext.readAttachmentData);
const attachmentsWithData =
await Promise.all(messageWithSchema.attachments.map(loadData));
await Promise.all(messageWithSchema.attachments.map(loadAttachmentData));
message.send(sendFunction(
this.get('id'),
body,

View file

@ -5,7 +5,7 @@
window.Whisper = window.Whisper || {};
const { Attachment, Message: TypedMessage } = window.Signal.Types;
const { context: migrationContext } = window.Signal.Migrations;
const { deleteAttachmentData } = window.Signal.Migrations;
var Message = window.Whisper.Message = Backbone.Model.extend({
database : Whisper.Database,
@ -147,9 +147,7 @@
async onDestroy() {
this.revokeImageUrl();
const attachments = this.get('attachments');
const deleteData =
Attachment.deleteData(migrationContext.deleteAttachmentData);
await Promise.all(attachments.map(deleteData));
await Promise.all(attachments.map(deleteAttachmentData));
},
/* jshint ignore:end */
/* eslint-disable */

View file

@ -7,7 +7,7 @@
window.Whisper = window.Whisper || {};
const { Attachment } = window.Signal.Types;
const { context: migrationContext } = window.Signal.Migrations;
const { loadAttachmentData } = window.Signal.Migrations;
var URL_REGEX = /(^|[\s\n]|<br\/?>)((?:https?|ftp):\/\/[\-A-Z0-9\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFD+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])/gi;
@ -416,11 +416,10 @@
return this.loadedAttachmentViews;
}
const loadData = Attachment.loadData(migrationContext.readAttachmentData);
const attachments = this.model.get('attachments') || [];
const loadedAttachmentViews = Promise.all(attachments.map(attachment =>
new Promise(async (resolve) => {
const attachmentWithData = await loadData(attachment);
const attachmentWithData = await loadAttachmentData(attachment);
const view = new Whisper.AttachmentView({
model: attachmentWithData,
timestamp: this.model.get('sent_at'),

View file

@ -4,7 +4,9 @@
console.log('preload');
const electron = require('electron');
const Attachment = require('./js/modules/types/attachment');
const Attachments = require('./app/attachments');
const Message = require('./js/modules/types/message');
const { app } = electron.remote;
@ -113,25 +115,27 @@
const readAttachmentData = Attachments.readData(attachmentsPath);
const writeAttachmentData = Attachments.writeData(attachmentsPath);
// Injected context functions to keep `Message` agnostic from Electron:
const upgradeSchemaContext = {
writeAttachmentData,
};
const upgradeMessageSchema = message =>
Message.upgradeSchema(message, upgradeSchemaContext);
window.Signal = window.Signal || {};
window.Signal.Logs = require('./js/modules/logs');
window.Signal.OS = require('./js/modules/os');
window.Signal.Backup = require('./js/modules/backup');
window.Signal.Crypto = require('./js/modules/crypto');
window.Signal.Migrations = window.Signal.Migrations || {};
// Injected context functions to keep `Message` agnostic from Electron:
window.Signal.Migrations.context = {
deleteAttachmentData,
readAttachmentData,
writeAttachmentData,
};
window.Signal.Migrations = {};
window.Signal.Migrations.loadAttachmentData = Attachment.loadData(readAttachmentData);
window.Signal.Migrations.deleteAttachmentData = Attachment.deleteData(deleteAttachmentData);
window.Signal.Migrations.upgradeMessageSchema = upgradeMessageSchema;
window.Signal.Migrations.V17 = require('./js/modules/migrations/17');
window.Signal.Types = window.Signal.Types || {};
window.Signal.Types.Attachment = require('./js/modules/types/attachment');
window.Signal.Types.Attachment = Attachment;
window.Signal.Types.Errors = require('./js/modules/types/errors');
window.Signal.Types.Message = require('./js/modules/types/message');
window.Signal.Types.Message = Message;
window.Signal.Types.MIME = require('./js/modules/types/mime');
window.Signal.Types.Settings = require('./js/modules/types/settings');