How does Youtube remember Likes?

旧城冷巷雨未停 提交于 2020-01-05 03:51:06

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/8968215/how-does-youtube-remember-likes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!