Commit 890c5c0c by Rocky Duan

async callback to generate feeds

parent 463bb4de
...@@ -20,12 +20,16 @@ class Comment ...@@ -20,12 +20,16 @@ class Comment
#validates_presence_of :author # allow anonymity? #validates_presence_of :author # allow anonymity?
before_destroy :delete_descendants before_destroy :delete_descendants
#after_create :create_feeds after_create :generate_feeds
def self.hash_tree(nodes) def self.hash_tree(nodes)
nodes.map{|node, sub_nodes| node.to_hash.merge("children" => hash_tree(sub_nodes).compact)} nodes.map{|node, sub_nodes| node.to_hash.merge("children" => hash_tree(sub_nodes).compact)}
end end
def comment_thread
comment_thread || root.comment_thread
end
def to_hash(params={}) def to_hash(params={})
if params[:recursive] if params[:recursive]
self.class.hash_tree(subtree(order_by: [[:parent_ids, :asc], [:created_at, :asc]])) self.class.hash_tree(subtree(order_by: [[:parent_ids, :asc], [:created_at, :asc]]))
...@@ -36,4 +40,22 @@ class Comment ...@@ -36,4 +40,22 @@ class Comment
end end
end end
def generate_feeds
feed = Feed.new(
feed_type: "post_reply",
info: {
comment_thread_id: comment_thread.id,
comment_thread_title: comment_thread.title,
comment_id: id,
},
)
feed.actor = author
feed.target = self
feed.subscribers << comment_thread.watchers
feed.subscribers << author.followers
feed.save!
end
handle_asynchronously :generate_feeds
end end
...@@ -21,7 +21,7 @@ class CommentThread ...@@ -21,7 +21,7 @@ class CommentThread
validates_presence_of :course_id # do we really need this? validates_presence_of :course_id # do we really need this?
#validates_presence_of :author #allow anonymity? #validates_presence_of :author #allow anonymity?
#after_create :create_feeds after_create :generate_feeds
def to_hash(params={}) def to_hash(params={})
doc = as_document.slice(*%w[title body course_id _id]). doc = as_document.slice(*%w[title body course_id _id]).
...@@ -33,4 +33,22 @@ class CommentThread ...@@ -33,4 +33,22 @@ class CommentThread
doc doc
end end
def generate_feeds
feed = Feed.new(
feed_type: "post_topic",
info: {
commentable_id: commentable.id,
commentable_type: commentable.type,
comment_thread_id: comment_thread.id,
comment_thread_title: comment_thread.title,
},
)
feed.actor = author
feed.target = self
feed.subscribers << commentable.watchers
feed.subscribers << author.followers
feed.save!
end
handle_asynchronously :generate_feeds
end end
...@@ -10,5 +10,9 @@ class Feed ...@@ -10,5 +10,9 @@ class Feed
attr_accessible :feed_type, :info attr_accessible :feed_type, :info
validates_presence_of :feed_type
validates_presence_of :actor
validates_presence_of :target
has_and_belongs_to_many :subscribers, class_name: "User", inverse_of: :subscribed_feeds has_and_belongs_to_many :subscribers, class_name: "User", inverse_of: :subscribed_feeds
end end
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