fix: move all actor object urls to immutable variants

This commit is contained in:
Julian Lam
2024-01-29 16:59:13 -05:00
parent 457bfe1685
commit 86a607ce8c
5 changed files with 27 additions and 25 deletions

View File

@@ -94,9 +94,15 @@ Helpers.resolveLocalUid = async (input) => {
const { host, pathname } = new URL(input); const { host, pathname } = new URL(input);
if (host === nconf.get('url_parsed').host) { if (host === nconf.get('url_parsed').host) {
return pathname.replace(nconf.get('relative_path'), '').split('/').filter(Boolean)[1]; const [type, value] = pathname.replace(nconf.get('relative_path'), '').split('/').filter(Boolean)[1];
if (type === 'uid') {
return value;
}
slug = value;
} else {
throw new Error('[[error:activitypub.invalid-id]]');
} }
throw new Error('[[error:activitypub.invalid-id]]');
} else if (input.indexOf('@') !== -1) { // Webfinger } else if (input.indexOf('@') !== -1) { // Webfinger
([slug] = input.replace(/^acct:/, '').split('@')); ([slug] = input.replace(/^acct:/, '').split('@'));
} else { } else {

View File

@@ -87,8 +87,7 @@ ActivityPub.sign = async (uid, url, payload) => {
const { host, pathname } = new URL(url); const { host, pathname } = new URL(url);
const date = new Date().toUTCString(); const date = new Date().toUTCString();
const key = await ActivityPub.getPrivateKey(uid); const key = await ActivityPub.getPrivateKey(uid);
const userslug = await user.getUserField(uid, 'userslug'); const keyId = `${nconf.get('url')}${uid > 0 ? `/uid/${uid}` : '/actor'}#key`;
const keyId = `${nconf.get('url')}${uid > 0 ? `/user/${userslug}` : '/actor'}#key`;
let digest = null; let digest = null;
let headers = '(request-target) host date'; let headers = '(request-target) host date';

View File

@@ -137,10 +137,10 @@ Mocks.actor = async (uid) => {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
id: `${nconf.get('url')}/uid/${uid}`, id: `${nconf.get('url')}/uid/${uid}`,
url: `${nconf.get('url')}/user/${userslug}`, url: `${nconf.get('url')}/user/${userslug}`,
followers: `${nconf.get('url')}/user/${userslug}/followers`, followers: `${nconf.get('url')}/uid/${uid}/followers`,
following: `${nconf.get('url')}/user/${userslug}/following`, following: `${nconf.get('url')}/uid/${uid}/following`,
inbox: `${nconf.get('url')}/user/${userslug}/inbox`, inbox: `${nconf.get('url')}/uid/${uid}/inbox`,
outbox: `${nconf.get('url')}/user/${userslug}/outbox`, outbox: `${nconf.get('url')}/uid/${uid}/outbox`,
type: 'Person', type: 'Person',
name, name,
@@ -150,8 +150,8 @@ Mocks.actor = async (uid) => {
image: cover, image: cover,
publicKey: { publicKey: {
id: `${nconf.get('url')}/user/${userslug}#key`, id: `${nconf.get('url')}/uid/${uid}#key`,
owner: `${nconf.get('url')}/user/${userslug}`, owner: `${nconf.get('url')}/uid/${uid}`,
publicKeyPem: publicKey, publicKeyPem: publicKey,
}, },
}; };
@@ -161,14 +161,11 @@ Mocks.note = async (post) => {
const id = `${nconf.get('url')}/post/${post.pid}`; const id = `${nconf.get('url')}/post/${post.pid}`;
const published = new Date(parseInt(post.timestamp, 10)).toISOString(); const published = new Date(parseInt(post.timestamp, 10)).toISOString();
const [raw, userslug] = await Promise.all([ const raw = await posts.getPostField(post.pid, 'content');
posts.getPostField(post.pid, 'content'),
user.getUserField(post.user.uid, 'userslug'),
]);
// todo: post visibility, category privileges integration // todo: post visibility, category privileges integration
const to = [activitypub._constants.publicAddress]; const to = [activitypub._constants.publicAddress];
const cc = [`${nconf.get('url')}/user/${userslug}/followers`]; const cc = [`${nconf.get('url')}/uid/${post.user.uid}/followers`];
let inReplyTo = null; let inReplyTo = null;
let name = null; let name = null;

View File

@@ -11,14 +11,14 @@ Controller.actors = require('./actors');
Controller.topics = require('./topics'); Controller.topics = require('./topics');
Controller.getFollowing = async (req, res) => { Controller.getFollowing = async (req, res) => {
const { followingCount: totalItems } = await user.getUserFields(res.locals.uid, ['followingCount']); const { followingCount: totalItems } = await user.getUserFields(req.params.uid, ['followingCount']);
const page = parseInt(req.query.page, 10) || 1; const page = parseInt(req.query.page, 10) || 1;
const resultsPerPage = 50; const resultsPerPage = 50;
const start = Math.max(0, page - 1) * resultsPerPage; const start = Math.max(0, page - 1) * resultsPerPage;
const stop = start + resultsPerPage - 1; const stop = start + resultsPerPage - 1;
let orderedItems = await user.getFollowing(res.locals.uid, start, stop); let orderedItems = await user.getFollowing(req.params.uid, start, stop);
orderedItems = orderedItems.map(({ userslug }) => `${nconf.get('url')}/user/${userslug}`); orderedItems = orderedItems.map(({ userslug }) => `${nconf.get('url')}/user/${userslug}`);
res.status(200).json({ res.status(200).json({
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
@@ -29,14 +29,14 @@ Controller.getFollowing = async (req, res) => {
}; };
Controller.getFollowers = async (req, res) => { Controller.getFollowers = async (req, res) => {
const { followerCount: totalItems } = await user.getUserFields(res.locals.uid, ['followerCount']); const { followerCount: totalItems } = await user.getUserFields(req.params.uid, ['followerCount']);
const page = parseInt(req.query.page, 10) || 1; const page = parseInt(req.query.page, 10) || 1;
const resultsPerPage = 50; const resultsPerPage = 50;
const start = Math.max(0, page - 1) * resultsPerPage; const start = Math.max(0, page - 1) * resultsPerPage;
const stop = start + resultsPerPage - 1; const stop = start + resultsPerPage - 1;
let orderedItems = await user.getFollowers(res.locals.uid, start, stop); let orderedItems = await user.getFollowers(req.params.uid, start, stop);
orderedItems = orderedItems.map(({ userslug }) => `${nconf.get('url')}/user/${userslug}`); orderedItems = orderedItems.map(({ userslug }) => `${nconf.get('url')}/user/${userslug}`);
res.status(200).json({ res.status(200).json({
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',

View File

@@ -18,14 +18,14 @@ module.exports = function (app, middleware, controllers) {
app.get('/uid/:uid', middlewares, controllers.activitypub.actors.user); app.get('/uid/:uid', middlewares, controllers.activitypub.actors.user);
app.get('/user/:userslug', [...middlewares, middleware.exposeUid], controllers.activitypub.actors.userBySlug); app.get('/user/:userslug', [...middlewares, middleware.exposeUid], controllers.activitypub.actors.userBySlug);
app.get('/user/:userslug/inbox', [...middlewares, middleware.exposeUid], controllers.activitypub.getInbox); app.get('/uid/:uid/inbox', middlewares, controllers.activitypub.getInbox);
app.post('/user/:userslug/inbox', [...middlewares, middleware.activitypub.validate, middleware.exposeUid], controllers.activitypub.postInbox); app.post('/uid/:uid/inbox', [...middlewares, middleware.activitypub.validate], controllers.activitypub.postInbox);
app.get('/user/:userslug/outbox', [...middlewares, middleware.exposeUid], controllers.activitypub.getOutbox); app.get('/uid/:uid/outbox', middlewares, controllers.activitypub.getOutbox);
app.post('/user/:userslug/outbox', [...middlewares, middleware.exposeUid], controllers.activitypub.postOutbox); app.post('/uid/:uid/outbox', middlewares, controllers.activitypub.postOutbox);
app.get('/user/:userslug/following', [...middlewares, middleware.exposeUid], controllers.activitypub.getFollowing); app.get('/uid/:uid/following', middlewares, controllers.activitypub.getFollowing);
app.get('/user/:userslug/followers', [...middlewares, middleware.exposeUid], controllers.activitypub.getFollowers); app.get('/uid/:uid/followers', middlewares, controllers.activitypub.getFollowers);
app.get('/post/:pid', middlewares, controllers.activitypub.actors.note); app.get('/post/:pid', middlewares, controllers.activitypub.actors.note);
}; };