Commit a6798555 by Rocky Duan

some minor refactors

parent cc0ef24e
...@@ -61,7 +61,7 @@ get "#{api_prefix}/search/threads" do ...@@ -61,7 +61,7 @@ get "#{api_prefix}/search/threads" do
end end
num_pages = [1, (search.total / per_page.to_f).ceil].max num_pages = [1, (search.total / per_page.to_f).ceil].max
{ {
collection: search.results.map{|t| t.to_hash(recursive: value_to_boolean(params["recursive"]))}, collection: search.results.map{|t| t.to_hash(recursive: bool_recursive)},
num_pages: num_pages, num_pages: num_pages,
page: page, page: page,
}.to_json }.to_json
...@@ -100,7 +100,7 @@ get "#{api_prefix}/:commentable_id/threads" do |commentable_id| ...@@ -100,7 +100,7 @@ get "#{api_prefix}/:commentable_id/threads" do |commentable_id|
page = [num_pages, [1, page].max].min page = [num_pages, [1, page].max].min
paged_comment_threads = comment_threads.page(page).per(per_page) paged_comment_threads = comment_threads.page(page).per(per_page)
{ {
collection: paged_comment_threads.map{|t| t.to_hash(recursive: value_to_boolean(params["recursive"]))}, collection: paged_comment_threads.map{|t| t.to_hash(recursive: bool_recursive)},
num_pages: num_pages, num_pages: num_pages,
page: page, page: page,
}.to_json }.to_json
...@@ -109,14 +109,14 @@ end ...@@ -109,14 +109,14 @@ end
post "#{api_prefix}/:commentable_id/threads" do |commentable_id| post "#{api_prefix}/:commentable_id/threads" do |commentable_id|
thread = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id)) thread = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id))
thread.anonymous = value_to_boolean(params["anonymous"]) || false thread.anonymous = bool_anonymous || false
thread.tags = params["tags"] || "" thread.tags = params["tags"] || ""
thread.author = user thread.author = user
thread.save thread.save
if thread.errors.any? if thread.errors.any?
error 400, thread.errors.full_messages.to_json error 400, thread.errors.full_messages.to_json
else else
user.subscribe(thread) if value_to_boolean params["auto_subscribe"] user.subscribe(thread) if bool_auto_subscribe
thread.to_hash.to_json thread.to_hash.to_json
end end
end end
...@@ -130,7 +130,7 @@ get "#{api_prefix}/threads/tags/autocomplete" do ...@@ -130,7 +130,7 @@ get "#{api_prefix}/threads/tags/autocomplete" do
end end
get "#{api_prefix}/threads/:thread_id" do |thread_id| get "#{api_prefix}/threads/:thread_id" do |thread_id|
CommentThread.find(thread_id).to_hash(recursive: value_to_boolean(params["recursive"])).to_json CommentThread.find(thread_id).to_hash(recursive: bool_recursive).to_json
end end
put "#{api_prefix}/threads/:thread_id" do |thread_id| put "#{api_prefix}/threads/:thread_id" do |thread_id|
...@@ -148,13 +148,13 @@ end ...@@ -148,13 +148,13 @@ end
post "#{api_prefix}/threads/:thread_id/comments" do |thread_id| post "#{api_prefix}/threads/:thread_id/comments" do |thread_id|
comment = thread.comments.new(params.slice(*%w[body course_id])) comment = thread.comments.new(params.slice(*%w[body course_id]))
comment.anonymous = value_to_boolean(params["anonymous"]) || false comment.anonymous = bool_anonymous || false
comment.author = user comment.author = user
comment.save comment.save
if comment.errors.any? if comment.errors.any?
error 400, comment.errors.full_messages.to_json error 400, comment.errors.full_messages.to_json
else else
user.subscribe(thread) if value_to_boolean params["auto_subscribe"] user.subscribe(thread) if bool_auto_subscribe
comment.to_hash.to_json comment.to_hash.to_json
end end
end end
...@@ -165,7 +165,7 @@ delete "#{api_prefix}/threads/:thread_id" do |thread_id| ...@@ -165,7 +165,7 @@ delete "#{api_prefix}/threads/:thread_id" do |thread_id|
end end
get "#{api_prefix}/comments/:comment_id" do |comment_id| get "#{api_prefix}/comments/:comment_id" do |comment_id|
comment.to_hash(recursive: value_to_boolean(params["recursive"])).to_json comment.to_hash(recursive: bool_recursive).to_json
end end
put "#{api_prefix}/comments/:comment_id" do |comment_id| put "#{api_prefix}/comments/:comment_id" do |comment_id|
...@@ -179,14 +179,14 @@ end ...@@ -179,14 +179,14 @@ end
post "#{api_prefix}/comments/:comment_id" do |comment_id| post "#{api_prefix}/comments/:comment_id" do |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.anonymous = value_to_boolean(params["anonymous"]) || false sub_comment.anonymous = bool_anonymous || false
sub_comment.author = user sub_comment.author = user
sub_comment.comment_thread = comment.comment_thread sub_comment.comment_thread = comment.comment_thread
sub_comment.save sub_comment.save
if sub_comment.errors.any? if sub_comment.errors.any?
error 400, sub_comment.errors.full_messages.to_json error 400, sub_comment.errors.full_messages.to_json
else else
user.subscribe(comment.comment_thread) if value_to_boolean params["auto_subscribe"] user.subscribe(comment.comment_thread) if bool_auto_subscribe
sub_comment.to_hash.to_json sub_comment.to_hash.to_json
end end
end end
...@@ -225,7 +225,7 @@ post "#{api_prefix}/users" do ...@@ -225,7 +225,7 @@ post "#{api_prefix}/users" do
end end
get "#{api_prefix}/users/:user_id" do |user_id| get "#{api_prefix}/users/:user_id" do |user_id|
user.to_hash(complete: value_to_boolean(params["complete"])).to_json user.to_hash(complete: bool_complete).to_json
end end
put "#{api_prefix}/users/:user_id" do |user_id| put "#{api_prefix}/users/:user_id" do |user_id|
......
...@@ -47,4 +47,20 @@ helpers do ...@@ -47,4 +47,20 @@ helpers do
!!(value.to_s =~ /^true$/i) !!(value.to_s =~ /^true$/i)
end end
def bool_recursive
value_to_boolean params["recursive"]
end
def bool_complete
value_to_boolean params["complete"]
end
def bool_auto_subscribe
value_to_boolean params["auto_subscribe"]
end
def bool_anonymous
value_to_boolean params["anonymous"]
end
end end
...@@ -8,20 +8,20 @@ class Comment < Content ...@@ -8,20 +8,20 @@ class Comment < Content
voteable self, :up => +1, :down => -1 voteable self, :up => +1, :down => -1
field :body, type: String
field :course_id, type: String field :course_id, type: String
field :body, type: String
field :endorsed, type: Boolean, default: false field :endorsed, type: Boolean, default: false
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: []
belongs_to :author, class_name: "User", index: true
belongs_to :comment_thread, index: true belongs_to :comment_thread, index: true
belongs_to :author, class_name: "User", index: true
attr_accessible :body, :course_id, :endorsed, :anonymous attr_accessible :body, :course_id, :anonymous, :endorsed
validates_presence_of :body
validates_presence_of :course_id # do we really need this?
validates_presence_of :comment_thread, autosave: false validates_presence_of :comment_thread, autosave: false
validates_presence_of :body
validates_presence_of :course_id
validates_presence_of :author, autosave: false validates_presence_of :author, autosave: false
counter_cache :comment_thread counter_cache :comment_thread
...@@ -32,6 +32,18 @@ class Comment < Content ...@@ -32,6 +32,18 @@ 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 thread_title
comment_thread.title
end
def commentable
comment_thread.commentable
end
def commentable_id
comment_thread.commentable_id
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.map(&:to_s) <=> y.parent_ids.map(&:to_s) arr_cmp = x.parent_ids.map(&:to_s) <=> y.parent_ids.map(&:to_s)
......
...@@ -105,6 +105,10 @@ class CommentThread < Content ...@@ -105,6 +105,10 @@ 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]/
......
class Content class Content
include Mongoid::Document include Mongoid::Document
def author_with_anonymity(attr=nil, attr_when_anonymous=nil)
if not attr
anonymous ? nil : author
else
anonymous ? attr_when_anonymous : author.send(attr)
end
end
end end
...@@ -24,16 +24,18 @@ class AtUserObserver < Mongoid::Observer ...@@ -24,16 +24,18 @@ class AtUserObserver < Mongoid::Observer
new_user_ids = current_user_ids - prev_user_ids new_user_ids = current_user_ids - prev_user_ids
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, content_id: content.id,
content_type: content_type, content_type: content_type,
thread_title: content_type == :thread ? content.title : content.comment_thread.title, thread_title: content.thread_title,
actor_username: (content.author.username if not content.anonymous), actor_username: content.author_with_anonymity(:username),
commentable_id: content.commentable_id,
} }
) )
notification.actor = content.author if not content.anonymous notification.actor = content.author_with_anonymity
notification.target = content notification.target = content
receivers = new_user_ids.map { |id| User.find(id) } receivers = new_user_ids.map { |id| User.find(id) }
receivers.delete(content.author) receivers.delete(content.author)
......
...@@ -14,15 +14,12 @@ class PostReplyObserver < Mongoid::Observer ...@@ -14,15 +14,12 @@ class PostReplyObserver < Mongoid::Observer
thread_title: comment.comment_thread.title, thread_title: comment.comment_thread.title,
comment_id: comment.id, comment_id: comment.id,
commentable_id: comment.comment_thread.commentable_id, commentable_id: comment.comment_thread.commentable_id,
actor_username: (comment.author.username if not comment.anonymous), actor_username: comment.author_with_anonymity(:username),
}, },
) )
notification.actor = comment.author if not comment.anonymous notification.actor = comment.author_with_anonymity
notification.target = comment notification.target = comment
receivers = comment.comment_thread.subscribers receivers = (comment.comment_thread.subscribers + comment.author_with_anonymity(:followers, [])).uniq_by(&:id)
if not comment.anonymous
receivers = (receivers + comment.author.followers).uniq_by(&:id)
end
receivers.delete(comment.author) receivers.delete(comment.author)
notification.receivers << receivers notification.receivers << receivers
notification.save! notification.save!
......
...@@ -11,18 +11,14 @@ class PostTopicObserver < Mongoid::Observer ...@@ -11,18 +11,14 @@ class PostTopicObserver < Mongoid::Observer
notification_type: "post_topic", notification_type: "post_topic",
info: { info: {
commentable_id: comment_thread.commentable_id, commentable_id: comment_thread.commentable_id,
#commentable_type: commentable.commentable_type,
thread_id: comment_thread.id, thread_id: comment_thread.id,
thread_title: comment_thread.title, thread_title: comment_thread.title,
actor_username: (comment_thread.author.username if not comment_thread.anonymous), actor_username: comment_thread.author_with_anonymity(:username),
}, },
) )
notification.actor = comment_thread.author if not comment_thread.anonymous notification.actor = comment_thread.author_with_anonymity
notification.target = comment_thread notification.target = comment_thread
receivers = comment_thread.commentable.subscribers receivers = (comment_thread.commentable.subscribers + comment_thread.author_with_anonymity(:followers, [])).uniq_by(&:id)
if not comment_thread.anonymous
receivers = (receivers + comment_thread.author.followers).uniq_by(&:id)
end
receivers.delete(comment_thread.author) receivers.delete(comment_thread.author)
notification.receivers << receivers notification.receivers << receivers
notification.save! notification.save!
......
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