comment_threads.rb 1.73 KB
Newer Older
Rocky Duan committed
1
get "#{APIPREFIX}/threads" do # retrieve threads by course
2 3 4
  #if a group id is sent, then process the set of threads with that group id or with no group id
  if params["group_id"]
    threads = CommentThread.any_of(
Kevin Chugh committed
5 6
    {:course_id => params["course_id"],:group_id => params[:group_id]},
    {:course_id => params["course_id"],:group_id.exists => false},
7 8 9 10 11
    )
  else
    threads = CommentThread.where(course_id: params["course_id"])
    #else process them all
  end
Kevin Chugh committed
12
  handle_threads_query(threads)
Rocky Duan committed
13 14
end

15
get "#{APIPREFIX}/threads/:thread_id" do |thread_id|
16 17 18
  thread = CommentThread.find(thread_id)

  if params["user_id"] and bool_mark_as_read
19 20
    user = User.only([:id, :read_states]).find_by(external_id: params["user_id"])
    user.mark_as_read(thread) if user
21 22 23
  end

  thread.to_hash(recursive: bool_recursive, user_id: params["user_id"]).to_json
24 25 26
end

put "#{APIPREFIX}/threads/:thread_id" do |thread_id|
27 28
  thread.update_attributes(params.slice(*%w[title body closed commentable_id group_id]))

29 30 31 32
  if params["tags"]
    thread.tags = params["tags"]
    thread.save
  end
Kevin Chugh committed
33

34 35 36 37 38 39 40 41
  if thread.errors.any?
    error 400, thread.errors.full_messages.to_json
  else
    thread.to_hash.to_json
  end
end

post "#{APIPREFIX}/threads/:thread_id/comments" do |thread_id|
Rocky Duan committed
42
  comment = Comment.new(params.slice(*%w[body course_id]))
43
  comment.anonymous = bool_anonymous || false
Arjun Singh committed
44
  comment.anonymous_to_peers = bool_anonymous_to_peers || false
45
  comment.author = user
Rocky Duan committed
46
  comment.comment_thread = thread
47 48 49 50 51 52 53 54 55 56 57 58 59
  comment.save
  if comment.errors.any?
    error 400, comment.errors.full_messages.to_json
  else
    user.subscribe(thread) if bool_auto_subscribe
    comment.to_hash.to_json
  end
end

delete "#{APIPREFIX}/threads/:thread_id" do |thread_id|
  thread.destroy
  thread.to_hash.to_json
end