Skip to content
Snippets Groups Projects
Commit cba73d6b authored by Akihiko Odaki's avatar Akihiko Odaki
Browse files

Implement account public key endpoint

parent a3cef6e9
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ import { extractPublic } from '../../../../crypto_key';
import { ILocalAccount } from '../../../../models/user';
export default ({ username, account }) => ({
id: `${config.url}/@${username}/publickey`,
type: 'Key',
owner: `${config.url}/@${username}`,
publicKeyPem: extractPublic((account as ILocalAccount).keypair)
......
......@@ -3,6 +3,7 @@ import * as express from 'express';
import user from './user';
import inbox from './inbox';
import outbox from './outbox';
import publicKey from './publickey';
import post from './post';
const app = express();
......@@ -11,6 +12,7 @@ app.disable('x-powered-by');
app.use(user);
app.use(inbox);
app.use(outbox);
app.use(publicKey);
app.use(post);
export default app;
......@@ -2,44 +2,26 @@ import * as express from 'express';
import context from '../../common/remote/activitypub/renderer/context';
import renderNote from '../../common/remote/activitypub/renderer/note';
import renderOrderedCollection from '../../common/remote/activitypub/renderer/ordered-collection';
import parseAcct from '../../common/user/parse-acct';
import config from '../../conf';
import Post from '../../models/post';
import User from '../../models/user';
import withUser from './with-user';
const app = express();
app.disable('x-powered-by');
app.get('/@:user/outbox', async (req, res) => {
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 id = `${config.url}/@${user.username}/inbox`;
if (username !== user.username) {
return res.redirect(id);
}
app.get('/@:user/outbox', withUser(username => {
return `${config.url}/@${username}/inbox`;
}, async (user, req, res) => {
const posts = await Post.find({ userId: user._id }, {
limit: 20,
sort: { _id: -1 }
});
const renderedPosts = await Promise.all(posts.map(post => renderNote(user, post)));
const rendered = renderOrderedCollection(id, user.postsCount, renderedPosts);
const rendered = renderOrderedCollection(`${config.url}/@${user.username}/inbox`, user.postsCount, renderedPosts);
rendered['@context'] = context;
res.json(rendered);
});
}));
export default app;
import * as express from 'express';
import context from '../../common/remote/activitypub/renderer/context';
import render from '../../common/remote/activitypub/renderer/key';
import config from '../../conf';
import withUser from './with-user';
const app = express();
app.disable('x-powered-by');
app.get('/@:user/publickey', withUser(username => {
return `${config.url}/@${username}/publickey`;
}, (user, req, res) => {
const rendered = render(user);
rendered['@context'] = context;
res.json(rendered);
}));
export default app;
......@@ -2,39 +2,26 @@ import * as express from 'express';
import config from '../../conf';
import context from '../../common/remote/activitypub/renderer/context';
import render from '../../common/remote/activitypub/renderer/person';
import parseAcct from '../../common/user/parse-acct';
import User from '../../models/user';
import withUser from './with-user';
const respond = withUser(username => `${config.url}/@${username}`, (user, req, res) => {
const rendered = render(user);
rendered['@context'] = context;
res.json(rendered);
});
const app = express();
app.disable('x-powered-by');
app.get('/@:user', async (req, res, next) => {
app.get('/@:user', (req, res, next) => {
const accepted = req.accepts(['html', 'application/activity+json', 'application/ld+json']);
if (!(['application/activity+json', 'application/ld+json'] as Array<any>).includes(accepted)) {
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);
}
if (username !== user.username) {
return res.redirect(`${config.url}/@${user.username}`);
if ((['application/activity+json', 'application/ld+json'] as Array<any>).includes(accepted)) {
respond(req, res, next);
} else {
next();
}
const rendered = render(user);
rendered['@context'] = context;
res.json(rendered);
});
export default app;
import parseAcct from '../../common/user/parse-acct';
import User from '../../models/user';
export default (redirect, respond) => async (req, res, 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);
}
if (username !== user.username) {
return res.redirect(redirect(user.username));
}
return respond(user, req, res, next);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment