AuthorManager: New getAuthorId hook

This commit is contained in:
Richard Hansen 2022-02-28 04:19:30 -05:00
parent 6d4085f5f0
commit ae092edf0c
4 changed files with 82 additions and 7 deletions

View file

@ -21,7 +21,8 @@
const db = require('./DB');
const CustomError = require('../utils/customError');
const randomString = require('../../static/js/pad_utils').randomString;
const hooks = require('../../static/js/pluginfw/hooks.js');
const {randomString, padutils: {warnDeprecated}} = require('../../static/js/pad_utils');
exports.getColorPalette = () => [
'#ffc7c7',
@ -102,17 +103,32 @@ exports.doesAuthorExist = async (authorID) => {
/* exported for backwards compatibility */
exports.doesAuthorExists = exports.doesAuthorExist;
/**
* Returns the AuthorID for a token.
* @param {String} token The token
*/
exports.getAuthor4Token = async (token) => {
const getAuthor4Token = async (token) => {
const author = await mapAuthorWithDBKey('token2author', token);
// return only the sub value authorID
return author ? author.authorID : author;
};
exports.getAuthorId = async (token, user) => {
const context = {dbKey: token, token, user};
let [authorId] = await hooks.aCallFirst('getAuthorId', context);
if (!authorId) authorId = await getAuthor4Token(context.dbKey);
return authorId;
};
/**
* Returns the AuthorID for a token.
*
* @deprecated Use `getAuthorId` instead.
* @param {String} token The token
*/
exports.getAuthor4Token = async (token) => {
warnDeprecated(
'AuthorManager.getAuthor4Token() is deprecated; use AuthorManager.getAuthorId() instead');
return await getAuthor4Token(token);
};
/**
* Returns the AuthorID for a mapper.
* @param {String} token The mapper