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 not

    the 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 moderation

    this 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 improve

    but.. 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 ?

  • NodeBB Admin

    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

  • NodeBB Admin

    Is your plugin's init method called? If it is not called there is a problem loading your plugin. Check the startup logs for nodebb.


  • @baris had a syntax error in the index.js.. oops.. not reported..

    node -c index.js

    reported it..

    now my logs work.. (see as admin)

    ugh.. not the raw html..

  • NodeBB Admin

    You can parse the content with

    const mockpost = { content: hookData.data.content };
    await Posts.parsePost(mockPost);
    console.log(mockpost.content);
    

    This will use the active parse plugin, usually nodebb-plugin-markdown to convert content into html.


  • @baris thanks.. where do I find available libraries and their functionality?, and what to import to get 'Posts'..?

    there is a nice sentence about using them, but not how to find a list and what the imports are..

  • NodeBB Admin

    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..

  • NodeBB Admin

    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.

    Has a section that mentions how to load core libraries.

    faf797ea-1afb-47b0-adb1-77f6388e0140-image.png


  • @baris thanks.. the 'use require.main.require' is like saying to find a phone number, use the phonebook.

    do I have to read the source for each object to learns its methods?

    user.exists for example from the image

  • NodeBB Admin

    do I have to read the source for each object to learns its methods?

    Yes, you will have to read the source code to see what methods are available.


Suggested Topics

| | | |