Forum Discussion

waldemarhelm's avatar
waldemarhelm
Director
2 years ago

Custom Component Number of experts online

Hi,
I would like to add a custom component on the home page that shows the number of experts (defined by ranks or roles) who are currently online and their avatars.

Is this feasible with the API, and if so, does anyone have a suggestion how?

Best regards
Waldemar

  • mcalcaterra's avatar
    mcalcaterra
    Khoros Alumni (Retired)

    Hi waldemarhelm something like this will print out a list of the ids of users that have the administrator role with a link to their profile page

     

    <#assign admins = (liql("SELECT id, online_status, href FROM users WHERE roles.id='t:Administrator'").data.items)![] />
    
    <#list admins as admin>
        <#if admin.online_status = 'online'>
            <a href="${admin.href}">${admin.id}</a>
        </#if>
    </#list>

     

     you should be able to use this as a base template and query for the avatars as well and display that instead of the ids.

    Keep in mind this will return 25 users, you can increase/reduce the limit as needed

    • steffenschoene's avatar
      steffenschoene
      Expert

      Hi mcalcaterra I think you can improve your liql by ordering by last_visit_time to get a better change to find online users. Sadly we have not the constraint option for online_status so we have to use such a workaround. 

      SELECT id, online_status, href 
      FROM users 
      WHERE roles.id='t:Administrator'
      ORDER BY last_visit_time DESC

       

  • waldemarhelm 
    Not feasible becuase online_status constraint can't be used with any other constraints. You can share your ideas/need under Idea section. 

  • Well, a query like the one provided by steffenschoene this will give you data to work with 

     

    SELECT id, online_status, last_visit_time, href FROM users WHERE roles.name='Administrator' AND last_visit_time > 2023-02-25T00:00:00.000-07:00 ORDER BY last_visit_time DESC LIMIT 100

     

    the problem is that the field "online_status" is absurdly unreliable, i just checked in one of our communities, there are "online" users with a last_visit_time in 2020 ğŸ™„...

    so do NOT rely on what that field says, so i'd probably use a combination of "online" and "last_visit_time" to filter out a more reasonable subset of users that might be online, e.g. like i adapted the query above. Still doesn't mean that user is still online though... I assume online_status is determined by session and maybe if someone checks "remember me" when logging in, the session does not expire for a long time, so that user just "stays" online in the eyes of Khoros. But that's only speculation.