Commit 4fd9756b by Rocky Duan

add last_updated_at field

parent a6798555
......@@ -27,21 +27,12 @@ class Comment < Content
counter_cache :comment_thread
before_destroy :delete_descendants # TODO async
def self.hash_tree(nodes)
nodes.map{|node, sub_nodes| node.to_hash.merge("children" => hash_tree(sub_nodes).compact)}
end
def thread_title
comment_thread.title
end
def commentable
comment_thread.commentable
end
before_create :set_thread_last_activity_at
before_update :set_thread_last_activity_at
def commentable_id
comment_thread.commentable_id
def self.hash_tree(nodes)
nodes.map{|node, sub_nodes| node.to_hash.merge("children" => hash_tree(sub_nodes).compact)}
end
def to_hash(params={})
......@@ -66,4 +57,10 @@ class Comment < Content
end
end
private
def set_thread_last_activity_at
self.comment_thread.update_attributes!(last_activity_at: Time.now.utc)
end
end
......@@ -14,14 +14,19 @@ class CommentThread < Content
field :commentable_id, type: String
field :anonymous, type: Boolean, default: false
field :at_position_list, type: Array, default: []
field :last_activity_at, type: Time
include Sunspot::Mongoid
searchable do
text :title, boost: 5.0, stored: true, more_like_this: true
text :body, stored: true, more_like_this: true
text :tags do
tags_array.join " "
end
time :created_at
time :updated_at
time :last_activity_at
integer :comment_count
integer :votes_point do
votes_point
......@@ -48,6 +53,10 @@ class CommentThread < Content
validate :tag_names_valid
validate :tag_names_unique
before_create :set_last_activity_at
before_update :set_last_activity_at
def self.new_dumb_thread(options={})
c = self.new
c.title = options[:title] || "title"
......@@ -105,10 +114,6 @@ class CommentThread < Content
!!(tag =~ RE_TAG)
end
def thread_title
title
end
private
RE_HEADCHAR = /[a-z0-9]/
......@@ -131,4 +136,8 @@ private
errors.add :tags, "must be unique"
end
end
def set_last_activity_at
self.last_activity_at = Time.now.utc unless last_activity_at_changed?
end
end
......@@ -23,16 +23,27 @@ class AtUserObserver < Mongoid::Observer
new_user_ids = current_user_ids - prev_user_ids
if content_type == :thread
thread_title = content.title
thread_id = content.id
commentable_id = content.commentable_id
else
thread_title = content.comment_thread.title
thread_id = content.comment_thread.id
commentable_id = content.comment_thread.commentable_id
end
unless new_user_ids.empty?
notification = Notification.new(
notification_type: "at_user",
info: {
content_id: content.id,
comment_id: (content.id if content_type == :comment),
content_type: content_type,
thread_title: content.thread_title,
thread_title: thread_title,
thread_id: thread_id,
actor_username: content.author_with_anonymity(:username),
commentable_id: content.commentable_id,
commentable_id: commentable_id,
}
)
notification.actor = content.author_with_anonymity
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment