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

View File

@ -4,14 +4,8 @@
// ENiGMA½
const { Errors } = require('../enig_error.js');
const UserProps = require('../user_property');
const {
ActivityStreamsContext,
webFingerProfileUrl,
makeUserUrl,
isValidLink,
makeSharedInboxUrl,
userNameFromSubject,
} = require('./util');
const { ActivityStreamsContext, isValidLink, userNameFromSubject } = require('./util');
const Endpoints = require('./endpoint');
const Log = require('../logger').log;
const { queryWebFinger } = require('../webfinger');
const EnigAssert = require('../enigma_assert');
@ -113,14 +107,14 @@ module.exports = class Actor extends ActivityPubObject {
? user.getSanitizedName('real')
: user.username,
endpoints: {
sharedInbox: makeSharedInboxUrl(webServer),
sharedInbox: Endpoints.sharedInbox(webServer),
},
inbox: makeUserUrl(webServer, user, '/ap/users/') + '/inbox',
outbox: makeUserUrl(webServer, user, '/ap/users/') + '/outbox',
followers: makeUserUrl(webServer, user, '/ap/users/') + '/followers',
following: makeUserUrl(webServer, user, '/ap/users/') + '/following',
inbox: Endpoints.inbox(webServer, user),
outbox: Endpoints.outbox(webServer, user),
followers: Endpoints.followers(webServer, user),
following: Endpoints.following(webServer, user),
summary: user.getProperty(UserProps.AutoSignature) || '',
url: webFingerProfileUrl(webServer, user),
url: Endpoints.profile(webServer, user),
manuallyApprovesFollowers: userSettings.manuallyApprovesFollowers,
discoverable: userSettings.discoverable,
// :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 apDb = require('../database').dbs.activitypub;
const { getISOTimestampString } = require('../database');
@ -73,8 +74,7 @@ module.exports = class Collection extends ActivityPubObject {
}
static addFollower(owningUser, followingActor, webServer, ignoreDupes, cb) {
const collectionId =
makeUserUrl(webServer, owningUser, '/ap/users/') + '/followers';
const collectionId = Endpoints.followers(webServer, owningUser);
return Collection.addToCollection(
'followers',
owningUser,
@ -89,7 +89,7 @@ module.exports = class Collection extends ActivityPubObject {
static addFollowRequest(owningUser, requestingActor, webServer, ignoreDupes, cb) {
const collectionId =
makeUserUrl(webServer, owningUser, '/ap/users/') + '/follow-requests';
Endpoints.makeUserUrl(webServer, owningUser) + 'follow-requests';
return Collection.addToCollection(
'follow-requests',
owningUser,
@ -103,8 +103,7 @@ module.exports = class Collection extends ActivityPubObject {
}
static addFollowing(owningUser, followingActor, webServer, ignoreDupes, cb) {
const collectionId =
makeUserUrl(webServer, owningUser, '/ap/users/') + '/following';
const collectionId = Endpoints.following(webServer, owningUser);
return Collection.addToCollection(
'following',
owningUser,
@ -122,7 +121,7 @@ module.exports = class Collection extends ActivityPubObject {
}
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(
'outbox',
owningUser,
@ -136,7 +135,7 @@ module.exports = class Collection extends ActivityPubObject {
}
static addInboxItem(inboxItem, owningUser, webServer, ignoreDupes, cb) {
const collectionId = makeUserUrl(webServer, owningUser, '/ap/users/') + '/inbox';
const collectionId = Endpoints.inbox(webServer, owningUser);
return Collection.addToCollection(
'inbox',
owningUser,
@ -315,7 +314,7 @@ module.exports = class Collection extends ActivityPubObject {
// e.g. http://somewhere.com/_enig/ap/users/NuSkooler/followers
const collectionId =
makeUserUrl(webServer, owningUser, '/ap/users/') + `/${collectionName}`;
Endpoints.makeUserUrl(webServer, owningUser) + `/${collectionName}`;
if (!page) {
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 { Errors, ErrorReasons } = require('../enig_error');
const UserProps = require('../user_property');
@ -21,16 +20,13 @@ exports.ActivityStreamsContext = 'https://www.w3.org/ns/activitystreams';
exports.parseTimestampOrNow = parseTimestampOrNow;
exports.isValidLink = isValidLink;
exports.makeSharedInboxUrl = makeSharedInboxUrl;
exports.makeUserUrl = makeUserUrl;
exports.webFingerProfileUrl = webFingerProfileUrl;
exports.localActorId = localActorId;
exports.userFromActorId = userFromActorId;
exports.getUserProfileTemplatedBody = getUserProfileTemplatedBody;
exports.messageBodyToHtml = messageBodyToHtml;
exports.messageToHtml = messageToHtml;
exports.htmlToMessageBody = htmlToMessageBody;
exports.userNameFromSubject = userNameFromSubject;
exports.extractMessageMetadata = extractMessageMetadata;
// :TODO: more info in default
// this profile template is the *default* for both WebFinger
@ -58,24 +54,6 @@ function isValidLink(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) {
User.getUserIdsWithProperty(UserProps.ActivityPubActorId, actorId, (err, userId) => {
if (err) {
@ -247,3 +225,19 @@ function htmlToMessageBody(html) {
function userNameFromSubject(subject) {
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 Collection = require('../activitypub/collection');
const Note = require('../activitypub/note');
const { makeUserUrl } = require('../activitypub/util');
const Endpoints = require('../activitypub/endpoint');
const { getAddressedToInfo } = require('../mail_util');
const { PublicCollectionId } = require('../activitypub/const');
const Actor = require('../activitypub/actor');
@ -296,8 +296,7 @@ exports.getModule = class ActivityPubScannerTosser extends MessageScanTossModule
}
_collectFollowersSharedInboxEndpoints(localUser, cb) {
const localFollowersEndpoint =
makeUserUrl(this._webServer(), localUser, '/ap/users/') + '/followers';
const localFollowersEndpoint = Endpoints.followers(this._webServer(), localUser);
Collection.followers(localFollowersEndpoint, 'all', (err, collection) => {
if (err) {

View File

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

View File

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