trying to start a plugin, but missing the understanding of how the parts play together and their data formats
-
the creating plugins 'doc' doesn't provide any links to the data structures
user, post, etc..
and I'm unsure what 'kind' of plugin this is ..
filters, actions, static, and response
I want to get the list of links in a users post, and if it contains links outside a specified list,
I want to place this post in the moderation queue, otherwise notthe user has already been selected for moderation (reputation<limit or postcount <limit)
I guess it must be 'filter' from the code flow
Posts.shouldQueue = async function (uid, data) { const [userData, isMemberOfExempt, categoryQueueEnabled] = await Promise.all([ user.getUserFields(uid, ['uid', 'reputation', 'postcount']), groups.isMemberOfAny(uid, meta.config.groupsExemptFromPostQueue), isCategoryQueueEnabled(data), ]); const shouldQueue = meta.config.postQueue && categoryQueueEnabled && !isMemberOfExempt && (!userData.uid || userData.reputation < meta.config.postQueueReputationThreshold || userData.postcount <= 0); const result = await plugins.hooks.fire('filter:post.shouldQueue', { shouldQueue: !!shouldQueue, uid: uid, data: data, }); return result.shouldQueue; };
if it doesn't contain links, then I would reply false
if it does contain links, and they are in the 'new user' expected list (our site, github, something raspi..whatever)..
need to add admin for this list
reply false
else
let it go to moderationthis would stop almost all of the spam and let good new users in without intervention.
but any escapes will always be in the moderation queue cause their rep will never improvebut.. I just don't understand what the data format looks like.. is it json/object? what is the text content? is it html? are the links resolved to clean text ?
-
You can always put a console.log(data) before the the
filter:post.shouldQueue
hook and see what is being sent to the plugins. In this case it gets the the below data I removed some of the bigger objects.{ uuid: 'b98bdadb-df49-40c6-a0f3-5bc0c03b544d', tid: '4706', content: 'a sample post with a markdown link [spam](https://spamsite.com)', toPid: null, uid: 1, req: { // request object }, timestamp: 1661795452133, fromQueue: false }
As you can see the content is in data.content. So a plugin using this hook would look like the below.
myPlugin.filterShouldQueue = async (hookData) => { if (hookData.data.content.contains('foo')) { hookData.shouldQueue = true; } return hookData; }
-
@baris thanks .. thats what I am trying to do (add the console.log)
my plug in installed, and 'activated'
but doesn't seem to be called.
my plugins.json{ "id": "nodebb-plugin-moderation_on_links", "url": "https://github.com/sdetweil/nodebb-plugin-moderation_on_links", "library": "./library.js", "hooks": [ { "hook": "static:app.load", "method": "init" }, { "hook": "filter:admin.header.build", "method": "addAdminNavigation" }, { "hook":"filter:post.shouldQueue", "method": "postQueue" } ], "staticDirs": { "static": "./static" }, "less": [ "static/style.less" ], "acpScripts": [ "static/lib/admin.js" ], "templates": "static/templates" }
my library.js for the postQueue
``
plugin= {}
plugin.postQueue = function (postData) {
try {
// assume no links, or good links
// let the post pass on thru
console.log("postQueue="+JSON.stringify(postData,null,2))
postData.shouldQueue = false;
{
} catch (error) {
console.error("oops. postQueue error=",error)
}return postData;
};
module.exports = plugin;
...should always override the postQueue in this simple test , but doesn't
i thought I should be able to see this in the logs, when logged on as admin, but don't see it..
dont see output on browser dev console or terminal window
-
-
Libraries come from core nodebb, https://github.com/NodeBB/NodeBB/tree/master/src.
To get the
Posts
object in your plugin you would add this at the top.const Posts = require.main.require('./src/posts');
Then you can use the methods in the Posts object like
parsePost
.Some other ones would be
const Topics = require.main.require('./src/topics'); const Categories = require.main.require('./src/categories'); const Groups = require.main.require('./src/groups'); /// etc...
-
@baris said in trying to start a plugin, but missing the understanding of how the parts play together and their data formats:
const Posts = require.main.require('./src/posts')
thanks.. how could I discover that myself?
nothing talks about the core libs in the creating a plugin section.,.
just trying to be self sufficient..
-
You need to be familiar with the nodebb core code at https://github.com/NodeBB/NodeBB/tree/master/src. The libraries are all in those folders for posts, topics, categories, users, groups etc.
https://docs.nodebb.org/development/plugins/ Has a section that mentions how to load core libraries.