2023-01-12 21:40:33 -07:00
|
|
|
import { IsNull } from "typeorm";
|
|
|
|
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 type { ILocalUser, User } from "@/models/entities/user.js";
|
|
|
|
import { Users, Relays } from "@/models/index.js";
|
|
|
|
import { genId } from "@/misc/gen-id.js";
|
|
|
|
import { Cache } from "@/misc/cache.js";
|
|
|
|
import type { Relay } from "@/models/entities/relay.js";
|
|
|
|
import { createSystemUser } from "./create-system-user.js";
|
|
|
|
|
|
|
|
const ACTOR_USERNAME = "relay.actor" as const;
|
2020-05-10 03:42:31 -06:00
|
|
|
|
2023-07-06 15:49:41 -06:00
|
|
|
const relaysCache = new Cache<Relay[]>("relay", 60 * 60);
|
2022-03-25 01:27:41 -06:00
|
|
|
|
2020-05-10 03:42:31 -06:00
|
|
|
export async function getRelayActor(): Promise<ILocalUser> {
|
2022-03-26 00:34:00 -06:00
|
|
|
const user = await Users.findOneBy({
|
|
|
|
host: IsNull(),
|
2021-12-09 07:58:30 -07:00
|
|
|
username: ACTOR_USERNAME,
|
2020-05-10 03:42:31 -06: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-02 11:17:16 -07:00
|
|
|
const relay = await Relays.insert({
|
2020-05-10 03:42:31 -06:00
|
|
|
id: genId(),
|
|
|
|
inbox,
|
2023-01-12 21:40:33 -07:00
|
|
|
status: "requesting",
|
|
|
|
}).then((x) => Relays.findOneByOrFail(x.identifiers[0]));
|
2020-05-10 03:42:31 -06:00
|
|
|
|
|
|
|
const relayActor = await getRelayActor();
|
2023-06-15 18:59:17 -06:00
|
|
|
const follow = renderFollowRelay(relay, relayActor);
|
2020-05-10 03:42:31 -06:00
|
|
|
const activity = renderActivity(follow);
|
|
|
|
deliver(relayActor, activity, relay.inbox);
|
|
|
|
|
|
|
|
return relay;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function removeRelay(inbox: string) {
|
2022-03-26 00:34:00 -06:00
|
|
|
const relay = await Relays.findOneBy({
|
2021-12-09 07:58:30 -07:00
|
|
|
inbox,
|
2020-05-10 03:42:31 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
if (relay == null) {
|
2023-01-12 21:40:33 -07:00
|
|
|
throw new Error("relay not found");
|
2020-05-10 03:42:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-06-15 18:59:17 -06:00
|
|
|
await updateRelaysCache();
|
2020-05-10 03:42:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function listRelay() {
|
|
|
|
const relays = await Relays.find();
|
|
|
|
return relays;
|
|
|
|
}
|
|
|
|
|
2023-06-15 18:59:17 -06:00
|
|
|
export async function getCachedRelays(): Promise<Relay[]> {
|
|
|
|
return await relaysCache.fetch(null, () =>
|
|
|
|
Relays.findBy({
|
|
|
|
status: "accepted",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-10 03:42:31 -06:00
|
|
|
export async function relayAccepted(id: string) {
|
|
|
|
const result = await Relays.update(id, {
|
2023-01-12 21:40:33 -07:00
|
|
|
status: "accepted",
|
2020-05-10 03:42:31 -06:00
|
|
|
});
|
|
|
|
|
2023-06-15 18:59:17 -06:00
|
|
|
await updateRelaysCache();
|
|
|
|
|
2020-05-10 03:42:31 -06:00
|
|
|
return JSON.stringify(result);
|
|
|
|
}
|
|
|
|
|
2023-06-15 18:59:17 -06:00
|
|
|
async function updateRelaysCache() {
|
|
|
|
const relays = await Relays.findBy({
|
|
|
|
status: "accepted",
|
|
|
|
});
|
2023-07-02 18:37:46 -06:00
|
|
|
await relaysCache.set(null, relays);
|
2023-06-15 18:59:17 -06:00
|
|
|
}
|
|
|
|
|
2020-05-10 03:42:31 -06:00
|
|
|
export async function relayRejected(id: string) {
|
|
|
|
const result = await Relays.update(id, {
|
2023-01-12 21:40:33 -07:00
|
|
|
status: "rejected",
|
2020-05-10 03:42:31 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
return JSON.stringify(result);
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
export async function deliverToRelays(
|
|
|
|
user: { id: User["id"]; host: null },
|
|
|
|
activity: any,
|
|
|
|
) {
|
2020-05-10 03:42:31 -06:00
|
|
|
if (activity == null) return;
|
|
|
|
|
2023-06-15 18:59:17 -06:00
|
|
|
const relays = await getCachedRelays();
|
2020-05-10 03:42:31 -06:00
|
|
|
if (relays.length === 0) return;
|
|
|
|
|
2022-06-04 02:26:56 -06:00
|
|
|
// TODO
|
|
|
|
//const copy = structuredClone(activity);
|
|
|
|
const copy = JSON.parse(JSON.stringify(activity));
|
2023-01-12 21:40:33 -07:00
|
|
|
if (!copy.to) copy.to = ["https://www.w3.org/ns/activitystreams#Public"];
|
2020-05-10 03:42:31 -06:00
|
|
|
|
|
|
|
const signed = await attachLdSignature(copy, user);
|
|
|
|
|
|
|
|
for (const relay of relays) {
|
2020-05-15 05:50:04 -06:00
|
|
|
deliver(user, signed, relay.inbox);
|
2020-05-10 03:42:31 -06:00
|
|
|
}
|
|
|
|
}
|