Automatically run TRIM_HISTORY on teams

This commit is contained in:
yflory 2024-08-29 15:26:17 +02:00
parent 07ee0c1382
commit 1619fb23ee
4 changed files with 67 additions and 0 deletions

View File

@ -136,6 +136,9 @@ define(req, function(AppConfig, Default, Language) {
} }
}; };
Messages.team_autoTrim = "Removing unused history... Please wait."; // XXX
return Messages; return Messages;
}); });

View File

@ -62,6 +62,19 @@ define([
return channels; 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) { var getEdPublic = function (ctx, teamId) {
if (!teamId) { return Util.find(ctx.store, ['proxy', 'edPublic']); } if (!teamId) { return Util.find(ctx.store, ['proxy', 'edPublic']); }
@ -155,6 +168,8 @@ define([
// If account trim history, get the correct channels here // If account trim history, get the correct channels here
if (data.account) { if (data.account) {
channels = getAccountChannels(ctx); channels = getAccountChannels(ctx);
} else if (data.team) {
channels = getTeamChannels(ctx, data.team);
} }
var size = 0; var size = 0;

View File

@ -292,6 +292,15 @@
margin: 0; margin: 0;
} }
} }
.cp-team-trim {
display: flex;
align-items: center;
justify-content: center;
.fa {
font-size: 30px;
margin-right: 10px;
}
}
.cp-teams-invite-uses { .cp-teams-invite-uses {
input { input {

View File

@ -191,6 +191,7 @@ define([
'cp-team-avatar', 'cp-team-avatar',
'cp-team-export', 'cp-team-export',
'cp-team-delete', '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 openTeam = function (common, id, team) {
var sframeChan = common.getSframeChannel(); var sframeChan = common.getSframeChannel();
APP.module.execCommand('SUBSCRIBE', id, function () { APP.module.execCommand('SUBSCRIBE', id, function () {
@ -422,6 +460,7 @@ define([
APP.team = id; APP.team = id;
APP.teamEdPublic = Util.find(team, ['keys', 'drive', 'edPublic']); APP.teamEdPublic = Util.find(team, ['keys', 'drive', 'edPublic']);
buildUI(common, true, team.owner); 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', { APP.module = common.makeUniversal('team', {
onEvent: onEvent onEvent: onEvent
}); });