2018-04-01 09:12:51 +00:00
|
|
|
import * as express from 'express';
|
2018-04-01 10:18:36 +00:00
|
|
|
import context from '../../common/remote/activitypub/renderer/context';
|
|
|
|
import render from '../../common/remote/activitypub/renderer/note';
|
2018-04-01 09:12:51 +00:00
|
|
|
import parseAcct from '../../common/user/parse-acct';
|
|
|
|
import Post from '../../models/post';
|
|
|
|
import User from '../../models/user';
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
|
|
|
app.get('/@:user/:post', async (req, res, next) => {
|
|
|
|
const accepted = req.accepts(['html', 'application/activity+json', 'application/ld+json']);
|
2018-04-01 09:20:17 +00:00
|
|
|
if (!(['application/activity+json', 'application/ld+json'] as any[]).includes(accepted)) {
|
2018-04-01 09:12:51 +00:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { username, host } = parseAcct(req.params.user);
|
|
|
|
if (host !== null) {
|
|
|
|
return res.sendStatus(422);
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await User.findOne({
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: null
|
|
|
|
});
|
|
|
|
if (user === null) {
|
|
|
|
return res.sendStatus(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
const post = await Post.findOne({
|
|
|
|
_id: req.params.post,
|
|
|
|
userId: user._id
|
|
|
|
});
|
|
|
|
if (post === null) {
|
|
|
|
return res.sendStatus(404);
|
|
|
|
}
|
|
|
|
|
2018-04-01 10:18:36 +00:00
|
|
|
const rendered = await render(user, post);
|
|
|
|
rendered['@context'] = context;
|
2018-04-01 09:12:51 +00:00
|
|
|
|
2018-04-01 10:18:36 +00:00
|
|
|
res.json(rendered);
|
2018-04-01 09:12:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default app;
|