Help with my Custom Function
-
Hi, this works in version 2.x, but I can't seem to make it work in Harmony v3.
library.js
library.actionTopicDevCheck = async function (hookData) { const isInDevGroup = await groups.isMember(hookData.data.uid, 'DEVS'); if (isInDevGroup) { await topics.setTopicField(hookData.post.topic.tid, 'postedInByDeveloper', isInDevGroup); } };
plugin.json
{ "id": "nodebb-theme-harmony", "hooks": [ { "hook": "static:app.load", "method": "init" }, { "hook": "filter:admin.header.build", "method": "addAdminNavigation" }, { "hook": "filter:widgets.getAreas", "method": "defineWidgetAreas" }, { "hook": "filter:config.get", "method": "getThemeConfig" }, { "hook": "filter:settings.get", "method": "getAdminSettings"}, { "hook": "filter:user.saveSettings", "method": "saveUserSettings" }, { "hook": "filter:user.profileMenu", "method": "addProfileItem" }, { "hook": "filter:middleware.renderHeader", "method": "filterMiddlewareRenderHeader" }, { "hook": "filter:middleware.render", "method": "removeFinalBreadcrumb" }, { "hook": "action:topic.reply", "method": "actionTopicDevCheck" }, { "hook": "action:topic.post", "method": "actionTopicDevCheck" } ], "scripts": [ "public/harmony.js" ], "modules": { "../admin/plugins/harmony.js": "public/admin.js", "../client/account/theme.js": "public/settings.js" }, "staticDirs": { "inter": "node_modules/@fontsource/inter/files", "poppins": "node_modules/@fontsource/poppins/files" }, "languages": "languages" }
topic_list.tpl
{{{ if topics.postedInByDeveloper }}} <div class="topic-badge"><a href="{config.relative_path}/groups/devs"><span class="topic-sds">DEV REPLY</span></a></div> {{{ end }}}
-
@baris I can see my console log being called in library.init.
I don't remember 'activating' our custom function in v2 before. And I don't see any error messages from our custom function in the log file.
These hooks haven't change right?
action:topic.reply
action:topic.post -
@baris yup, I did restart a few times, but the function is still not triggering.
EDIT:
It's triggering now. I added a new user to the group, and when I create a new post using the new user, it triggered my function. I guess the function does not trigger for old posts?
-
Note that hooks are generally for reacting to (or modifying) events.
Some may happen during startup, but usually the function from a hook is triggered at some point during some request to NodeBB.If you want to run something on existing content after installing a plugin you might want to look into upgrades - functions that run on startup only once, saved into database afterwards to avoid re-running them. See for example the dbsearch plugin changing the MongoDB schema: https://github.com/barisusakli/nodebb-plugin-dbsearch/ (tl;dr you just need a module that exports what's needed to
plugin.json
in theupgrades
array. You can see the format in theupgrades/dbsearch_change_mongodb_schema.js
.).You can obviously also try doing something similar yourself in e.g.
static:app.load
, but why bother when NodeBB already has a mechanism for it -
@oplik0 yeah and if you don't want an upgrade script and just want to run some code that connects to the database to modify stuff or create reports etc. you can run custom script. These go in the nodebb folder and can be executed by
node my_custom_script.js
. Below is a template that we use frequently./* globals require, console, process */ 'use strict'; const nconf = require('nconf'); nconf.file({ file: 'config.json', }); nconf.defaults({ base_dir: __dirname, views_dir: './build/public/templates', upload_path: 'public/uploads', }); const db = require('./src/database'); db.init(async (err) => { if (err) { console.log(`NodeBB could not connect to your database. Error: ${err.message}`); process.exit(); } await doSomethingUseful(); console.log('done'); process.exit(); }); async function doSomethingUseful() { console.log('global object', await db.getObject('global')); }
-