Unread topics for a specific user with API



  • Hi,
    I have on my own server nodebb (recently upgrade to the last version)
    I discover yesterday the great plugin api and have an idea for our use.

    We have a web app develop in php (helpdesk). In this web application, we have on the topbar schorcut to access to our intranet forum (nodebb)

    Before nodebb, we had phpbb3. On the topbar, with a mysql query, i was able to display the unread topics for the connect user.

    When we decide to replace phpbb3 by nodebb, we assume losing this functionnality... But, with the API, i think it could be possible!

    First, with the api, i can display the total unread for the connect nodebb user : http:\myforum:myport\api\unread\total
    However, with an ajax request... i have this error : Access-Control-Allow-Origin => on NGINX, i have already encounter the problem and i can modify server parameter... on nodebb... i'm a newbie ;) and i don't know what i must do?

    Second, with the api, it could be really great if i can do something like this : http:\myforum:myport\api\user:userlug\unread\total
    (my users on my helpdesk are the same on my self-hosting nodebb) (we haven't sso on our web application...)

    If you have some ideas, i really appreciate!

    Thanks


  • Admin

    For the CORS issue:

    0_1457842796379_Selection_107.png

    You can set the appropriate headers there.

    For the latter, if your users are logged in, /api/unread will show their unread posts. Otherwise, as guest, it won't show anything.



  • Thanks for the info julian!
    To do my tests, i have set :

    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods : GET, POST
    Access-Control-Allow-Headers : Content-Type

    My ajax call is :

    $.ajax({
        url:"http://ip:port/api/unread/total",
        dataType:"json",
        type:"GET",
        success:function(data){
            console.log(data);
        }
    });
    

    Unfortunately, i have this return : GET http://ip:port/api/unread/total 401 (Unauthorized)

    This call was ok but this one no need to connect :

    $.ajax({
        url:"http://ip:port/api/user/username/myuser",
        dataType:"json",
        type:"GET",
        success:function(data){
            console.log(data);
        }
    });
    

    I suppose i must give "data" to my ajax call to authenticate the user (my user was connect on the forum but in the API i see for the unread/total method "middleware.authenticate" and i have tried something like data:{username:"myuser",password:"mypass"} without success)

    if you have an idea?


  • Admin

    You'll need to pass in the cookie otherwise NodeBB won't know which user is making the call. Not sure if jQuery .ajax() does so automatically or not...



  • Thanks julian for your time and investigation ;)
    In ajax, i can add this option :
    xhrFields: { withCredentials: true }

    Does NodeBB have this parameter : Access-Control-Allow-Credentials

    Maybe a track...



  • Hi!
    I think i found a solution but need some help to finalize.

    First, i add Access-Control-Allow-Credentials in middleware.js (nodebb) :

      var headers = {
    	'X-Powered-By': meta.config['powered-by'],
    	'X-Frame-Options': meta.config['allow-from-uri'] ? 'ALLOW-FROM ' + meta.config['allow-from-uri'] : undefined,
    	'Access-Control-Allow-Origin': meta.config['access-control-allow-origin'],
    	'Access-Control-Allow-Methods': meta.config['access-control-allow-methods'],
    	'Access-Control-Allow-Headers': meta.config['access-control-allow-headers'],
    	'Access-Control-Allow-Credentials' : true
    };
    

    After that, my ajax request (modify with credentials) give me the number of unread on console browser :

      $.ajax({
            url:"http://ip:port/api/unread/total",
    	dataType:"json",
    	type:"GET",
    	xhrFields: { withCredentials: true }
    	success:function(data){
    			console.log(data);
    	}
    });
    

    First step ok ;)

    Second step, obtain unread for a non connect user!
    I have installed nodebb-plugin-ns-login plugin which provide user information with api access.

    This request give me user information in my console browser :

      $.ajax({
        url:"http://ip:port/api/ns/login",
        dataType:"json",
        type:"POST",
        data:{username:"myuser",password:"mypassword"},
        success:function(data){
            console.log(data);
        }
    });
    

    So, i can obtain specific user information, i can obtain unread with the first method... the goal was to obtain via nodebb-plugin-ns-login the unread for a specific user and here... i'm block...

    I suppose i must modify res.json(user); by something like controllers.unreadTotal but... i don't knwo what i must write... and if someone get an idea... 🍻 :)

    Thanks by advance!



  • No ideas? :(

    The link to the source of the plugin is : nodebb-plugin-ns-login

    I want to replace the res.json(user); line 103 by the total unread for the user... but i'm not familiar with nodebb code architecture and don't know how to get this value... i hope somebody can help me^^


Log in to reply
 


Looks like your connection to NodeBB was lost, please wait while we try to reconnect.