Commit 58015e80 by Rocky Duan

make auto-subscribe an optional parameter instead of config

parent c8e929cf
......@@ -31,8 +31,12 @@ end
post '/api/v1/:commentable_id/threads' do |commentable_id|
thread = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id))
thread.author = User.find_or_create_by(external_id: params["user_id"]) if params["user_id"]
author = User.find_or_create_by(external_id: params["user_id"]) if params["user_id"]
thread.author = author
thread.save!
if params["auto_subscribe"]
author.subscribe(thread)
end
thread.to_hash.to_json
end
......@@ -50,8 +54,12 @@ end
post '/api/v1/threads/:thread_id/comments' do |thread_id|
thread = CommentThread.find(thread_id)
comment = thread.comments.new(params.slice(*%w[body course_id]))
comment.author = User.find_or_create_by(external_id: params["user_id"]) if params["user_id"]
author = User.find_or_create_by(external_id: params["user_id"]) if params["user_id"]
comment.author = author
comment.save!
if params["auto_subscribe"]
author.subscribe(thread)
end
comment.to_hash.to_json
end
......
level_limit: 3
send_notifications_to_author: false
auto_subscribe_comment_threads: true
allow_anonymity: false
......@@ -21,7 +21,7 @@ class Comment < Content
validates_presence_of :author if not CommentService.config["allow_anonymity"]
before_destroy :delete_descendants # TODO async
after_create :handle_after_create
after_create :generate_notifications
def self.hash_tree(nodes)
nodes.map{|node, sub_nodes| node.to_hash.merge("children" => hash_tree(sub_nodes).compact)}
......@@ -73,17 +73,6 @@ private
end
end
def auto_subscribe_comment_thread
if CommentService.config["auto_subscribe_comment_threads"] and author
author.subscribe(get_comment_thread)
end
end
def handle_after_create
generate_notifications
auto_subscribe_comment_thread
end
handle_asynchronously :handle_after_create
handle_asynchronously :generate_notifications
end
......@@ -32,7 +32,7 @@ class CommentThread < Content
validates_presence_of :commentable_id
validates_presence_of :author if not CommentService.config["allow_anonymity"]
after_create :handle_after_create
after_create :generate_notifications
def commentable
Commentable.find(commentable_id)
......@@ -86,16 +86,5 @@ private
end
end
def auto_subscribe_comment_thread
if CommentService.config["auto_subscribe_comment_threads"] and author
author.subscribe(self)
end
end
def handle_after_create
generate_notifications
auto_subscribe_comment_thread
end
handle_asynchronously :handle_after_create
handle_asynchronously :generate_notifications
end
......@@ -22,6 +22,7 @@ def init_without_subscriptions
thread = CommentThread.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1", commentable_id: commentable.id)
thread.author = user
thread.save!
user.subscribe(thread)
comment = thread.comments.new(body: "this problem is so easy", course_id: "1")
comment.author = user
......@@ -43,6 +44,7 @@ def init_without_subscriptions
thread = CommentThread.new(title: "This problem is wrong", body: "it is unsolvable", course_id: "2", commentable_id: commentable.id)
thread.author = user
thread.save!
user.subscribe(thread)
comment = thread.comments.new(body: "how do you know?", course_id: "1")
comment.author = user
......@@ -89,6 +91,7 @@ def init_with_subscriptions
thread = CommentThread.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1", commentable_id: commentable.id)
thread.author = user1
user1.subscribe(thread)
user2.subscribe(thread)
thread.save!
......@@ -106,6 +109,7 @@ def init_with_subscriptions
thread = CommentThread.new(title: "This problem is wrong", body: "it is unsolvable", course_id: "2", commentable_id: commentable.id)
thread.author = user2
user2.subscribe(thread)
thread.save!
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