Clean up URLs to use central area, less confusion

This commit is contained in:
Bryan Ashby 2023-02-06 14:34:18 -07:00
parent 834dfd693f
commit 926f45b917
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
8 changed files with 94 additions and 60 deletions

View File

@ -1,4 +1,3 @@
const { localActorId } = require('./util');
const { const {
ActivityStreamMediaType, ActivityStreamMediaType,
WellKnownActivityTypes, WellKnownActivityTypes,
@ -6,6 +5,7 @@ const {
WellKnownRecipientFields, WellKnownRecipientFields,
HttpSignatureSignHeaders, HttpSignatureSignHeaders,
} = require('./const'); } = require('./const');
const Endpoints = require('./endpoint');
const ActivityPubObject = require('./object'); const ActivityPubObject = require('./object');
const { Errors } = require('../enig_error'); const { Errors } = require('../enig_error');
const UserProps = require('../user_property'); const UserProps = require('../user_property');
@ -97,7 +97,7 @@ module.exports = class Activity extends ActivityPubObject {
}, },
sign: { sign: {
key: privateKey, key: privateKey,
keyId: localActorId(webServer, fromUser) + '#main-key', keyId: Endpoints.actorId(webServer, fromUser) + '#main-key',
authorizationHeaderName: 'Signature', authorizationHeaderName: 'Signature',
headers: HttpSignatureSignHeaders, headers: HttpSignatureSignHeaders,
}, },

View File

@ -4,14 +4,8 @@
// ENiGMA½ // ENiGMA½
const { Errors } = require('../enig_error.js'); const { Errors } = require('../enig_error.js');
const UserProps = require('../user_property'); const UserProps = require('../user_property');
const { const { ActivityStreamsContext, isValidLink, userNameFromSubject } = require('./util');
ActivityStreamsContext, const Endpoints = require('./endpoint');
webFingerProfileUrl,
makeUserUrl,
isValidLink,
makeSharedInboxUrl,
userNameFromSubject,
} = require('./util');
const Log = require('../logger').log; const Log = require('../logger').log;
const { queryWebFinger } = require('../webfinger'); const { queryWebFinger } = require('../webfinger');
const EnigAssert = require('../enigma_assert'); const EnigAssert = require('../enigma_assert');
@ -113,14 +107,14 @@ module.exports = class Actor extends ActivityPubObject {
? user.getSanitizedName('real') ? user.getSanitizedName('real')
: user.username, : user.username,
endpoints: { endpoints: {
sharedInbox: makeSharedInboxUrl(webServer), sharedInbox: Endpoints.sharedInbox(webServer),
}, },
inbox: makeUserUrl(webServer, user, '/ap/users/') + '/inbox', inbox: Endpoints.inbox(webServer, user),
outbox: makeUserUrl(webServer, user, '/ap/users/') + '/outbox', outbox: Endpoints.outbox(webServer, user),
followers: makeUserUrl(webServer, user, '/ap/users/') + '/followers', followers: Endpoints.followers(webServer, user),
following: makeUserUrl(webServer, user, '/ap/users/') + '/following', following: Endpoints.following(webServer, user),
summary: user.getProperty(UserProps.AutoSignature) || '', summary: user.getProperty(UserProps.AutoSignature) || '',
url: webFingerProfileUrl(webServer, user), url: Endpoints.profile(webServer, user),
manuallyApprovesFollowers: userSettings.manuallyApprovesFollowers, manuallyApprovesFollowers: userSettings.manuallyApprovesFollowers,
discoverable: userSettings.discoverable, discoverable: userSettings.discoverable,
// :TODO: we can start to define BBS related stuff with the community perhaps // :TODO: we can start to define BBS related stuff with the community perhaps

View File

@ -1,4 +1,5 @@
const { makeUserUrl, parseTimestampOrNow } = require('./util'); const { parseTimestampOrNow } = require('./util');
const Endpoints = require('./endpoint');
const ActivityPubObject = require('./object'); const ActivityPubObject = require('./object');
const apDb = require('../database').dbs.activitypub; const apDb = require('../database').dbs.activitypub;
const { getISOTimestampString } = require('../database'); const { getISOTimestampString } = require('../database');
@ -73,8 +74,7 @@ module.exports = class Collection extends ActivityPubObject {
} }
static addFollower(owningUser, followingActor, webServer, ignoreDupes, cb) { static addFollower(owningUser, followingActor, webServer, ignoreDupes, cb) {
const collectionId = const collectionId = Endpoints.followers(webServer, owningUser);
makeUserUrl(webServer, owningUser, '/ap/users/') + '/followers';
return Collection.addToCollection( return Collection.addToCollection(
'followers', 'followers',
owningUser, owningUser,
@ -89,7 +89,7 @@ module.exports = class Collection extends ActivityPubObject {
static addFollowRequest(owningUser, requestingActor, webServer, ignoreDupes, cb) { static addFollowRequest(owningUser, requestingActor, webServer, ignoreDupes, cb) {
const collectionId = const collectionId =
makeUserUrl(webServer, owningUser, '/ap/users/') + '/follow-requests'; Endpoints.makeUserUrl(webServer, owningUser) + 'follow-requests';
return Collection.addToCollection( return Collection.addToCollection(
'follow-requests', 'follow-requests',
owningUser, owningUser,
@ -103,8 +103,7 @@ module.exports = class Collection extends ActivityPubObject {
} }
static addFollowing(owningUser, followingActor, webServer, ignoreDupes, cb) { static addFollowing(owningUser, followingActor, webServer, ignoreDupes, cb) {
const collectionId = const collectionId = Endpoints.following(webServer, owningUser);
makeUserUrl(webServer, owningUser, '/ap/users/') + '/following';
return Collection.addToCollection( return Collection.addToCollection(
'following', 'following',
owningUser, owningUser,
@ -122,7 +121,7 @@ module.exports = class Collection extends ActivityPubObject {
} }
static addOutboxItem(owningUser, outboxItem, isPrivate, webServer, ignoreDupes, cb) { static addOutboxItem(owningUser, outboxItem, isPrivate, webServer, ignoreDupes, cb) {
const collectionId = makeUserUrl(webServer, owningUser, '/ap/users/') + '/outbox'; const collectionId = Endpoints.outbox(webServer, owningUser);
return Collection.addToCollection( return Collection.addToCollection(
'outbox', 'outbox',
owningUser, owningUser,
@ -136,7 +135,7 @@ module.exports = class Collection extends ActivityPubObject {
} }
static addInboxItem(inboxItem, owningUser, webServer, ignoreDupes, cb) { static addInboxItem(inboxItem, owningUser, webServer, ignoreDupes, cb) {
const collectionId = makeUserUrl(webServer, owningUser, '/ap/users/') + '/inbox'; const collectionId = Endpoints.inbox(webServer, owningUser);
return Collection.addToCollection( return Collection.addToCollection(
'inbox', 'inbox',
owningUser, owningUser,
@ -315,7 +314,7 @@ module.exports = class Collection extends ActivityPubObject {
// e.g. http://somewhere.com/_enig/ap/users/NuSkooler/followers // e.g. http://somewhere.com/_enig/ap/users/NuSkooler/followers
const collectionId = const collectionId =
makeUserUrl(webServer, owningUser, '/ap/users/') + `/${collectionName}`; Endpoints.makeUserUrl(webServer, owningUser) + `/${collectionName}`;
if (!page) { if (!page) {
return apDb.get( return apDb.get(

View File

@ -0,0 +1,51 @@
const { WellKnownLocations } = require('../servers/content/web');
exports.makeUserUrl = makeUserUrl;
exports.inbox = inbox;
exports.outbox = outbox;
exports.followers = followers;
exports.following = following;
exports.actorId = actorId;
exports.profile = profile;
exports.avatar = avatar;
exports.sharedInbox = sharedInbox;
const ActivityPubUsersPrefix = '/ap/users/';
function makeUserUrl(webServer, user, relPrefix = ActivityPubUsersPrefix) {
return webServer.buildUrl(
WellKnownLocations.Internal + `${relPrefix}${user.username}`
);
}
function inbox(webServer, user) {
return makeUserUrl(webServer, user, ActivityPubUsersPrefix) + '/inbox';
}
function outbox(webServer, user) {
return makeUserUrl(webServer, user, ActivityPubUsersPrefix) + '/outbox';
}
function followers(webServer, user) {
return makeUserUrl(webServer, user, ActivityPubUsersPrefix) + '/followers';
}
function following(webServer, user) {
return makeUserUrl(webServer, user, ActivityPubUsersPrefix) + '/following';
}
function actorId(webServer, user) {
return makeUserUrl(webServer, user, ActivityPubUsersPrefix);
}
function profile(webServer, user) {
return webServer.buildUrl(WellKnownLocations.Internal + `/wf/@${user.username}`);
}
function avatar(webServer, user, filename) {
return makeUserUrl(this.webServer, user, '/users/') + `/avatar/${filename}`;
}
function sharedInbox(webServer) {
return webServer.buildUrl(WellKnownLocations.Internal + '/ap/shared-inbox');
}

View File

@ -1,4 +1,3 @@
const { WellKnownLocations } = require('../servers/content/web');
const User = require('../user'); const User = require('../user');
const { Errors, ErrorReasons } = require('../enig_error'); const { Errors, ErrorReasons } = require('../enig_error');
const UserProps = require('../user_property'); const UserProps = require('../user_property');
@ -21,16 +20,13 @@ exports.ActivityStreamsContext = 'https://www.w3.org/ns/activitystreams';
exports.parseTimestampOrNow = parseTimestampOrNow; exports.parseTimestampOrNow = parseTimestampOrNow;
exports.isValidLink = isValidLink; exports.isValidLink = isValidLink;
exports.makeSharedInboxUrl = makeSharedInboxUrl;
exports.makeUserUrl = makeUserUrl;
exports.webFingerProfileUrl = webFingerProfileUrl;
exports.localActorId = localActorId;
exports.userFromActorId = userFromActorId; exports.userFromActorId = userFromActorId;
exports.getUserProfileTemplatedBody = getUserProfileTemplatedBody; exports.getUserProfileTemplatedBody = getUserProfileTemplatedBody;
exports.messageBodyToHtml = messageBodyToHtml; exports.messageBodyToHtml = messageBodyToHtml;
exports.messageToHtml = messageToHtml; exports.messageToHtml = messageToHtml;
exports.htmlToMessageBody = htmlToMessageBody; exports.htmlToMessageBody = htmlToMessageBody;
exports.userNameFromSubject = userNameFromSubject; exports.userNameFromSubject = userNameFromSubject;
exports.extractMessageMetadata = extractMessageMetadata;
// :TODO: more info in default // :TODO: more info in default
// this profile template is the *default* for both WebFinger // this profile template is the *default* for both WebFinger
@ -58,24 +54,6 @@ function isValidLink(l) {
return /^https?:\/\/.+$/.test(l); return /^https?:\/\/.+$/.test(l);
} }
function makeSharedInboxUrl(webServer) {
return webServer.buildUrl(WellKnownLocations.Internal + '/ap/shared-inbox');
}
function makeUserUrl(webServer, user, relPrefix) {
return webServer.buildUrl(
WellKnownLocations.Internal + `${relPrefix}${user.username}`
);
}
function webFingerProfileUrl(webServer, user) {
return webServer.buildUrl(WellKnownLocations.Internal + `/wf/@${user.username}`);
}
function localActorId(webServer, user) {
return makeUserUrl(webServer, user, '/ap/users/');
}
function userFromActorId(actorId, cb) { function userFromActorId(actorId, cb) {
User.getUserIdsWithProperty(UserProps.ActivityPubActorId, actorId, (err, userId) => { User.getUserIdsWithProperty(UserProps.ActivityPubActorId, actorId, (err, userId) => {
if (err) { if (err) {
@ -247,3 +225,19 @@ function htmlToMessageBody(html) {
function userNameFromSubject(subject) { function userNameFromSubject(subject) {
return subject.replace(/^acct:(.+)$/, '$1'); return subject.replace(/^acct:(.+)$/, '$1');
} }
function extractMessageMetadata(body) {
const metadata = { mentions: new Set(), hashTags: new Set() };
const re = /(@\w+)|(#[A-Za-z0-9_]+)/g;
const matches = body.matchAll(re);
for (const m of matches) {
if (m[1]) {
metadata.mentions.add(m[1]);
} else if (m[2]) {
metadata.hashTags.add(m[2]);
}
}
return metadata;
}

View File

@ -7,7 +7,7 @@ const { WellKnownAreaTags, AddressFlavor } = require('../message_const');
const { Errors } = require('../enig_error'); const { Errors } = require('../enig_error');
const Collection = require('../activitypub/collection'); const Collection = require('../activitypub/collection');
const Note = require('../activitypub/note'); const Note = require('../activitypub/note');
const { makeUserUrl } = require('../activitypub/util'); const Endpoints = require('../activitypub/endpoint');
const { getAddressedToInfo } = require('../mail_util'); const { getAddressedToInfo } = require('../mail_util');
const { PublicCollectionId } = require('../activitypub/const'); const { PublicCollectionId } = require('../activitypub/const');
const Actor = require('../activitypub/actor'); const Actor = require('../activitypub/actor');
@ -296,8 +296,7 @@ exports.getModule = class ActivityPubScannerTosser extends MessageScanTossModule
} }
_collectFollowersSharedInboxEndpoints(localUser, cb) { _collectFollowersSharedInboxEndpoints(localUser, cb) {
const localFollowersEndpoint = const localFollowersEndpoint = Endpoints.followers(this._webServer(), localUser);
makeUserUrl(this._webServer(), localUser, '/ap/users/') + '/followers';
Collection.followers(localFollowersEndpoint, 'all', (err, collection) => { Collection.followers(localFollowersEndpoint, 'all', (err, collection) => {
if (err) { if (err) {

View File

@ -3,9 +3,8 @@ const {
userFromActorId, userFromActorId,
getUserProfileTemplatedBody, getUserProfileTemplatedBody,
DefaultProfileTemplate, DefaultProfileTemplate,
makeUserUrl,
localActorId,
} = require('../../../activitypub/util'); } = require('../../../activitypub/util');
const Endpoints = require('../../../activitypub/endpoint');
const { ActivityStreamMediaType } = require('../../../activitypub/const'); const { ActivityStreamMediaType } = require('../../../activitypub/const');
const Config = require('../../../config').get; const Config = require('../../../config').get;
const Activity = require('../../../activitypub/activity'); const Activity = require('../../../activitypub/activity');
@ -858,7 +857,7 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
`Preparing ActivityPub settings for "${user.username}"` `Preparing ActivityPub settings for "${user.username}"`
); );
const actorId = localActorId(this.webServer, user); const actorId = Endpoints.actorId(this.webServer, user);
user.setProperty(UserProps.ActivityPubActorId, actorId); user.setProperty(UserProps.ActivityPubActorId, actorId);
user.updateActivityPubKeyPairProperties(err => { user.updateActivityPubKeyPairProperties(err => {
@ -882,8 +881,7 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
const apSettings = ActivityPubSettings.fromUser(user); const apSettings = ActivityPubSettings.fromUser(user);
const filename = paths.basename(outPath); const filename = paths.basename(outPath);
const avatarUrl = const avatarUrl = Endpoints.avatar(this.webServer, user, filename);
makeUserUrl(this.webServer, user, '/users/') + `/avatar/${filename}`;
apSettings.image = avatarUrl; apSettings.image = avatarUrl;
apSettings.icon = avatarUrl; apSettings.icon = avatarUrl;

View File

@ -3,11 +3,10 @@ const Config = require('../../../config').get;
const { Errors, ErrorReasons } = require('../../../enig_error'); const { Errors, ErrorReasons } = require('../../../enig_error');
const { WellKnownLocations } = require('../web'); const { WellKnownLocations } = require('../web');
const { const {
localActorId,
webFingerProfileUrl,
getUserProfileTemplatedBody, getUserProfileTemplatedBody,
DefaultProfileTemplate, DefaultProfileTemplate,
} = require('../../../activitypub/util'); } = require('../../../activitypub/util');
const Endpoints = require('../../../activitypub/endpoint');
const EngiAssert = require('../../../enigma_assert'); const EngiAssert = require('../../../enigma_assert');
const User = require('../../../user'); const User = require('../../../user');
const UserProps = require('../../../user_property'); const UserProps = require('../../../user_property');
@ -220,7 +219,7 @@ exports.getModule = class WebFingerWebHandler extends WebHandlerModule {
} }
_profileUrl(user) { _profileUrl(user) {
return webFingerProfileUrl(this.webServer, user); return Endpoints.profile(this.webServer, user);
} }
_profilePageLink(user) { _profilePageLink(user) {
@ -233,7 +232,7 @@ exports.getModule = class WebFingerWebHandler extends WebHandlerModule {
} }
_userActorId(user) { _userActorId(user) {
return localActorId(this.webServer, user); return Endpoints.actorId(this.webServer, user);
} }
// :TODO: only if ActivityPub is enabled // :TODO: only if ActivityPub is enabled