Noob Plugin question: Alterin error message on filter hook.
-
Hello,
Would like to ask a very simple question regarding plugin development. But i'm not seeing how to do it.
Firstable if you can suggest basic examples of plugins or docs i would be very grateful because i'm feeling kind of stuck regarding plugin official documentation. I come from a java background and i decided to begin developing some basic plugins this month just to play around, but cant understand 100% how the plugin mechanism works.
I understand the filter/action approach but i can't see how to serve new vies from plugins or providing data in responses from them. I know they should be very basic operations.
Well lets explain my specific problem. I'm trying to code a plugin that check certain permissions regarding user data (number of topics witten, join data etc) to let the user view or not, a certain topic. I'm using filter:privileges.topics.get to execute my plugin logic, and so far is working.
I just want to show and specific error message regarding my validations to let the user know the specific reason he/she can't view the topic instead the above message that is generated when i return an error from my plugin via the callback function (callback(new Error("My message",postcontent))In previous versions of nodebb, the plugin i'm trying to fix was returning a piece of javascript on the error texto in order to get executed whe te vie was rendered, chanign the text on the screen. But that apparently doesn't work on 1.0.3.
Sorry for the long text, any example, suggestion or answer is greatly appreciated.
-
@baris said in Noob Plugin question: Alterin error message on filter hook.:
Instead of returning an error, try changing the
read
property that is passed to the hook to false, this way users will get a you don't have permission error message.I'm already doing that, in addition of firing the error, like this:
# code block postContent.read = false; callback(new Error(tagsTitle.mostrarError[0]+tagsTitle.mensajeError[i]+tagsTitle.mostrarError[1]), postContent);
It is true, what you are telling, that if i make the callback without the error, the resulting message showed on the view is the "Acces Denied" one.
Appart from that, there's no way of changing the error message from the plugin?
I would like to change it and let the user know why he/she has not permissions according to my plugin checks.
This way was a bit tricky but it was working on 0.7.x:
# code block postContent.read = false; callback(new Error("<script>$('.alert.alert-danger').html('My custom message to show')"), postContent);
It doesn't need to be in this way, i don't mind changing the way the plugin works, serving a custom view or something, the problem is that i need a little suggestion on how to achieve it.
*For reference: https://github.com/exo-do/nodebb-plugin-tagstitle (library.js line 112).
Thanks again.
-
i can't help you but it should be easy to make a redirect to another template, isn't it?
-
Not sure if this is exactly what you want, but I think you can do this.
// filter:privileges.topics.get plugin.privilegesTopicsGet = function(privileges, callback) { //... do thing if (thing) privileges.redirectUrl = 'someurl'; callback(null, privileges); }; // filter:topic.build plugin.topicBuild = function (data, callback) { if (data.templateData.privileges.redirectUrl) { data.res.redirect(data.templateData.privileges.redirectUrl); }else{ callback(null, data); } };
-
@yariplus when it try the way that you suggest, when making the redirect i get the following error:
25/4 17:28 [4568] - [31merror[39m: /api/topic/2/privileges-topic-test MongoError: server 127.0.0.1:27017 sockets closed at Server.destroy (C:\Users\jose\Documents\GitHub\NodeBB\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:1042:47) at Server.close (C:\Users\jose\Documents\GitHub\NodeBB\node_modules\mongodb\lib\server.js:398:17) at Db.close (C:\Users\jose\Documents\GitHub\NodeBB\node_modules\mongodb\lib\db.js:357:19) at Object.module.close (C:\Users\jose\Documents\GitHub\NodeBB\src\database\mongo.js:226:6) at shutdown (C:\Users\jose\Documents\GitHub\NodeBB\app.js:306:28) at process.<anonymous> (C:\Users\jose\Documents\GitHub\NodeBB\app.js:173:3) at emitOne (events.js:77:13) at process.emit (events.js:169:7) at process._fatalException (node.js:224:26)
Any idea why could be the cause? Maybe when the callback is executed on the privilegesTopicsGet method, the sockets are closed?
Should I try all the logic on the filter:topic.build hook instead of using the filter:privileges.topics.get for the privilege cheking? -
@yariplus said in Noob Plugin question: Alterin error message on filter hook.:
@jarey no problem.
I fixed some typos in my code above, and tested it on my forum, and verified it works. Let me know if you run into any other errors.
I'm feeling pretty stupic, but got to ask again in order to solve my doubts.
As i said, now i'm only trying to acces a topic, execute a dummy assigment of an url in the privilege object, and then redirect to that url.
As you pointed out, i try to do exactly that binding my dummy functions to the hooks filter:privileges.topics.get for the url assignment on the privilege object and filter:topic.build for the redirection.Well i keep getting the following error on the browser what makes the page stay with the laoding animation forever:
nodebb.min.js?16247918-a870-4cf5-8ec8-e783ea5bf570:21353 Uncaught TypeError: Cannot read property 'name' of undefined
My dummy code is as follows: (pretty reading on my updated dummy repo https://github.com/jarey/nodebb-plugin-tagstitle )
// filter:privileges.topics.get tagsTitle.privilegesTopicsGet = function(privileges, callback) { var thing = true; //Dummy asignment for the redirect url. if (thing){ privileges.redirectUrl = 'http://localhost:4567/users'; } callback(null, privileges); }; // filter:topic.build tagsTitle.topicBuild = function (data, callback) { if (data.templateData.privileges.redirectUrl) { //Execute redirect to the new page. data.res.redirect(data.templateData.privileges.redirectUrl); //data.template.name= 'topic-error'; }else{ callback(null, data); } };
I really don't know what I'm doing wrong; i've tried populating the data.template.name attribute, but if i try to make the redirect using that, i get the error of the closed sockets again on mongo.
Any pice of advice is really appreciated.
Thanks again.
-
@yariplus Same result:
nodebb.min.js?16247918-a870-4cf5-8ec8-e783ea5bf570:21353 Uncaught TypeError: Cannot read property 'name' of undefined
The weird thing is that if i refresh the browser with F5, while the page keeps hanging, iget to the /users page correctly :S.
Thought it could be a debugger thing, but i restarted on dev mode, and the result was the same. -
Okay, I understand the error now. When you redirect, the page doesn't know how to get the data for the new url. Instead of redirecting, you'll have to use the Controller for the page you want to redirect to.
var usersController = require.main.require('./src/controllers').users; // filter:topic.build Plugin.topicBuild = function (data, callback) { if (data.templateData.privileges.redirectUrl) { if (data.templateData.privileges.redirectUrl === '/users') { usersController.getUsersSortedByJoinDate(data.req, data.res); // You could use your custom page's controller here the same way. }else{ callback(null, data); } }else{ callback(null, data); } };
-
@yariplus I understand, rhanks for the explanation.
I was wondering if it is possile to redirect to a custom route created in the plugin itself. Should it be the same way? I mean coding a 'local' controller to obtain the data?
Again thank you very much!
-
Yep, you can use this function here the same way.
nodebb-plugin-tagstitle/library.js at master · jarey/nodebb-plugin-tagstitle
Contribute to jarey/nodebb-plugin-tagstitle development by creating an account on GitHub.
GitHub (github.com)
Just declare it at the global scope so that your hook can see it.
-
Oh god, the TABS. Make it stop. MY EYES
-
@yariplus said in Noob Plugin question: Alterin error message on filter hook.:
Yep, you can use this function here the same way.
nodebb-plugin-tagstitle/library.js at master · jarey/nodebb-plugin-tagstitle
Contribute to jarey/nodebb-plugin-tagstitle development by creating an account on GitHub.
GitHub (github.com)
Just declare it at the global scope so that your hook can see it.
Thank you very much @yariplus ; I achieved what i needed thanks to your help.
I was able to see my custom page with my custom messages like you pointed out.
Then, because i only wanted to change the message displayed on the 403 error regarding the logic on my plugin i was able to change it to using another method; only if it could help anyone else, i just imported the helpers module and called the function notAllowed with my custom message:
var helpers = require.main.require('./src/controllers/helpers'); // filter:topic.build tagsTitle.topicBuild = function (data, callback) { if (data.templateData.privileges.errorMessage) { helpers.notAllowed(data.req,data.res, data.templateData.privileges.errorMessage); }else{ callback(null, data); } };
Thanks again. I learned a lot of basic stuff just trying your suggestions.
Kind regards.