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
|
|
|
|
env = env_arg || ENV["SINATRA_ENV"] || "development"
|
2012-07-15 02:09:10 +04:00
|
|
|
|
2012-07-16 23:43:14 +04:00
|
|
|
module CommentService
|
|
|
|
class << self; attr_accessor :config; end
|
|
|
|
end
|
|
|
|
|
|
|
|
CommentService.config = YAML.load_file("config/application.yml")
|
|
|
|
|
2012-07-15 02:09:10 +04:00
|
|
|
Mongoid.load!("config/mongoid.yml")
|
|
|
|
Mongoid.logger.level = Logger::INFO
|
|
|
|
|
2012-07-16 23:43:14 +04:00
|
|
|
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
|
2012-07-15 02:09:10 +04:00
|
|
|
|
2012-07-17 03:35:34 +04:00
|
|
|
delete '/api/v1/:commentable_type/:commentable_id/threads' do |commentable_type, commentable_id|
|
2012-07-16 19:04:35 +04:00
|
|
|
commentable = Commentable.find_or_initialize_by(commentable_type: commentable_type, commentable_id: commentable_id)
|
2012-07-15 02:09:10 +04:00
|
|
|
commentable.destroy
|
|
|
|
commentable.to_hash.to_json
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-07-17 02:07:11 +04:00
|
|
|
get '/api/v1/:commentable_type/:commentable_id/threads' do |commentable_type, commentable_id|
|
2012-07-15 02:09:10 +04:00
|
|
|
commentable = Commentable.find_or_create_by(commentable_type: commentable_type, commentable_id: commentable_id)
|
|
|
|
commentable.comment_threads.map{|t| t.to_hash(recursive: params["recursive"])}.to_json
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-07-17 02:07:11 +04:00
|
|
|
post '/api/v1/:commentable_type/:commentable_id/threads' do |commentable_type, commentable_id|
|
2012-07-15 02:09:10 +04:00
|
|
|
commentable = Commentable.find_or_create_by(commentable_type: commentable_type, commentable_id: commentable_id)
|
2012-07-17 02:07:11 +04:00
|
|
|
thread = commentable.comment_threads.new(params.slice(*%w[title body course_id]))
|
|
|
|
thread.author = User.find_or_create_by(external_id: params["user_id"]) if params["user_id"]
|
|
|
|
thread.save!
|
|
|
|
thread.to_hash.to_json
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-07-17 02:07:11 +04:00
|
|
|
get '/api/v1/threads/:thread_id' do |thread_id|
|
|
|
|
thread = CommentThread.find(thread_id)
|
|
|
|
thread.to_hash(recursive: params["recursive"]).to_json
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-07-17 02:07:11 +04:00
|
|
|
put '/api/v1/threads/:thread_id' do |thread_id|
|
|
|
|
thread = CommentThread.find(thread_id)
|
|
|
|
thread.update_attributes!(params.slice(*%w[title body]))
|
|
|
|
thread.to_hash.to_json
|
2012-06-22 00:47:14 +04:00
|
|
|
end
|
|
|
|
|
2012-07-17 02:07:11 +04:00
|
|
|
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]))
|
2012-07-17 00:19:05 +04:00
|
|
|
comment.author = User.find_or_create_by(external_id: params["user_id"]) if params["user_id"]
|
2012-07-15 02:09:10 +04:00
|
|
|
comment.save!
|
|
|
|
comment.to_hash.to_json
|
|
|
|
end
|
|
|
|
|
2012-07-17 02:07:11 +04:00
|
|
|
delete '/api/v1/threads/:thread_id' do |thread_id|
|
|
|
|
thread = CommentThread.find(thread_id)
|
|
|
|
thread.destroy
|
|
|
|
thread.to_hash.to_json
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-07-15 02:09:10 +04:00
|
|
|
get '/api/v1/comments/:comment_id' do |comment_id|
|
|
|
|
comment = Comment.find(comment_id)
|
|
|
|
comment.to_hash(recursive: params["recursive"]).to_json
|
|
|
|
end
|
|
|
|
|
2012-06-21 02:25:35 +04:00
|
|
|
put '/api/v1/comments/:comment_id' do |comment_id|
|
2012-07-15 02:09:10 +04:00
|
|
|
comment = Comment.find(comment_id)
|
2012-07-16 19:04:35 +04:00
|
|
|
comment.update_attributes!(params.slice(*%w[body endorsed]))
|
|
|
|
comment.to_hash.to_json
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-07-15 02:09:10 +04:00
|
|
|
post '/api/v1/comments/:comment_id' do |comment_id|
|
|
|
|
comment = Comment.find(comment_id)
|
2012-07-16 19:04:35 +04:00
|
|
|
sub_comment = comment.children.new(params.slice(*%w[body course_id]))
|
2012-07-16 23:43:14 +04:00
|
|
|
sub_comment.author = User.find_or_create_by(external_id: params["user_id"])
|
2012-07-15 02:09:10 +04:00
|
|
|
sub_comment.save!
|
|
|
|
sub_comment.to_hash.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
delete '/api/v1/comments/:comment_id' do |comment_id|
|
|
|
|
comment = Comment.find(comment_id)
|
|
|
|
comment.destroy
|
|
|
|
comment.to_hash.to_json
|
|
|
|
end
|
|
|
|
|
2012-07-17 03:35:34 +04:00
|
|
|
put '/api/v1/comments/:comment_id/votes' do |comment_id|
|
2012-07-15 02:09:10 +04:00
|
|
|
comment = Comment.find(comment_id)
|
2012-07-17 05:31:42 +04:00
|
|
|
vote_for comment
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-07-17 03:35:34 +04:00
|
|
|
delete '/api/v1/comments/:comment_id/votes' do |comment_id|
|
2012-07-15 02:09:10 +04:00
|
|
|
comment = Comment.find(comment_id)
|
2012-07-17 05:31:42 +04:00
|
|
|
delete_vote_for comment
|
2012-07-15 02:09:10 +04:00
|
|
|
end
|
|
|
|
|
2012-07-17 03:35:34 +04:00
|
|
|
put '/api/v1/threads/:thread_id/votes' do |thread_id|
|
2012-07-17 02:07:11 +04:00
|
|
|
thread = CommentThread.find(thread_id)
|
2012-07-17 05:31:42 +04:00
|
|
|
vote_for thread
|
2012-07-15 02:09:10 +04:00
|
|
|
end
|
|
|
|
|
2012-07-17 03:35:34 +04:00
|
|
|
delete '/api/v1/threads/:thread_id/votes' do |thread_id|
|
2012-07-17 02:07:11 +04:00
|
|
|
thread = CommentThread.find(thread_id)
|
2012-07-17 05:31:42 +04:00
|
|
|
delete_vote_for thread
|
2012-07-16 01:35:44 +04:00
|
|
|
end
|
|
|
|
|
2012-07-17 03:35:34 +04:00
|
|
|
get '/api/v1/users/:user_id/notifications' do |user_id|
|
2012-07-16 23:43:14 +04:00
|
|
|
user = User.find_or_create_by(external_id: 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-07-17 03:35:34 +04:00
|
|
|
post '/api/v1/users/:user_id/subscriptions' do |user_id|
|
2012-07-16 23:43:14 +04:00
|
|
|
user = User.find_or_create_by(external_id: user_id)
|
2012-07-17 09:42:43 +04:00
|
|
|
source = case params["source_type"]
|
2012-07-17 03:35:34 +04:00
|
|
|
when "user"
|
2012-07-17 09:42:43 +04:00
|
|
|
User.find_or_create_by(external_id: params["source_id"])
|
2012-07-17 03:35:34 +04:00
|
|
|
when "thread"
|
2012-07-17 09:42:43 +04:00
|
|
|
CommentThread.find(params["source_id"])
|
2012-07-17 03:35:34 +04:00
|
|
|
else
|
2012-07-17 09:42:43 +04:00
|
|
|
Commentable.find_or_create_by(commentable_type: params["source_type"], commentable_id: params["source_id"])
|
2012-07-17 03:35:34 +04:00
|
|
|
end
|
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-07-17 03:35:34 +04:00
|
|
|
delete '/api/v1/users/:user_id/subscriptions' do |user_id|
|
2012-07-16 23:43:14 +04:00
|
|
|
user = User.find_or_create_by(external_id: user_id)
|
2012-07-17 09:42:43 +04:00
|
|
|
source = case params["source_type"]
|
2012-07-17 03:35:34 +04:00
|
|
|
when "user"
|
2012-07-17 09:42:43 +04:00
|
|
|
User.find_or_create_by(external_id: params["source_id"])
|
2012-07-17 03:35:34 +04:00
|
|
|
when "thread"
|
2012-07-17 09:42:43 +04:00
|
|
|
CommentThread.find(params["source_id"])
|
2012-07-17 03:35:34 +04:00
|
|
|
else
|
2012-07-17 09:42:43 +04:00
|
|
|
Commentable.find_or_create_by(commentable_type: params["source_type"], commentable_id: params["source_id"])
|
2012-07-17 03:35:34 +04:00
|
|
|
end
|
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-06-21 02:25:35 +04:00
|
|
|
if env.to_s == "development"
|
|
|
|
get '/api/v1/clean' do
|
|
|
|
Comment.delete_all
|
|
|
|
CommentThread.delete_all
|
2012-07-15 02:09:10 +04:00
|
|
|
Commentable.delete_all
|
|
|
|
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-07-17 05:31:42 +04:00
|
|
|
def vote_for(obj)
|
2012-07-17 03:35:34 +04:00
|
|
|
user = User.find_or_create_by(external_id: params["user_id"])
|
|
|
|
user.vote(obj, params["value"].to_sym)
|
|
|
|
obj.reload.to_hash.to_json
|
|
|
|
end
|
|
|
|
|
2012-07-17 05:31:42 +04:00
|
|
|
def delete_vote_for(obj)
|
2012-07-17 03:35:34 +04:00
|
|
|
user = User.find_or_create_by(external_id: params["user_id"])
|
|
|
|
user.unvote(obj)
|
|
|
|
obj.reload.to_hash.to_json
|
|
|
|
end
|