Which users liked topic

How do I tell which users liked a topic? In particular, if I grab a topic with /t/120.json (to get topic id 120), how can I tell if the currently logged in user has liked this topic? In the returned json, it just lists the number of likes in the actions_summary section. In this json below, it shows one user liked this topic. How can I tell who? How could I tell if it was the currently logged in user?

{
    "post_stream" : {
        "posts" : [{
                "id" : 163,
                "username" : "fudoj8",
                "avatar_template" : "/letter_avatar/fudoj8/{size}/2.png",
                "uploaded_avatar_id" : null,
                "created_at" : "2015-09-02T02:15:17.213-04:00",
                "cooked" : "<p>alsdkfaoifaafvcxzslfdoiwefjafsdlaskdfpoaijs</p>",
                "post_number" : 1,
                "post_type" : 1,
                "updated_at" : "2015-09-02T02:15:17.213-04:00",
                "reply_count" : 0,
                "reply_to_post_number" : null,
                "quote_count" : 0,
                "avg_time" : null,
                "incoming_link_count" : 0,
                "reads" : 2,
                "score" : 0.2,
                "yours" : false,
                "topic_id" : 120,
                "topic_slug" : "will-my-question-show-up-immediately",
                "topic_auto_close_at" : null,
                "primary_group_name" : null,
                "version" : 1,
                "can_edit" : false,
                "can_delete" : false,
                "can_recover" : false,
                "read" : true,
                "user_title" : null,
                "actions_summary" : [{
                        "id" : 2,
                        "count" : 1,
                        "hidden" : false,
                        "can_act" : false,
                        "acted" : true,
                        "can_undo" : true
                    }, {
                        "id" : 3,
                        "count" : 0,
                        "hidden" : false,
                        "can_act" : true,
                        "can_defer_flags" : false
                    }, {
                        "id" : 4,
                        "count" : 0,
                        "hidden" : false,
                        "can_act" : true,
                        "can_defer_flags" : false
                    }, {
                        "id" : 5,
                        "count" : 0,
                        "hidden" : true,
                        "can_act" : true,
                        "can_defer_flags" : false
                    }, {
                        "id" : 6,
                        "count" : 0,
                        "hidden" : false,
                        "can_act" : true,
                        "can_defer_flags" : false
                    }, {
                        "id" : 7,
                        "count" : 0,
                        "hidden" : false,
                        "can_act" : true,
                        "can_defer_flags" : false
                    }, {
                        "id" : 8,
                        "count" : 0,
                        "hidden" : false,
                        "can_act" : true,
                        "can_defer_flags" : false
                    }
                ],
                "moderator" : false,
                "admin" : false,
                "staff" : false,
                "user_id" : 28,
                "hidden" : false,
                "hidden_reason_id" : null,
                "trust_level" : 0,
                "deleted_at" : null,
                "user_deleted" : false,
                "edit_reason" : null,
                "can_view_edit_history" : true,
                "wiki" : false
            }
        ],
        "stream" : [163]
    },
    "draft" : null,
    "draft_key" : "topic_120",
    "draft_sequence" : 0,
    "starred" : false,
    "posted" : false,
    "unpinned" : null,
    "pinned_globally" : false,
    "pinned" : false,
    "pinned_at" : null,
    "details" : {
        "auto_close_at" : null,
        "auto_close_hours" : null,
        "auto_close_based_on_last_post" : false,
        "created_by" : {
            "id" : 28,
            "username" : "fudoj8",
            "uploaded_avatar_id" : null,
            "avatar_template" : "/letter_avatar/fudoj8/{size}/2.png"
        },
        "last_poster" : {
            "id" : 28,
            "username" : "fudoj8",
            "uploaded_avatar_id" : null,
            "avatar_template" : "/letter_avatar/fudoj8/{size}/2.png"
        },
        "participants" : [{
                "id" : 28,
                "username" : "fudoj8",
                "uploaded_avatar_id" : null,
                "avatar_template" : "/letter_avatar/fudoj8/{size}/2.png",
                "post_count" : 1
            }
        ],
        "suggested_topics" : [{
                "id" : 275,
                "title" : "Does it work now for new user",
                "fancy_title" : "Does it work now for new user",
                "slug" : "does-it-work-now-for-new-user",
                "posts_count" : 1,
                "reply_count" : 0,
                "highest_post_number" : 1,
                "image_url" : null,
                "created_at" : "2016-05-04T14:13:01.167-04:00",
                "last_posted_at" : "2016-05-04T14:13:01.401-04:00",
                "bumped" : true,
                "bumped_at" : "2016-05-04T14:13:01.401-04:00",
                "unseen" : false,
                "pinned" : false,
                "unpinned" : null,
                "visible" : true,
                "closed" : false,
                "archived" : false,
                "archetype" : "regular",
                "like_count" : 0,
                "views" : 12,
                "category_id" : 1
            }, {
                "id" : 141,
                "title" : "Should I watch football on Sunday",
                "fancy_title" : "Should I watch football on Sunday",
                "slug" : "should-i-watch-football-on-sunday",
                "posts_count" : 1,
                "reply_count" : 0,
                "highest_post_number" : 1,
                "image_url" : null,
                "created_at" : "2015-09-04T22:50:44.464-04:00",
                "last_posted_at" : "2015-09-04T22:50:45.497-04:00",
                "bumped" : true,
                "bumped_at" : "2015-09-04T22:50:45.497-04:00",
                "unseen" : false,
                "pinned" : false,
                "unpinned" : null,
                "visible" : true,
                "closed" : false,
                "archived" : false,
                "archetype" : "regular",
                "like_count" : 0,
                "views" : 28,
                "category_id" : 1
            }, {
                "id" : 280,
                "title" : "Testing creation now after a delay?",
                "fancy_title" : "Testing creation now after a delay?",
                "slug" : "testing-creation-now-after-a-delay",
                "posts_count" : 5,
                "reply_count" : 0,
                "highest_post_number" : 5,
                "image_url" : null,
                "created_at" : "2016-05-04T14:21:08.483-04:00",
                "last_posted_at" : "2016-05-05T02:48:48.740-04:00",
                "bumped" : true,
                "bumped_at" : "2016-05-05T02:48:48.740-04:00",
                "unseen" : false,
                "last_read_post_number" : 5,
                "unread" : 0,
                "new_posts" : 0,
                "pinned" : false,
                "unpinned" : null,
                "visible" : true,
                "closed" : false,
                "archived" : false,
                "notification_level" : 2,
                "archetype" : "regular",
                "like_count" : 0,
                "views" : 14,
                "category_id" : 1
            }
        ],
        "notification_level" : 1,
        "notifications_reason_id" : null,
        "can_invite_to" : true,
        "can_create_post" : true,
        "can_reply_as_new_topic" : true,
        "can_flag_topic" : true
    },
    "highest_post_number" : 1,
    "last_read_post_number" : 1,
    "deleted_by" : null,
    "actions_summary" : [{
            "id" : 4,
            "count" : 0,
            "hidden" : false,
            "can_act" : true
        }, {
            "id" : 7,
            "count" : 0,
            "hidden" : false,
            "can_act" : true
        }, {
            "id" : 8,
            "count" : 0,
            "hidden" : false,
            "can_act" : true
        }
    ],
    "id" : 120,
    "title" : "Will my question show up immediately?",
    "fancy_title" : "Will my question show up immediately?",
    "posts_count" : 1,
    "created_at" : "2015-09-02T02:15:16.987-04:00",
    "views" : 32,
    "reply_count" : 0,
    "participant_count" : 1,
    "like_count" : 1,
    "last_posted_at" : "2015-09-02T02:15:17.213-04:00",
    "visible" : true,
    "closed" : false,
    "archived" : false,
    "has_summary" : false,
    "archetype" : "regular",
    "slug" : "will-my-question-show-up-immediately",
    "category_id" : 1,
    "word_count" : 1,
    "deleted_at" : null
}

It’s listed right in the JSON you quoted, although it’s hidden quite well: :slight_smile:

"id" : 2,
"count" : 1,
"hidden" : false,
"can_act" : false,
"acted" : true,
"can_undo" : true

Just look for the acted flag on the action with id 2.

1 Like

I don’t think that works.

I’m talking about the post with id 120. Which would be the main topic thread. In this example it has the title: “Will my question show up immediately?” It has one post, 163, made by fudoj8. If I am logged in as jon129, how can I tell if I liked the main topic? Or if I liked any reply posts to the topic also? When getting the topic, it is done with www.websitename/t/120.json. The request has no idea who is logged in or making the request.

There is the ability to like a topic and the reply posts. I wanted to know, or get a list, of who liked a topic and reply posts. The discourse page shows a “You liked this” next to a topic or reply post when a user is logged in, but I can’t figure out how they did it.

1 Like

Ah, good old topic_users

topic_users
user_id    integer    fkey users 
topic_id    integer    fkey topics 
posted    boolean    default false 
last_read_post_number    integer    null 
highest_seen_post_number    integer    null 
last_visited_at    timestamp    null 
first_visited_at    timestamp    null 
notification_level    integer    default 1 
notifications_changed_at    timestamp    null 
notifications_reason_id    integer    null 
total_msecs_viewed    integer    default 0 
cleared_pinned_at    timestamp    null 
id    serial    primary key 
last_emailed_post_number    integer    null 
liked    boolean    null, default false, denormal post_actions 
bookmarked    boolean    null, default false, denormal post_actions

Wait who is making the request? In your original post, you were talking about the “currently logged in user”. If the request is made with proper credentials (a cookie, for example), it will contain the acted-Flag, just as in the JSON you quoted in the original post.

2 Likes

How do I get that information? The API doesn’t list any topic_users.json endpoint?

I was not requesting the topic with an api_key and api_username combination. I tried and it didn’t work before, but I tried again and now it is working. Thanks!!

1 Like

Question: when getting the latest.json, is there a way with that data to see if the current user has liked that topic? I don’t see any actions_summary for the latest.json query.

I’m pretty sure you need separate requests to get this information.

This topic was automatically closed after 2892 days. New replies are no longer allowed.