Commit 31a04cee by Rocky Duan

make every comment linked to its thread

parent 9250057d
...@@ -42,15 +42,17 @@ namespace :test do ...@@ -42,15 +42,17 @@ namespace :test do
comment.save! comment.save!
comment1 = comment.children.new(body: "not for me!", course_id: "1") comment1 = comment.children.new(body: "not for me!", course_id: "1")
comment1.author = user comment1.author = user
comment1.comment_thread = comment_thread
comment1.save! comment1.save!
comment2 = comment1.children.new(body: "not for me neither!", course_id: "1") comment2 = comment1.children.new(body: "not for me neither!", course_id: "1")
comment2.author = user comment2.author = user
comment2.comment_thread = comment_thread
comment2.save! comment2.save!
children = comment_thread.comments.first.to_hash(recursive: true)["children"] children = comment_thread.root_comments.first.to_hash(recursive: true)["children"]
if children.length == 2 if children.length == 2
pp comment_thread.to_hash(recursive: true) pp comment_thread.to_hash(recursive: true)
pp comment_thread.comments.first.descendants_and_self.to_a pp comment_thread.root_comments.first.descendants_and_self.to_a
puts "error!" puts "error!"
break break
end end
...@@ -121,6 +123,7 @@ namespace :db do ...@@ -121,6 +123,7 @@ namespace :db do
comment = comment_thread.comments.new(body: comment_body_seeds.sample, course_id: "1") comment = comment_thread.comments.new(body: comment_body_seeds.sample, course_id: "1")
comment.author = users.sample comment.author = users.sample
comment.endorsed = [true, false].sample comment.endorsed = [true, false].sample
comment.comment_thread = comment_thread
comment.save! comment.save!
comments << comment comments << comment
end end
...@@ -129,6 +132,7 @@ namespace :db do ...@@ -129,6 +132,7 @@ namespace :db do
sub_comment = comment.children.new(body: comment_body_seeds.sample, course_id: "1") sub_comment = comment.children.new(body: comment_body_seeds.sample, course_id: "1")
sub_comment.author = users.sample sub_comment.author = users.sample
sub_comment.endorsed = [true, false].sample sub_comment.endorsed = [true, false].sample
sub_comment.comment_thread = comment_thread
sub_comment.save! sub_comment.save!
comments << sub_comment comments << sub_comment
end end
......
...@@ -84,6 +84,7 @@ post '/api/v1/comments/:comment_id' do |comment_id| ...@@ -84,6 +84,7 @@ post '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id) comment = Comment.find(comment_id)
sub_comment = comment.children.new(params.slice(*%w[body course_id])) sub_comment = comment.children.new(params.slice(*%w[body course_id]))
sub_comment.author = User.find_or_create_by(external_id: params["user_id"]) sub_comment.author = User.find_or_create_by(external_id: params["user_id"])
sub_comment.comment_thread = comment.comment_thread
sub_comment.save! sub_comment.save!
sub_comment.to_hash.to_json sub_comment.to_hash.to_json
end end
......
...@@ -19,6 +19,7 @@ class Comment < Content ...@@ -19,6 +19,7 @@ class Comment < Content
validates_presence_of :body validates_presence_of :body
validates_presence_of :course_id # do we really need this? validates_presence_of :course_id # do we really need this?
validates_presence_of :author if not CommentService.config["allow_anonymity"] validates_presence_of :author if not CommentService.config["allow_anonymity"]
validates_presence_of :comment_thread
before_destroy :delete_descendants # TODO async before_destroy :delete_descendants # TODO async
after_create :generate_notifications after_create :generate_notifications
...@@ -27,14 +28,6 @@ class Comment < Content ...@@ -27,14 +28,6 @@ class Comment < Content
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 get_comment_thread
if comment_thread
comment_thread
else
root.comment_thread
end
end
def to_hash(params={}) def to_hash(params={})
sort_by_parent_and_time = Proc.new do |x, y| sort_by_parent_and_time = Proc.new do |x, y|
arr_cmp = x.parent_ids <=> y.parent_ids arr_cmp = x.parent_ids <=> y.parent_ids
...@@ -50,24 +43,25 @@ class Comment < Content ...@@ -50,24 +43,25 @@ class Comment < Content
as_document.slice(*%w[body course_id endorsed created_at updated_at]). as_document.slice(*%w[body course_id endorsed created_at updated_at]).
merge("id" => _id). merge("id" => _id).
merge("user_id" => author.id). merge("user_id" => author.id).
merge("thread_id" => comment_thread.id).
merge("votes" => votes.slice(*%w[count up_count down_count point])) merge("votes" => votes.slice(*%w[count up_count down_count point]))
end end
end end
private private
def generate_notifications def generate_notifications
if get_comment_thread.subscribers or (author.followers if author) if comment_thread.subscribers or (author.followers if author)
notification = Notification.new( notification = Notification.new(
notification_type: "post_reply", notification_type: "post_reply",
info: { info: {
thread_id: get_comment_thread.id, thread_id: comment_thread.id,
thread_title: get_comment_thread.title, thread_title: comment_thread.title,
comment_id: id, comment_id: id,
}, },
) )
notification.actor = author notification.actor = author
notification.target = self notification.target = self
notification.receivers << (get_comment_thread.subscribers + author.followers).uniq_by(&:id) notification.receivers << (comment_thread.subscribers + author.followers).uniq_by(&:id)
notification.receivers.delete(author) if not CommentService.config["send_notifications_to_author"] notification.receivers.delete(author) if not CommentService.config["send_notifications_to_author"]
notification.save! notification.save!
end end
......
...@@ -34,6 +34,10 @@ class CommentThread < Content ...@@ -34,6 +34,10 @@ class CommentThread < Content
after_create :generate_notifications after_create :generate_notifications
def root_comments
Comment.roots.where(comment_thread_id: self.id)
end
def commentable def commentable
Commentable.find(commentable_id) Commentable.find(commentable_id)
end end
...@@ -52,7 +56,7 @@ class CommentThread < Content ...@@ -52,7 +56,7 @@ class CommentThread < Content
merge("user_id" => (author.id if author)). merge("user_id" => (author.id if author)).
merge("votes" => votes.slice(*%w[count up_count down_count point])) merge("votes" => votes.slice(*%w[count up_count down_count point]))
if params[:recursive] if params[:recursive]
doc = doc.merge("children" => comments.map{|c| c.to_hash(recursive: true)}) doc = doc.merge("children" => root_comments.map{|c| c.to_hash(recursive: true)})
end end
doc doc
end end
......
...@@ -29,9 +29,11 @@ def init_without_subscriptions ...@@ -29,9 +29,11 @@ def init_without_subscriptions
comment.save! comment.save!
comment1 = comment.children.new(body: "not for me!", course_id: "1") comment1 = comment.children.new(body: "not for me!", course_id: "1")
comment1.author = user comment1.author = user
comment1.comment_thread = thread
comment1.save! comment1.save!
comment2 = comment1.children.new(body: "not for me neither!", course_id: "1") comment2 = comment1.children.new(body: "not for me neither!", course_id: "1")
comment2.author = user comment2.author = user
comment2.comment_thread = thread
comment2.save! comment2.save!
comment = thread.comments.new(body: "see the textbook on page 69. it's quite similar", course_id: "1") comment = thread.comments.new(body: "see the textbook on page 69. it's quite similar", course_id: "1")
...@@ -39,6 +41,7 @@ def init_without_subscriptions ...@@ -39,6 +41,7 @@ def init_without_subscriptions
comment.save! comment.save!
comment1 = comment.children.new(body: "thank you!", course_id: "1") comment1 = comment.children.new(body: "thank you!", course_id: "1")
comment1.author = user comment1.author = user
comment1.comment_thread = thread
comment1.save! comment1.save!
thread = CommentThread.new(title: "This problem is wrong", body: "it is unsolvable", course_id: "2", commentable_id: commentable.id) thread = CommentThread.new(title: "This problem is wrong", body: "it is unsolvable", course_id: "2", commentable_id: commentable.id)
...@@ -51,12 +54,14 @@ def init_without_subscriptions ...@@ -51,12 +54,14 @@ def init_without_subscriptions
comment.save! comment.save!
comment1 = comment.children.new(body: "because blablabla", course_id: "1") comment1 = comment.children.new(body: "because blablabla", course_id: "1")
comment1.author = user comment1.author = user
comment1.comment_thread = thread
comment1.save! comment1.save!
comment = thread.comments.new(body: "no wonder I can't solve it", course_id: "1") comment = thread.comments.new(body: "no wonder I can't solve it", course_id: "1")
comment.author = user comment.author = user
comment.save! comment.save!
comment1 = comment.children.new(body: "+1", course_id: "1") comment1 = comment.children.new(body: "+1", course_id: "1")
comment1.author = user comment1.author = user
comment1.comment_thread = thread
comment1.save! comment1.save!
users = (2..10).map{|id| User.find_or_create_by(external_id: id.to_s)} users = (2..10).map{|id| User.find_or_create_by(external_id: id.to_s)}
...@@ -102,9 +107,11 @@ def init_with_subscriptions ...@@ -102,9 +107,11 @@ def init_with_subscriptions
comment.save! comment.save!
comment1 = comment.children.new(body: "not for me!", course_id: "1") comment1 = comment.children.new(body: "not for me!", course_id: "1")
comment1.author = user1 comment1.author = user1
comment1.comment_thread = thread
comment1.save! comment1.save!
comment2 = comment1.children.new(body: "not for me neither!", course_id: "1") comment2 = comment1.children.new(body: "not for me neither!", course_id: "1")
comment2.author = user2 comment2.author = user2
comment2.comment_thread = thread
comment2.save! comment2.save!
thread = CommentThread.new(title: "This problem is wrong", body: "it is unsolvable", course_id: "2", commentable_id: commentable.id) thread = CommentThread.new(title: "This problem is wrong", body: "it is unsolvable", course_id: "2", commentable_id: commentable.id)
...@@ -185,8 +192,8 @@ describe "app" do ...@@ -185,8 +192,8 @@ describe "app" do
thread.course_id.should == response_thread["course_id"] thread.course_id.should == response_thread["course_id"]
thread.votes_point.should == response_thread["votes"]["point"] thread.votes_point.should == response_thread["votes"]["point"]
response_thread["children"].should_not be_nil response_thread["children"].should_not be_nil
response_thread["children"].length.should == thread.comments.length response_thread["children"].length.should == thread.root_comments.length
response_thread["children"].index{|c| c["body"] == thread.comments.first.body}.should_not be_nil response_thread["children"].index{|c| c["body"] == thread.root_comments.first.body}.should_not be_nil
end end
end end
describe "PUT /api/v1/threads/:thread_id" do describe "PUT /api/v1/threads/:thread_id" do
......
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