Forum Discussion

jaread83's avatar
jaread83
Champion
8 years ago

Total Kudos Givers

Hi,

We recently implemented a rank that grants the user 2 points per kudos.

I have a component that is supposed to do a count of the amount of users and this worked fine until we granted the thanks weight.

I see it was answered in this post but the user had the exact same problem as me and noone came back to answer the OP:

https://community.lithium.com/t5/Developer-Discussion/Count-of-kudo-givers-to-a-particular-post/m-p/204236#M9166

So my question is basically the answer that was not given on the last reply in that thread. Is there a way to count the amount of kudos givers rather than the total points.

Thanks

  • jaread83 - Found two errors in your query . 

     

    <#assign kudosCount = rest(apiVersion,"/search?q=" + "SELECT * FROM kudos where message.id = messageId limit kudosCountFirst"?url).data.items![] />

     

    messageId and kudosCountFirst are treated as string as you must need to wrap freemaker variable with ${}. Second you need to wrap messageId in quotes.  

     

    Updated Query : 

    <#assign kudosCount = rest(apiVersion,"/search?q=" + "SELECT * FROM kudos where message.id = '${messageId}' limit ${kudosCountFirst}"?url).data.items![] />

     

    PS : I don't drink beer :smileyvery-happy:

     

    Give kudos if you find my posts helpful or mark solution if it answers your query.

    Tariq

     

10 Replies

  • jaread83 - LIQL doesn't support count(*) for KUDOS. It is not possible to achieve this using one query . 

     

    However you still can get the exact result . You will need to get the kudos count and pass it to the limit of next query as kudos count will always be greater than giver count. 

     

    /restapi/vc/messages/id/1894/kudos/count/
    select * from kudos where message.id = 'message.id' and limit <kudos count value>

    Let me know if this helps.

     

    Give kudos if you find my posts helpful or mark solution if it answers your query

    Tariq

     

     

  • jaread83's avatar
    jaread83
    Champion
    8 years ago

    Hi TariqHussain, thanks for the advice. I have got it to half work but I need to convert the API v2 call to a number in order to apply some logic to it.

    Is there a way to convert it to a number so I can get the count?

    Basically, what I am trying to do is count the amount of kudos for a message, list the users who gave the kudos and cut it off at a specific number to add a 'and X amount of others'. Here is my code with your implementation:

     

    <#assign messageId = env.context.message.uniqueId />
    <#if messageId?? && messageId gt 0>
        <#assign apiVersion = "2.0"/>
        <#assign cutoff = 10 />
        <#assign counter = 0 />
        <#attempt>
            <#assign users = rest("/messages/id/${messageId}/kudos/givers?page_size=${cutoff}").users />
        <#recover>
            <#assign users = "" />
        </#attempt>
        <#attempt>
            <#assign kudosCountFirst = rest("/messages/id/${messageId}/kudos/count").value />
            <#assign kudosCount = rest(apiVersion,"/search?q=" + "SELECT * FROM kudos where message.id = messageId limit kudosCountFirst"?url).data.items![] />
        <#recover>
            <#assign kudosCountFirst = "" />
            <#assign kudosCount = "" />
        </#attempt>
        <#assign remaining = kudosCount?number - cutoff?number /> 
        <#if users??>
    <#if kudosCount?number gte 1 ><span class="kudosgivers hidden">${kudosCount} <#if kudosCount?number == 1>user<#else>users</#if> liked/thanked this post:
    <#list users.user as kudosGiver><#assign counter = counter+1 />${kudosGiver.login}<#if counter?number == cutoff?number && kudosCount?number gte cutoff?number+1>... and ${remaining} other <#if remaining == 1> user<#else>users</#if>. <#else><#if counter?number == kudosCount?number>. <#else>, </#if></#if></#list>
    <#if kudosCount?number gte cutoff?number+1 >Click the number to view who else liked/thanked this post.</#if></span></#if>
        </#if>
    </#if>

    I hope you sre able to help. This is a bit of a head scratcher for me at the moment.

    This is what the end result should look like:

    With the Kudos weight added for some users, this throws it all off though.

  • TariqHussain's avatar
    TariqHussain
    Boss
    8 years ago

    jaread83 - Do you want to count the which that you are fetching from API V2? 

     

    You can use ?size context object to count the size of array .  

     

    <#assign remaining = kudosCount?size?number - cutoff?number /> 

    You can also achieve this with below code.

     

    <#assign kudosQuery = rest(apiVersion,"/search?q=" + "SELECT * FROM kudos where message.id = messageId limit kudosCountFirst"?url).data />
    <#assign kudoCount = kudosQuery.size />
    <#assign kudoResult = kudosQuery.items![] />

    Give kudos if you find my posts helpful or mark solution if it answers your query

    Tariq

  • jaread83's avatar
    jaread83
    Champion
    8 years ago

    That didn't seem to work :(

    For "?number" left-hand operand: Expected a string or something automatically convertible to string (number, date or boolean), but this has evaluated to a sequence (wrapper: f.t.SimpleSequence):
    ==> kudosCount  [in template "module-WhoThankedThisPost" at line 20, column 6]
    ----
    FTL stack trace ("~" means nesting-related):
    	- Failed at: #if kudosCount?number gte 1  [in template "module-WhoThankedThisPost" at line 20, column 1]

    How do I get it so the kudosCount to be converted to a number? I am using the ?number but its saying its a string.

  • TariqHussain's avatar
    TariqHussain
    Boss
    8 years ago

    jaread83- You have used ?size?number context objects on line number 18. However,  This need to be updated below as well (line no 20).

    I have updated your code.  Could you please try it now.

     

     

    <#assign messageId = env.context.message.uniqueId />
    <#if messageId?? && messageId gt 0>
        <#assign apiVersion = "2.0"/>
        <#assign cutoff = 10 />
        <#assign counter = 0 />
        <#attempt>
            <#assign users = rest("/messages/id/${messageId}/kudos/givers?page_size=${cutoff}").users />
        <#recover>
            <#assign users = "" />
        </#attempt>
        <#attempt>
            <#assign kudosCountFirst = rest("/messages/id/${messageId}/kudos/count").value />
            <#assign kudosCount = rest(apiVersion,"/search?q=" + "SELECT * FROM kudos where message.id = messageId limit kudosCountFirst"?url).data.items![] />
        <#recover>
            <#assign kudosCountFirst = "" />
            <#assign kudosCount = "" />
        </#attempt>
        <#assign remaining = kudosCount?size?number - cutoff?number /> 
        <#if users??>
    <#if kudosCount?size?number gte 1 ><span class="kudosgivers hidden">${kudosCount} <#if kudosCount?size?number == 1>user<#else>users</#if> liked/thanked this post:
    <#list users.user as kudosGiver><#assign counter = counter+1 />${kudosGiver.login}<#if counter?number == cutoff?number && kudosCount?size?number gte cutoff?number+1>... and ${remaining} other <#if remaining == 1> user<#else>users</#if>. <#else><#if counter?number == kudosCount?size?number>. <#else>, </#if></#if></#list>
    <#if kudosCount?size?number gte cutoff?number+1 >Click the number to view who else liked/thanked this post.</#if></span></#if>
        </#if>
    </#if>
    
    
    

    Give kudos if you find my posts helpful or mark solution if it answers your query

    Tariq

     

  • jaread83's avatar
    jaread83
    Champion
    8 years ago

    Thanks TariqHussain, its starting to get somewhere now. The problem now is that the kudosCount?size?number is always equating to 0 so the api call isn't working as expected. It works in the API browser in Studio if I wrap the messageId with quotes but it doesn't work in the query when I output the kudosCount as it always comes back with 0.

    Edit: Sorry to keep badgering you with this! I am very grateful for your help, I will have to buy you a beer at Lithy Awards '17 if you're going to the London event.

  • TariqHussain's avatar
    TariqHussain
    Boss
    8 years ago

    jaread83 - Found two errors in your query . 

     

    <#assign kudosCount = rest(apiVersion,"/search?q=" + "SELECT * FROM kudos where message.id = messageId limit kudosCountFirst"?url).data.items![] />

     

    messageId and kudosCountFirst are treated as string as you must need to wrap freemaker variable with ${}. Second you need to wrap messageId in quotes.  

     

    Updated Query : 

    <#assign kudosCount = rest(apiVersion,"/search?q=" + "SELECT * FROM kudos where message.id = '${messageId}' limit ${kudosCountFirst}"?url).data.items![] />

     

    PS : I don't drink beer :smileyvery-happy:

     

    Give kudos if you find my posts helpful or mark solution if it answers your query.

    Tariq

     

  • jaread83's avatar
    jaread83
    Champion
    8 years ago

    YES!!!!! That sorted it! I had to also apply the ?size?number to the kudosCount within the output too and it is now working!

    Thank you so much for sticking with me on this one. I have learned a bit more thanks to you!

    Here is the final version of the code:

    <#assign messageId = env.context.message.uniqueId />
    <#if messageId?? && messageId gt 0>
        <#assign apiVersion = "2.0"/>
        <#assign cutoff = 10 />
        <#assign counter = 0 />
        <#attempt>
            <#assign users = rest("/messages/id/${messageId}/kudos/givers?page_size=${cutoff}").users />
        <#recover>
            <#assign users = "" />
        </#attempt>
        <#attempt>
            <#assign kudosCountFirst = rest("/messages/id/${messageId}/kudos/count").value />
            <#assign kudosCount = rest(apiVersion,"/search?q=" + "SELECT * FROM kudos where message.id = '${messageId}' limit ${kudosCountFirst}"?url).data.items![] />
        <#recover>
            <#assign kudosCountFirst = "" />
            <#assign kudosCount = "" />
        </#attempt>
        <#assign remaining = kudosCount?size?number - cutoff?number /> 
        <#if users??>
    <#if kudosCount?size?number gte 1 ><span class="kudosgivers hidden">${kudosCount?size?number} <#if kudosCount?size?number == 1>user<#else>users</#if> liked/thanked this post:
    <#list users.user as kudosGiver><#assign counter = counter+1 />${kudosGiver.login}<#if counter?number == cutoff?number && kudosCount?size?number gte cutoff?number+1>... and ${remaining} other <#if remaining == 1> user<#else>users</#if>. <#else><#if counter?number == kudosCount?size?number>. <#else>, </#if></#if></#list>
    <#if kudosCount?size?number gte cutoff?number+1 >Click the number to view who else liked/thanked this post.</#if></span></#if>
        </#if>
    </#if>

    And for anyone interested in applying this to their own Kudos element, here is the javascript to inject it into the link to the kudos page:

    <@liaAddScript>
    ;(function($) {
        $(".kudosgivers").each(function() {
            var $kudosgivers = $(this);
            $kudosgivers.parent().find('.kudos-count-link').attr('title', $kudosgivers.text());
            $(this).remove();
        });
    })(LITHIUM.jQuery);
    </@liaAddScript>

    Thanks TariqHussain, you're awesome :)

  • TariqHussain's avatar
    TariqHussain
    Boss
    8 years ago

    Thanks jaread83, it is always a pleasure to help out folks here at community. I will not be there this year for the LiNC but my colleague VarunGrazitti will be there and he loves beer :smileyhappy: Don't forget to catch up with him since you both are All-Stars :smileyhappy: