YouTube 如何记住点赞数?

发布于 2024-12-28 05:20:02 字数 179 浏览 1 评论 0原文

所以我想知道像 YouTube 这样的网站如何记住用户“喜欢”某个视频并阻止他们再次喜欢该视频。类似地,像 Reddit 这样的网站如何记住赞成票和反对票,并防止用户对他们已经赞成的内容进行赞成。

它是否像存储内容 ID 以及用户 ID 及其响应的数据库表一样简单?我觉得那会变成一张巨大的桌子。是不是有什么更棘手的事情发生了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

榕城若虚 2025-01-04 05:20:02

这些网站要求您先登录,然后才能单击“喜欢”和“投票”。数据库中的内容文档将有一个字段,用于存储收到的点赞数。在呈现“喜欢”按钮之前,网站将检查登录用户在数据库中的记录,以检查他是否已经喜欢它 - 相应地会显示“喜欢”或“不喜欢”选项。

如果您在 Stackoverflow 中查看此处,如果您对自己的问题或答案点击“赞成”,则会显示一条消息,表明您无法赞成我们自己的帖子。发生的情况是,当您单击“upvote”时,ajax 请求将发送到服务器,其中包含用户 ID 和其他信息(如问题 ID 等),服务器代码将检查您是否被允许投票,即您的用户 ID 不应该相同作为帖子创建者的 ID。

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.

爱你不解释 2025-01-04 05:20:02

我的页面上有一个喜欢/不喜欢系统。

数据库表:

1.) 包含您的帖子的表,每个帖子都有唯一的 ID 以及创建该帖子的用户的 user_id(以及内容、标签等其他信息)。

2.) 名为 Likes 的表至少包含以下字段:ID、post_id(对应于帖子表中被喜欢或不喜欢的帖子)、user_id(对应于用户表中喜欢/不喜欢的用户),状态(0 或 1,0 表示喜欢该帖子,1 表示不喜欢该帖子)。

当用户喜欢某个帖子时,将该行及其 user_id 和 post_id 插入到 Likes 表中,将状态设置为 0(或者保留为空,因为 0 是默认值)。当用户不喜欢某个帖子时,执行相同的操作,但将状态设置为 1。

这样,在帖子页面上,您可以获取所有喜欢或不喜欢某个帖子的用户的计数。在用户的个人资料页面上,您可以获取用户喜欢或不喜欢的所有帖子。您还可以根据最喜欢或最不喜欢的帖子对帖子进行排名。甚至可以根据最喜欢或最不喜欢的发布内容的用户对特定用户进行排名。

如果用户在数据库中已有记录,则不允许用户喜欢/不喜欢某个帖子。 (基本上只需检查 Likes 表中的记录数,其中 post_id 等于当前帖子,user_id 等于登录用户)

交叉引用 post 表以获取帖子作者的 user_id。如果帖子作者 user_id 与登录用户相同,或者用户当前未登录,则不允许他们投票。

执行所有这些操作的查询很简单(只需 SELECT * 或 SELECT user_id),但这就是基本思想。

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.

夜夜流光相皎洁 2025-01-04 05:20:02

是的,就是这么简单。一般来说,网站使用 IP 地址,这样用户就不会在不同的帐户上投票两次。

编辑:抱歉,我错了。根据 Quentin 的说法,网站不会以 IP 为基础,因为多个用户可能拥有相同的 IP 并且不会尝试利用该系统。较小规模的网站(至少是我使用过的一些网站)已经实施了基于 IP 的投票系统,因为否则更容易操纵内容排名。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文