Noob Plugin question: Alterin error message on filter hook.


  • Plugin & Theme Dev

    i can't help you but it should be easy to make a redirect to another template, isn't it?


  • Community Rep

    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] - error: /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?


  • Community Rep

    @jarey Yeah, probably one of the callbacks is being called twice.



  • @yariplus Thank you, will check that. Sorry if the questions are very basic.


  • Community Rep

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



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


  • Community Rep

    @jarey Try putting just '/users' as the redirectUrl.



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


  • Community Rep

    @jarey Yeah, I get the same result. Something with ajaxify doesn't like doing res.redirect


  • Community Rep

    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!


  • Community Rep

    Yep, you can use this function here the same way.

    https://github.com/jarey/nodebb-plugin-tagstitle/blob/master/library.js#L17

    Just declare it at the global scope so that your hook can see it.


  • Global Moderator

    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.

    https://github.com/jarey/nodebb-plugin-tagstitle/blob/master/library.js#L17

    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.


Log in to reply
 

Suggested Topics

  • 5
  • 11
  • 20
  • 2
  • 2
| |