Likes on a post

Do discourse keep count of who has liked which post ?
I’m working on a method, which changes the color of the like icon, when a user have liked the post.
I have made it so, that if a user likes a post, the “like-icon” changes from gray to red for all users.
I only want it to change to red, if the user have liked that post.
Is there some kind of variable or something I can use from the discourse platform?

I’m not sure I follow becuase that’s how it works now.

A topic / post with no likes looks like this:


If it has likes but you didn’t like it, it shows up to you like this:


If you liked it, it shows up like this to you:


Are you trying to change the color of the heart for posts you’ve liked?


Thanks for the reply.
I have customized the discourse platform for a client

What i’m trying to is to only change the color of the heart icon, if the current user have liked that post.
The problem is, that the heart icons, changes color for all the users.
I want to know if, discourse keep tap over who liked what?

In that case the answer to your question is yes.

liked is an attribute just like like_count. It should return true if you’ve liked the topic.



I will give that a try
This is an attribute from post/topic ?

It’s a topic attribute.

1 Like

Okay I tested it in the postman

As you can see the like_count is 1.
Why is my liked attribute null?

In my system the property ‘liked’ that you are referring to is going from null to false upon liking. The id that is passed as ‘likesId’ on the picture below is the id of the topic I am trying to like.
Do you have any explanation for this behaviour, or possibly a solution, so that the property returns true as intended, upon posting a like? It seems to be hindered by something.
Thank you

I may not be understanding, but are you trying to increment a post action (i.e. a like) by passing in a topic id instead of a post id? AFAIK, there is no such thing as a topic action.

I realise there is no such thing as a topic action, but in my case I need something equivelant to topic_action, otherwise, how does it make sense to like anything but a post?

Well, Likes can be given only to posts. But Likes can be associated with other data. eg.

  • number of Likes on posts made by a member (eg. the /user page)
  • number of Likes on posts in a topic
  • number of Likes on the first post of a topic

I don’t know what would be best considered as topic Likes. For example, which is “better”

  • a topic with 100 posts where every other post got 1 Like (50 total for the topic)
  • a topic with 2 posts where the second post got 40 Likes

They could be further broken down by eg. individual members contributing to the total, date/time ranges.

But again, which is “better”

  • a topic with a total of 50 Likes all by one or few members
  • a short topic with a total of 40 Likes by 40 different members.
  • a year old topic with 50 Likes
  • a week old topic with 40 Likes

I have really been trying to understand the database structure and I admit to fail miserably at that. The only thing I can see is that on the live and dev site we have a difference in the response of a “not yet liked” idea. If you’d like to take a look at the responses I have some well documented pictures. It seems that liked property on the running site for “%liked” ideas is false, and on alpha site it is null by default.


Problem inducing Alpha site response:

These are both Topics and have not been liked.

That looks like JSON for the latest topic list page. The “liked” is whether or not you liked a post in the topic. (false if you read it but didn’t Like, null if you didn’t read it while logged in). The “like_count” is the number of likes for all the posts in the topic combined.

Depending on what kind of information you want, there may not be what you need in that JSON alone. More intricate criteria may need to run a query against more than one table. Or maybe XHR calls to other pages to get their JSON.

I’m guessing your interest is to be able to show Like counts on topic list pages? Or are you interested in analyzing statistics? If it’s more a general interest in the database, checking out the schemas near the bottom of model files can help:
discourse/app/models at main · discourse/discourse · GitHub

Or better, get familiar with the Data Explorer plugin:

1 Like