2012-06-21 02:25:35 +04:00
|
|
|
require 'rubygems'
|
2012-07-15 22:14:46 +04:00
|
|
|
require 'bundler'
|
|
|
|
|
|
|
|
Bundler.setup
|
|
|
|
Bundler.require
|
|
|
|
|
2012-06-21 02:25:35 +04:00
|
|
|
env_index = ARGV.index("-e")
|
|
|
|
env_arg = ARGV[env_index + 1] if env_index
|
2012-07-27 03:58:39 +04:00
|
|
|
environment = env_arg || ENV["SINATRA_ENV"] || "development"
|
|
|
|
RACK_ENV = environment
|
2012-07-15 02:09:10 +04:00
|
|
|
|
2012-07-16 23:43:14 +04:00
|
|
|
module CommentService
|
|
|
|
class << self; attr_accessor :config; end
|
2012-08-04 20:35:18 +04:00
|
|
|
API_VERSION = 'v1'
|
|
|
|
API_PREFIX = "/api/#{API_VERSION}"
|
2012-07-16 23:43:14 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
CommentService.config = YAML.load_file("config/application.yml")
|
|
|
|
|
2012-08-02 03:43:17 +04:00
|
|
|
Mongoid.load!("config/mongoid.yml", environment)
|
2012-07-15 02:09:10 +04:00
|
|
|
Mongoid.logger.level = Logger::INFO
|
|
|
|
|
2012-07-24 00:15:44 +04:00
|
|
|
Dir[File.dirname(__FILE__) + '/lib/**/*.rb'].each {|file| require file}
|
2012-07-31 01:36:52 +04:00
|
|
|
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
|
2012-08-04 23:25:46 +04:00
|
|
|
Dir[File.dirname(__FILE__) + '/models/observers/*.rb'].each {|file| require file}
|
|
|
|
|
|
|
|
Mongoid.observers = PostReplyObserver, PostTopicObserver, AtUserObserver
|
|
|
|
Mongoid.instantiate_observers
|
2012-07-15 02:09:10 +04:00
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
api_prefix = CommentService::API_PREFIX
|
|
|
|
|
|
|
|
get "#{api_prefix}/search/threads" do
|
2012-08-02 03:43:17 +04:00
|
|
|
|
|
|
|
sort_key_mapper = {
|
|
|
|
"date" => :created_at,
|
|
|
|
"votes" => :votes_point,
|
|
|
|
"comments" => :comment_count,
|
|
|
|
}
|
|
|
|
|
|
|
|
sort_order_mapper = {
|
|
|
|
"desc" => :desc,
|
|
|
|
"asc" => :asc,
|
|
|
|
}
|
|
|
|
|
|
|
|
sort_key = sort_key_mapper[params["sort_key"]]
|
|
|
|
sort_order = sort_order_mapper[params["sort_order"]]
|
|
|
|
sort_keyword_valid = (!params["sort_key"] && !params["sort_order"] || sort_key && sort_order)
|
|
|
|
|
|
|
|
if (!params["text"] && !params["tags"]) || !sort_keyword_valid
|
|
|
|
{}.to_json
|
|
|
|
else
|
|
|
|
page = (params["page"] || 1).to_i
|
|
|
|
per_page = (params["per_page"] || 20).to_i
|
2012-08-06 20:36:08 +04:00
|
|
|
tags = params["tags"].split /,/ if params["tags"]
|
2012-08-07 01:12:34 +04:00
|
|
|
|
|
|
|
search = CommentThread.tire.search page: page, per_page: per_page do |search|
|
|
|
|
if params["text"]
|
2012-08-06 20:36:08 +04:00
|
|
|
search.query do |query|
|
2012-08-07 01:12:34 +04:00
|
|
|
query.text(:_all, params["text"])
|
|
|
|
end
|
|
|
|
search.highlight({title: { number_of_fragments: 0 } } , {body: { number_of_fragments: 0 } }, options: { tag: "<highlight>" })
|
|
|
|
end
|
|
|
|
|
2012-08-07 01:14:35 +04:00
|
|
|
search.filter :bool, :must => tags.map{ |tag| { :term => { :tags_array => tag } } } if params["tags"]
|
|
|
|
|
|
|
|
search.filter(:term, commentable_id: params["commentable_id"]) if params["commentable_id"]
|
|
|
|
search.filter(:term, course_id: params["course_id"]) if params["course_id"]
|
|
|
|
search.sort {|sort| sort.by sort_key, sort_order} if sort_key && sort_order #TODO should have search option 'auto sort or sth'
|
|
|
|
|
2012-08-02 03:43:17 +04:00
|
|
|
end
|
2012-08-06 20:36:08 +04:00
|
|
|
|
2012-08-06 19:09:37 +04:00
|
|
|
num_pages = search.total_pages
|
2012-08-02 03:43:17 +04:00
|
|
|
{
|
2012-08-07 01:12:34 +04:00
|
|
|
collection: search.results.map{|t| CommentThread.search_result_to_hash(t, recursive: bool_recursive)},
|
2012-08-02 03:43:17 +04:00
|
|
|
num_pages: num_pages,
|
|
|
|
page: page,
|
|
|
|
}.to_json
|
|
|
|
end
|
2012-07-30 00:45:48 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
delete "#{api_prefix}/:commentable_id/threads" do |commentable_id|
|
2012-07-27 03:12:30 +04:00
|
|
|
commentable.comment_threads.destroy_all
|
2012-07-18 20:53:32 +04:00
|
|
|
{}.to_json
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
get "#{api_prefix}/:commentable_id/threads" do |commentable_id|
|
2012-08-02 03:43:17 +04:00
|
|
|
|
|
|
|
sort_key_mapper = {
|
|
|
|
"date" => :created_at,
|
|
|
|
"votes" => :"votes.point",
|
|
|
|
"comments" => :comment_count,
|
|
|
|
}
|
|
|
|
|
|
|
|
sort_order_mapper = {
|
|
|
|
"desc" => :desc,
|
|
|
|
"asc" => :asc,
|
|
|
|
}
|
|
|
|
|
|
|
|
sort_key = sort_key_mapper[params["sort_key"]]
|
|
|
|
sort_order = sort_order_mapper[params["sort_order"]]
|
|
|
|
sort_keyword_valid = (!params["sort_key"] && !params["sort_order"] || sort_key && sort_order)
|
|
|
|
if not sort_keyword_valid
|
|
|
|
{}.to_json
|
|
|
|
else
|
|
|
|
page = (params["page"] || 1).to_i
|
|
|
|
per_page = (params["per_page"] || 20).to_i
|
|
|
|
comment_threads = commentable.comment_threads
|
|
|
|
comment_threads = comment_threads.order_by("#{sort_key} #{sort_order}") if sort_key && sort_order
|
|
|
|
num_pages = [1, (comment_threads.count / per_page.to_f).ceil].max
|
|
|
|
page = [num_pages, [1, page].max].min
|
|
|
|
paged_comment_threads = comment_threads.page(page).per(per_page)
|
|
|
|
{
|
2012-08-05 03:05:32 +04:00
|
|
|
collection: paged_comment_threads.map{|t| t.to_hash(recursive: bool_recursive)},
|
2012-08-02 03:43:17 +04:00
|
|
|
num_pages: num_pages,
|
|
|
|
page: page,
|
|
|
|
}.to_json
|
|
|
|
end
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
post "#{api_prefix}/:commentable_id/threads" do |commentable_id|
|
2012-07-31 19:59:07 +04:00
|
|
|
thread = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id))
|
2012-08-05 03:05:32 +04:00
|
|
|
thread.anonymous = bool_anonymous || false
|
2012-07-28 21:07:05 +04:00
|
|
|
thread.tags = params["tags"] || ""
|
2012-07-27 03:12:30 +04:00
|
|
|
thread.author = user
|
2012-07-30 05:26:26 +04:00
|
|
|
thread.save
|
|
|
|
if thread.errors.any?
|
2012-07-31 01:36:52 +04:00
|
|
|
error 400, thread.errors.full_messages.to_json
|
2012-07-30 05:26:26 +04:00
|
|
|
else
|
2012-08-05 03:05:32 +04:00
|
|
|
user.subscribe(thread) if bool_auto_subscribe
|
2012-07-30 05:26:26 +04:00
|
|
|
thread.to_hash.to_json
|
2012-07-24 04:39:27 +04:00
|
|
|
end
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
get "#{api_prefix}/threads/tags" do
|
2012-07-29 02:21:03 +04:00
|
|
|
CommentThread.tags.to_json
|
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
get "#{api_prefix}/threads/tags/autocomplete" do
|
2012-07-30 05:26:26 +04:00
|
|
|
CommentThread.tags_autocomplete(params["value"].strip, max: 5, sort_by_count: true).map(&:first).to_json
|
2012-07-30 00:45:48 +04:00
|
|
|
end
|
2012-07-29 19:27:12 +04:00
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
get "#{api_prefix}/threads/:thread_id" do |thread_id|
|
2012-08-05 03:05:32 +04:00
|
|
|
CommentThread.find(thread_id).to_hash(recursive: bool_recursive).to_json
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
put "#{api_prefix}/threads/:thread_id" do |thread_id|
|
2012-08-06 19:11:26 +04:00
|
|
|
thread.update_attributes(params.slice(*%w[title body closed]))
|
2012-07-28 21:07:05 +04:00
|
|
|
if params["tags"]
|
|
|
|
thread.tags = params["tags"]
|
2012-07-30 05:26:26 +04:00
|
|
|
thread.save
|
|
|
|
end
|
|
|
|
if thread.errors.any?
|
2012-07-31 01:36:52 +04:00
|
|
|
error 400, thread.errors.full_messages.to_json
|
2012-07-30 05:26:26 +04:00
|
|
|
else
|
|
|
|
thread.to_hash.to_json
|
2012-07-28 21:07:05 +04:00
|
|
|
end
|
2012-06-22 00:47:14 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
post "#{api_prefix}/threads/:thread_id/comments" do |thread_id|
|
2012-07-31 19:59:07 +04:00
|
|
|
comment = thread.comments.new(params.slice(*%w[body course_id]))
|
2012-08-05 03:05:32 +04:00
|
|
|
comment.anonymous = bool_anonymous || false
|
2012-07-27 03:12:30 +04:00
|
|
|
comment.author = user
|
2012-07-30 05:26:26 +04:00
|
|
|
comment.save
|
|
|
|
if comment.errors.any?
|
2012-07-31 01:36:52 +04:00
|
|
|
error 400, comment.errors.full_messages.to_json
|
2012-07-30 05:26:26 +04:00
|
|
|
else
|
2012-08-05 03:05:32 +04:00
|
|
|
user.subscribe(thread) if bool_auto_subscribe
|
2012-07-30 05:26:26 +04:00
|
|
|
comment.to_hash.to_json
|
2012-07-24 04:39:27 +04:00
|
|
|
end
|
2012-07-15 02:09:10 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
delete "#{api_prefix}/threads/:thread_id" do |thread_id|
|
2012-07-17 02:07:11 +04:00
|
|
|
thread.destroy
|
|
|
|
thread.to_hash.to_json
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
get "#{api_prefix}/comments/:comment_id" do |comment_id|
|
2012-08-05 03:05:32 +04:00
|
|
|
comment.to_hash(recursive: bool_recursive).to_json
|
2012-07-15 02:09:10 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
put "#{api_prefix}/comments/:comment_id" do |comment_id|
|
2012-07-30 05:26:26 +04:00
|
|
|
comment.update_attributes(params.slice(*%w[body endorsed]))
|
|
|
|
if comment.errors.any?
|
2012-07-31 01:36:52 +04:00
|
|
|
error 400, comment.errors.full_messages.to_json
|
2012-07-30 05:26:26 +04:00
|
|
|
else
|
|
|
|
comment.to_hash.to_json
|
|
|
|
end
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
post "#{api_prefix}/comments/:comment_id" do |comment_id|
|
2012-07-16 19:04:35 +04:00
|
|
|
sub_comment = comment.children.new(params.slice(*%w[body course_id]))
|
2012-08-05 03:05:32 +04:00
|
|
|
sub_comment.anonymous = bool_anonymous || false
|
2012-07-27 03:12:30 +04:00
|
|
|
sub_comment.author = user
|
2012-07-25 22:07:37 +04:00
|
|
|
sub_comment.comment_thread = comment.comment_thread
|
2012-07-30 05:26:26 +04:00
|
|
|
sub_comment.save
|
|
|
|
if sub_comment.errors.any?
|
2012-07-31 01:36:52 +04:00
|
|
|
error 400, sub_comment.errors.full_messages.to_json
|
2012-07-30 05:26:26 +04:00
|
|
|
else
|
2012-08-05 03:05:32 +04:00
|
|
|
user.subscribe(comment.comment_thread) if bool_auto_subscribe
|
2012-07-30 05:26:26 +04:00
|
|
|
sub_comment.to_hash.to_json
|
|
|
|
end
|
2012-07-15 02:09:10 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
delete "#{api_prefix}/comments/:comment_id" do |comment_id|
|
2012-07-15 02:09:10 +04:00
|
|
|
comment.destroy
|
|
|
|
comment.to_hash.to_json
|
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
put "#{api_prefix}/comments/:comment_id/votes" do |comment_id|
|
2012-07-17 05:31:42 +04:00
|
|
|
vote_for comment
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
delete "#{api_prefix}/comments/:comment_id/votes" do |comment_id|
|
2012-07-18 22:01:04 +04:00
|
|
|
undo_vote_for comment
|
2012-07-15 02:09:10 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
put "#{api_prefix}/threads/:thread_id/votes" do |thread_id|
|
2012-07-17 05:31:42 +04:00
|
|
|
vote_for thread
|
2012-07-15 02:09:10 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
delete "#{api_prefix}/threads/:thread_id/votes" do |thread_id|
|
2012-07-18 21:02:17 +04:00
|
|
|
undo_vote_for thread
|
2012-07-16 01:35:44 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
post "#{api_prefix}/users" do
|
2012-08-05 01:58:08 +04:00
|
|
|
user = User.new(external_id: params["id"])
|
2012-08-04 19:49:16 +04:00
|
|
|
user.username = params["username"]
|
|
|
|
user.email = params["email"]
|
|
|
|
user.save
|
|
|
|
if user.errors.any?
|
|
|
|
error 400, user.errors.full_messages.to_json
|
|
|
|
else
|
|
|
|
user.to_hash.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
get "#{api_prefix}/users/:user_id" do |user_id|
|
2012-08-05 03:05:32 +04:00
|
|
|
user.to_hash(complete: bool_complete).to_json
|
2012-07-25 22:58:33 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
put "#{api_prefix}/users/:user_id" do |user_id|
|
2012-08-05 01:58:08 +04:00
|
|
|
user = User.where(external_id: user_id).first
|
|
|
|
if not user
|
|
|
|
user = User.new(external_id: user_id)
|
|
|
|
end
|
2012-08-04 20:35:18 +04:00
|
|
|
user.update_attributes(params.slice(*%w[username email]))
|
2012-08-04 19:49:16 +04:00
|
|
|
if user.errors.any?
|
|
|
|
error 400, user.errors.full_messages.to_json
|
|
|
|
else
|
|
|
|
user.to_hash.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
get "#{api_prefix}/users/:user_id/notifications" do |user_id|
|
2012-07-17 03:35:34 +04:00
|
|
|
user.notifications.map(&:to_hash).to_json
|
2012-07-16 01:35:44 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
post "#{api_prefix}/users/:user_id/subscriptions" do |user_id|
|
2012-07-17 07:05:16 +04:00
|
|
|
user.subscribe(source).to_hash.to_json
|
2012-07-16 01:35:44 +04:00
|
|
|
end
|
|
|
|
|
2012-08-04 20:35:18 +04:00
|
|
|
delete "#{api_prefix}/users/:user_id/subscriptions" do |user_id|
|
2012-07-17 07:05:16 +04:00
|
|
|
user.unsubscribe(source).to_hash.to_json
|
2012-07-16 01:35:44 +04:00
|
|
|
end
|
|
|
|
|
2012-07-27 03:58:39 +04:00
|
|
|
if environment.to_s == "development"
|
2012-08-04 20:35:18 +04:00
|
|
|
get "#{api_prefix}/clean" do
|
2012-06-21 02:25:35 +04:00
|
|
|
Comment.delete_all
|
|
|
|
CommentThread.delete_all
|
2012-07-15 02:09:10 +04:00
|
|
|
User.delete_all
|
2012-07-17 03:35:34 +04:00
|
|
|
Notification.delete_all
|
2012-07-17 07:05:16 +04:00
|
|
|
Subscription.delete_all
|
2012-06-21 02:25:35 +04:00
|
|
|
{}.to_json
|
|
|
|
end
|
|
|
|
end
|
2012-07-17 03:35:34 +04:00
|
|
|
|
2012-08-01 06:44:16 +04:00
|
|
|
error Moped::Errors::InvalidObjectId do
|
2012-07-31 01:36:52 +04:00
|
|
|
error 400, ["requested object not found"].to_json
|
2012-07-27 03:58:39 +04:00
|
|
|
end
|
|
|
|
|
2012-07-27 04:17:19 +04:00
|
|
|
error Mongoid::Errors::DocumentNotFound do
|
2012-07-31 01:36:52 +04:00
|
|
|
error 400, ["requested object not found"].to_json
|
2012-07-17 03:35:34 +04:00
|
|
|
end
|
2012-07-30 05:26:26 +04:00
|
|
|
|
2012-07-31 01:36:52 +04:00
|
|
|
error ArgumentError do
|
|
|
|
error 400, [env['sinatra.error'].message].to_json
|
2012-07-30 05:26:26 +04:00
|
|
|
end
|