Commit e097ed98 by Arjun Singh

Anonymous to peers

parent d0656e8e
......@@ -22,6 +22,7 @@ end
post "#{APIPREFIX}/threads/:thread_id/comments" do |thread_id|
comment = Comment.new(params.slice(*%w[body course_id]))
comment.anonymous = bool_anonymous || false
comment.anonymous_to_peers = bool_anonymous_to_peers || false
comment.author = user
comment.comment_thread = thread
comment.save
......
......@@ -12,6 +12,7 @@ end
post "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
thread = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id))
thread.anonymous = bool_anonymous || false
thread.anonymous_to_peers = bool_anonymous_to_peers || false
thread.tags = params["tags"] || ""
thread.author = user
thread.save
......
......@@ -14,6 +14,7 @@ end
post "#{APIPREFIX}/comments/:comment_id" do |comment_id|
sub_comment = comment.children.new(params.slice(*%w[body course_id]))
sub_comment.anonymous = bool_anonymous || false
sub_comment.anonymous_to_peers = bool_anonymous_to_peers || false
sub_comment.author = user
sub_comment.comment_thread = comment.comment_thread
sub_comment.save
......
......@@ -23,7 +23,7 @@ get "#{APIPREFIX}/users/:user_id/active_threads" do |user_id|
page = (params["page"] || DEFAULT_PAGE).to_i
per_page = (params["per_page"] || DEFAULT_PER_PAGE).to_i
active_contents = Content.where(author_id: user_id, anonymous: false, course_id: params["course_id"])
active_contents = Content.where(author_id: user_id, anonymous: false, anonymous_to_peers: false, course_id: params["course_id"])
.order_by(updated_at: :desc)
num_pages = [1, (active_contents.count / per_page.to_f).ceil].max
......
......@@ -63,6 +63,10 @@ helpers do
value_to_boolean params["anonymous"]
end
def bool_anonymous_to_peers
value_to_boolean params["anonymous_to_peers"]
end
def handle_paged_threads_query(paged_comment_threads)
end
......@@ -126,7 +130,7 @@ helpers do
contents.map do |content|
content['children'] = author_contents_only(content['children'], author_id)
if content['children'].length > 0 or \
(content['user_id'] == author_id and not content['anonymous'])
(content['user_id'] == author_id and not content['anonymous'] and not content['anonymous_to_peers'])
content
else
nil
......
......@@ -13,6 +13,7 @@ class Comment < Content
field :body, type: String
field :endorsed, type: Boolean, default: false
field :anonymous, type: Boolean, default: false
field :anonymous_to_peers, type: Boolean, default: false
field :at_position_list, type: Array, default: []
index({author_id: 1, course_id: 1})
......@@ -20,7 +21,7 @@ class Comment < Content
belongs_to :comment_thread, index: true
belongs_to :author, class_name: "User", index: true
attr_accessible :body, :course_id, :anonymous, :endorsed
attr_accessible :body, :course_id, :anonymous, :anonymous_to_peers, :endorsed
validates_presence_of :comment_thread, autosave: false
validates_presence_of :body
......@@ -50,7 +51,7 @@ class Comment < Content
if params[:recursive]
self.class.hash_tree(subtree(sort: sort_by_parent_and_time)).first
else
as_document.slice(*%w[body course_id endorsed anonymous created_at updated_at at_position_list])
as_document.slice(*%w[body course_id endorsed anonymous anonymous_to_peers created_at updated_at at_position_list])
.merge("id" => _id)
.merge("user_id" => author.id)
.merge("username" => author.username)
......
......@@ -17,6 +17,7 @@ class CommentThread < Content
field :course_id, type: String
field :commentable_id, type: String
field :anonymous, type: Boolean, default: false
field :anonymous_to_peers, type: Boolean, default: false
field :closed, type: Boolean, default: false
field :at_position_list, type: Array, default: []
field :last_activity_at, type: Time
......@@ -47,7 +48,7 @@ class CommentThread < Content
has_many :comments, dependent: :destroy#, autosave: true# Use destroy to envoke callback on the top-level comments TODO async
has_many :activities, autosave: true
attr_accessible :title, :body, :course_id, :commentable_id, :anonymous, :closed
attr_accessible :title, :body, :course_id, :commentable_id, :anonymous, :anonymous_to_peers, :closed
validates_presence_of :title
validates_presence_of :body
......@@ -153,7 +154,7 @@ class CommentThread < Content
end
def to_hash(params={})
doc = as_document.slice(*%w[title body course_id anonymous commentable_id created_at updated_at at_position_list closed])
doc = as_document.slice(*%w[title body course_id anonymous anonymous_to_peers commentable_id created_at updated_at at_position_list closed])
.merge("id" => _id, "user_id" => author.id,
"username" => author.username,
"votes" => votes.slice(*%w[count up_count down_count point]),
......
......@@ -4,9 +4,9 @@ class Content
def author_with_anonymity(attr=nil, attr_when_anonymous=nil)
if not attr
anonymous ? nil : author
(anonymous || anonymous_to_peers) ? nil : author
else
anonymous ? attr_when_anonymous : author.send(attr)
(anonymous || anonymous_to_peers) ? attr_when_anonymous : author.send(attr)
end
end
end
......@@ -9,13 +9,13 @@ class PostReplyObserver < Mongoid::Observer
activity = Activity.new
activity.happend_at = comment.created_at
activity.anonymous = comment.anonymous
activity.anonymous = (comment.anonymous || comment.anonymous_to_peers)
activity.actor = comment.author
activity.target = comment.comment_thread
activity.activity_type = "post_reply"
activity.save!
if comment.comment_thread.subscribers or (comment.author.followers if not comment.anonymous)
if comment.comment_thread.subscribers or (comment.author.followers if not activity.anonymous)
notification = Notification.new(
notification_type: "post_reply",
info: {
......
......@@ -8,13 +8,13 @@ class PostTopicObserver < Mongoid::Observer
def self.generate_notifications(comment_thread)
activity = Activity.new
activity.happend_at = comment_thread.created_at
activity.anonymous = comment_thread.anonymous
activity.anonymous = (comment_thread.anonymous || comment_thread.anonymous_to_peers)
activity.actor = comment_thread.author
#activity.target_id = comment_thread.commentable.id
#activity.target_type = comment_thread.commentable._type
activity.activity_type = "post_topic"
activity.save!
if comment_thread.commentable.subscribers or (author.followers if not anonymous)
if comment_thread.commentable.subscribers or (author.followers if not activity.anonymous)
notification = Notification.new(
notification_type: "post_topic",
info: {
......
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