Some tidy and log cleanup

This commit is contained in:
Bryan Ashby 2023-01-06 18:05:11 -07:00
parent b252f69f05
commit 60238de017
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
2 changed files with 35 additions and 18 deletions

View File

@ -62,7 +62,7 @@ function userFromAccount(accountName, cb) {
const accountStatus = user.getPropertyAsNumber(UserProps.AccountStatus); const accountStatus = user.getPropertyAsNumber(UserProps.AccountStatus);
if ( if (
User.AccountStatus.disabled == accountStatus || User.AccountStatus.disabled == accountStatus ||
User.AccountStatus.inactive == accountStatus User.AccountStatus.inactive == accountStatus
) { ) {
return cb(Errors.AccessDenied('Account disabled', ErrorReasons.Disabled)); return cb(Errors.AccessDenied('Account disabled', ErrorReasons.Disabled));
} }

View File

@ -10,10 +10,10 @@ const {
const UserProps = require('../../../user_property'); const UserProps = require('../../../user_property');
const { Errors } = require('../../../enig_error'); const { Errors } = require('../../../enig_error');
const Config = require('../../../config').get; const Config = require('../../../config').get;
const Log = require('../../../logger').log;
// deps // deps
const _ = require('lodash'); const _ = require('lodash');
const { trim } = require('lodash');
exports.moduleInfo = { exports.moduleInfo = {
name: 'ActivityPub', name: 'ActivityPub',
@ -25,6 +25,8 @@ exports.moduleInfo = {
exports.getModule = class ActivityPubWebHandler extends WebHandlerModule { exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
constructor() { constructor() {
super(); super();
this.log = require('../../../logger').log.child({ webHandler: 'ActivityPub' });
} }
init(cb) { init(cb) {
@ -32,7 +34,6 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
if (!this.webServer) { if (!this.webServer) {
return cb(Errors.UnexpectedState('Cannot access web server!')); return cb(Errors.UnexpectedState('Cannot access web server!'));
} }
Log.debug('Adding route for ActivityPub');
this.webServer.addRoute({ this.webServer.addRoute({
method: 'GET', method: 'GET',
@ -44,7 +45,8 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
} }
_selfUrlRequestHandler(req, resp) { _selfUrlRequestHandler(req, resp) {
Log.debug({ url: req.url }, 'Received request for self url'); this.log.debug({ url: req.url }, 'Received request for "self" URL');
const url = new URL(req.url, `https://${req.headers.host}`); const url = new URL(req.url, `https://${req.headers.host}`);
let accountName = url.pathname.substring(url.pathname.lastIndexOf('/') + 1); let accountName = url.pathname.substring(url.pathname.lastIndexOf('/') + 1);
let sendActor = false; let sendActor = false;
@ -54,24 +56,24 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
sendActor = true; sendActor = true;
accountName = accountName.slice(0, -5); accountName = accountName.slice(0, -5);
} }
Log.debug({ accountName: accountName }, 'Retrieving self url for account.');
userFromAccount(accountName, (err, user) => { userFromAccount(accountName, (err, user) => {
if (err) { if (err) {
Log.info( this.log.info(
{ accountName: accountName }, { accountName: accountName },
'Unable to find user from account retrieving self url.' 'Unable to find user from account retrieving self url.'
); );
return this._notFound(resp); return this._notFound(resp);
} }
const accept = req.headers['accept'] || '*/*'; // Additionally, serve activity JSON if the proper 'Accept' header was sent
const headerValues = ['application/activity+json', 'application/ld+json', 'application/json']; const accept = req.headers['accept'].split(',').map(v => v.trim()) || ['*/*'];
if (headerValues.some(mime => accept.includes(mime))) { const headerValues = [
sendActor = true; 'application/activity+json',
} 'application/ld+json',
'application/json',
Log.debug({ sendActor: sendActor }, 'Sending actor JSON'); ];
sendActor = accept.some(v => headerValues.includes(v));
if (sendActor) { if (sendActor) {
return this._selfAsActorHandler(user, req, resp); return this._selfAsActorHandler(user, req, resp);
@ -82,14 +84,19 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
} }
_selfAsActorHandler(user, req, resp) { _selfAsActorHandler(user, req, resp) {
const sUrl = selfUrl(this.webServer, user); this.log.trace(
{ username: user.username },
`Serving ActivityPub Actor for ${user.username}`
);
const userSelfUrl = selfUrl(this.webServer, user);
const bodyJson = { const bodyJson = {
'@context': [ '@context': [
'https://www.w3.org/ns/activitystreams', 'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1', 'https://w3id.org/security/v1',
], ],
id: sUrl, id: userSelfUrl,
type: 'Person', type: 'Person',
preferredUsername: user.username, preferredUsername: user.username,
name: user.getSanitizedName('real'), name: user.getSanitizedName('real'),
@ -104,17 +111,27 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
url: webFingerProfileUrl(this.webServer, user), url: webFingerProfileUrl(this.webServer, user),
// :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
attachment: [
{
name: 'SomeNetwork Address',
type: 'PropertyValue',
value: 'Mateo@21:1/121',
},
],
}; };
const publicKeyPem = user.getProperty(UserProps.PublicKeyMain); const publicKeyPem = user.getProperty(UserProps.PublicKeyMain);
if (!_.isEmpty(publicKeyPem)) { if (!_.isEmpty(publicKeyPem)) {
bodyJson['publicKey'] = { bodyJson['publicKey'] = {
id: sUrl + '#main-key', id: userSelfUrl + '#main-key',
owner: sUrl, owner: userSelfUrl,
publicKeyPem: user.getProperty(UserProps.PublicKeyMain), publicKeyPem: user.getProperty(UserProps.PublicKeyMain),
}; };
} else { } else {
Log.debug({ username: user.username }, 'User does not have a publickey.'); this.log.debug(
{ username: user.username },
'User does not have a publickey.'
);
} }
const body = JSON.stringify(bodyJson); const body = JSON.stringify(bodyJson);