diff --git a/www/common/common-ui-elements.js b/www/common/common-ui-elements.js index e977f1f49..3cb044231 100644 --- a/www/common/common-ui-elements.js +++ b/www/common/common-ui-elements.js @@ -2684,6 +2684,45 @@ define([ }; + Messages.history_trimPrompt = "This document's history is very large ({0}) and it may impact the loading time. You can delete the unnecessary history."; + UIElements.displayTrimHistoryPrompt = function (common, data) { + var mb = Util.bytesToMegabytes(data.size); + var text = Messages._getKey('history_trimPrompt', [ + Messages._getKey('formattedMB', [mb]) + ]); + var yes = h('button.cp-corner-primary', [ + h('span.fa.fa-trash-o'), + Messages.trimHistory_button + ]); + var no = h('button.cp-corner-cancel', Messages.crowdfunding_popup_no); // Not now + var actions = h('div', [no, yes]); + + var dontShowAgain = function () { + var until = (+new Date()) + (7 * 24 * 3600 * 1000); // 7 days from now + until = (+new Date()) + 30000; // XXX 30s from now + if (data.drive) { + common.setAttribute(['drive', 'trim'], until); + return; + } + common.setPadAttribute('trim', until); + }; + + var modal = UI.cornerPopup(text, actions, '', {}); + + $(yes).click(function () { + modal.delete(); + if (data.drive) { + common.openURL('/settings/#drive'); + return; + } + common.getSframeChannel().event('EV_PROPERTIES_OPEN'); + }); + $(no).click(function () { + dontShowAgain(); + modal.delete(); + }); + }; + UIElements.displayFriendRequestModal = function (common, data) { var msg = data.content.msg; var userData = msg.content.user; diff --git a/www/common/onlyoffice/inner.js b/www/common/onlyoffice/inner.js index 28d06dd32..c9efb1eda 100644 --- a/www/common/onlyoffice/inner.js +++ b/www/common/onlyoffice/inner.js @@ -1310,6 +1310,16 @@ define([ if (APP.migrate && !readOnly) { onMigrateRdy.fire(); } + + // Check if history can/should be trimmed + var cp = getLastCp(); + if (cp && cp.file && cp.hash) { + var channels = [{ + channel: content.channel, + lastKnownHash: cp.hash + }]; + common.checkTrimHistory(channels); + } } } }; diff --git a/www/common/sframe-app-framework.js b/www/common/sframe-app-framework.js index 3097cf6d2..5f28373c2 100644 --- a/www/common/sframe-app-framework.js +++ b/www/common/sframe-app-framework.js @@ -551,6 +551,8 @@ define([ Thumb.initPadThumbnails(common, options.thumbnail); } } + + common.checkTrimHistory(); }); }; var onConnectionChange = function (info) { diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index 3eb86ff93..4198a649a 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -264,6 +264,66 @@ define([ return teamChatChannel; }; + // When opening a pad, if were an owner check the history size and prompt for trimming if + // necessary + funcs.checkTrimHistory = function (channels, isDrive) { + channels = channels || []; + var priv = ctx.metadataMgr.getPrivateData(); + + var limit = 100 * 1024 * 1024; // 100MB + limit = 100 * 1024; // XXX 100KB + + var owned; + nThen(function (w) { + if (isDrive) { + funcs.getAttribute(['drive', 'trim'], w(function (err, val) { + if (err || typeof(val) !== "number") { return; } + if (val < (+new Date())) { return; } + w.abort(); + })); + return; + } + funcs.getPadAttribute('trim', w(function (err, val) { + if (err || typeof(val) !== "number") { return; } + if (val < (+new Date())) { return; } + w.abort(); + })); + }).nThen(function (w) { + // Check ownership + // DRIVE + if (isDrive) { + if (!priv.isDriveOwned) { return void w.abort(); } + return; + } + // PAD + channels.push({ channel: priv.channel }); + funcs.getPadMetadata({ + channel: priv.channel + }, w(function (md) { + if (md && md.error) { return void w.abort(); } + var owners = md.owners; + owned = funcs.isOwned(owners); + if (!owned) { return void w.abort(); } + })); + }).nThen(function () { + // We're an owner: check the history size + var history = funcs.makeUniversal('history'); + history.execCommand('GET_HISTORY_SIZE', { + account: isDrive, + pad: !isDrive, + channels: channels, + teamId: typeof(owned) === "number" && owned + }, function (obj) { + if (obj && obj.error) { return; } // can't get history size: abort + var bytes = obj.size; + if (!bytes || typeof(bytes) !== "number") { return; } // no history: abort + if (bytes < limit) { return; } + obj.drive = isDrive; + UIElements.displayTrimHistoryPrompt(funcs, obj); + }); + }); + }; + var cursorChannel; // common-ui-elements needs to be able to get the cursor channel to put it in metadata when // importing a template diff --git a/www/drive/inner.js b/www/drive/inner.js index f85819083..2be9e06e6 100644 --- a/www/drive/inner.js +++ b/www/drive/inner.js @@ -310,6 +310,10 @@ define([ onReconnect(); }); common.onLogout(function () { setEditable(false); }); + + // Check if our drive history needs to be trimmed + common.checkTrimHistory(null, true); + }); }; main(); diff --git a/www/poll/inner.js b/www/poll/inner.js index b36466a13..bd40bc678 100644 --- a/www/poll/inner.js +++ b/www/poll/inner.js @@ -1087,6 +1087,8 @@ define([ common.openPadChat(function () {}); UI.removeLoadingScreen(); + + common.checkTrimHistory(); }; var onError = function (info) {