Trim ActivityPub SharedInbox collection (Notes) along with assoc message area

This commit is contained in:
Bryan Ashby 2023-03-14 21:58:03 -06:00
parent 5314fb4ad9
commit 10e2abffc6
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
3 changed files with 123 additions and 10 deletions

View File

@ -15,7 +15,7 @@ const { getJson } = require('../http_util');
// deps
const { isString } = require('lodash');
const Log = require('../logger');
const Log = require('../logger').log;
const async = require('async');
module.exports = class Collection extends ActivityPubObject {
@ -672,6 +672,64 @@ module.exports = class Collection extends ActivityPubObject {
);
}
static removeByMaxCount(collectionName, maxCount, cb) {
apDb.run(
`DELETE FROM collection
WHERE _rowid_ IN (
SELECT _rowid_
FROM collection
WHERE name = ?
ORDER BY _rowid_ DESC
LIMIT -1 OFFSET ${maxCount}
);`,
[maxCount],
function res(err) {
// non-arrow function for 'this'
Collection._removeByLogHelper(
collectionName,
'MaxCount',
err,
maxCount,
this.changes
);
return cb(err);
}
);
}
static removeByMaxAgeDays(collectionName, maxAgeDays, cb) {
apDb.run(
`DELETE FROM collection
WHERE name = ? AND timestamp < DATE('now', '-${maxAgeDays} days');`,
[maxAgeDays],
function res(err) {
// non-arrow function for 'this'
Collection._removeByLogHelper(
collectionName,
'MaxAgeDays',
err,
maxAgeDays,
this.changes
);
return cb(err);
}
);
}
static _removeByLogHelper(collectionName, type, err, value, deletedCount) {
if (err) {
Log.error(
{ collectionName, error: err.message, type, value },
'Error trimming collection'
);
} else {
Log.debug(
{ collectionName, type, value, deletedCount },
'Collection trimmed successfully'
);
}
}
static _rowToObjectInfo(row) {
return {
name: row.name,

View File

@ -923,6 +923,8 @@ module.exports = () => {
alwaysExportExternal: true,
subjectOptional: true,
addressFlavor: 'activitypub',
maxAgeDays: 365 * 2,
maxMessages: 100000,
},
},
},

View File

@ -16,6 +16,8 @@ const {
WellKnownConfTags,
WellKnownAreaTags,
} = require('./message_const');
const Collection = require('./activitypub/collection');
const { Collections } = require('./activitypub/const');
// deps
const async = require('async');
@ -824,19 +826,70 @@ function trimMessageAreasScheduledEvent(args, cb) {
return callback(null, areaInfos);
},
function trimGeneralAreas(areaInfos, callback) {
const cbWrap = (e, t, c) => {
if (e) {
Log.warn({ error: e.message, type: t }, `Failed trimming (${t})`);
}
return c(null);
};
const ApSharedAreaTag = Message.WellKnownAreaTags.ActivityPubShared;
// Clean up messages, and any associated ActivityPub 'SharedInbox'
// Notes (ie: the source of said messages)
async.each(
areaInfos,
(areaInfo, next) => {
(areaInfo, nextArea) => {
async.series(
[
next => {
trimMessageAreaByMaxMessages(areaInfo, err => {
if (err) {
return next(err);
return cbWrap(err, 'Messages:MaxCount', next);
});
},
next => {
if (areaInfo.areaTag !== ApSharedAreaTag) {
return next(null);
}
Collection.removeByMaxCount(
Collections.SharedInbox,
areaInfo.maxMessages,
err => {
return cbWrap(
err,
'ActivityPubShared:MaxCount',
next
);
}
);
},
next => {
trimMessageAreaByMaxAgeDays(areaInfo, err => {
return next(err);
});
return cbWrap(err, 'Messages:MaxAgeDays', next);
});
},
next => {
if (areaInfo.areaTag !== ApSharedAreaTag) {
return next(null);
}
Collection.removeByMaxAgeDays(
Collections.SharedInbox,
areaInfo.maxAgeDays,
err => {
return cbWrap(
err,
'ActivityPubShared:MaxAgeDays',
next
);
}
);
},
],
err => {
return nextArea(err);
}
);
},
callback
);
},