2018-04-01 03:33:26 +00:00
|
|
|
import * as express from 'express';
|
2018-04-01 06:58:49 +00:00
|
|
|
import config from '../../conf';
|
|
|
|
import { extractPublic } from '../../crypto_key';
|
2018-04-01 09:12:51 +00:00
|
|
|
import context from '../../common/remote/activitypub/context';
|
2018-04-01 06:58:49 +00:00
|
|
|
import parseAcct from '../../common/user/parse-acct';
|
|
|
|
import User, { ILocalAccount } from '../../models/user';
|
2018-04-01 03:24:29 +00:00
|
|
|
|
|
|
|
const app = express();
|
2018-04-01 03:43:59 +00:00
|
|
|
app.disable('x-powered-by');
|
2018-04-01 03:24:29 +00:00
|
|
|
|
|
|
|
app.get('/@:user', async (req, res, next) => {
|
|
|
|
const accepted = req.accepts(['html', 'application/activity+json', 'application/ld+json']);
|
2018-04-01 06:58:49 +00:00
|
|
|
if (!(['application/activity+json', 'application/ld+json'] as Array<any>).includes(accepted)) {
|
2018-04-01 03:24:29 +00:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { username, host } = parseAcct(req.params.user);
|
|
|
|
if (host !== null) {
|
2018-04-01 03:33:26 +00:00
|
|
|
return res.sendStatus(422);
|
2018-04-01 03:24:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const user = await User.findOne({
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: null
|
|
|
|
});
|
|
|
|
if (user === null) {
|
2018-04-01 03:33:26 +00:00
|
|
|
return res.sendStatus(404);
|
2018-04-01 03:24:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const id = `${config.url}/@${user.username}`;
|
|
|
|
|
|
|
|
if (username !== user.username) {
|
|
|
|
return res.redirect(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
2018-04-01 09:12:51 +00:00
|
|
|
'@context': context,
|
2018-04-01 03:24:29 +00:00
|
|
|
type: 'Person',
|
|
|
|
id,
|
2018-04-01 06:58:49 +00:00
|
|
|
inbox: `${id}/inbox`,
|
2018-04-01 03:24:29 +00:00
|
|
|
preferredUsername: user.username,
|
|
|
|
name: user.name,
|
|
|
|
summary: user.description,
|
|
|
|
icon: user.avatarId && {
|
|
|
|
type: 'Image',
|
|
|
|
url: `${config.drive_url}/${user.avatarId}`
|
|
|
|
},
|
|
|
|
image: user.bannerId && {
|
|
|
|
type: 'Image',
|
|
|
|
url: `${config.drive_url}/${user.bannerId}`
|
|
|
|
},
|
|
|
|
publicKey: {
|
|
|
|
type: 'Key',
|
|
|
|
owner: id,
|
|
|
|
publicKeyPem: extractPublic((user.account as ILocalAccount).keypair)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
export default app;
|