Skip to content

Commit d763e0c

Browse files
committed
refactor: some improvements
1 parent 4c82660 commit d763e0c

6 files changed

Lines changed: 184 additions & 237 deletions

File tree

src/api/v1/statuses.ts

Lines changed: 21 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
serializeAccount,
2929
serializeAccountOwner,
3030
} from "../../entities/account";
31+
import { forwardActivityToRelays } from "../../entities/relay";
3132
import { getPostRelations, serializePost } from "../../entities/status";
3233
import federation from "../../federation";
3334
import { updateAccountStats } from "../../federation/account";
@@ -281,26 +282,9 @@ app.post(
281282
preferSharedInbox: true,
282283
excludeBaseUris: [new URL(c.req.url)],
283284
});
284-
if (!owner.account.protected) {
285-
const acceptedRelays = await db.query.relays.findMany({
286-
where: eq(relays.state, "accepted"),
287-
with: {
288-
relayServerActor: true,
289-
},
290-
});
291-
await fedCtx.sendActivity(
292-
{ handle },
293-
acceptedRelays.map((relay) => ({
294-
id: new URL(relay.relayServerActor.iri),
295-
inboxId: new URL(relay.relayServerActor.inboxUrl),
296-
})),
297-
activity,
298-
{
299-
preferSharedInbox: true,
300-
excludeBaseUris: [new URL(c.req.url)],
301-
},
302-
);
303-
}
285+
}
286+
if (owner.discoverable && post.visibility === "public") {
287+
await forwardActivityToRelays(db, fedCtx, { handle }, activity);
304288
}
305289
return c.json(serializePost(post, owner, c.req.url));
306290
},
@@ -392,25 +376,8 @@ app.put(
392376
preferSharedInbox: true,
393377
excludeBaseUris: [new URL(c.req.url)],
394378
});
395-
if (post?.visibility !== "direct" && !owner.account.protected) {
396-
const acceptedRelays = await db.query.relays.findMany({
397-
where: eq(relays.state, "accepted"),
398-
with: {
399-
relayServerActor: true,
400-
},
401-
});
402-
await fedCtx.sendActivity(
403-
owner,
404-
acceptedRelays.map((relay) => ({
405-
id: new URL(relay.relayServerActor.iri),
406-
inboxId: new URL(relay.relayServerActor.inboxUrl),
407-
})),
408-
activity,
409-
{
410-
preferSharedInbox: true,
411-
excludeBaseUris: [new URL(c.req.url)],
412-
},
413-
);
379+
if (owner.discoverable && post!.visibility === "public") {
380+
await forwardActivityToRelays(db, fedCtx, owner, activity);
414381
}
415382
return c.json(serializePost(post!, owner, c.req.url));
416383
},
@@ -474,24 +441,9 @@ app.delete(
474441
excludeBaseUris: [new URL(c.req.url)],
475442
},
476443
);
477-
const acceptedRelays = await db.query.relays.findMany({
478-
where: eq(relays.state, "accepted"),
479-
with: {
480-
relayServerActor: true,
481-
},
482-
});
483-
await fedCtx.sendActivity(
484-
owner,
485-
acceptedRelays.map((relay) => ({
486-
id: new URL(relay.relayServerActor.iri),
487-
inboxId: new URL(relay.relayServerActor.inboxUrl),
488-
})),
489-
activity,
490-
{
491-
preferSharedInbox: true,
492-
excludeBaseUris: [new URL(c.req.url)],
493-
},
494-
);
444+
}
445+
if (owner.discoverable && post.visibility === "public") {
446+
await forwardActivityToRelays(db, fedCtx, owner, activity);
495447
}
496448
return c.json({
497449
...serializePost(post, owner, c.req.url),
@@ -866,25 +818,8 @@ app.post(
866818
excludeBaseUris: [new URL(c.req.url)],
867819
},
868820
);
869-
if (!owner.account.protected) {
870-
const acceptedRelays = await db.query.relays.findMany({
871-
where: eq(relays.state, "accepted"),
872-
with: {
873-
relayServerActor: true,
874-
},
875-
});
876-
await fedCtx.sendActivity(
877-
owner,
878-
acceptedRelays.map((relay) => ({
879-
id: new URL(relay.relayServerActor.iri),
880-
inboxId: new URL(relay.relayServerActor.inboxUrl),
881-
})),
882-
activity,
883-
{
884-
preferSharedInbox: true,
885-
excludeBaseUris: [new URL(c.req.url)],
886-
},
887-
);
821+
if (owner.discoverable && post!.visibility === "public") {
822+
await forwardActivityToRelays(db, fedCtx, owner, activity);
888823
}
889824
return c.json(serializePost(post!, owner, c.req.url));
890825
},
@@ -945,25 +880,8 @@ app.post(
945880
excludeBaseUris: [new URL(c.req.url)],
946881
},
947882
);
948-
if (!owner.account.protected) {
949-
const acceptedRelays = await db.query.relays.findMany({
950-
where: eq(relays.state, "accepted"),
951-
with: {
952-
relayServerActor: true,
953-
},
954-
});
955-
await fedCtx.sendActivity(
956-
owner,
957-
acceptedRelays.map((relay) => ({
958-
id: new URL(relay.relayServerActor.iri),
959-
inboxId: new URL(relay.relayServerActor.inboxUrl),
960-
})),
961-
activity,
962-
{
963-
preferSharedInbox: true,
964-
excludeBaseUris: [new URL(c.req.url)],
965-
},
966-
);
883+
if (owner.discoverable && post.visibility === "public") {
884+
await forwardActivityToRelays(db, fedCtx, owner, activity);
967885
}
968886
}
969887
const originalPost = await db.query.posts.findFirst({
@@ -1128,25 +1046,8 @@ app.post(
11281046
preferSharedInbox: true,
11291047
excludeBaseUris: [new URL(c.req.url)],
11301048
});
1131-
if (!owner.account.protected) {
1132-
const acceptedRelays = await db.query.relays.findMany({
1133-
where: eq(relays.state, "accepted"),
1134-
with: {
1135-
relayServerActor: true,
1136-
},
1137-
});
1138-
await fedCtx.sendActivity(
1139-
owner,
1140-
acceptedRelays.map((relay) => ({
1141-
id: new URL(relay.relayServerActor.iri),
1142-
inboxId: new URL(relay.relayServerActor.inboxUrl),
1143-
})),
1144-
activity,
1145-
{
1146-
preferSharedInbox: true,
1147-
excludeBaseUris: [new URL(c.req.url)],
1148-
},
1149-
);
1049+
if (owner.discoverable && post.visibility === "public") {
1050+
await forwardActivityToRelays(db, fedCtx, owner, activity);
11501051
}
11511052
const resultPost = await db.query.posts.findFirst({
11521053
where: eq(posts.id, postId),
@@ -1200,25 +1101,8 @@ app.post(
12001101
preferSharedInbox: true,
12011102
excludeBaseUris: [new URL(c.req.url)],
12021103
});
1203-
if (!owner.account.protected) {
1204-
const acceptedRelays = await db.query.relays.findMany({
1205-
where: eq(relays.state, "accepted"),
1206-
with: {
1207-
relayServerActor: true,
1208-
},
1209-
});
1210-
await fedCtx.sendActivity(
1211-
owner,
1212-
acceptedRelays.map((relay) => ({
1213-
id: new URL(relay.relayServerActor.iri),
1214-
inboxId: new URL(relay.relayServerActor.inboxUrl),
1215-
})),
1216-
activity,
1217-
{
1218-
preferSharedInbox: true,
1219-
excludeBaseUris: [new URL(c.req.url)],
1220-
},
1221-
);
1104+
if (owner.discoverable && post!.visibility === "public") {
1105+
await forwardActivityToRelays(db, fedCtx, owner, activity);
12221106
}
12231107
return c.json(serializePost(post!, owner, c.req.url));
12241108
},
@@ -1341,25 +1225,8 @@ async function addEmojiReaction(
13411225
activity,
13421226
{ preferSharedInbox: true, excludeBaseUris: [new URL(c.req.url)] },
13431227
);
1344-
if (!owner.account.protected) {
1345-
const acceptedRelays = await db.query.relays.findMany({
1346-
where: eq(relays.state, "accepted"),
1347-
with: {
1348-
relayServerActor: true,
1349-
},
1350-
});
1351-
await fedCtx.sendActivity(
1352-
owner,
1353-
acceptedRelays.map((relay) => ({
1354-
id: new URL(relay.relayServerActor.iri),
1355-
inboxId: new URL(relay.relayServerActor.inboxUrl),
1356-
})),
1357-
activity,
1358-
{
1359-
preferSharedInbox: true,
1360-
excludeBaseUris: [new URL(c.req.url)],
1361-
},
1362-
);
1228+
if (owner.discoverable && post.visibility === "public") {
1229+
await forwardActivityToRelays(db, fedCtx, owner, activity);
13631230
}
13641231
return c.json(serializePost(post, owner, c.req.url));
13651232
}
@@ -1454,25 +1321,8 @@ async function removeEmojiReaction(
14541321
activity,
14551322
{ preferSharedInbox: true, excludeBaseUris: [new URL(c.req.url)] },
14561323
);
1457-
if (!owner.account.protected) {
1458-
const acceptedRelays = await db.query.relays.findMany({
1459-
where: eq(relays.state, "accepted"),
1460-
with: {
1461-
relayServerActor: true,
1462-
},
1463-
});
1464-
await fedCtx.sendActivity(
1465-
owner,
1466-
acceptedRelays.map((relay) => ({
1467-
id: new URL(relay.relayServerActor.iri),
1468-
inboxId: new URL(relay.relayServerActor.inboxUrl),
1469-
})),
1470-
activity,
1471-
{
1472-
preferSharedInbox: true,
1473-
excludeBaseUris: [new URL(c.req.url)],
1474-
},
1475-
);
1324+
if (owner.discoverable && post.visibility === "public") {
1325+
await forwardActivityToRelays(db, fedCtx, owner, activity);
14761326
}
14771327
return c.json(serializePost(post, owner, c.req.url));
14781328
}

src/entities/relay.ts

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
import { type DocumentLoader, Follow, Undo } from "@fedify/fedify";
1+
import {
2+
type Activity,
3+
type Context,
4+
type DocumentLoader,
5+
Follow,
6+
type Recipient,
7+
type SenderKeyPair,
8+
Undo,
9+
} from "@fedify/fedify";
10+
import {
11+
type ExtractTablesWithRelations,
12+
and,
13+
eq,
14+
isNotNull,
15+
not,
16+
} from "drizzle-orm";
17+
import type { PgDatabase } from "drizzle-orm/pg-core";
18+
import type { PostgresJsQueryResultHKT } from "drizzle-orm/postgres-js";
19+
import * as schema from "../schema";
220

321
// Use fixed username and id for the relay client actor
422
// Why use fixed username and id?
@@ -8,7 +26,7 @@ import { type DocumentLoader, Follow, Undo } from "@fedify/fedify";
826
// and the realy keeps our server in the database.
927
// And because we won't receive an activity to check whether the Undo was successful, we can't delete the relay client actor safely.
1028
export const HOLLO_RELAY_ACTOR_ID = "8a683714-6fa2-4e53-9f05-b4acbcda4db7";
11-
export const HOLLO_RELAY_ACTOR_USERNAME = "hollo-relay-follower";
29+
export const HOLLO_RELAY_ACTOR_USERNAME = "$hollo~relay~follower$";
1230

1331
/**
1432
* Workaround fedify jsonld serialization for relay follow.
@@ -63,8 +81,63 @@ export class RelayUndo extends Undo {
6381
| Record<string, string>
6482
| (string | Record<string, string>)[];
6583
}): Promise<unknown> {
84+
await this.getObject();
6685
const json = (await super.toJsonLd(options)) as { object: unknown };
6786
json.object = await (await this.getObject())?.toJsonLd();
6887
return json;
6988
}
7089
}
90+
91+
export async function getRelayRecipients(
92+
db: PgDatabase<
93+
PostgresJsQueryResultHKT,
94+
typeof schema,
95+
ExtractTablesWithRelations<typeof schema>
96+
>,
97+
): Promise<Recipient[]> {
98+
const acceptedRelays = await db.query.relays.findMany({
99+
where: and(
100+
eq(schema.relays.state, "accepted"),
101+
isNotNull(schema.relays.relayServerActorId),
102+
),
103+
with: {
104+
relayServerActor: true,
105+
},
106+
});
107+
return acceptedRelays.map((relay) => ({
108+
id: new URL(relay.relayServerActor!.iri),
109+
inboxId: new URL(relay.relayServerActor!.inboxUrl),
110+
}));
111+
}
112+
113+
/**
114+
* Forward a given activity to all relays currently active
115+
* @param db Database instance or transaction
116+
* @param ctx fedify context
117+
* @param sender Sender of the activity
118+
* @param activity Activity to forward to relays
119+
*/
120+
export async function forwardActivityToRelays(
121+
db: PgDatabase<
122+
PostgresJsQueryResultHKT,
123+
typeof schema,
124+
ExtractTablesWithRelations<typeof schema>
125+
>,
126+
ctx: Context<unknown>,
127+
sender:
128+
| SenderKeyPair
129+
| SenderKeyPair[]
130+
| {
131+
identifier: string;
132+
}
133+
| {
134+
username: string;
135+
}
136+
| {
137+
handle: string;
138+
},
139+
activity: Activity,
140+
) {
141+
const recipients = await getRelayRecipients(db);
142+
await ctx.sendActivity(sender, recipients, activity);
143+
}

0 commit comments

Comments
 (0)