cs_comments_service/app.rb

290 строки
8.1 KiB
Ruby
Исходник Обычный вид История

2012-06-21 02:25:35 +04:00
require 'rubygems'
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
module CommentService
class << self; attr_accessor :config; end
API_VERSION = 'v1'
API_PREFIX = "/api/#{API_VERSION}"
end
CommentService.config = YAML.load_file("config/application.yml")
2012-08-02 03:43:17 +04:00
Mongoid.load!("config/mongoid.yml", environment)
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
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,
2012-08-07 06:25:43 +04:00
"activity" => :last_activity_at,
2012-08-02 03:43:17 +04:00
"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
delete "#{api_prefix}/:commentable_id/threads" do |commentable_id|
2012-07-27 03:12:30 +04:00
commentable.comment_threads.destroy_all
{}.to_json
2012-06-21 02:25:35 +04:00
end
get "#{api_prefix}/:commentable_id/threads" do |commentable_id|
2012-08-02 03:43:17 +04:00
sort_key_mapper = {
"date" => :created_at,
2012-08-07 06:25:43 +04:00
"activity" => :last_activity_at,
2012-08-02 03:43:17 +04:00
"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
post "#{api_prefix}/:commentable_id/threads" do |commentable_id|
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
thread.save
if thread.errors.any?
2012-07-31 01:36:52 +04:00
error 400, thread.errors.full_messages.to_json
else
2012-08-05 03:05:32 +04:00
user.subscribe(thread) if bool_auto_subscribe
thread.to_hash.to_json
end
2012-06-21 02:25:35 +04:00
end
get "#{api_prefix}/threads/tags" do
2012-07-29 02:21:03 +04:00
CommentThread.tags.to_json
end
get "#{api_prefix}/threads/tags/autocomplete" do
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
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
put "#{api_prefix}/threads/:thread_id" do |thread_id|
thread.update_attributes(params.slice(*%w[title body closed]))
2012-07-28 21:07:05 +04:00
if params["tags"]
thread.tags = params["tags"]
thread.save
end
if thread.errors.any?
2012-07-31 01:36:52 +04:00
error 400, thread.errors.full_messages.to_json
else
thread.to_hash.to_json
2012-07-28 21:07:05 +04:00
end
2012-06-22 00:47:14 +04:00
end
post "#{api_prefix}/threads/:thread_id/comments" do |thread_id|
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
comment.save
if comment.errors.any?
2012-07-31 01:36:52 +04:00
error 400, comment.errors.full_messages.to_json
else
2012-08-05 03:05:32 +04:00
user.subscribe(thread) if bool_auto_subscribe
comment.to_hash.to_json
end
end
delete "#{api_prefix}/threads/:thread_id" do |thread_id|
thread.destroy
thread.to_hash.to_json
2012-06-21 02:25:35 +04:00
end
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
end
put "#{api_prefix}/comments/:comment_id" do |comment_id|
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
else
comment.to_hash.to_json
end
2012-06-21 02:25:35 +04:00
end
post "#{api_prefix}/comments/:comment_id" do |comment_id|
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
sub_comment.comment_thread = comment.comment_thread
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
else
2012-08-05 03:05:32 +04:00
user.subscribe(comment.comment_thread) if bool_auto_subscribe
sub_comment.to_hash.to_json
end
end
delete "#{api_prefix}/comments/:comment_id" do |comment_id|
comment.destroy
comment.to_hash.to_json
end
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
delete "#{api_prefix}/comments/:comment_id/votes" do |comment_id|
2012-07-18 22:01:04 +04:00
undo_vote_for comment
end
put "#{api_prefix}/threads/:thread_id/votes" do |thread_id|
2012-07-17 05:31:42 +04:00
vote_for thread
end
delete "#{api_prefix}/threads/:thread_id/votes" do |thread_id|
2012-07-18 21:02:17 +04:00
undo_vote_for thread
end
post "#{api_prefix}/users" do
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
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
put "#{api_prefix}/users/:user_id" do |user_id|
user = User.where(external_id: user_id).first
if not user
user = User.new(external_id: user_id)
end
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
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
end
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
end
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
end
2012-07-27 03:58:39 +04:00
if environment.to_s == "development"
get "#{api_prefix}/clean" do
2012-06-21 02:25:35 +04:00
Comment.delete_all
CommentThread.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-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-31 01:36:52 +04:00
error ArgumentError do
error 400, [env['sinatra.error'].message].to_json
end