Commit 4fd9756b by Rocky Duan

add last_updated_at field

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