• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

trying to start a plugin, but missing the understanding of how the parts play together and their data formats

Scheduled Pinned Locked Moved Plugin Development
12 Posts 2 Posters 422 Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sdetweil
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by baris
    #2

    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;
    }
    
    S 1 Reply Last reply
    0
  • S Offline
    S Offline
    sdetweil
    replied to <baris> on last edited by
    #3

    @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

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #4

    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.

    S 1 Reply Last reply
    0
  • S Offline
    S Offline
    sdetweil
    replied to <baris> on last edited by
    #5

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

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #6

    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.

    S 1 Reply Last reply
    0
  • S Offline
    S Offline
    sdetweil
    replied to <baris> on last edited by
    #7

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

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #8

    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...
    
    S 1 Reply Last reply
    0
  • S Offline
    S Offline
    sdetweil
    replied to <baris> on last edited by
    #9

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

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #10

    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.

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

    S 1 Reply Last reply
    0
  • S Offline
    S Offline
    sdetweil
    replied to <baris> on last edited by
    #11

    @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

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #12

    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.

    1 Reply Last reply
    0

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development