Skip to content

Node.js based forum software built for the modern web

refactor(activitypub): replace filter with three-tier action system

Julian Lamca93eda8c123
diff --git a/public/language/en-GB/admin/settings/activitypub.json b/public/language/en-GB/admin/settings/activitypub.json
index d03cd84f2c..3a1a5007c3 100644
--- a/public/language/en-GB/admin/settings/activitypub.json
+++ b/public/language/en-GB/admin/settings/activitypub.json
@@ -31,9 +31,12 @@
"rules.help-user": "Topics created by the entered user will match. Enter a handle or full ID (e.g. <code>[email protected]</code> or <code>https://example.org/users/bob</code>.",
"rules.type": "Type",
"rules.value": "Value",
+ "rules.action": "Action",
+ "rules.categorize": "Categorize",
"rules.filter": "Queue",
- "rules.filter.help": "If checked, matching content will be passed to the post queue for review before being categorized.",
- "rules.filter-warning": "You have categorization rules set to queue posts, but the post queue is currently disabled. Posts matching these rules will be published immediately.",
+ "rules.reject": "Reject",
+ "rules.action.help": "What to do when a rule matches: categorize (publish normally), queue (hold for review), or reject (drop the content).",
+ "rules.filter-warning": "You have categorization rules set to queue or reject posts, but the post queue is currently disabled. Posts matching these rules will be published immediately.",
"rules.cid": "Category",
"rules.cutoff.title": "Age Cutoff",
"rules.cutoff.label": "Categorization age cutoff (days)",
diff --git a/public/src/admin/federation/rules.js b/public/src/admin/federation/rules.js
index 1c25de2493..7007b6a50f 100644
--- a/public/src/admin/federation/rules.js
+++ b/public/src/admin/federation/rules.js
@@ -74,6 +74,7 @@ function throwModal() {
}
const payload = Object.fromEntries(new FormData(formEl));
+ payload.action = parseInt(payload.action, 10);
post('/admin/activitypub/rules', payload).then(async (data) => {
const html = await render('admin/federation/rules', { rules: data }, 'rules');
const tbodyEl = document.querySelector('#rules tbody');
@@ -101,6 +102,19 @@ function throwModal() {
modal.find('#value').focus();
});
+ // live update action label
+ const actionEl = modal.get(0).querySelector('#action');
+ const labelsEl = modal.get(0).querySelector('#action-labels');
+ if (actionEl && labelsEl) {
+ const labels = labelsEl.querySelectorAll('span');
+ const updateActionLabel = () => {
+ labels.forEach((l, i) => {
+ l.style.fontWeight = i === parseInt(actionEl.value, 10) ? 'bold' : 'normal';
+ });
+ };
+ updateActionLabel();
+ actionEl.addEventListener('input', updateActionLabel);
+ }
// help text
const updateHelp = async (key, el) => {
diff --git a/src/activitypub/notes.js b/src/activitypub/notes.js
index 6b608d0834..c17b49ad63 100644
--- a/src/activitypub/notes.js
+++ b/src/activitypub/notes.js
@@ -193,14 +193,18 @@ Notes.assert = async (uid, input, options = { skipChecks: false, queue: false })
}
// Auto-categorization (takes place only if all other categorization efforts fail)
- const { cid, filter: ruleFilter } = await assignCategory(mainPost);
- options.queue = ruleFilter;
+ const { cid, action: ruleAction } = await assignCategory(mainPost);
+ if (ruleAction === 2) {
+ activitypub.helpers.log(`[activitypub/notes.assert] Rule rejected post (${mainPid})`);
+ return null;
+ }
+ options.queue = ruleAction === 1;
crosspostCid = cid;
if (!options.cid) {
options.cid = crosspostCid;
crosspostCid = false;
}
- // filter is used below to decide whether to queue or add the crosspost
+ // action is used below to decide whether to queue or add the crosspost
// mainPid ok to leave as-is
if (!title) {
@@ -588,28 +592,28 @@ async function assignCategory(post) {
let tags = await Notes._normalizeTags(post._activitypub.tag || []);
tags = tags.map(tag => tag.toLowerCase());
- const matched = rules.reduce((matched, { type, value, cid: target, filter: ruleFilter }) => {
+ const matched = rules.reduce((matched, { type, value, cid: target, action: ruleAction }) => {
if (!matched.cid) {
switch (type) {
case 'hashtag': {
if (tags.includes(value.toLowerCase())) {
- activitypub.helpers.log(`[activitypub] - Rule match: #${value}; cid: ${target}`);
- return { cid: target, filter: ruleFilter };
+ activitypub.helpers.log(`[activitypub] - Rule match: #${value}; cid: ${target}, action: ${ruleAction}`);
+ return { cid: target, action: ruleAction };
}
break;
}
case 'user': {
if (post.uid === value) {
- activitypub.helpers.log(`[activitypub] - Rule match: user ${value}; cid: ${target}`);
- return { cid: target, filter: ruleFilter };
+ activitypub.helpers.log(`[activitypub] - Rule match: user ${value}; cid: ${target}, action: ${ruleAction}`);
+ return { cid: target, action: ruleAction };
}
}
}
}
return matched;
- }, { cid: undefined, filter: false });
+ }, { cid: undefined, action: 0 });
return matched;
}
diff --git a/src/activitypub/rules.js b/src/activitypub/rules.js
index 1cdaf8b1da..62c3343755 100644
--- a/src/activitypub/rules.js
+++ b/src/activitypub/rules.js
@@ -14,14 +14,21 @@ Rules.list = async () => {
rules = rules.map((rule, idx) => {
rule.rid = rids[idx];
rule.cid = parseInt(rule.cid, 10);
- rule.filter = typeof rule.filter === 'string' ? rule.filter === 'true' : rule.filter;
+ // Normalize legacy filter boolean to action integer
+ if (rule.action === undefined || rule.action === null) {
+ rule.action = typeof rule.filter === 'string' ? (rule.filter === 'true' ? 1 : 0) : (rule.filter ? 1 : 0);
+ } else {
+ rule.action = parseInt(rule.action, 10);
+ }
return rule;
});
return rules;
};
-Rules.upsert = async (type, value, cid, filter) => {
+Rules.upsert = async (type, value, cid, action) => {
+ action = parseInt(action, 10);
+
const rules = await Rules.list();
const existing = rules.find(rule => rule.type === type && rule.value === value);
@@ -37,7 +44,7 @@ Rules.upsert = async (type, value, cid, filter) => {
if (existing) {
await db.setObject(`rid:${existing.rid}`, {
cid,
- filter: !!filter,
+ action,
});
return existing.rid;
@@ -45,7 +52,7 @@ Rules.upsert = async (type, value, cid, filter) => {
const uuid = utils.generateUUID();
await Promise.all([
- db.setObject(`rid:${uuid}`, { type, value, cid, filter: !!filter }),
+ db.setObject(`rid:${uuid}`, { type, value, cid, action }),
db.sortedSetAdd('categorization:rid', Date.now(), uuid),
]);
return uuid;
diff --git a/src/controllers/admin/federation.js b/src/controllers/admin/federation.js
index d455bded8e..6861073463 100644
--- a/src/controllers/admin/federation.js
+++ b/src/controllers/admin/federation.js
@@ -22,13 +22,13 @@ federationController.content = function (req, res) {
federationController.rules = async function (req, res) {
const rules = await activitypub.rules.list();
const postQueueEnabled = !!(await meta.configs.get('postQueue'));
- const hasFilterRules = rules.some(rule => rule.filter === 'true');
+ const hasFilterOrRejectRules = rules.some(rule => rule.action >= 1);
res.render(`admin/federation/rules`, {
title: '[[admin/menu:federation/rules]]',
rules,
postQueueEnabled,
- hasFilterRules,
+ hasFilterOrRejectRules,
});
};
diff --git a/src/controllers/write/admin.js b/src/controllers/write/admin.js
index cfd135a554..364304ebc6 100644
--- a/src/controllers/write/admin.js
+++ b/src/controllers/write/admin.js
@@ -89,7 +89,7 @@ Admin.listGroups = async (req, res) => {
Admin.activitypub = {};
Admin.activitypub.addRule = async (req, res) => {
- let { type, value, cid, filter } = req.body;
+ let { type, value, cid, action } = req.body;
let exists = true;
const parsedCid = parseInt(cid, 10);
@@ -104,7 +104,7 @@ Admin.activitypub.addRule = async (req, res) => {
return helpers.formatApiResponse(400, res);
}
- await activitypub.rules.upsert(type, value, cid, filter === 'true');
+ await activitypub.rules.upsert(type, value, cid, action);
helpers.formatApiResponse(200, res, await activitypub.rules.list());
};
diff --git a/src/views/admin/federation/rules.tpl b/src/views/admin/federation/rules.tpl
index ea17062222..95de4544d7 100644
--- a/src/views/admin/federation/rules.tpl
+++ b/src/views/admin/federation/rules.tpl
@@ -6,7 +6,7 @@
<div id="rules" class="mb-4">
<p class="lead">{{tx("admin/settings/activitypub:rules-intro")}}</p>
- {{{ if (hasFilterRules && !postQueueEnabled) }}}
+ {{{ if (hasFilterOrRejectRules && !postQueueEnabled) }}}
<div class="alert alert-warning">{{tx("admin/settings/activitypub:rules.filter-warning")}}</div>
{{{ end }}}
@@ -29,7 +29,7 @@
<th></th>
<th>{{tx("admin/settings/activitypub:rules.type")}}</th>
<th>{{tx("admin/settings/activitypub:rules.value")}}</th>
- <th>{{tx("admin/settings/activitypub:rules.filter")}}</th>
+ <th>{{tx("admin/settings/activitypub:rules.action")}}</th>
<th>{{tx("admin/settings/activitypub:rules.cid")}}</th>
<th></th>
</thead>
@@ -41,7 +41,13 @@
</td>
<td>{./type}</td>
<td>{./value}</td>
- <td>{{{ if ./filter }}}✓{{{ else }}}✗{{{ end }}}</td>
+ <td>
+ {{{ if (./action == "2") }}}[[admin/settings/activitypub:rules.reject]]{{{ else }}}
+ {{{ if (./action == "1") }}}[[admin/settings/activitypub:rules.filter]]{{{ else }}}
+ [[admin/settings/activitypub:rules.categorize]]
+ {{{ end }}}
+ {{{ end }}}
+ </td>
<td>{./cid}</td>
<td><a href="#" data-action="rules.delete"><i class="fa fa-trash link-danger"></i></a></td>
</tr>
diff --git a/src/views/admin/partials/activitypub/rules.tpl b/src/views/admin/partials/activitypub/rules.tpl
index 53399e35dd..4297e34c96 100644
--- a/src/views/admin/partials/activitypub/rules.tpl
+++ b/src/views/admin/partials/activitypub/rules.tpl
@@ -24,9 +24,14 @@
</div>
<input type="hidden" name="cid" />
</div>
- <div class="mb-3 form-check">
- <input type="checkbox" class="form-check-input" name="filter" id="filter" value="true" />
- <label class="form-check-label" for="filter">{{tx("admin/settings/activitypub:rules.filter")}}</label>
- <p class="form-text">{{tx("admin/settings/activitypub:rules.filter.help")}}</p>
+ <div class="mb-3">
+ <label class="form-label" for="action">{{tx("admin/settings/activitypub:rules.action")}}</label>
+ <input type="range" class="form-range" name="action" id="action" min="0" max="2" value="0" />
+ <div class="d-flex justify-content-between form-text" id="action-labels">
+ <span>{{tx("admin/settings/activitypub:rules.categorize")}}</span>
+ <span>{{tx("admin/settings/activitypub:rules.filter")}}</span>
+ <span>{{tx("admin/settings/activitypub:rules.reject")}}</span>
+ </div>
+ <p class="form-text">{{tx("admin/settings/activitypub:rules.action.help")}}</p>
</div>
-</form>
\ No newline at end of file
+</form>