forked from fedify-dev/fedify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfederation.ts
More file actions
141 lines (133 loc) · 3.53 KB
/
Copy pathfederation.ts
File metadata and controls
141 lines (133 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import {
createFederation,
generateCryptoKeyPair,
MemoryKvStore,
} from "@fedify/fedify";
import {
Accept,
Endpoints,
Follow,
Note,
Person,
type Recipient,
Undo,
} from "@fedify/vocab";
import { keyPairsStore, relationStore } from "./store";
const federation = createFederation<void>({
kv: new MemoryKvStore(),
});
const IDENTIFIER = "demo";
federation.setNodeInfoDispatcher("/nodeinfo/2.1", (_ctx) => {
return {
software: {
name: "fedify-solidstart",
version: "0.0.1",
},
protocols: ["activitypub"],
usage: {
users: { total: 1, activeHalfyear: 1, activeMonth: 1 },
localPosts: 0,
localComments: 0,
},
};
});
federation
.setActorDispatcher("/users/{identifier}", async (context, identifier) => {
if (identifier !== IDENTIFIER) {
return null;
}
const keyPairs = await context.getActorKeyPairs(identifier);
return new Person({
id: context.getActorUri(identifier),
name: "Fedify Demo",
summary: "This is a Fedify Demo account on SolidStart.",
preferredUsername: identifier,
url: new URL("/", context.url),
inbox: context.getInboxUri(identifier),
endpoints: new Endpoints({ sharedInbox: context.getInboxUri() }),
publicKey: keyPairs[0].cryptographicKey,
assertionMethods: keyPairs.map((keyPair) => keyPair.multikey),
});
})
.setKeyPairsDispatcher(async (_, identifier) => {
if (identifier !== IDENTIFIER) {
return [];
}
const keyPairs = keyPairsStore.get(identifier);
if (keyPairs) {
return keyPairs;
}
const { privateKey, publicKey } = await generateCryptoKeyPair();
keyPairsStore.set(identifier, [{ privateKey, publicKey }]);
return [{ privateKey, publicKey }];
});
federation
.setInboxListeners("/users/{identifier}/inbox", "/inbox")
.on(Follow, async (context, follow) => {
if (
follow.id == null ||
follow.actorId == null ||
follow.objectId == null
) {
return;
}
const result = context.parseUri(follow.objectId);
if (result?.type !== "actor" || result.identifier !== IDENTIFIER) {
return;
}
const follower = (await follow.getActor(context)) as Person;
if (!follower?.id || follower.id === null) {
throw new Error("follower is null");
}
await context.sendActivity(
{ identifier: result.identifier },
follower,
new Accept({
id: new URL(
`#accepts/${follower.id.href}`,
context.getActorUri(IDENTIFIER),
),
actor: follow.objectId,
object: follow,
}),
);
relationStore.set(follower.id.href, follower);
})
.on(Undo, async (context, undo) => {
const activity = await undo.getObject(context);
if (activity instanceof Follow) {
if (activity.id == null) {
return;
}
if (undo.actorId == null) {
return;
}
relationStore.delete(undo.actorId.href);
} else {
console.debug(undo);
}
});
federation.setObjectDispatcher(
Note,
"/users/{identifier}/posts/{id}",
(ctx, values) => {
return new Note({
id: ctx.getObjectUri(Note, values),
attribution: ctx.getActorUri(values.identifier),
name: values.id,
});
},
);
federation.setFollowersDispatcher(
"/users/{identifier}/followers",
() => {
const followers = Array.from(relationStore.values());
const items: Recipient[] = followers.map((f) => ({
id: f.id,
inboxId: f.inboxId,
endpoints: f.endpoints,
}));
return { items };
},
);
export default federation;