From e0428355be0262b04adde96ea510db8b2bf2af1c Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Wed, 14 Mar 2018 20:44:45 -0400 Subject: [PATCH] Wire up `writeAttachment` --- app/types/attachment/write_attachment_data.js | 5 +++++ js/background.js | 4 +++- js/models/conversations.js | 3 ++- main.js | 7 ++++++- preload.js | 11 +++++++++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/types/attachment/write_attachment_data.js b/app/types/attachment/write_attachment_data.js index 308888e3c..947ea6785 100644 --- a/app/types/attachment/write_attachment_data.js +++ b/app/types/attachment/write_attachment_data.js @@ -5,6 +5,9 @@ const isString = require('lodash/isString'); const Path = require('path'); +// _writeAttachmentData :: AttachmentsPath -> +// ArrayBuffer -> +// IO (Promise Path) exports.writeAttachmentData = (root) => { if (!isString(root)) { throw new TypeError('`root` must be a path'); @@ -23,11 +26,13 @@ exports.writeAttachmentData = (root) => { }; }; +// _getAttachmentName :: Unit -> IO String exports._getAttachmentName = () => { const buffer = crypto.randomBytes(32); return buffer.toString('hex'); }; +// _getAttachmentPath :: Unit -> IO Path exports._getAttachmentPath = () => { const name = exports._getAttachmentName(); const prefix = name.slice(0, 3); diff --git a/js/background.js b/js/background.js index 734bc9284..1561e70f8 100644 --- a/js/background.js +++ b/js/background.js @@ -15,6 +15,7 @@ 'use strict'; const { Errors, Message } = window.Signal.Types; + const { context: migrationsContext } = 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 @@ -573,7 +574,8 @@ return event.confirm(); } - const upgradedMessage = await Message.upgradeSchema(data.message); + const upgradedMessage = + await Message.upgradeSchema(data.message, migrationsContext); await ConversationController.getOrCreateAndWait( messageDescriptor.id, messageDescriptor.type diff --git a/js/models/conversations.js b/js/models/conversations.js index d24882a77..bb7239ffe 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -10,6 +10,7 @@ window.Whisper = window.Whisper || {}; const { Attachment, Message } = window.Signal.Types; + const { context: migrationContext } = window.Signal.Migrations; // TODO: Factor out private and group subclasses of Conversation @@ -626,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 }); diff --git a/main.js b/main.js index 874355082..9f2d87a3b 100644 --- a/main.js +++ b/main.js @@ -16,6 +16,7 @@ const { const packageJson = require('./package.json'); +const Attachments = require('./app/attachments'); const autoUpdate = require('./app/auto_update'); const createTrayIcon = require('./app/tray_icon'); const GlobalErrors = require('./js/modules/global_errors'); @@ -417,7 +418,7 @@ app.on('ready', () => { let loggingSetupError; logging.initialize().catch((error) => { loggingSetupError = error; - }).then(() => { + }).then(async () => { /* eslint-enable more/no-then */ logger = logging.getLogger(); logger.info('app ready'); @@ -431,6 +432,10 @@ app.on('ready', () => { locale = loadLocale({ appLocale, logger }); } + console.log('Ensure attachments directory exists'); + const userDataPath = app.getPath('userData'); + await Attachments.ensureDirectory(userDataPath); + ready = true; autoUpdate.initialize(getMainWindow, locale.messages); diff --git a/preload.js b/preload.js index cd701ad38..f99d51b54 100644 --- a/preload.js +++ b/preload.js @@ -4,6 +4,11 @@ console.log('preload'); const electron = require('electron'); + const Attachments = require('./app/attachments'); + + const { app } = electron.remote; + + window.PROTO_ROOT = 'protos'; window.config = require('url').parse(window.location.toString(), true).query; window.wrapDeferred = function(deferred) { @@ -110,6 +115,12 @@ window.Signal.Crypto = require('./js/modules/crypto'); window.Signal.Migrations = window.Signal.Migrations || {}; + const attachmentsPath = Attachments.getPath(app.getPath('userData')); + const { writeAttachmentData } = require('./app/types/attachment/write_attachment_data'); + // Injected context functions to keep `Message` agnostic from Electron: + window.Signal.Migrations.context = { + writeAttachmentData: writeAttachmentData(attachmentsPath), + }; window.Signal.Migrations.V17 = require('./js/modules/migrations/17'); window.Signal.Types = window.Signal.Types || {};