feat: send local posts out to established relays

This commit is contained in:
Julian Lam
2025-08-27 12:33:27 -04:00
parent 6576468e2e
commit aa26dfb372
3 changed files with 21 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ const nconf = require('nconf');
const posts = require('../posts'); const posts = require('../posts');
const utils = require('../utils'); const utils = require('../utils');
const { default: PG } = require('pg');
const activitypub = module.parent.exports; const activitypub = module.parent.exports;
const Feps = module.exports; const Feps = module.exports;
@@ -18,21 +19,29 @@ Feps.announce = async function announce(id, activity) {
return; return;
} }
let relays = await activitypub.relays.list();
relays = relays.reduce((memo, { state, url }) => {
if (state === 2) {
memo.push(url);
}
return memo;
}, []);
const followers = await activitypub.notes.getCategoryFollowers(cid); const followers = await activitypub.notes.getCategoryFollowers(cid);
if (!followers.length) { const targets = relays.concat(followers);
if (!targets.length) {
return; return;
} }
const { actor } = activity; const { actor } = activity;
if (actor && !actor.startsWith(nconf.get('url'))) { if (actor && !actor.startsWith(nconf.get('url'))) {
followers.unshift(actor); targets.unshift(actor);
} }
const now = Date.now(); const now = Date.now();
if (activity.type === 'Create') { if (activity.type === 'Create') {
const isMain = await posts.isMain(localId || id); const isMain = await posts.isMain(localId || id);
if (isMain) { if (isMain) {
activitypub.helpers.log(`[activitypub/inbox.announce(1b12)] Announcing plain object (${activity.id}) to followers of cid ${cid}`); activitypub.helpers.log(`[activitypub/inbox.announce(1b12)] Announcing plain object (${activity.id}) to followers of cid ${cid} and ${relays.length} relays`);
await activitypub.send('cid', cid, followers, { await activitypub.send('cid', cid, targets, {
id: `${nconf.get('url')}/post/${encodeURIComponent(id)}#activity/announce/${now}`, id: `${nconf.get('url')}/post/${encodeURIComponent(id)}#activity/announce/${now}`,
type: 'Announce', type: 'Announce',
actor: `${nconf.get('url')}/category/${cid}`, actor: `${nconf.get('url')}/category/${cid}`,
@@ -43,8 +52,8 @@ Feps.announce = async function announce(id, activity) {
} }
} }
activitypub.helpers.log(`[activitypub/inbox.announce(1b12)] Announcing ${activity.type} (${activity.id}) to followers of cid ${cid}`); activitypub.helpers.log(`[activitypub/inbox.announce(1b12)] Announcing ${activity.type} (${activity.id}) to followers of cid ${cid} and ${relays.length} relays`);
await activitypub.send('cid', cid, followers, { await activitypub.send('cid', cid, targets, {
id: `${nconf.get('url')}/post/${encodeURIComponent(id)}#activity/announce/${now + 1}`, id: `${nconf.get('url')}/post/${encodeURIComponent(id)}#activity/announce/${now + 1}`,
type: 'Announce', type: 'Announce',
actor: `${nconf.get('url')}/category/${cid}`, actor: `${nconf.get('url')}/category/${cid}`,

View File

@@ -14,22 +14,23 @@ Relays.is = async (actor) => {
Relays.list = async () => { Relays.list = async () => {
let relays = await db.getSortedSetMembersWithScores('relays:state'); let relays = await db.getSortedSetMembersWithScores('relays:state');
relays = relays.reduce((memo, { value, score }) => { relays = relays.reduce((memo, { value, score }) => {
let state = '[[admin/settings/activitypub:relays.state-0]]'; let label = '[[admin/settings/activitypub:relays.state-0]]';
switch(score) { switch(score) {
case 1: { case 1: {
state = '[[admin/settings/activitypub:relays.state-1]]'; label = '[[admin/settings/activitypub:relays.state-1]]';
break; break;
} }
case 2: { case 2: {
state = '[[admin/settings/activitypub:relays.state-2]]'; label = '[[admin/settings/activitypub:relays.state-2]]';
break; break;
} }
} }
memo.push({ memo.push({
url: value, url: value,
state, state: score,
label,
}); });
return memo; return memo;

View File

@@ -94,7 +94,7 @@
{{{ each relays }}} {{{ each relays }}}
<tr data-url="{./url}"> <tr data-url="{./url}">
<td>{./url}</td> <td>{./url}</td>
<td>{./state}</td> <td>{./label}</td>
<td><a href="#" data-action="relays.remove"><i class="fa fa-trash link-danger"></i></a></td> <td><a href="#" data-action="relays.remove"><i class="fa fa-trash link-danger"></i></a></td>
</tr> </tr>
{{{ end }}} {{{ end }}}