Forum Discussion

keithkelly's avatar
10 months ago

What's the Best Practice for showing Badge Count beside Profile Pic on all posts? (Caching?)

A reasonable request from SME's to encourage competition & bragging rights:  Display a user's Badge Count beside their name / profile pic.  

My current prototype feels like a worst practice:

<#assign userBadges = rest("/users/id/"+res.author.id+"/badges/?page_size=1000&restapi.response_style=view").user_badges />
<#assign userBadgeCount = userBadges.user_badge?size />

 

What are some best (or heck, at least less bad) practices for doing something like this? 

 

 

(above, mocked up in a topic list by mistake 😂 ... we'd primarily want to use these within the discussions themselves.)

 

  • I would at least upgrade it to API v2 with something like this untested code:

    <#assign userData = (liql('SELECT login, user_badges FROM users WHERE id="${res.author.id}"').data.items) />
    
    ${userData.user_badges.size}

    That won't solve for having to repeatedly call this within a thread where the same author appears multiple times though. The article on caching states that it's not worth caching a users login and avatar, so following this argument it's probably not worth caching the badge count ğŸ˜‡

    • AmanMalhotra's avatar
      AmanMalhotra
      Expert

      Hi Claudius,

      In APIv1 we can get only earned badges but in APIv2 we get earned+unearned badges which are visible on the profile page and we don't have any constraint to filter unearned badges.

      In other words, we also get badges that users have not yet earned in APIv2 because those badges are visible on the user profile page due to the below checkbox not being checked when creating a new badge.
      Screenshot: https://prnt.sc/MMbhCdIdX4oI