mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
feat: #8493, plugin helper for standardised link/button injection
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const validator = require('validator');
|
const validator = require('validator');
|
||||||
const winston = require('winston');
|
|
||||||
const nconf = require('nconf');
|
const nconf = require('nconf');
|
||||||
|
|
||||||
const user = require('../../user');
|
const user = require('../../user');
|
||||||
@@ -12,6 +11,7 @@ const utils = require('../../utils');
|
|||||||
const privileges = require('../../privileges');
|
const privileges = require('../../privileges');
|
||||||
const translator = require('../../translator');
|
const translator = require('../../translator');
|
||||||
const messaging = require('../../messaging');
|
const messaging = require('../../messaging');
|
||||||
|
const { buildLinks } = require('../helpers');
|
||||||
|
|
||||||
const helpers = module.exports;
|
const helpers = module.exports;
|
||||||
|
|
||||||
@@ -83,7 +83,55 @@ helpers.getUserDataByUserSlug = async function (userslug, callerUID) {
|
|||||||
userData['reputation:disabled'] = meta.config['reputation:disabled'] === 1;
|
userData['reputation:disabled'] = meta.config['reputation:disabled'] === 1;
|
||||||
userData['downvote:disabled'] = meta.config['downvote:disabled'] === 1;
|
userData['downvote:disabled'] = meta.config['downvote:disabled'] === 1;
|
||||||
userData['email:confirmed'] = !!userData['email:confirmed'];
|
userData['email:confirmed'] = !!userData['email:confirmed'];
|
||||||
userData.profile_links = filterLinks(results.profile_menu.links, {
|
|
||||||
|
const profileMenuLinks = [{
|
||||||
|
id: 'info',
|
||||||
|
route: 'info',
|
||||||
|
name: '[[user:account_info]]',
|
||||||
|
icon: 'fa-info',
|
||||||
|
visibility: {
|
||||||
|
self: false,
|
||||||
|
other: false,
|
||||||
|
moderator: false,
|
||||||
|
globalMod: false,
|
||||||
|
admin: true,
|
||||||
|
canViewInfo: true,
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
id: 'sessions',
|
||||||
|
route: 'sessions',
|
||||||
|
name: '[[pages:account/sessions]]',
|
||||||
|
icon: 'fa-group',
|
||||||
|
visibility: {
|
||||||
|
self: true,
|
||||||
|
other: false,
|
||||||
|
moderator: false,
|
||||||
|
globalMod: false,
|
||||||
|
admin: false,
|
||||||
|
canViewInfo: false,
|
||||||
|
},
|
||||||
|
}];
|
||||||
|
|
||||||
|
if (meta.config.gdpr_enabled) {
|
||||||
|
profileMenuLinks.push({
|
||||||
|
id: 'consent',
|
||||||
|
route: 'consent',
|
||||||
|
name: '[[user:consent.title]]',
|
||||||
|
icon: 'fa-thumbs-o-up',
|
||||||
|
visibility: {
|
||||||
|
self: true,
|
||||||
|
other: false,
|
||||||
|
moderator: false,
|
||||||
|
globalMod: false,
|
||||||
|
admin: false,
|
||||||
|
canViewInfo: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const { links } = await plugins.fireHook('filter:user.profileMenu', { uid, callerUID, links: profileMenuLinks });
|
||||||
|
|
||||||
|
userData.profile_links = await buildLinks('accounts/profile', links, {
|
||||||
self: isSelf,
|
self: isSelf,
|
||||||
other: !isSelf,
|
other: !isSelf,
|
||||||
moderator: isModerator,
|
moderator: isModerator,
|
||||||
@@ -127,7 +175,6 @@ async function getAllData(uid, callerUID) {
|
|||||||
isModerator: user.isModeratorOfAnyCategory(callerUID),
|
isModerator: user.isModeratorOfAnyCategory(callerUID),
|
||||||
isFollowing: user.isFollowing(callerUID, uid),
|
isFollowing: user.isFollowing(callerUID, uid),
|
||||||
ips: user.getIPs(uid, 4),
|
ips: user.getIPs(uid, 4),
|
||||||
profile_menu: getProfileMenu(uid, callerUID),
|
|
||||||
groups: groups.getUserGroups([uid]),
|
groups: groups.getUserGroups([uid]),
|
||||||
sso: plugins.fireHook('filter:auth.list', { uid: uid, associations: [] }),
|
sso: plugins.fireHook('filter:auth.list', { uid: uid, associations: [] }),
|
||||||
canEdit: privileges.users.canEdit(callerUID, uid),
|
canEdit: privileges.users.canEdit(callerUID, uid),
|
||||||
@@ -138,59 +185,6 @@ async function getAllData(uid, callerUID) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getProfileMenu(uid, callerUID) {
|
|
||||||
const links = [{
|
|
||||||
id: 'info',
|
|
||||||
route: 'info',
|
|
||||||
name: '[[user:account_info]]',
|
|
||||||
icon: 'fa-info',
|
|
||||||
visibility: {
|
|
||||||
self: false,
|
|
||||||
other: false,
|
|
||||||
moderator: false,
|
|
||||||
globalMod: false,
|
|
||||||
admin: true,
|
|
||||||
canViewInfo: true,
|
|
||||||
},
|
|
||||||
}, {
|
|
||||||
id: 'sessions',
|
|
||||||
route: 'sessions',
|
|
||||||
name: '[[pages:account/sessions]]',
|
|
||||||
icon: 'fa-group',
|
|
||||||
visibility: {
|
|
||||||
self: true,
|
|
||||||
other: false,
|
|
||||||
moderator: false,
|
|
||||||
globalMod: false,
|
|
||||||
admin: false,
|
|
||||||
canViewInfo: false,
|
|
||||||
},
|
|
||||||
}];
|
|
||||||
|
|
||||||
if (meta.config.gdpr_enabled) {
|
|
||||||
links.push({
|
|
||||||
id: 'consent',
|
|
||||||
route: 'consent',
|
|
||||||
name: '[[user:consent.title]]',
|
|
||||||
icon: 'fa-thumbs-o-up',
|
|
||||||
visibility: {
|
|
||||||
self: true,
|
|
||||||
other: false,
|
|
||||||
moderator: false,
|
|
||||||
globalMod: false,
|
|
||||||
admin: false,
|
|
||||||
canViewInfo: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return await plugins.fireHook('filter:user.profileMenu', {
|
|
||||||
uid: uid,
|
|
||||||
callerUID: callerUID,
|
|
||||||
links: links,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function parseAboutMe(userData) {
|
async function parseAboutMe(userData) {
|
||||||
if (!userData.aboutme) {
|
if (!userData.aboutme) {
|
||||||
userData.aboutme = '';
|
userData.aboutme = '';
|
||||||
@@ -202,30 +196,4 @@ async function parseAboutMe(userData) {
|
|||||||
userData.aboutmeParsed = translator.escape(parsed);
|
userData.aboutmeParsed = translator.escape(parsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterLinks(links, states) {
|
|
||||||
return links.filter(function (link, index) {
|
|
||||||
// "public" is the old property, if visibility is defined, discard `public`
|
|
||||||
if (link.hasOwnProperty('public') && !link.hasOwnProperty('visibility')) {
|
|
||||||
winston.warn('[account/profileMenu (' + link.id + ')] Use of the `.public` property is deprecated, use `visibility` now');
|
|
||||||
return link && (link.public || states.self);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default visibility
|
|
||||||
link.visibility = { self: true,
|
|
||||||
other: true,
|
|
||||||
moderator: true,
|
|
||||||
globalMod: true,
|
|
||||||
admin: true,
|
|
||||||
canViewInfo: true,
|
|
||||||
...link.visibility };
|
|
||||||
|
|
||||||
var permit = Object.keys(states).some(function (state) {
|
|
||||||
return states[state] && link.visibility[state];
|
|
||||||
});
|
|
||||||
|
|
||||||
links[index].public = permit;
|
|
||||||
return permit;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
require('../../promisify')(helpers);
|
require('../../promisify')(helpers);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const winston = require('winston');
|
||||||
const nconf = require('nconf');
|
const nconf = require('nconf');
|
||||||
const validator = require('validator');
|
const validator = require('validator');
|
||||||
const querystring = require('querystring');
|
const querystring = require('querystring');
|
||||||
@@ -218,6 +219,34 @@ helpers.buildTitle = function (pageTitle) {
|
|||||||
return title;
|
return title;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
helpers.buildLinks = async function (event, links, states) {
|
||||||
|
const filtered = await plugins.fireHook(`filter:buildLinks.${event}`, { links, states });
|
||||||
|
|
||||||
|
return filtered.links.filter(function (link, index) {
|
||||||
|
// "public" is the old property, if visibility is defined, discard `public`
|
||||||
|
if (link.hasOwnProperty('public') && !link.hasOwnProperty('visibility')) {
|
||||||
|
winston.warn('[account/profileMenu (' + link.id + ')] Use of the `.public` property is deprecated, use `visibility` now');
|
||||||
|
return link && (link.public || filtered.states.self);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default visibility
|
||||||
|
link.visibility = { self: true,
|
||||||
|
other: true,
|
||||||
|
moderator: true,
|
||||||
|
globalMod: true,
|
||||||
|
admin: true,
|
||||||
|
canViewInfo: true,
|
||||||
|
...link.visibility };
|
||||||
|
|
||||||
|
var permit = Object.keys(filtered.states).some(function (state) {
|
||||||
|
return filtered.states[state] && link.visibility[state];
|
||||||
|
});
|
||||||
|
|
||||||
|
filtered.links[index].public = permit;
|
||||||
|
return permit;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
helpers.getCategories = async function (set, uid, privilege, selectedCid) {
|
helpers.getCategories = async function (set, uid, privilege, selectedCid) {
|
||||||
const cids = await categories.getCidsByPrivilege(set, uid, privilege);
|
const cids = await categories.getCidsByPrivilege(set, uid, privilege);
|
||||||
return await getCategoryData(cids, uid, selectedCid);
|
return await getCategoryData(cids, uid, selectedCid);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ const utils = require('../utils');
|
|||||||
|
|
||||||
module.exports = function (Plugins) {
|
module.exports = function (Plugins) {
|
||||||
Plugins.deprecatedHooks = {
|
Plugins.deprecatedHooks = {
|
||||||
|
'filter:user.profileMenu': 'filter:buildLinks.accounts/profile',
|
||||||
};
|
};
|
||||||
|
|
||||||
Plugins.internals = {
|
Plugins.internals = {
|
||||||
|
|||||||
Reference in New Issue
Block a user