Forum Discussion

frardouin's avatar
7 years ago

Get kudoed messages by user

Hi everyone,

I'm trying to display all the messages kudoed by a user in his profile page. This is currently not working in my component. I think the problem come maybe from the last query in the listing but I can't figure what's wrong. This is what I have:

 

<#assign currentUserid = page.context.user???then(page.context.user.id, user.id) />
<#assign kudoedMessages = restadmin("2.0", "/search?q=" + ("SELECT message.id FROM kudos WHERE user.id = '${currentUserid}'")?url).data.items![] />

<#list kudoedMessages as kudoedMessage>
    <#assign kudoedMessage = rest("2.0","/search?q=" + "SELECT * FROM messages WHERE id='${message.id}'"?url).data.items![] />
    <p>${kudoedMessage.id}</p>
</#list>

(Last quick question: Is it possible to "var_dump" variable in Freemarker? I have a hard time to get the property I need when in 2nd level query  )

  • VikasB's avatar
    VikasB
    7 years ago

    frardouin

    As 2nd APi also returning the array so we must put it in list also. Here is the corrected version. 

    <#assign currentUserid = page.context.user???then(page.context.user.id, user.id) />
    <#assign kudoedMessages = restadmin("2.0", "/search?q=" + ("SELECT * FROM kudos WHERE user.id = '${currentUserid}'")?url).data.items![] />
    
    <#list kudoedMessages as kudoedMessage>
        <#assign messages= rest("2.0","/search?q=" + "SELECT * FROM messages WHERE id='${kudoedMessage.message.id}'"?url).data.items![] />
        <#list messages as message>
    	    <p>${message.id}</p>
    	    <p>${message.subject}</p>
    	    <p>${message.post_time?date}</p>
    	    <p>${message.author.login}</p>
        </#list>
    </#list>
  • frardouin

    You need to send kudoedmessage.message.id instead message.id. Here is the corrected version.

    <#assign currentUserid = page.context.user???then(page.context.user.id, user.id) />
    <#assign kudoedMessages = restadmin("2.0", "/search?q=" + ("SELECT * FROM kudos WHERE user.id = '${currentUserid}'")?url).data.items![] />
    
    <#list kudoedMessages as kudoedMessage>
        <#assign kudoedMessage = rest("2.0","/search?q=" + "SELECT * FROM messages WHERE id='${kudoedMessage.message.id}'"?url).data.items![] />
        <p>${kudoedMessage.id}</p>
    </#list>

     

    • frardouin's avatar
      frardouin
      Guide

      Thanks it's helping VikasB

       

      Now I'm able to render the message id with:

      <p>${kudoedMessage.message.id}</p>

      But I don't understand why I'm unable to render the rest of the message fields. Logically, when I check the message data structure, I should be able to get those field like that :

      <p>${kudoedMessage.message.subject}</p>
      <p>${kudoedMessage.message.post_time}</p>
      <p>${kudoedMessage.message.author.login}</p>
      • VikasB's avatar
        VikasB
        Boss

        frardouin

        As 2nd APi also returning the array so we must put it in list also. Here is the corrected version. 

        <#assign currentUserid = page.context.user???then(page.context.user.id, user.id) />
        <#assign kudoedMessages = restadmin("2.0", "/search?q=" + ("SELECT * FROM kudos WHERE user.id = '${currentUserid}'")?url).data.items![] />
        
        <#list kudoedMessages as kudoedMessage>
            <#assign messages= rest("2.0","/search?q=" + "SELECT * FROM messages WHERE id='${kudoedMessage.message.id}'"?url).data.items![] />
            <#list messages as message>
        	    <p>${message.id}</p>
        	    <p>${message.subject}</p>
        	    <p>${message.post_time?date}</p>
        	    <p>${message.author.login}</p>
            </#list>
        </#list>