2022-02-27 11:07:39 +09:00
|
|
|
import { createSystemUser } from './create-system-user.js';
|
|
|
|
import { renderFollowRelay } from '@/remote/activitypub/renderer/follow-relay.js';
|
|
|
|
import { renderActivity, attachLdSignature } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import renderUndo from '@/remote/activitypub/renderer/undo.js';
|
|
|
|
import { deliver } from '@/queue/index.js';
|
|
|
|
import { ILocalUser, User } from '@/models/entities/user.js';
|
|
|
|
import { Users, Relays } from '@/models/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
2020-05-10 18:42:31 +09:00
|
|
|
|
|
|
|
const ACTOR_USERNAME = 'relay.actor' as const;
|
|
|
|
|
|
|
|
export async function getRelayActor(): Promise<ILocalUser> {
|
|
|
|
const user = await Users.findOne({
|
|
|
|
host: null,
|
2021-12-09 23:58:30 +09:00
|
|
|
username: ACTOR_USERNAME,
|
2020-05-10 18:42:31 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if (user) return user as ILocalUser;
|
|
|
|
|
|
|
|
const created = await createSystemUser(ACTOR_USERNAME);
|
|
|
|
return created as ILocalUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function addRelay(inbox: string) {
|
2022-01-03 03:17:16 +09:00
|
|
|
const relay = await Relays.insert({
|
2020-05-10 18:42:31 +09:00
|
|
|
id: genId(),
|
|
|
|
inbox,
|
2021-12-09 23:58:30 +09:00
|
|
|
status: 'requesting',
|
2022-01-03 03:17:16 +09:00
|
|
|
}).then(x => Relays.findOneOrFail(x.identifiers[0]));
|
2020-05-10 18:42:31 +09:00
|
|
|
|
|
|
|
const relayActor = await getRelayActor();
|
|
|
|
const follow = await renderFollowRelay(relay, relayActor);
|
|
|
|
const activity = renderActivity(follow);
|
|
|
|
deliver(relayActor, activity, relay.inbox);
|
|
|
|
|
|
|
|
return relay;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function removeRelay(inbox: string) {
|
|
|
|
const relay = await Relays.findOne({
|
2021-12-09 23:58:30 +09:00
|
|
|
inbox,
|
2020-05-10 18:42:31 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if (relay == null) {
|
|
|
|
throw 'relay not found';
|
|
|
|
}
|
|
|
|
|
|
|
|
const relayActor = await getRelayActor();
|
|
|
|
const follow = renderFollowRelay(relay, relayActor);
|
|
|
|
const undo = renderUndo(follow, relayActor);
|
|
|
|
const activity = renderActivity(undo);
|
|
|
|
deliver(relayActor, activity, relay.inbox);
|
|
|
|
|
|
|
|
await Relays.delete(relay.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function listRelay() {
|
|
|
|
const relays = await Relays.find();
|
|
|
|
return relays;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function relayAccepted(id: string) {
|
|
|
|
const result = await Relays.update(id, {
|
2021-12-09 23:58:30 +09:00
|
|
|
status: 'accepted',
|
2020-05-10 18:42:31 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
return JSON.stringify(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function relayRejected(id: string) {
|
|
|
|
const result = await Relays.update(id, {
|
2021-12-09 23:58:30 +09:00
|
|
|
status: 'rejected',
|
2020-05-10 18:42:31 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
return JSON.stringify(result);
|
|
|
|
}
|
|
|
|
|
2021-03-24 11:05:37 +09:00
|
|
|
export async function deliverToRelays(user: { id: User['id']; host: null; }, activity: any) {
|
2020-05-10 18:42:31 +09:00
|
|
|
if (activity == null) return;
|
|
|
|
|
|
|
|
const relays = await Relays.find({
|
2021-12-09 23:58:30 +09:00
|
|
|
status: 'accepted',
|
2020-05-10 18:42:31 +09:00
|
|
|
});
|
|
|
|
if (relays.length === 0) return;
|
|
|
|
|
|
|
|
const copy = JSON.parse(JSON.stringify(activity));
|
|
|
|
if (!copy.to) copy.to = ['https://www.w3.org/ns/activitystreams#Public'];
|
|
|
|
|
|
|
|
const signed = await attachLdSignature(copy, user);
|
|
|
|
|
|
|
|
for (const relay of relays) {
|
2020-05-15 20:50:04 +09:00
|
|
|
deliver(user, signed, relay.inbox);
|
2020-05-10 18:42:31 +09:00
|
|
|
}
|
|
|
|
}
|