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

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

问题描述




  • FooBar对您的状态发表了评论

    / li>
  • 对您的照片发表了评论

  • MegaMan和另外5人对您的照片发表了评论


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




    • FooBar对您的状态(动作)发表了评论。

    • Red1,Green2和Pink5对您的照片发表了评论[+]

    • MegaMan和另外3人对您的活动发表了评论[ - ]

      • MegaMan评论了您的活动(动作)

      • ProtoMan对您的活动发表了评论

      • Bass评论了您的活动(动作)

      • DrWilly评论了您的活动(动作)




    干杯!



    PS 我使用postgres和rails BTW。

    解决方案

    有很多方法可以实现。这取决于您想要覆盖哪些类型的通知,以及您需要收集有关通知的哪些信息以向适当的用户展示。如果您正在寻找一个只包含发布评论通知的简单设计,您可以使用多态关联观察者回调

      class Photo< ActiveRecord :: Base 
    #或状态或事件
    has_many:comments,:as => :commentable
    end

    class Comment< ActiveRecord :: Base
    belongs_to:commenter
    belongs_to:commentable,:polymorphic => true#照片,状态或事件
    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


    b $ b

    这里发生的是,每张照片,状态,事件等有很多评论。 注释显然属于:commenter ,但也属于:commentable ,这是一个照片,状态,事件或任何其他模型,你要允许评论。然后你有一个 CommentObserver ,将观察你的注释模型,并做某事发生与注释表。在这种情况下,在创建注释之后,观察者将创建一个带有注释id的 CommentNotification 拥有该评论所关注事项的用户的ID( comment.commentable.owner.id )。这将要求您为每个想要注释的模型实现一个简单的方法:owner 。例如,如果可评论的是照片,则所有者将是发布照片的用户。



    这个基本设计应该足以让您开始注意,如果您要为除注释以外的事情创建通知,您可以通过在更通用的通知模型中使用多态关联来扩展此设计。

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

    观察所有通知(要为其创建通知的模型),并在 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 方法。所有可通知的模型必须根据模型的不同以某种方式实现它。例如, wall_post.user_to_notify 将只是墙的所有者,或 like.user_to_notify 喜欢的东西。您甚至可以有多个人通知,例如当有人评论时,通知所有在相片中标记的人。



    希望这会有帮助。


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

    • FooBar commented on your status
    • Red1, Green2 and Blue3 commented on your photo
    • MegaMan and 5 others commented on your event

    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 commented on your status (actions)
    • Red1, Green2 and Pink5 commented on your photo [+]
    • MegaMan and 3 others commented on your event [-]
      • MegaMan commented on your event (actions)
      • ProtoMan commented on your event (actions)
      • Bass commented on your event (actions)
      • DrWilly commented on your event (actions)

    Cheers!

    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
    

    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.

    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
    

    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
    

    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.

    Hope this helps.

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

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