From 1619fb23ee04cde729ee6d473a3ed78c7876dbcd Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 29 Aug 2024 15:26:17 +0200 Subject: [PATCH] Automatically run TRIM_HISTORY on teams --- customize.dist/messages.js | 3 +++ www/common/outer/history.js | 15 ++++++++++++++ www/teams/app-team.less | 9 +++++++++ www/teams/inner.js | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) diff --git a/customize.dist/messages.js b/customize.dist/messages.js index a5784b495..3104fab18 100755 --- a/customize.dist/messages.js +++ b/customize.dist/messages.js @@ -136,6 +136,9 @@ define(req, function(AppConfig, Default, Language) { } }; + + Messages.team_autoTrim = "Removing unused history... Please wait."; // XXX + return Messages; }); diff --git a/www/common/outer/history.js b/www/common/outer/history.js index 10078f116..d8df615cb 100644 --- a/www/common/outer/history.js +++ b/www/common/outer/history.js @@ -62,6 +62,19 @@ define([ return channels; }; + let getTeamChannels = function (ctx, teamId) { + let team = Util.find(ctx.store, ['proxy', 'teams', teamId]); + if (!team) { return []; } + + let channels = [team.channel]; + let roster = team.keys.roster; + channels.push({ + channel: roster.channel, + lastKnownHash: roster.lastKnownHash + }); + return channels; + }; + var getEdPublic = function (ctx, teamId) { if (!teamId) { return Util.find(ctx.store, ['proxy', 'edPublic']); } @@ -155,6 +168,8 @@ define([ // If account trim history, get the correct channels here if (data.account) { channels = getAccountChannels(ctx); + } else if (data.team) { + channels = getTeamChannels(ctx, data.team); } var size = 0; diff --git a/www/teams/app-team.less b/www/teams/app-team.less index db1efa29b..9e99d0e70 100644 --- a/www/teams/app-team.less +++ b/www/teams/app-team.less @@ -292,6 +292,15 @@ margin: 0; } } + .cp-team-trim { + display: flex; + align-items: center; + justify-content: center; + .fa { + font-size: 30px; + margin-right: 10px; + } + } .cp-teams-invite-uses { input { diff --git a/www/teams/inner.js b/www/teams/inner.js index 9f35da7d8..99157708d 100644 --- a/www/teams/inner.js +++ b/www/teams/inner.js @@ -191,6 +191,7 @@ define([ 'cp-team-avatar', 'cp-team-export', 'cp-team-delete', + 'cp-team-history', ], }; @@ -398,6 +399,43 @@ define([ ]); }); + + let AUTOTRIM_LIMIT = 102400; // 100kB history before auto trim + var trimHistory = function(common) { + var size; + var channels = []; + nThen(function(waitFor) { + APP.history.execCommand('GET_HISTORY_SIZE', { + team: APP.team, + channels: [] + }, waitFor(function(obj) { + if (obj && obj.error) { + waitFor.abort(); + console.error(obj.error); + return; + } + channels = obj.channels; + size = Number(obj.size); + })); + }).nThen(function() { + if (!size || size < AUTOTRIM_LIMIT) { + // Nothing to delete + return; + } + var div = h('div.cp-team-trim', [ + h('span.fa.fa-spin.fa-spinner'), + h('span', Messages.team_autoTrim) + ]); + let modal = UI.openCustomModal(UI.dialog.customModal(div, {buttons: []})); + console.log('Trimming team history', APP.team, size); + APP.history.execCommand('TRIM_HISTORY', { + channels: channels + }, function(obj) { + if (obj && obj.error) { console.error(obj.error); } + UI.removeModals(); + }); + }); + }; var openTeam = function (common, id, team) { var sframeChan = common.getSframeChannel(); APP.module.execCommand('SUBSCRIBE', id, function () { @@ -422,6 +460,7 @@ define([ APP.team = id; APP.teamEdPublic = Util.find(team, ['keys', 'drive', 'edPublic']); buildUI(common, true, team.owner); + if (team.owner) { trimHistory(common); } }); }); }; @@ -1556,6 +1595,7 @@ define([ } }; + APP.history = common.makeUniversal('history'); APP.module = common.makeUniversal('team', { onEvent: onEvent });