Use id in uri instead of username

This commit is contained in:
syuilo 2018-04-08 15:15:22 +09:00
parent 1fced8a59b
commit e63f884bc6
14 changed files with 52 additions and 87 deletions

View file

@ -1,25 +1,23 @@
import parseAcct from '../../../acct/parse';
import User, { IRemoteUser } from '../../../models/user'; import User, { IRemoteUser } from '../../../models/user';
import config from '../../../config'; import config from '../../../config';
import follow from '../../../services/following/create'; import follow from '../../../services/following/create';
import { IFollow } from '../type'; import { IFollow } from '../type';
export default async (actor: IRemoteUser, activity: IFollow): Promise<void> => { export default async (actor: IRemoteUser, activity: IFollow): Promise<void> => {
const prefix = config.url + '/@';
const id = typeof activity.object == 'string' ? activity.object : activity.object.id; const id = typeof activity.object == 'string' ? activity.object : activity.object.id;
if (!id.startsWith(prefix)) { if (!id.startsWith(config.url + '/')) {
return null; return null;
} }
const { username, host } = parseAcct(id.slice(prefix.length)); const followee = await User.findOne({ _id: id.split('/').pop() });
if (host !== null) {
throw new Error(); if (followee === null) {
throw new Error('followee not found');
} }
const followee = await User.findOne({ username, host }); if (followee.host != null) {
if (followee === null) { throw new Error('フォローしようとしているユーザーはローカルユーザーではありません');
throw new Error();
} }
await follow(actor, followee, activity); await follow(actor, followee, activity);

View file

@ -1,25 +1,23 @@
import parseAcct from '../../../../acct/parse';
import User, { IRemoteUser } from '../../../../models/user'; import User, { IRemoteUser } from '../../../../models/user';
import config from '../../../../config'; import config from '../../../../config';
import unfollow from '../../../../services/following/delete'; import unfollow from '../../../../services/following/delete';
import { IFollow } from '../../type'; import { IFollow } from '../../type';
export default async (actor: IRemoteUser, activity: IFollow): Promise<void> => { export default async (actor: IRemoteUser, activity: IFollow): Promise<void> => {
const prefix = config.url + '/@'; const id = typeof activity.object == 'string' ? activity.object : activity.object.id;
const id = typeof activity == 'string' ? activity : activity.id;
if (!id.startsWith(prefix)) { if (!id.startsWith(config.url + '/')) {
return null; return null;
} }
const { username, host } = parseAcct(id.slice(prefix.length)); const followee = await User.findOne({ _id: id.split('/').pop() });
if (host !== null) {
throw new Error(); if (followee === null) {
throw new Error('followee not found');
} }
const followee = await User.findOne({ username, host }); if (followee.host != null) {
if (followee === null) { throw new Error('フォロー解除しようとしているユーザーはローカルユーザーではありません');
throw new Error();
} }
await unfollow(actor, followee, activity); await unfollow(actor, followee, activity);

View file

@ -3,6 +3,6 @@ import { IRemoteUser, ILocalUser } from '../../../models/user';
export default (follower: ILocalUser, followee: IRemoteUser) => ({ export default (follower: ILocalUser, followee: IRemoteUser) => ({
type: 'Follow', type: 'Follow',
actor: `${config.url}/@${follower.username}`, actor: `${config.url}/users/${follower._id}`,
object: followee.uri object: followee.uri
}); });

View file

@ -3,8 +3,8 @@ import { extractPublic } from '../../../crypto_key';
import { ILocalUser } from '../../../models/user'; import { ILocalUser } from '../../../models/user';
export default (user: ILocalUser) => ({ export default (user: ILocalUser) => ({
id: `${config.url}/@${user.username}/publickey`, id: `${config.url}/users/${user._id}/publickey`,
type: 'Key', type: 'Key',
owner: `${config.url}/@${user.username}`, owner: `${config.url}/users/${user._id}`,
publicKeyPem: extractPublic(user.keypair) publicKeyPem: extractPublic(user.keypair)
}); });

View file

@ -1,10 +1,8 @@
import config from '../../../config'; import config from '../../../config';
import { ILocalUser } from '../../../models/user'; import { ILocalUser } from '../../../models/user';
export default (user: ILocalUser, note) => { export default (user: ILocalUser, note) => ({
return { type: 'Like',
type: 'Like', actor: `${config.url}/users/${user._id}`,
actor: `${config.url}/@${user.username}`, object: note.uri ? note.uri : `${config.url}/notes/${note._id}`
object: note.uri ? note.uri : `${config.url}/notes/${note._id}` });
};
};

View file

@ -34,7 +34,7 @@ export default async (note: INote) => {
_id: note.userId _id: note.userId
}); });
const attributedTo = `${config.url}/@${user.username}`; const attributedTo = `${config.url}/users/${user._id}`;
return { return {
id: `${config.url}/notes/${note._id}`, id: `${config.url}/notes/${note._id}`,

View file

@ -3,7 +3,7 @@ import renderKey from './key';
import config from '../../../config'; import config from '../../../config';
export default user => { export default user => {
const id = `${config.url}/@${user.username}`; const id = `${config.url}/users/${user._id}`;
return { return {
type: 'Person', type: 'Person',

View file

@ -1,6 +1,5 @@
import { JSDOM } from 'jsdom'; import { JSDOM } from 'jsdom';
import { toUnicode } from 'punycode'; import { toUnicode } from 'punycode';
import parseAcct from '../../acct/parse';
import config from '../../config'; import config from '../../config';
import User, { validateUsername, isValidName, isValidDescription, IUser } from '../../models/user'; import User, { validateUsername, isValidName, isValidDescription, IUser } from '../../models/user';
import webFinger from '../webfinger'; import webFinger from '../webfinger';
@ -10,10 +9,9 @@ import { isCollectionOrOrderedCollection, IObject } from './type';
export default async (value: string | IObject, verifier?: string): Promise<IUser> => { export default async (value: string | IObject, verifier?: string): Promise<IUser> => {
const id = typeof value == 'string' ? value : value.id; const id = typeof value == 'string' ? value : value.id;
const localPrefix = config.url + '/@';
if (id.startsWith(localPrefix)) { if (id.startsWith(config.url + '/')) {
return await User.findOne(parseAcct(id.substr(localPrefix.length))); return await User.findOne({ _id: id.split('/').pop() });
} }
const resolver = new Resolver(); const resolver = new Resolver();

View file

@ -50,7 +50,7 @@ export default class Resolver {
//#region resolve local objects //#region resolve local objects
// TODO // TODO
//if (value.startsWith(`${config.url}/@`)) { //if (value.startsWith(`${config.url}/`)) {
//#endregion //#endregion
const object = await request({ const object = await request({

View file

@ -5,7 +5,7 @@ import { createHttp } from '../../queue';
const app = express.Router(); const app = express.Router();
app.post('/@:user/inbox', bodyParser.json({ app.post('/users/:user/inbox', bodyParser.json({
type() { type() {
return true; return true;
} }

View file

@ -4,23 +4,25 @@ import renderNote from '../../remote/activitypub/renderer/note';
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection'; import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
import config from '../../config'; import config from '../../config';
import Note from '../../models/note'; import Note from '../../models/note';
import withUser from './with-user'; import User from '../../models/user';
const app = express.Router(); const app = express.Router();
app.get('/@:user/outbox', withUser(username => { app.get('/users/:user/outbox', async (req, res) => {
return `${config.url}/@${username}/inbox`; const userId = req.params.user;
}, async (user, req, res) => {
const user = await User.findOne({ _id: userId });
const notes = await Note.find({ userId: user._id }, { const notes = await Note.find({ userId: user._id }, {
limit: 20, limit: 20,
sort: { _id: -1 } sort: { _id: -1 }
}); });
const renderedNotes = await Promise.all(notes.map(note => renderNote(note))); const renderedNotes = await Promise.all(notes.map(note => renderNote(note)));
const rendered = renderOrderedCollection(`${config.url}/@${user.username}/inbox`, user.notesCount, renderedNotes); const rendered = renderOrderedCollection(`${config.url}/users/${userId}/inbox`, user.notesCount, renderedNotes);
rendered['@context'] = context; rendered['@context'] = context;
res.json(rendered); res.json(rendered);
})); });
export default app; export default app;

View file

@ -1,18 +1,19 @@
import * as express from 'express'; import * as express from 'express';
import context from '../../remote/activitypub/renderer/context'; import context from '../../remote/activitypub/renderer/context';
import render from '../../remote/activitypub/renderer/key'; import render from '../../remote/activitypub/renderer/key';
import config from '../../config'; import User from '../../models/user';
import withUser from './with-user';
const app = express.Router(); const app = express.Router();
app.get('/@:user/publickey', withUser(username => { app.get('/users/:user/publickey', async (req, res) => {
return `${config.url}/@${username}/publickey`; const userId = req.params.user;
}, (user, req, res) => {
const user = await User.findOne({ _id: userId });
const rendered = render(user); const rendered = render(user);
rendered['@context'] = context; rendered['@context'] = context;
res.json(rendered); res.json(rendered);
})); });
export default app; export default app;

View file

@ -1,26 +1,19 @@
import * as express from 'express'; import * as express from 'express';
import config from '../../config';
import context from '../../remote/activitypub/renderer/context'; import context from '../../remote/activitypub/renderer/context';
import render from '../../remote/activitypub/renderer/person'; import render from '../../remote/activitypub/renderer/person';
import withUser from './with-user'; import User from '../../models/user';
const app = express.Router();
app.get('/users/:user', async (req, res) => {
const userId = req.params.user;
const user = await User.findOne({ _id: userId });
const respond = withUser(username => `${config.url}/@${username}`, (user, req, res) => {
const rendered = render(user); const rendered = render(user);
rendered['@context'] = context; rendered['@context'] = context;
res.json(rendered); res.json(rendered);
}); });
const app = express.Router();
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 any[]).includes(accepted)) {
respond(req, res, next);
} else {
next();
}
});
export default app; export default app;

View file

@ -1,23 +0,0 @@
import parseAcct from '../../acct/parse';
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);
};