Node.js based forum software built for the modern web
'use strict';
const db = require('../../database');
const meta = require('../../meta');
const activitypub = require('../../activitypub');
const analytics = require('../../analytics');
const federationController = module.exports;
federationController.general = function (req, res) {
res.render(`admin/federation/general`, {
title: '[[admin/menu:federation/general]]',
});
};
federationController.content = function (req, res) {
res.render(`admin/federation/content`, {
title: '[[admin/menu:federation/content]]',
});
};
federationController.rules = async function (req, res) {
const rules = await activitypub.rules.list();
const postQueueEnabled = !!(await meta.configs.get('postQueue'));
const hasFilterOrRejectRules = rules.some(rule => rule.action >= 1);
res.render(`admin/federation/rules`, {
title: '[[admin/menu:federation/rules]]',
rules,
postQueueEnabled,
hasFilterOrRejectRules,
});
};
federationController.relays = async function (req, res) {
const relays = await activitypub.relays.list();
const urls = relays.map(({ url }) => url);
let { relay, term } = req.query;
if (!urls.includes(relay)) {
relay = undefined;
}
let method = 'getHourlyStatsForSet';
let count = 24;
if (term === 'daily') {
method = 'getDailyStatsForSet';
count = 30;
}
const inSet = relay ? `ap.relayIn:byRelay:${relay}` : 'ap.relayIn';
const outSet = relay ? `ap.relayOut:byRelay:${relay}` : 'ap.relayOut';
const incoming = await analytics[method](inSet, Date.now(), count);
const out = await analytics[method](outSet, Date.now(), count);
res.render(`admin/federation/relays`, {
title: '[[admin/menu:federation/relays]]',
relays,
data: { in: incoming, out },
});
};
federationController.pruning = function (req, res) {
res.render(`admin/federation/pruning`, {
title: '[[admin/menu:federation/pruning]]',
});
};
federationController.safety = async function (req, res) {
const instanceCount = await activitypub.instances.getCount();
const blocklists = (await activitypub.blocklists.list()).filter(bl => bl.url !== 'core');
const core = await activitypub.blocklists.get('core');
res.render(`admin/federation/safety`, {
title: '[[admin/menu:federation/safety]]',
blocklists,
instanceCount,
domains: core.domains,
});
};
federationController.analytics = async function (req, res) {
const instances = await activitypub.instances.list();
instances.sort();
let { host, term } = req.query;
if (!instances.includes(host)) {
host = undefined;
}
let method = 'getHourlyStatsForSet';
let count = 24;
if (term === 'daily') {
method = 'getDailyStatsForSet';
count = 30;
}
const receivedSet = host ? `activities:byHost:${host}` : 'activities';
const receivedErrSet = host ? `ap.inErr:byHost:${host}` : 'ap.inErr';
const sentSet = host ? `ap.out:byHost:${host}` : 'ap.out';
const sentErrSet = host ? `ap.outErr:byHost:${host}` : 'ap.outErr';
const received = await analytics[method](receivedSet, Date.now(), count);
const receivedErr = await analytics[method](receivedErrSet, Date.now(), count);
const sent = await analytics[method](sentSet, Date.now(), count);
const sentErr = await analytics[method](sentErrSet, Date.now(), count);
const byType = await getActivitiesByType();
res.render('admin/federation/analytics', {
title: '[[admin/menu:federation/analytics]]',
instances,
data: {
received,
receivedErr,
sent,
sentErr,
byType,
},
hideSave: 1,
});
};
async function getActivitiesByType() {
const analyticsKeys = await db.getSortedSetRange('analyticsKeys', 0, -1);
const typeKeys = analyticsKeys.filter(key => key.startsWith('activities:byType:'));
const results = await Promise.all(typeKeys.map(async (key) => {
const entries = await db.getSortedSetRangeWithScores(`analytics:${key}`, 0, -1);
return {
type: key.replace('activities:byType:', ''),
count: entries.reduce((sum, { score }) => sum + score, 0),
};
}));
return Object.fromEntries(results.map(({ type, count }) => [type, count]));
}
federationController.hashtags = async function (req, res) {
const hashtags = await activitypub.hashtags.list();
const relay = await activitypub.hashtags.getRelay();
const relayOptions = activitypub.hashtags.getRelayOptions();
res.render('admin/federation/hashtags', {
title: '[[admin/menu:federation/hashtags]]',
hashtags,
relay,
relayOptions,
});
};
federationController.errors = async function (req, res) {
const { hostname: filterHostname, type: filterType } = req.query;
let errors = await db.getSortedSetRevRangeByScoreWithScores('ap.errors', 0, -1, Date.now(), '-inf');
const errorObj = await db.getObjects(errors.map(({ value: id }) => `ap.errors:${id}`));
errors = errors.map(({ value: id, score: timestamp }, idx) => {
if (!errorObj[idx]) {
return null;
}
let { type, body, stack, recipient } = errorObj[idx];
let activityType;
let hostname = 'Invalid hostname';
const timestampISO = new Date(timestamp).toISOString();
try {
const parsed = JSON.parse(body);
({ type: activityType } = parsed);
body = JSON.stringify(parsed, null, 4);
stack = stack.replace(/\s+$/gm, '');
({ hostname } = new URL(id));
} catch (e) {
// noop
}
if (filterHostname && hostname !== filterHostname) {
return null;
}
if (filterType && type !== filterType) {
return null;
}
return {
id: id || '',
type: type || '',
activityType: activityType || '',
body: body || '',
recipient: recipient || '',
stack,
hostname,
timestamp,
timestampISO,
};
}).filter(Boolean);
res.render('admin/federation/errors', {
title: '[[admin/menu:federation/errors]]',
errors,
});
};