Glad to help.
Pretty simple to do those things. Here's a whole plugin. Put in as many comments as I could think of.
library.js
(function(module){
"use strict";
// The modules we need.
var User = module.parent.require("./user");
var Topics = module.parent.require("./topics");
var nconf = require('nconf');
var Plugin = {};
// This event is fired whenever a post is created. (reply or new topic)
Plugin.postSave = function (postData) {
// If you wanted to see what's inside use
// console.log(postData);
// Get the topic info...
// We only need the title and slug, so we use getTopicFields, but you could call getTopicData if you wanted all the topic fields.
Topics.getTopicFields(postData.tid, ['title', 'slug'], function (err, topicData) {
// If there is an error or missing data, bail out and log it.
if (err || !topicData.title || !topicData.slug) return console.log("Couldn't find topic data.");
// Save the data we need.
var topic = topicData.title;
var topicSlug = topicData.slug;
// Get the user info...
// We only need the name, but again we could use getUserData if we wanted all fields.
User.getUserField(postData.uid, 'username', function (err, name) {
// If there is an error or missing data, bail out and log it.
if (err || !name) return console.log("Couldn't find username.");
// Links always follow this same structure.
// the site url + "topic/" + the topic slug + "/" + the post id
var link = nconf.get('url') + "topic/" + topicSlug + "/" + postData.pid;
// Now do whatever we want with the data.
someCoolThing({
name: name,
topic: topic,
link: link
});
});
});
};
function someCoolThing(data) {
console.log("We get signal!!!");
console.log("Username: " + data.name);
console.log("Topic: " + data.topic);
console.log("Link: " + data.link);
}
module.exports = Plugin;
}(module));
Most of the nodebb modules follow the same 'get' structure.
- getThingData() takes the Thing ID. Then returns an object with all fields.
- getThingFields() takes the Thing ID and an array with the fields wanted. Then returns an object with the fields wanted.
- getThingField() the Thing ID and a field name string. Then returns the field value.
All of them return an Error object as the first parameter, which is null if no error occured.
For completeness, here are the plugin.json and package.json:
{
"id": "nodebb-plugin-test",
"name": "NodeBB Test",
"description": "Test",
"url": "https://github.com/yariplus/nodebb-plugin-test",
"library": "./library.js",
"hooks": [
{ "hook": "action:post.save", "method": "postSave" }
]
}
{
"name": "nodebb-plugin-test",
"version": "1.0.0"
}
If you have trouble with hooks:
The 'action' hooks will typically return nothing or an object with the fields used to process that action.
Ex.
Plugin.someAction = function (someObject) {
console.log(someObject);
}
The 'filter' hooks will typically return an object and a callback function. You must call the callback function or bad things happen. The callback function typically needs an Error object (or null if no error) and the sent object as it's parameters.
Ex.
Plugin.someFilter = function (objectToFilter, callback) {
callback(null, objectToFilter);
}