通知 ala Facebook(数据库实现) [英] Notifications ala Facebook (database implementation)

查看:16
本文介绍了通知 ala Facebook(数据库实现)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 Facebook 如何实施他们的通知系统,因为我正在做类似的事情.

I am wondering how Facebook implements their notifications system as I'm looking to do something similar.

  • FooBar 评论了您的状态
  • Red1、Green2 和 Blue3 对您的照片发表了评论
  • MegaMan 和其他 5 人对您的活动发表了评论

我不能将多个通知写入单个记录,因为最终我将有与每个通知相关联的操作.此外,在视图中,当单个主题存在一定数量的通知时,我希望将通知呈现为可扩展列表.

I can't have multiple notifications written into a single record, as eventually I will have actions associated with each notification. Also, in the view I'd like notifications to be rendered as expandable lists when a certain number of them exist for a single subject.

  • FooBar 评论了您的状态(操作)
  • Red1、Green2 和 Pink5 对您的照片发表了评论 [+]
  • MegaMan 和其他 3 人对您的活动发表了评论 [-]
    • MegaMan 评论了您的活动(动作)
    • ProtoMan 评论了您的活动(动作)
    • Bass 评论了您的活动(动作)
    • DrWilly 评论了您的活动(行动)

    干杯!

    PS 顺便说一句,我正在使用 postgres 和 rails.

    PS I am using postgres and rails BTW.

    推荐答案

    有多种方法可以实现这一点.这实际上取决于您想要涵盖哪些类型的通知以及您需要收集哪些有关通知的信息以将其显示给正确的用户.如果您正在寻找一种仅涵盖有关已发布评论的通知的简单设计,您可以使用 多态关联观察者回调:

    There are a number of ways to go about implementing this. It really depends on what kinds of notifications you want to cover and what information you need to collect about the notification to show it to the right user(s). If you are looking for a simple design that just covers notifications about posted comments, you could use a combination of polymorphic associations and observer callbacks:

    class Photo < ActiveRecord::Base
    # or Status or Event
        has_many :comments, :as => :commentable
    end
    
    class Comment < ActiveRecord::Base
        belongs_to :commenter
        belongs_to :commentable, :polymorphic => true # the photo, status or event
    end
    
    class CommentNotification < ActiveRecord::Base
        belongs_to :comment
        belongs_to :target_user
    end
    
    class CommentObserver < ActiveRecord::Observer
        observe :comment
    
        def after_create(comment)
            ...
            CommentNotification.create!(comment_id: comment.id,
              target_user_id: comment.commentable.owner.id)
            ...
        end
    end
    

    这里发生的事情是每张照片、状态、事件等都有很多评论.Comment 显然属于 :commenter 但也属于 :commentable,它可以是照片、状态、事件或任何其他模型你想允许评论.然后,您有一个 CommentObserver,它将观察您的 Comment 模型,并在 Comment 表发生某些事情时执行某些操作.在这种情况下,在创建一个 Comment 之后,观察者将创建一个 CommentNotification,其中包含评论的 id 和拥有该评论的事物的用户的 id关于(comment.commentable.owner.id).这将要求您为每个想要评论的模型实现一个简单的方法 :owner.因此,例如,如果可评论是一张照片,则所有者就是发布该照片的用户.

    What's happening here is that each photo, status, event etc. has many comments. A Comment obviously belongs to a :commenter but also to a :commentable, which is either a photo, status, event or any other model that you want to allow comments for. You then have a CommentObserver that will observe your Comment model and do something whenever something happens with the Comment table. In this case, after a Comment is created, the observer will create a CommentNotification with the id of the comment and the id of the user who owns the thing that the comment is about (comment.commentable.owner.id). This would require that you implement a simple method :owner for each model you want to have comments for. So, for example, if the commentable is a photo, the owner would be the user who posted the photo.

    这个基本设计应该足以让您入门,但请注意,如果您想为评论以外的内容创建通知,您可以通过在更通用的Notification 模型.

    This basic design should be enough to get you started, but note that if you want to create notifications for things other than comments, you could extend this design by using a polymorphic association in a more general Notification model.

    class Notification < ActiveRecord::Base
        belongs_to :notifiable, :polymorphic => true
        belongs_to :target_user
    end
    

    使用此设计,您将观察"所有 notifiable(您要为其创建通知的模型)并在 after_create 回调中执行类似以下操作:

    With this design, you would then 'observe' all your notifiables (models that you want to create notifications for) and do something like the following in your after_create callback:

    class GenericObserver < ActiveRecord::Observer
        observe :comment, :like, :wall_post
    
        def after_create(notifiable)
            ...
            Notification.create!(notifiable_id: notifiable.id, 
                               notifiable_type: notifiable.class.name,
                                target_user_id: notifiable.user_to_notify.id)
            ...
        end
    end
    

    这里唯一棘手的部分是 user_to_notify 方法.notifiable 的所有模型都必须以某种方式实现它,具体取决于模型是什么.例如,wall_post.user_to_notify 只是墙的所有者,或者 like.user_to_notify 将是喜欢"的东西的所有者.您甚至可能要通知多个人,例如在有人评论照片时通知照片中标记的所有人.

    The only tricky part here is the user_to_notify method. All models that are notifiable would have to implement it in some way depending on what the model is. For example, wall_post.user_to_notify would just be the owner of the wall, or like.user_to_notify would be the owner of the thing that was 'liked'. You might even have multiple people to notify, like when notifying all the people tagged in a photo when someone comments on it.

    希望这会有所帮助.

    这篇关于通知 ala Facebook(数据库实现)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆