Forum Discussion

Han's avatar
Han
Ace
6 years ago

Get all latest posts for a user

Hi all,

I'd like to create a custom "latest contributions" component for profile pages. I'm trying to select all latest posts for a user with API V2. 

At the moment, I'm always getting the same 5 posts back, no matter which user id I am viewing. Here is what I have so far

SELECT * FROM messages WHERE depth = 0 AND author.id = 'user.id' ORDER BY conversation.last_post_time DESC LIMIT 10


I'm quite new to using API V2, so any help here is appreciated. 

  • luk's avatar
    luk
    6 years ago

     

    Now we are talking freemarker which of course changes things =)... So with the code from Parshant it would be clear why you get always the same result, because you're asking for the last 10 topics of the currently logged in user, e.g. yourself (which is what ${user.id} will give you), I think what would be correct for your scenario is the page.context.user context-object which according to the docs (here) state:

    If this page is associated with a user, then this returns a user context object, otherwise returns null. You can chain calls on the user object from this method. The following pages return a user:
    - MyProfilePage (only when an admin is editing another user's profile. When the current user is editing their profile use the $user object instead)
    - ViewProfilePage
    - MobileViewProfilePage
    - TkbUserContributedArticlesPage

    I assume you're targeting the "ViewProfilePage" quilt with  your custom component,it would work as follows with your query:

    SELECT * FROM messages WHERE depth = 0 AND author.id = '${page.context.user.id}' ORDER BY conversation.last_post_time DESC LIMIT 10

    BUT, careful, as the docs say, if this component was placed on a page with no user context, your query would fail, therefore I'd check if page.context.user exists before setting off the API request, e.g.

    <#if (page.context.user)?has_content>
        // whatever you want to do here
    </#if>

    hope that helps!

  • Han not sure what's going wrong, but your query seems to work for me, e.g.

    SELECT * FROM messages WHERE depth = 0 AND author.id = '3' ORDER BY conversation.last_post_time DESC LIMIT 10
    
    // and
    
    SELECT * FROM messages WHERE depth = 0 AND author.id = '8' ORDER BY conversation.last_post_time DESC LIMIT 10
    
    

    return

    a) 10 results (not 5) and

    b) different results for both queries

    • Hi Han,

      Can you try copy and paste using below api again.

      SELECT * FROM messages WHERE depth = 0 AND author.id = '${user.id}' ORDER BY conversation.last_post_time DESC LIMIT 10

       Let me know the response on this.

      • luk's avatar
        luk
        Boss

         

        Now we are talking freemarker which of course changes things =)... So with the code from Parshant it would be clear why you get always the same result, because you're asking for the last 10 topics of the currently logged in user, e.g. yourself (which is what ${user.id} will give you), I think what would be correct for your scenario is the page.context.user context-object which according to the docs (here) state:

        If this page is associated with a user, then this returns a user context object, otherwise returns null. You can chain calls on the user object from this method. The following pages return a user:
        - MyProfilePage (only when an admin is editing another user's profile. When the current user is editing their profile use the $user object instead)
        - ViewProfilePage
        - MobileViewProfilePage
        - TkbUserContributedArticlesPage

        I assume you're targeting the "ViewProfilePage" quilt with  your custom component,it would work as follows with your query:

        SELECT * FROM messages WHERE depth = 0 AND author.id = '${page.context.user.id}' ORDER BY conversation.last_post_time DESC LIMIT 10

        BUT, careful, as the docs say, if this component was placed on a page with no user context, your query would fail, therefore I'd check if page.context.user exists before setting off the API request, e.g.

        <#if (page.context.user)?has_content>
            // whatever you want to do here
        </#if>

        hope that helps!