如何创建评论回复通知?

时间:2019-05-28 11:48:18

标签: ruby-on-rails ruby

我的应用上有一种线程注释系统。每当创建评论时,我都会使用https://github.com/rails-engine/notifications/进行通知。如何同时为评论回复创建通知?

这就是我对单条评论通知所做的

comment.rb

belongs_to :commentable, polymorphic: true
belongs_to :user
belongs_to :parent, class_name: 'Comment', optional: true
has_many :replies, class_name: 'Comment', foreign_key: :parent_id, dependent: :destroy

  validates :content, presence: true, length: { maximum: 255 }

  after_commit :notify_comment_created, on: [:create]
  def notify_comment_created
    return if self.commentable.blank?
    receiver_id = self.commentable&.user_id

    Notification.create(
      notify_type: "comment",
      target: self,
      second_target: self.commentable,
      actor_id: self.user_id,
      user_id: receiver_id
    )
  end

notifications / comment.html.erb

<% if notification.second_target.is_a?(Article) %>
    <div class=''>
      <p class="notifications-p"><i class="fa fa-comment text-secondary"></i> <b><%= link_to notification.actor.username, main_app.profile_path(notification.actor.username) %></b> commented on your <b><%= notification.second_target.class.name %>:</b> <b><%= link_to notification.second_target.title, main_app.article_path(notification.second_target) %></b></p>
    </div>

    <div class=''>
      <p class="notifications-p"><i><%= truncate(notification.target.content, length: 100) %></i></p>
    </div>
  <% elsif notification.second_target.is_a?(Announcement) %>
    <div class=''>
      <p class="notifications-p"><i class="fa fa-comment text-secondary"></i> <b><%= link_to notification.actor.username, main_app.profile_path(notification.actor.username) %></b> commented on your <b><%= notification.second_target.class.name %>:</b> <b><%= link_to notification.second_target.title, main_app.announcement_path(notification.second_target) %></b></p>
    </div>

    <div class=''>
      <p class="notifications-p"><i><%= truncate(notification.target.content, length: 100) %></i></p>
    </div>
<% end %>

模式中的注释表

create_table "comments", force: :cascade do |t|
    t.string "commentable_type"
    t.bigint "commentable_id"
    t.bigint "user_id"
    t.text "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "parent_id"
    t.index ["commentable_type", "commentable_id"], name: "index_comments_on_commentable_type_and_commentable_id"
    t.index ["parent_id"], name: "index_comments_on_parent_id"
    t.index ["user_id"], name: "index_comments_on_user_id"
end

_comment.html.erb

...
<li id="comment_<%= comment.id %>" class="comment">
   <%= link_to "Reply", polymorphic_path([:reply, comment.commentable, comment]), class: 'reply-comment-link', remote: true  %>
  <% if comment.replies.any? %>
    <ul>
      <%= render comment.replies %>
    </ul>
  <% end %>
</li>

reply.js.erb

<% if @comment.errors.empty? %>
  $("#comment_<%= @comment.id %> > .comment-info .reply-comment-link").removeAttr("href");
  $("#comment_<%= @comment.id %> > .comment-info").after("<%=j render('comments/form', comment: @reply) %>");
...
<% end %>

当前,如果回复了评论,则self.commentable会将回复通知作为新评论。总之,我想要的是comment.user收到通知作为答复。

0 个答案:

没有答案
相关问题