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