Forum Discussion

peterlu's avatar
peterlu
Champion
12 years ago

query any user's online status

Use Case

Query any user online status "online" or "offline". eg. There are 5 users on the category experts panel. I want to use ajax to query their online status.

 

I am trying to setup an endpoint to achieve this.

<#assign username = "pleasechangeme" >
<#assign login_time = restadmin("users/login/" + username + "/sessions/online/latest").user_session.login_time?datetime("yyyy-MM-dd'T'HH:mm:ss+00:00") />
<#assign login_time_milliseconds = datesupport.setDate(login_time).millisecondsAsString?number />
<#assign now_milliseconds = datesupport.now.millisecondsAsString?number />
${login_time_milliseconds} ${now_milliseconds}

 

But it seems datesupport.now is using +08:00 timezone and login_time is using +00:00 timezone. And my testing result is always like now_milliseconds > login_time_milliseconds, which is wrong.

What I tried to do is to compare these 2 datetime, and if now - login_time > 30 minutes, I consider this user is not online.

 

Any ideas how to solve this problem?

 

And this method is not the best, because if this user logout immediately, he will still be considered online based on the 30 minutes logic.

 

Lithium built-in module can detect user's online status accurately. 

http://lithosphere.lithium.com/t5/user/viewprofilepage/user-id/4654

http://lithosphere.lithium.com/t5/user/viewprofilepage/user-id/10728 

You can see there is a online status on those viewprofilepage.

 

I was wondering is there a way to do this correctly.

BTW, I do know there is a rest call "/users/online". But I dont want to use it. What if it returns me 10,000 users in the xml.

    • clemlith's avatar
      clemlith
      Expert

      Hi,

      This thread is a bit old but I would like to add something about the call mentioned by ChhamaJ.

      As users can set their privacy setting related to their online status to 'no one' you may have response with a 303 error if you are using an account which hasn't the right permissions.

      To avoid this you must have the permission 'Manage roles, user bans, and abuse notifications in admin and user profiles' granted.

       

      Clement