Fix a few more inbox/addressing bugs, cleanup, add methtod to request follow

This commit is contained in:
Bryan Ashby 2023-02-08 21:32:54 -07:00
parent fb039c1abc
commit c0914af002
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
6 changed files with 63 additions and 48 deletions

View File

@ -1,20 +1,8 @@
const { const { WellKnownActivityTypes, WellKnownActivity } = require('./const');
ActivityStreamMediaType,
WellKnownActivityTypes,
WellKnownActivity,
HttpSignatureSignHeaders,
} = require('./const');
const { recipientIdsFromObject } = require('./util'); const { recipientIdsFromObject } = require('./util');
const Endpoints = require('./endpoint');
const ActivityPubObject = require('./object'); const ActivityPubObject = require('./object');
const { Errors } = require('../enig_error');
const UserProps = require('../user_property');
const { postJson } = require('../http_util');
const { getISOTimestampString } = require('../database'); const { getISOTimestampString } = require('../database');
// deps
const _ = require('lodash');
module.exports = class Activity extends ActivityPubObject { module.exports = class Activity extends ActivityPubObject {
constructor(obj, withContext = ActivityPubObject.DefaultContext) { constructor(obj, withContext = ActivityPubObject.DefaultContext) {
super(obj, withContext); super(obj, withContext);
@ -84,32 +72,6 @@ module.exports = class Activity extends ActivityPubObject {
}); });
} }
sendTo(inboxEndpoint, fromUser, webServer, cb) {
const privateKey = fromUser.getProperty(UserProps.PrivateActivityPubSigningKey);
if (_.isEmpty(privateKey)) {
return cb(
Errors.MissingProperty(
`User "${fromUser.username}" is missing the '${UserProps.PrivateActivityPubSigningKey}' property`
)
);
}
const reqOpts = {
headers: {
'Content-Type': ActivityStreamMediaType,
},
sign: {
key: privateKey,
keyId: Endpoints.actorId(webServer, fromUser) + '#main-key',
authorizationHeaderName: 'Signature',
headers: HttpSignatureSignHeaders,
},
};
const activityJson = JSON.stringify(this);
return postJson(inboxEndpoint, activityJson, reqOpts, cb);
}
recipientIds() { recipientIds() {
return recipientIdsFromObject(this); return recipientIdsFromObject(this);
} }

View File

@ -253,7 +253,7 @@ module.exports = class Actor extends ActivityPubObject {
static _fromCache(actorIdOrSubject, cb) { static _fromCache(actorIdOrSubject, cb) {
apDb.get( apDb.get(
`SELECT rowid, actor_json, subject, timestamp, `SELECT actor_json, subject, timestamp
FROM actor_cache FROM actor_cache
WHERE actor_id = ? OR subject = ? WHERE actor_id = ? OR subject = ?
LIMIT 1;`, LIMIT 1;`,

View File

@ -1,8 +1,15 @@
const { ActivityStreamsContext } = require('./util'); const {
ActivityStreamsContext,
ActivityStreamMediaType,
HttpSignatureSignHeaders,
} = require('./const');
const Endpoints = require('./endpoint'); const Endpoints = require('./endpoint');
const UserProps = require('../user_property');
const { Errors } = require('../enig_error');
const { postJson } = require('../http_util');
// deps // deps
const { isString, isObject } = require('lodash'); const { isString, isObject, isEmpty } = require('lodash');
const Context = '@context'; const Context = '@context';
@ -79,4 +86,30 @@ module.exports = class ActivityPubObject {
static makeObjectId(webServer, objectType) { static makeObjectId(webServer, objectType) {
return Endpoints.objectId(webServer, objectType); return Endpoints.objectId(webServer, objectType);
} }
sendTo(inboxEndpoint, fromUser, webServer, cb) {
const privateKey = fromUser.getProperty(UserProps.PrivateActivityPubSigningKey);
if (isEmpty(privateKey)) {
return cb(
Errors.MissingProperty(
`User "${fromUser.username}" is missing the '${UserProps.PrivateActivityPubSigningKey}' property`
)
);
}
const reqOpts = {
headers: {
'Content-Type': ActivityStreamMediaType,
},
sign: {
key: privateKey,
keyId: Endpoints.actorId(webServer, fromUser) + '#main-key',
authorizationHeaderName: 'Signature',
headers: HttpSignatureSignHeaders,
},
};
const activityJson = JSON.stringify(this);
return postJson(inboxEndpoint, activityJson, reqOpts, cb);
}
}; };

View File

@ -3,7 +3,9 @@ const { Errors, ErrorReasons } = require('../enig_error');
const UserProps = require('../user_property'); const UserProps = require('../user_property');
const ActivityPubSettings = require('./settings'); const ActivityPubSettings = require('./settings');
const { stripAnsiControlCodes } = require('../string_util'); const { stripAnsiControlCodes } = require('../string_util');
const { WellKnownRecipientFields } = require('./const'); const { WellKnownRecipientFields, WellKnownActivity } = require('./const');
const ActivityPubObject = require('./object');
const Log = require('../logger').log;
// deps // deps
const _ = require('lodash'); const _ = require('lodash');
@ -15,9 +17,6 @@ const moment = require('moment');
const { striptags } = require('striptags'); const { striptags } = require('striptags');
const { encode, decode } = require('html-entities'); const { encode, decode } = require('html-entities');
const { isString } = require('lodash'); const { isString } = require('lodash');
const Log = require('../logger').log;
exports.ActivityStreamsContext = 'https://www.w3.org/ns/activitystreams';
exports.parseTimestampOrNow = parseTimestampOrNow; exports.parseTimestampOrNow = parseTimestampOrNow;
exports.isValidLink = isValidLink; exports.isValidLink = isValidLink;
@ -29,6 +28,7 @@ exports.htmlToMessageBody = htmlToMessageBody;
exports.userNameFromSubject = userNameFromSubject; exports.userNameFromSubject = userNameFromSubject;
exports.extractMessageMetadata = extractMessageMetadata; exports.extractMessageMetadata = extractMessageMetadata;
exports.recipientIdsFromObject = recipientIdsFromObject; exports.recipientIdsFromObject = recipientIdsFromObject;
exports.sendFollowRequest = sendFollowRequest;
// :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
@ -259,3 +259,23 @@ function recipientIdsFromObject(obj) {
return Array.from(new Set(ids)); return Array.from(new Set(ids));
} }
function sendFollowRequest(fromUser, toActor, webServer, cb) {
const fromActorId = fromUser.getProperty(UserProps.ActivityPubActorId);
if (!fromActorId) {
return cb(
Errors.MissingProperty(
`User missing "${UserProps.ActivityPubActorId}" property`
)
);
}
const followRequest = new ActivityPubObject({
id: ActivityPubObject.makeObjectId(webServer, 'follow'),
type: WellKnownActivity.Follow,
actor: fromActorId,
object: toActor.id,
});
return followRequest.sendTo(toActor.inbox, fromUser, webServer, cb);
}

View File

@ -57,7 +57,7 @@ function postJson(url, json, options, cb) {
function _makeRequest(url, options, cb) { function _makeRequest(url, options, cb) {
if (options.body) { if (options.body) {
options.headers['Content-Length'] = Buffer(options.body).length; options.headers['Content-Length'] = Buffer.from(options.body).length;
if (options?.sign?.headers?.includes('digest')) { if (options?.sign?.headers?.includes('digest')) {
options.headers['Digest'] = options.headers['Digest'] =

View File

@ -283,7 +283,7 @@ exports.getModule = class ActivityPubScannerTosser extends MessageScanTossModule
} }
return cb(null, { return cb(null, {
additionalTo: actor.id, additionalTo: actor.inbox,
sharedInboxes: endpoints, sharedInboxes: endpoints,
followers: followersEndpoint, followers: followersEndpoint,
}); });