Sharkey/src/server/activitypub/user.ts

41 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-04-01 06:33:26 +03:00
import * as express from 'express';
2018-04-01 09:58:49 +03:00
import config from '../../conf';
2018-04-01 13:18:36 +03:00
import context from '../../common/remote/activitypub/renderer/context';
import render from '../../common/remote/activitypub/renderer/person';
2018-04-01 09:58:49 +03:00
import parseAcct from '../../common/user/parse-acct';
2018-04-01 13:18:36 +03:00
import User from '../../models/user';
const app = express();
2018-04-01 06:43:59 +03:00
app.disable('x-powered-by');
app.get('/@:user', async (req, res, next) => {
const accepted = req.accepts(['html', 'application/activity+json', 'application/ld+json']);
2018-04-01 09:58:49 +03:00
if (!(['application/activity+json', 'application/ld+json'] as Array<any>).includes(accepted)) {
return next();
}
const { username, host } = parseAcct(req.params.user);
if (host !== null) {
2018-04-01 06:33:26 +03:00
return res.sendStatus(422);
}
const user = await User.findOne({
usernameLower: username.toLowerCase(),
host: null
});
if (user === null) {
2018-04-01 06:33:26 +03:00
return res.sendStatus(404);
}
if (username !== user.username) {
2018-04-01 13:18:36 +03:00
return res.redirect(`${config.url}/@${user.username}`);
}
2018-04-01 13:18:36 +03:00
const rendered = render(user);
rendered['@context'] = context;
res.json(rendered);
});
export default app;