oputil ap condition USERNAME

This commit is contained in:
Bryan Ashby 2023-03-17 18:40:54 -06:00
parent 8c609b79bb
commit e915527427
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
9 changed files with 187 additions and 18 deletions

View File

@ -275,16 +275,11 @@ exports.getModule = class ActivityPubUserConfig extends MenuModule {
_updateCustomViews() {
const enabledToggleView = this.getView('main', MciViewIds.main.enabledToggle);
const ws = this._webServer();
const enabled = enabledToggleView.isTrue();
const formatObj = {
enabled,
status: enabled ? 'enabled' : 'disabled',
subject: enabled
? ws
? userNameToSubject(this.client.user.username, ws)
: 'N/A'
: '',
subject: enabled ? userNameToSubject(this.client.user.username) : 'N/A',
};
this.updateCustomViewTextsWithFilter(

View File

@ -5,6 +5,7 @@ const ActivityPubSettings = require('./settings');
const { stripAnsiControlCodes } = require('../string_util');
const { WellKnownRecipientFields } = require('./const');
const Log = require('../logger').log;
const { getWebDomain } = require('../web_util');
// deps
const _ = require('lodash');
@ -226,8 +227,8 @@ function userNameFromSubject(subject) {
return subject.replace(/^acct:(.+)$/, '$1');
}
function userNameToSubject(userName, webServer) {
return `@${userName}@${webServer.getDomain()}`;
function userNameToSubject(userName) {
return `@${userName}@${getWebDomain()}`;
}
function extractMessageMetadata(body) {

152
core/oputil/activitypub.js Normal file
View File

@ -0,0 +1,152 @@
const {
printUsageAndSetExitCode,
ExitCodes,
argv,
initConfigAndDatabases,
} = require('./oputil_common');
const getHelpFor = require('./oputil_help.js').getHelpFor;
const UserProps = require('../user_property');
const { Errors } = require('../enig_error');
// deps
const async = require('async');
const { get } = require('lodash');
exports.handleUserCommand = handleUserCommand;
function applyAction(username, actionFunc, cb) {
initConfigAndDatabases(err => {
if (err) {
return cb(err);
}
if (!validateActivityPub()) {
return cb(Errors.General('Activity Pub is not enabled'));
}
if ('*' === username) {
return actionFunc(null, cb);
} else {
const User = require('../../core/user.js');
User.getUserIdAndName(username, (err, userId) => {
if (err) {
// try user ID if number was supplied
userId = parseInt(userId);
if (isNaN(userId)) {
return cb(err);
}
}
User.getUser(userId, (err, user) => {
if (err) {
return cb(err);
}
return actionFunc(user, cb);
});
});
}
});
}
function conditionSingleUser(User, username, userId, settings, cb) {
const { userNameToSubject } = require('../activitypub/util');
const subject = userNameToSubject(username);
if (!subject) {
return cb(Errors.General(`Failed to get subject for ${username}`));
}
console.info(`Conditioning ${username} (${userId}) -> ${subject}...`);
User.persistPropertyByUserId(userId, UserProps.ActivityPubSettings, settings, err => {
return cb(err);
});
}
function actionConditionAllUsers(_, cb) {
const User = require('../../core/user.js');
const ActivityPubSettings = require('../activitypub/settings');
const defaultSettings = JSON.stringify(new ActivityPubSettings());
User.getUserList({}, (err, userList) => {
if (err) {
return cb(err);
}
async.each(
userList,
(entry, next) => {
conditionSingleUser(
User,
entry.userName,
entry.userId,
defaultSettings,
next
);
},
err => {
return cb(err);
}
);
});
}
function actionConditionUser(user, cb) {
const User = require('../../core/user.js');
const ActivityPubSettings = require('../activitypub/settings');
const defaultSettings = JSON.stringify(new ActivityPubSettings());
return conditionSingleUser(User, user.username, user.userId, defaultSettings, cb);
}
function validateActivityPub() {
//
// Web Server, and ActivityPub both must be enabled
//
const sysConfig = require('../config').get;
const config = sysConfig();
if (
true !== get(config, 'contentServers.web.http.enabled') &&
true !== get(config, 'contentServers.web.https.enabled')
) {
return false;
}
return true === get(config, 'contentServers.web.handlers.activityPub.enabled');
}
function conditionUser(action, username) {
return applyAction(
username,
'*' === username ? actionConditionAllUsers : actionConditionUser,
err => {
if (err) {
console.error(err.message);
}
}
);
}
function handleUserCommand() {
const errUsage = () => {
return printUsageAndSetExitCode(getHelpFor('ActivityPub'), ExitCodes.ERROR);
};
if (true === argv.help) {
return errUsage();
}
const action = argv._[1];
const usernameIdx = ['condition'].includes(action)
? argv._.length - 1
: argv._.length;
const username = argv._[usernameIdx];
if (!username) {
return errUsage();
}
return (
{
condition: conditionUser,
}[action] || errUsage
)(action, username);
}

View File

@ -20,6 +20,7 @@ Commands:
config Configuration management
fb File base management
mb Message base management
ap ActivityPub management
`,
User: `usage: oputil.js user <action> [<arguments>]
@ -208,6 +209,12 @@ qwk-export arguments:
TIMESTAMP.
--no-qwke Disable QWKE extensions.
--no-synchronet Disable Synchronet style extensions.
`,
ActivityPub: `usage: oputil.js ap <action> [<arguments>]
Actions:
condition USERNAME Condition user with system ActivityPub defaults
Instead of an actual USERNAME, the '*' character may be substituted.
`,
});

View File

@ -10,6 +10,7 @@ const handleFileBaseCommand = require('./oputil_file_base.js').handleFileBaseCom
const handleMessageBaseCommand =
require('./oputil_message_base.js').handleMessageBaseCommand;
const handleConfigCommand = require('./oputil_config.js').handleConfigCommand;
const handleApCommand = require('./activitypub').handleUserCommand;
const getHelpFor = require('./oputil_help.js').getHelpFor;
module.exports = function () {
@ -32,6 +33,8 @@ module.exports = function () {
return handleFileBaseCommand();
case 'mb':
return handleMessageBaseCommand();
case 'ap':
return handleApCommand();
default:
return printUsageAndSetExitCode(getHelpFor('General'), ExitCodes.BAD_COMMAND);
}

View File

@ -154,7 +154,7 @@ const PREDEFINED_MCI_GENERATORS = {
if (!webServer) {
return 'N/A';
}
return userNameToSubject(client.user.username, webServer);
return userNameToSubject(client.user.username);
},
LO: function location(client) {
return userStatAsString(client, UserProps.Location, '');

View File

@ -5,6 +5,7 @@ const Config = require('../../config.js').get;
const { Errors } = require('../../enig_error.js');
const { loadModulesForCategory, moduleCategories } = require('../../module_util');
const WebHandlerModule = require('../../web_handler_module');
const { getWebDomain } = require('../../web_util');
// deps
const http = require('http');
@ -90,14 +91,7 @@ exports.getModule = class WebServerModule extends ServerModule {
}
getDomain() {
const config = Config();
const overridePrefix = _.get(config, 'contentServers.web.overrideUrlPrefix');
if (_.isString(overridePrefix)) {
const url = new URL(overridePrefix);
return url.hostname;
}
return config.contentServers.web.domain;
return getWebDomain();
}
baseUrl() {

View File

@ -18,7 +18,7 @@ exports.moduleInfo = {
packageName: 'codes.l33t.enigma.web.handler.nodeinfo2',
};
exports.getModule = class NodeInfo2WebHadnler extends WebHandlerModule {
exports.getModule = class NodeInfo2WebHandler extends WebHandlerModule {
constructor() {
super();
}

17
core/web_util.js Normal file
View File

@ -0,0 +1,17 @@
const Config = require('./config').get;
// deps
const { get, isString } = require('lodash');
exports.getWebDomain = getWebDomain;
function getWebDomain() {
const config = Config();
const overridePrefix = get(config, 'contentServers.web.overrideUrlPrefix');
if (isString(overridePrefix)) {
const url = new URL(overridePrefix);
return url.hostname;
}
return config.contentServers.web.domain;
}