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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
import { Router, Request, Response, NextFunction } from "express";
import { createFeaturedPost, createWebfinger } from "../activitypub.js";
import { acceptsActivityPub } from "../lib/activitypub.js";
import { frontendConfig } from "../lib/config.js";
import Event from "../models/Event.js";
import { addToLog } from "../helpers.js";
import { getConfigMiddleware } from "../lib/middleware.js";
const router = Router();
router.use(getConfigMiddleware);
const send404IfNotFederated = (
req: Request,
res: Response,
next: NextFunction,
) => {
if (!res.locals.config?.general.is_federated) {
return res.status(404).render("404", frontendConfig(res));
}
next();
};
// return the JSON for the featured/pinned post for this event
router.get(
"/:eventID/featured",
send404IfNotFederated,
(req: Request, res: Response) => {
const { eventID } = req.params;
const featured = {
"@context": "https://www.w3.org/ns/activitystreams",
id: `https://${res.locals.config?.general.domain}/${eventID}/featured`,
type: "OrderedCollection",
orderedItems: [createFeaturedPost(eventID)],
};
if (acceptsActivityPub(req)) {
res.header("Content-Type", "application/activity+json").send(
featured,
);
} else {
res.header("Content-Type", "application/json").send(featured);
}
},
);
// return the JSON for a given activitypub message
router.get(
"/:eventID/m/:hash",
send404IfNotFederated,
async (req: Request, res: Response) => {
const { hash, eventID } = req.params;
const id = `https://${res.locals.config?.general.domain}/${eventID}/m/${hash}`;
try {
const event = await Event.findOne({
id: eventID,
});
if (!event) {
return res.status(404).render("404", frontendConfig(res));
} else {
if (!event.activityPubMessages) {
return res.status(404).render("404", frontendConfig(res));
}
const message = event.activityPubMessages.find(
(el) => el.id === id,
);
if (message) {
if (acceptsActivityPub(req)) {
res.header(
"Content-Type",
"application/activity+json",
).send(JSON.parse(message.content || "{}"));
} else {
res.header("Content-Type", "application/json").send(
JSON.parse(message.content || "{}"),
);
}
} else {
return res.status(404).render("404", frontendConfig(res));
}
}
} catch (err) {
addToLog(
"getActivityPubMessage",
"error",
"Attempt to get Activity Pub Message for " +
id +
" failed with error: " +
err,
);
return res.status(404).render("404", frontendConfig(res));
}
},
);
router.get("/.well-known/nodeinfo", send404IfNotFederated, (req, res) => {
if (!res.locals.config?.general.is_federated) {
return res.status(404).render("404", frontendConfig(res));
}
const nodeInfo = {
links: [
{
rel: "http://nodeinfo.diaspora.software/ns/schema/2.2",
href: `https://${res.locals.config?.general.domain}/.well-known/nodeinfo/2.2`,
},
],
};
res.header(
"Content-Type",
'application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.1#"',
).send(nodeInfo);
});
router.get(
"/.well-known/nodeinfo/2.2",
send404IfNotFederated,
async (req, res) => {
const eventCount = await Event.countDocuments();
if (!res.locals.config?.general.is_federated) {
return res.status(404).render("404", frontendConfig(res));
}
const nodeInfo = {
version: "2.2",
instance: {
name: res.locals.config?.general.site_name,
description:
"Federated, no-registration, privacy-respecting event hosting.",
},
software: {
name: "Gathio",
version: process.env.npm_package_version || "unknown",
repository: "https://github.com/lowercasename/gathio",
homepage: "https://gath.io",
},
protocols: ["activitypub"],
services: {
inbound: [],
outbound: [],
},
openRegistrations: true,
usage: {
users: {
total: eventCount,
},
},
};
res.header(
"Content-Type",
'application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.1#"',
).send(nodeInfo);
},
);
router.get(
"/.well-known/webfinger",
send404IfNotFederated,
async (req, res) => {
let resource = req.query.resource as string;
if (!resource || !resource.includes("acct:")) {
return res
.status(400)
.send(
'Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.',
);
} else {
// "foo@domain"
let activityPubAccount = resource.replace("acct:", "");
// "foo"
let eventID = activityPubAccount.replace(/@.*/, "");
try {
const event = await Event.findOne({ id: eventID });
if (!event) {
return res.status(404).render("404", frontendConfig(res));
} else {
if (acceptsActivityPub(req)) {
res.header(
"Content-Type",
"application/activity+json",
).send(
createWebfinger(
eventID,
res.locals.config?.general.domain,
),
);
} else {
res.header("Content-Type", "application/json").send(
createWebfinger(
eventID,
res.locals.config?.general.domain,
),
);
}
}
} catch (err) {
addToLog(
"renderWebfinger",
"error",
`Attempt to render webfinger for ${resource} failed with error: ${err}`,
);
return res.status(404).render("404", frontendConfig(res));
}
}
},
);
router.get("/:eventID/followers", send404IfNotFederated, async (req, res) => {
const eventID = req.params.eventID;
try {
const event = await Event.findOne({ id: eventID });
if (event && event.followers) {
const followers = event.followers.map((el) => el.actorId);
let followersCollection = {
type: "OrderedCollection",
totalItems: followers.length,
id: `https://${res.locals.config?.general.domain}/${eventID}/followers`,
first: {
type: "OrderedCollectionPage",
totalItems: followers.length,
partOf: `https://${res.locals.config?.general.domain}/${eventID}/followers`,
orderedItems: followers,
id: `https://${res.locals.config?.general.domain}/${eventID}/followers?page=1`,
},
"@context": ["https://www.w3.org/ns/activitystreams"],
};
if (acceptsActivityPub(req)) {
return res
.header("Content-Type", "application/activity+json")
.send(followersCollection);
} else {
return res
.header("Content-Type", "application/json")
.send(followersCollection);
}
} else {
return res.status(400).send("Bad request.");
}
} catch (err) {
addToLog(
"renderFollowers",
"error",
`Attempt to render followers for ${eventID} failed with error: ${err}`,
);
return res.status(404).render("404", frontendConfig(res));
}
});
export default router;
|