From dbb726e4cebc4aef11a368e4aa0e9b56d7661ca8 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 20 Jan 2020 18:05:31 +0100 Subject: [PATCH] Pin images included in the spreadsheets --- www/common/onlyoffice/inner.js | 19 +++++++++++++++++++ www/common/onlyoffice/main.js | 26 ++++++++++++++++++++++++++ www/common/proxy-manager.js | 8 +++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/www/common/onlyoffice/inner.js b/www/common/onlyoffice/inner.js index bbf2ccfb5..00431fddb 100644 --- a/www/common/onlyoffice/inner.js +++ b/www/common/onlyoffice/inner.js @@ -1260,6 +1260,23 @@ define([ return stringify(obj); }; + var pinImages = function () { + if (content.mediasSources) { + var toPin = Object.keys(content.mediasSources || {}).map(function (id) { + var data = content.mediasSources[id] || {}; + var src = data.src; + if (!src) { return; } + // Remove trailing slash + if (src.slice(-1) === '/') { + src = src.slice(0, -1); + } + // Extract the channel id from the source href + return src.slice(src.lastIndexOf('/') + 1); + }).filter(Boolean); + sframeChan.query('EV_OO_PIN_IMAGES', toPin); + } + }; + APP.getContent = function () { return content; }; APP.onLocal = config.onLocal = function () { @@ -1269,6 +1286,7 @@ define([ // Update metadata var content = stringifyInner(); APP.realtime.contentUpdate(content); + pinImages(); }; config.onInit = function (info) { @@ -1422,6 +1440,7 @@ define([ handleNewLocks(oldLocks, content.locks); oldLocks = JSON.parse(JSON.stringify(content.locks)); } + pinImages(); }; config.onAbort = function () { diff --git a/www/common/onlyoffice/main.js b/www/common/onlyoffice/main.js index 6bd6c7df5..71ddfa42d 100644 --- a/www/common/onlyoffice/main.js +++ b/www/common/onlyoffice/main.js @@ -88,6 +88,32 @@ define([ }, cb); }); }); + sframeChan.on('EV_OO_PIN_IMAGES', function (list) { + Cryptpad.getPadAttribute('ooImages', function (err, res) { + if (err) { return; } + if (!res || !Array.isArray(res)) { res = []; } + var toPin = []; + var toUnpin = []; + res.forEach(function (id) { + if (list.indexOf(id) === -1) { + toUnpin.push(id); + } + }); + list.forEach(function (id) { + if (res.indexOf(id) === -1) { + toPin.push(id); + } + }); + toPin = Utils.Util.deduplicateString(toPin); + toUnpin = Utils.Util.deduplicateString(toUnpin); + Cryptpad.pinPads(toPin, function () {}); + Cryptpad.unpinPads(toUnpin, function () {}); + if (!toPin.length && !toUnpin.length) { return; } + Cryptpad.setPadAttribute('ooImages', list, function (err) { + if (err) { console.error(err); } + }); + }); + }); sframeChan.on('Q_OO_COMMAND', function (obj, cb) { if (obj.cmd === 'SEND_MESSAGE') { obj.data.msg = Utils.crypto.encrypt(JSON.stringify(obj.data.msg)); diff --git a/www/common/proxy-manager.js b/www/common/proxy-manager.js index 796c52898..c8c86cd1e 100644 --- a/www/common/proxy-manager.js +++ b/www/common/proxy-manager.js @@ -972,16 +972,22 @@ define([ if (!data) { return; } // Don't pin pads owned by someone else if (_ownedByOther(Env, data.owners)) { return; } - // Don't push duplicates + // Pin onlyoffice checkpoints if (data.lastVersion) { var otherChan = Hash.hrefToHexChannelId(data.lastVersion); if (result.indexOf(otherChan) === -1) { result.push(otherChan); } } + // Pin onlyoffice realtime patches if (data.rtChannel && result.indexOf(data.rtChannel) === -1) { result.push(data.rtChannel); } + // Pin onlyoffice images + if (data.ooImages && Array.isArray(data.ooImages)) { + Array.prototype.push.apply(result, data.ooImages); + } + // Pin the pad if (result.indexOf(data.channel) === -1) { result.push(data.channel); }