3

So I'm wondering how sites like YouTube remember when a user has "Liked" a video and prevents them from liking it again. Similarly, how a site like Reddit remembers upvotes and downvotes and prevents a user from upvoting a piece of content they already upvoted.

Is it as simple as a database table where the ID of the content is stored along with the ID of the user and their response? I feel like that would become a massively huge table. Is there something trickier going on?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
John
  • 295
  • 3
  • 11
  • Source code for reddit is available. Use it... – plaes Jan 23 '12 at 07:07
  • Yeah I have looked into it but I'm fairly a beginner so it was a little overwhelming to go through. Will continue to delve through it, though. – John Jan 23 '12 at 07:11

3 Answers3

10

The sites require you to login before clicking on "like" and "upvote". The content document in the DB will have a field which will store the number of likes received. Before the like button is rendered, the sites will check against the logged in user's records in the DB, to check if he has already liked it- accordingly a "like" or "unlike" option is displayed.

If you check here in Stackoverflow, if you click "upvote" on your own question or answer, a message is displayed, that you can't upvote our own post. What happens is when you click "upvote", ajax request will be sent to server with the user ID and other information like question id etc. and the server code will check if you are allowed to upvote i.e your user ID should not be the same as the post creator's ID.

Ninja
  • 5,082
  • 6
  • 37
  • 59
3

I have a like/dislike system on my page.

Database tables:

1.) One that contains your posts with a unique ID for each post and a user_id of who created it (along with other info like content, tags, etc).

2.) table called likes with AT LEAST the following fields, ID, post_id (corresponds to the post in the post table that was liked or disliked), user_id (corresponds to the user in the users table that did the liking/disliking), status (0 or 1, 0 being liked the post, 1 being disliked the post).

When a user likes a post, insert the row into the likes table with their user_id and the post_id, set the status as 0 (or just leave empty because 0 is the default). When a user dislikes a post, do the same but set the status as 1.

That way, on a post page you can get the count of all the users that liked or disliked a post. On a users's profile page, you can get all of the posts a user either likes or dislikes. You can also rank posts by which has the most likes or dislikes. Or even rank specific users by who has posted content with the most likes or dislikes.

Do not allow a users to like/dislike a post if they already have a record in the database. (Basically just check the count of records in the likes table where the post_id is equal to the current post and user_id is equal to the logged in user)

Cross reference the post table to get the post's author's user_id. If the post author user_id is the same as the logged in user, or the user is NOT currently logged in, do not allow them to vote.

The queries for doing all of those are simple (simply SELECT * or SELECT user_id) but that is the basic idea.

bowlerae
  • 924
  • 1
  • 14
  • 37
-4

Yes, it's that simple. Generally, sites use ip addresses though, so that users don't vote twice on different accounts.

edit: Sorry, I was wrong. According to Quentin, websites don't base it off IP because of the possibility that multiple users have the same IP and aren't trying to exploit the system. Smaller scale sites (at least some of the ones I've used) have implemented a voting system based on IP because it would otherwise be easier to manipulate content ranking.

mowwwalker
  • 16,634
  • 25
  • 104
  • 157
  • 3
    Generally, sites like that do *not* use IP addresses because there is so much NAT out there that huge numbers of users share their ip addresses with someone else. – Quentin Jan 23 '12 at 07:08
  • @Quentin, Sorry, I'll delete my answer in a second. I didn't know that. – mowwwalker Jan 23 '12 at 07:09
  • 1
    And if they don't require to login to vote then they would use cookies... Which might not be as good.., – edgarator Jan 23 '12 at 07:09
  • 1
    Both example sites given in the question require logins. – Quentin Jan 23 '12 at 07:10
  • 1
    @edgarator, I highly doubt that. – mowwwalker Jan 23 '12 at 07:10
  • @Quentin, Can you elaborate on what you mean by so much NAT? I'm familiar with the concept. – mowwwalker Jan 23 '12 at 07:12
  • 1
    @Walkerneo — Everybody in an office often shares the same IP. Everybody in a home shares the same IP. Sometimes everybody using an IP share the same small group of IPs (possibly on rotating proxies) (not that that last example is NAT of course). An IP address does not map onto a person. – Quentin Jan 23 '12 at 07:19
  • @Quentin, Is that practical though, for an entire office to use a single ip? I mean I can't think of a reason against it, but it seems strange. – mowwwalker Jan 23 '12 at 07:20
  • 1
    @Walkerneo — Practical and common. – Quentin Jan 23 '12 at 07:21
  • @Quentin, What if you wanted to access a specific computer from outside the network? – mowwwalker Jan 23 '12 at 07:23
  • @Walkerneo — Use a VPN (or make said computer responsible for running a specific service and implement port forwarding on a reverse proxy at the edge of the network). – Quentin Jan 23 '12 at 07:26