From e63f884bc6ba89902e2efd20f1c6d8939f7c4270 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 8 Apr 2018 15:15:22 +0900 Subject: [PATCH] Use id in uri instead of username --- src/remote/activitypub/act/follow.ts | 16 +++++++--------- src/remote/activitypub/act/undo/follow.ts | 18 ++++++++---------- src/remote/activitypub/renderer/follow.ts | 2 +- src/remote/activitypub/renderer/key.ts | 4 ++-- src/remote/activitypub/renderer/like.ts | 12 +++++------- src/remote/activitypub/renderer/note.ts | 2 +- src/remote/activitypub/renderer/person.ts | 2 +- src/remote/activitypub/resolve-person.ts | 6 ++---- src/remote/activitypub/resolver.ts | 2 +- src/server/activitypub/inbox.ts | 2 +- src/server/activitypub/outbox.ts | 14 ++++++++------ src/server/activitypub/publickey.ts | 13 +++++++------ src/server/activitypub/user.ts | 23 ++++++++--------------- src/server/activitypub/with-user.ts | 23 ----------------------- 14 files changed, 52 insertions(+), 87 deletions(-) delete mode 100644 src/server/activitypub/with-user.ts diff --git a/src/remote/activitypub/act/follow.ts b/src/remote/activitypub/act/follow.ts index 236886dc6..6a8b5a1be 100644 --- a/src/remote/activitypub/act/follow.ts +++ b/src/remote/activitypub/act/follow.ts @@ -1,25 +1,23 @@ -import parseAcct from '../../../acct/parse'; import User, { IRemoteUser } from '../../../models/user'; import config from '../../../config'; import follow from '../../../services/following/create'; import { IFollow } from '../type'; export default async (actor: IRemoteUser, activity: IFollow): Promise => { - const prefix = config.url + '/@'; const id = typeof activity.object == 'string' ? activity.object : activity.object.id; - if (!id.startsWith(prefix)) { + if (!id.startsWith(config.url + '/')) { return null; } - const { username, host } = parseAcct(id.slice(prefix.length)); - if (host !== null) { - throw new Error(); + const followee = await User.findOne({ _id: id.split('/').pop() }); + + if (followee === null) { + throw new Error('followee not found'); } - const followee = await User.findOne({ username, host }); - if (followee === null) { - throw new Error(); + if (followee.host != null) { + throw new Error('フォローしようとしているユーザーはローカルユーザーではありません'); } await follow(actor, followee, activity); diff --git a/src/remote/activitypub/act/undo/follow.ts b/src/remote/activitypub/act/undo/follow.ts index fcf27c950..a85cb0305 100644 --- a/src/remote/activitypub/act/undo/follow.ts +++ b/src/remote/activitypub/act/undo/follow.ts @@ -1,25 +1,23 @@ -import parseAcct from '../../../../acct/parse'; import User, { IRemoteUser } from '../../../../models/user'; import config from '../../../../config'; import unfollow from '../../../../services/following/delete'; import { IFollow } from '../../type'; export default async (actor: IRemoteUser, activity: IFollow): Promise => { - const prefix = config.url + '/@'; - const id = typeof activity == 'string' ? activity : activity.id; + const id = typeof activity.object == 'string' ? activity.object : activity.object.id; - if (!id.startsWith(prefix)) { + if (!id.startsWith(config.url + '/')) { return null; } - const { username, host } = parseAcct(id.slice(prefix.length)); - if (host !== null) { - throw new Error(); + const followee = await User.findOne({ _id: id.split('/').pop() }); + + if (followee === null) { + throw new Error('followee not found'); } - const followee = await User.findOne({ username, host }); - if (followee === null) { - throw new Error(); + if (followee.host != null) { + throw new Error('フォロー解除しようとしているユーザーはローカルユーザーではありません'); } await unfollow(actor, followee, activity); diff --git a/src/remote/activitypub/renderer/follow.ts b/src/remote/activitypub/renderer/follow.ts index 89993d945..bf8eeff06 100644 --- a/src/remote/activitypub/renderer/follow.ts +++ b/src/remote/activitypub/renderer/follow.ts @@ -3,6 +3,6 @@ import { IRemoteUser, ILocalUser } from '../../../models/user'; export default (follower: ILocalUser, followee: IRemoteUser) => ({ type: 'Follow', - actor: `${config.url}/@${follower.username}`, + actor: `${config.url}/users/${follower._id}`, object: followee.uri }); diff --git a/src/remote/activitypub/renderer/key.ts b/src/remote/activitypub/renderer/key.ts index 76e2f13bc..0d5e52557 100644 --- a/src/remote/activitypub/renderer/key.ts +++ b/src/remote/activitypub/renderer/key.ts @@ -3,8 +3,8 @@ import { extractPublic } from '../../../crypto_key'; import { ILocalUser } from '../../../models/user'; export default (user: ILocalUser) => ({ - id: `${config.url}/@${user.username}/publickey`, + id: `${config.url}/users/${user._id}/publickey`, type: 'Key', - owner: `${config.url}/@${user.username}`, + owner: `${config.url}/users/${user._id}`, publicKeyPem: extractPublic(user.keypair) }); diff --git a/src/remote/activitypub/renderer/like.ts b/src/remote/activitypub/renderer/like.ts index 744896cc4..061a10ba8 100644 --- a/src/remote/activitypub/renderer/like.ts +++ b/src/remote/activitypub/renderer/like.ts @@ -1,10 +1,8 @@ import config from '../../../config'; import { ILocalUser } from '../../../models/user'; -export default (user: ILocalUser, note) => { - return { - type: 'Like', - actor: `${config.url}/@${user.username}`, - object: note.uri ? note.uri : `${config.url}/notes/${note._id}` - }; -}; +export default (user: ILocalUser, note) => ({ + type: 'Like', + actor: `${config.url}/users/${user._id}`, + object: note.uri ? note.uri : `${config.url}/notes/${note._id}` +}); diff --git a/src/remote/activitypub/renderer/note.ts b/src/remote/activitypub/renderer/note.ts index 48799af08..7cc388dc3 100644 --- a/src/remote/activitypub/renderer/note.ts +++ b/src/remote/activitypub/renderer/note.ts @@ -34,7 +34,7 @@ export default async (note: INote) => { _id: note.userId }); - const attributedTo = `${config.url}/@${user.username}`; + const attributedTo = `${config.url}/users/${user._id}`; return { id: `${config.url}/notes/${note._id}`, diff --git a/src/remote/activitypub/renderer/person.ts b/src/remote/activitypub/renderer/person.ts index 7ea6f532f..82e261029 100644 --- a/src/remote/activitypub/renderer/person.ts +++ b/src/remote/activitypub/renderer/person.ts @@ -3,7 +3,7 @@ import renderKey from './key'; import config from '../../../config'; export default user => { - const id = `${config.url}/@${user.username}`; + const id = `${config.url}/users/${user._id}`; return { type: 'Person', diff --git a/src/remote/activitypub/resolve-person.ts b/src/remote/activitypub/resolve-person.ts index 7d7989a01..0140811f0 100644 --- a/src/remote/activitypub/resolve-person.ts +++ b/src/remote/activitypub/resolve-person.ts @@ -1,6 +1,5 @@ import { JSDOM } from 'jsdom'; import { toUnicode } from 'punycode'; -import parseAcct from '../../acct/parse'; import config from '../../config'; import User, { validateUsername, isValidName, isValidDescription, IUser } from '../../models/user'; import webFinger from '../webfinger'; @@ -10,10 +9,9 @@ import { isCollectionOrOrderedCollection, IObject } from './type'; export default async (value: string | IObject, verifier?: string): Promise => { const id = typeof value == 'string' ? value : value.id; - const localPrefix = config.url + '/@'; - if (id.startsWith(localPrefix)) { - return await User.findOne(parseAcct(id.substr(localPrefix.length))); + if (id.startsWith(config.url + '/')) { + return await User.findOne({ _id: id.split('/').pop() }); } const resolver = new Resolver(); diff --git a/src/remote/activitypub/resolver.ts b/src/remote/activitypub/resolver.ts index 1466139c4..7d45783b4 100644 --- a/src/remote/activitypub/resolver.ts +++ b/src/remote/activitypub/resolver.ts @@ -50,7 +50,7 @@ export default class Resolver { //#region resolve local objects // TODO - //if (value.startsWith(`${config.url}/@`)) { + //if (value.startsWith(`${config.url}/`)) { //#endregion const object = await request({ diff --git a/src/server/activitypub/inbox.ts b/src/server/activitypub/inbox.ts index 1b6cc0c00..643d2945b 100644 --- a/src/server/activitypub/inbox.ts +++ b/src/server/activitypub/inbox.ts @@ -5,7 +5,7 @@ import { createHttp } from '../../queue'; const app = express.Router(); -app.post('/@:user/inbox', bodyParser.json({ +app.post('/users/:user/inbox', bodyParser.json({ type() { return true; } diff --git a/src/server/activitypub/outbox.ts b/src/server/activitypub/outbox.ts index 4557871bc..1c97c17a2 100644 --- a/src/server/activitypub/outbox.ts +++ b/src/server/activitypub/outbox.ts @@ -4,23 +4,25 @@ import renderNote from '../../remote/activitypub/renderer/note'; import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection'; import config from '../../config'; import Note from '../../models/note'; -import withUser from './with-user'; +import User from '../../models/user'; const app = express.Router(); -app.get('/@:user/outbox', withUser(username => { - return `${config.url}/@${username}/inbox`; -}, async (user, req, res) => { +app.get('/users/:user/outbox', async (req, res) => { + const userId = req.params.user; + + const user = await User.findOne({ _id: userId }); + const notes = await Note.find({ userId: user._id }, { limit: 20, sort: { _id: -1 } }); 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; res.json(rendered); -})); +}); export default app; diff --git a/src/server/activitypub/publickey.ts b/src/server/activitypub/publickey.ts index b48504927..aa0c4271b 100644 --- a/src/server/activitypub/publickey.ts +++ b/src/server/activitypub/publickey.ts @@ -1,18 +1,19 @@ import * as express from 'express'; import context from '../../remote/activitypub/renderer/context'; import render from '../../remote/activitypub/renderer/key'; -import config from '../../config'; -import withUser from './with-user'; +import User from '../../models/user'; const app = express.Router(); -app.get('/@:user/publickey', withUser(username => { - return `${config.url}/@${username}/publickey`; -}, (user, req, res) => { +app.get('/users/:user/publickey', async (req, res) => { + const userId = req.params.user; + + const user = await User.findOne({ _id: userId }); + const rendered = render(user); rendered['@context'] = context; res.json(rendered); -})); +}); export default app; diff --git a/src/server/activitypub/user.ts b/src/server/activitypub/user.ts index f05497451..9e98e92b6 100644 --- a/src/server/activitypub/user.ts +++ b/src/server/activitypub/user.ts @@ -1,26 +1,19 @@ import * as express from 'express'; -import config from '../../config'; import context from '../../remote/activitypub/renderer/context'; 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); rendered['@context'] = context; 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; diff --git a/src/server/activitypub/with-user.ts b/src/server/activitypub/with-user.ts deleted file mode 100644 index bdbbefb42..000000000 --- a/src/server/activitypub/with-user.ts +++ /dev/null @@ -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); -};