Refactor Ajax::*Controllers

Also removed the unused `Ajax::QuestionController#preview` method and
route
This commit is contained in:
Georg Gadinger 2020-04-28 20:27:59 +02:00
parent acc394a110
commit e07d069c73
41 changed files with 264 additions and 388 deletions

View file

@ -1,11 +1,4 @@
class Ajax::AnswerController < ApplicationController class Ajax::AnswerController < AjaxController
rescue_from(ActionController::ParameterMissing) do |titanic_param|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: titanic_param.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def create def create
params.require :id params.require :id
params.require :answer params.require :answer
@ -18,27 +11,24 @@ class Ajax::AnswerController < ApplicationController
inbox_entry = Inbox.find(params[:id]) inbox_entry = Inbox.find(params[:id])
unless current_user == inbox_entry.user unless current_user == inbox_entry.user
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.answer.create.fail') @response[:message] = I18n.t('messages.answer.create.fail')
@success = false
return return
end end
else else
question = Question.find(params[:id]) question = Question.find(params[:id])
unless question.user.privacy_allow_stranger_answers unless question.user.privacy_allow_stranger_answers
@status = :privacy_stronk @response[:status] = :privacy_stronk
@message = I18n.t('messages.answer.create.privacy_stronk') @response[:message] = I18n.t('messages.answer.create.privacy_stronk')
@success = false
return return
end end
end end
# this should never trigger because empty params throw ParameterMissing # this should never trigger because empty params throw ParameterMissing
unless params[:answer].length > 0 unless params[:answer].length > 0
@status = :peter_dinklage @response[:status] = :peter_dinklage
@message = I18n.t('messages.answer.create.peter_dinklage') @response[:message] = I18n.t('messages.answer.create.peter_dinklage')
@success = false
return return
end end
@ -51,9 +41,8 @@ class Ajax::AnswerController < ApplicationController
current_user.answer question, params[:answer] current_user.answer question, params[:answer]
end end
rescue rescue
@status = :err @response[:status] = :err
@message = I18n.t('messages.error') @response[:message] = I18n.t('messages.error')
@success = false
return return
end end
@ -61,12 +50,13 @@ class Ajax::AnswerController < ApplicationController
ShareWorker.perform_async(current_user.id, answer.id, services) ShareWorker.perform_async(current_user.id, answer.id, services)
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.answer.create.okay') @response[:message] = I18n.t('messages.answer.create.okay')
@success = true @response[:success] = true
unless inbox unless inbox
# this assign is needed because shared/_answerbox relies on it, I think
@question = 1 @question = 1
@render = render_to_string(partial: 'shared/answerbox', locals: { a: answer, show_question: false }) @response[:render] = render_to_string(partial: 'shared/answerbox', locals: { a: answer, show_question: false })
end end
end end
@ -76,9 +66,8 @@ class Ajax::AnswerController < ApplicationController
answer = Answer.find(params[:answer]) answer = Answer.find(params[:answer])
unless (current_user == answer.user) or (privileged? answer.user) unless (current_user == answer.user) or (privileged? answer.user)
@status = :nopriv @response[:status] = :nopriv
@message = I18n.t('messages.answer.destroy.nopriv') @response[:message] = I18n.t('messages.answer.destroy.nopriv')
@success = false
return return
end end
@ -87,8 +76,8 @@ class Ajax::AnswerController < ApplicationController
end # TODO: decide what happens with the question end # TODO: decide what happens with the question
answer.destroy answer.destroy
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.answer.destroy.okay') @response[:message] = I18n.t('messages.answer.destroy.okay')
@success = true @response[:success] = true
end end
end end

View file

@ -1,11 +1,4 @@
class Ajax::CommentController < ApplicationController class Ajax::CommentController < AjaxController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def create def create
params.require :answer params.require :answer
params.require :comment params.require :comment
@ -15,38 +8,35 @@ class Ajax::CommentController < ApplicationController
begin begin
current_user.comment(answer, params[:comment]) current_user.comment(answer, params[:comment])
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid
@status = :rec_inv @response[:status] = :rec_inv
@message = I18n.t('messages.comment.create.rec_inv') @response[:message] = I18n.t('messages.comment.create.rec_inv')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.comment.create.okay') @response[:message] = I18n.t('messages.comment.create.okay')
@success = true @response[:success] = true
@render = render_to_string(partial: 'shared/comments', locals: { a: answer }) @response[:render] = render_to_string(partial: 'shared/comments', locals: { a: answer })
@count = answer.comment_count @response[:count] = answer.comment_count
end end
def destroy def destroy
params.require :comment params.require :comment
@status = :err @response[:status] = :err
@success = false
comment = Comment.find(params[:comment]) comment = Comment.find(params[:comment])
unless (current_user == comment.user) or (current_user == comment.answer.user) or (privileged? comment.user) unless (current_user == comment.user) or (current_user == comment.answer.user) or (privileged? comment.user)
@status = :nopriv @response[:status] = :nopriv
@message = I18n.t('messages.comment.destroy.nopriv') @response[:message] = I18n.t('messages.comment.destroy.nopriv')
@success = false
return return
end end
@count = comment.answer.comment_count - 1 @response[:count] = comment.answer.comment_count - 1
comment.destroy comment.destroy
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.comment.destroy.okay') @response[:message] = I18n.t('messages.comment.destroy.okay')
@success = true @response[:success] = true
end end
end end

View file

@ -1,11 +1,4 @@
class Ajax::FriendController < ApplicationController class Ajax::FriendController < AjaxController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def create def create
params.require :screen_name params.require :screen_name
@ -14,15 +7,14 @@ class Ajax::FriendController < ApplicationController
begin begin
current_user.follow target_user current_user.follow target_user
rescue rescue
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.friend.create.fail') @response[:message] = I18n.t('messages.friend.create.fail')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.friend.create.okay') @response[:message] = I18n.t('messages.friend.create.okay')
@success = true @response[:success] = true
end end
def destroy def destroy
@ -33,14 +25,13 @@ class Ajax::FriendController < ApplicationController
begin begin
current_user.unfollow target_user current_user.unfollow target_user
rescue rescue
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.friend.destroy.fail') @response[:message] = I18n.t('messages.friend.destroy.fail')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.friend.destroy.okay') @response[:message] = I18n.t('messages.friend.destroy.okay')
@success = true @response[:success] = true
end end
end end

View file

@ -1,26 +1,18 @@
class Ajax::GroupController < ApplicationController class Ajax::GroupController < AjaxController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def create def create
@status = :err @response[:status] = :err
@success = false
unless user_signed_in? unless user_signed_in?
@status = :noauth @response[:status] = :noauth
@message = I18n.t('messages.noauth') @response[:message] = I18n.t('messages.noauth')
return return
end end
begin begin
params.require :name params.require :name
rescue ActionController::ParameterMissing rescue ActionController::ParameterMissing
@status = :toolong @response[:status] = :toolong
@message = I18n.t('messages.group.create.noname') @response[:message] = I18n.t('messages.group.create.noname')
return return
end end
params.require :user params.require :user
@ -29,32 +21,31 @@ class Ajax::GroupController < ApplicationController
target_user = User.find_by_screen_name(params[:user]) target_user = User.find_by_screen_name(params[:user])
group = Group.create! user: current_user, display_name: params[:name] group = Group.create! user: current_user, display_name: params[:name]
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid
@status = :toolong @response[:status] = :toolong
@message = I18n.t('messages.group.create.toolong') @response[:message] = I18n.t('messages.group.create.toolong')
return return
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
@status = :notfound @response[:status] = :notfound
@message = I18n.t('messages.group.create.notfound') @response[:message] = I18n.t('messages.group.create.notfound')
return return
rescue ActiveRecord::RecordNotUnique rescue ActiveRecord::RecordNotUnique
@status = :exists @response[:status] = :exists
@message = I18n.t('messages.group.create.exists') @response[:message] = I18n.t('messages.group.create.exists')
return return
end end
@status = :okay @response[:status] = :okay
@success = true @response[:success] = true
@message = I18n.t('messages.group.create.okay') @response[:message] = I18n.t('messages.group.create.okay')
@render = render_to_string(partial: 'user/modal_group_item', locals: { group: group, user: target_user }) @response[:render] = render_to_string(partial: 'user/modal_group_item', locals: { group: group, user: target_user })
end end
def destroy def destroy
@status = :err @response[:status] = :err
@success = false
unless user_signed_in? unless user_signed_in?
@status = :noauth @response[:status] = :noauth
@message = I18n.t('messages.noauth') @response[:message] = I18n.t('messages.noauth')
return return
end end
@ -63,23 +54,22 @@ class Ajax::GroupController < ApplicationController
begin begin
Group.where(user: current_user, name: params[:group]).first.destroy! Group.where(user: current_user, name: params[:group]).first.destroy!
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
@status = :notfound @response[:status] = :notfound
@message = I18n.t('messages.group.destroy.notfound') @response[:message] = I18n.t('messages.group.destroy.notfound')
return return
end end
@status = :okay @response[:status] = :okay
@success = true @response[:success] = true
@message = I18n.t('messages.group.destroy.okay') @response[:message] = I18n.t('messages.group.destroy.okay')
end end
def membership def membership
@status = :err @response[:status] = :err
@success = false
unless user_signed_in? unless user_signed_in?
@status = :noauth @response[:status] = :noauth
@message = I18n.t('messages.noauth') @response[:message] = I18n.t('messages.noauth')
return return
end end
@ -92,8 +82,8 @@ class Ajax::GroupController < ApplicationController
begin begin
group = current_user.groups.find_by_name(params[:group]) group = current_user.groups.find_by_name(params[:group])
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
@status = :notfound @response[:status] = :notfound
@message = I18n.t('messages.group.membership.notfound') @response[:message] = I18n.t('messages.group.membership.notfound')
return return
end end
@ -101,15 +91,15 @@ class Ajax::GroupController < ApplicationController
if add if add
group.add_member target_user if group.members.find_by_user_id(target_user.id).nil? group.add_member target_user if group.members.find_by_user_id(target_user.id).nil?
@checked = true @response[:checked] = true
@message = I18n.t('messages.group.membership.add') @response[:message] = I18n.t('messages.group.membership.add')
else else
group.remove_member target_user unless group.members.find_by_user_id(target_user.id).nil? group.remove_member target_user unless group.members.find_by_user_id(target_user.id).nil?
@checked = false @response[:checked] = false
@message = I18n.t('messages.group.membership.remove') @response[:message] = I18n.t('messages.group.membership.remove')
end end
@status = :okay @response[:status] = :okay
@success = true @response[:success] = true
end end
end end

View file

@ -1,16 +1,8 @@
class Ajax::InboxController < ApplicationController class Ajax::InboxController < AjaxController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def create def create
unless user_signed_in? unless user_signed_in?
@status = :noauth @response[:status] = :noauth
@message = I18n.t('messages.noauth') @response[:message] = I18n.t('messages.noauth')
@success = false
return return
end end
@ -21,10 +13,10 @@ class Ajax::InboxController < ApplicationController
inbox = Inbox.create!(user: current_user, question_id: question.id, new: true) inbox = Inbox.create!(user: current_user, question_id: question.id, new: true)
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.inbox.create.okay') @response[:message] = I18n.t('messages.inbox.create.okay')
@success = true @response[:success] = true
@render = render_to_string(partial: 'inbox/entry', locals: { i: inbox }) @response[:render] = render_to_string(partial: 'inbox/entry', locals: { i: inbox })
inbox.update(new: false) inbox.update(new: false)
end end
@ -34,40 +26,36 @@ class Ajax::InboxController < ApplicationController
inbox = Inbox.find(params[:id]) inbox = Inbox.find(params[:id])
unless current_user == inbox.user unless current_user == inbox.user
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.inbox.remove.fail') @response[:message] = I18n.t('messages.inbox.remove.fail')
@success = false
return return
end end
begin begin
inbox.remove inbox.remove
rescue rescue
@status = :err @response[:status] = :err
@message = I18n.t('messages.error') @response[:message] = I18n.t('messages.error')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.inbox.remove.okay') @response[:message] = I18n.t('messages.inbox.remove.okay')
@success = true @response[:success] = true
end end
def remove_all def remove_all
begin begin
Inbox.where(user: current_user).each { |i| i.remove } Inbox.where(user: current_user).each { |i| i.remove }
rescue rescue
@status = :err @response[:status] = :err
@message = I18n.t('messages.error') @response[:message] = I18n.t('messages.error')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.inbox.remove_all.okay') @response[:message] = I18n.t('messages.inbox.remove_all.okay')
@success = true @response[:success] = true
render 'ajax/inbox/remove'
end end
def remove_all_author def remove_all_author
@ -77,15 +65,13 @@ class Ajax::InboxController < ApplicationController
.where(questions: { user_id: @target_user.id, author_is_anonymous: false }) .where(questions: { user_id: @target_user.id, author_is_anonymous: false })
@inbox.each { |i| i.remove } @inbox.each { |i| i.remove }
rescue rescue
@status = :err @response[:status] = :err
@message = I18n.t('messages.error') @response[:message] = I18n.t('messages.error')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.inbox.remove_all.okay') @response[:message] = I18n.t('messages.inbox.remove_all.okay')
@success = true @response[:success] = true
render 'ajax/inbox/remove'
end end
end end

View file

@ -1,11 +1,4 @@
class Ajax::ModerationController < ApplicationController class Ajax::ModerationController < AjaxController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def vote def vote
params.require :id params.require :id
params.require :upvote params.require :upvote
@ -15,16 +8,15 @@ class Ajax::ModerationController < ApplicationController
begin begin
current_user.report_vote(report, params[:upvote]) current_user.report_vote(report, params[:upvote])
rescue rescue
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.moderation.vote.fail') @response[:message] = I18n.t('messages.moderation.vote.fail')
@success = false
return return
end end
@count = report.votes @response[:count] = report.votes
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.moderation.vote.okay') @response[:message] = I18n.t('messages.moderation.vote.okay')
@success = true @response[:success] = true
end end
def destroy_vote def destroy_vote
@ -35,16 +27,15 @@ class Ajax::ModerationController < ApplicationController
begin begin
current_user.report_unvote report current_user.report_unvote report
rescue rescue
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.moderation.destroy_vote.fail') @response[:message] = I18n.t('messages.moderation.destroy_vote.fail')
@success = false
return return
end end
@count = report.votes @response[:count] = report.votes
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.moderation.destroy_vote.okay') @response[:message] = I18n.t('messages.moderation.destroy_vote.okay')
@success = true @response[:success] = true
end end
def destroy_report def destroy_report
@ -56,15 +47,14 @@ class Ajax::ModerationController < ApplicationController
report.deleted = true report.deleted = true
report.save report.save
rescue rescue
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.moderation.destroy_report.fail') @response[:message] = I18n.t('messages.moderation.destroy_report.fail')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.moderation.destroy_report.okay') @response[:message] = I18n.t('messages.moderation.destroy_report.okay')
@success = true @response[:success] = true
end end
def create_comment def create_comment
@ -73,48 +63,44 @@ class Ajax::ModerationController < ApplicationController
report = Report.find(params[:id]) report = Report.find(params[:id])
@success = false
begin begin
current_user.report_comment(report, params[:comment]) current_user.report_comment(report, params[:comment])
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid
@status = :rec_inv @response[:status] = :rec_inv
@message = I18n.t('messages.moderation.create_comment.rec_inv') @response[:message] = I18n.t('messages.moderation.create_comment.rec_inv')
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.moderation.create_comment.okay') @response[:message] = I18n.t('messages.moderation.create_comment.okay')
@success = true @response[:success] = true
@render = render_to_string(partial: 'moderation/discussion', locals: { report: report }) @response[:render] = render_to_string(partial: 'moderation/discussion', locals: { report: report })
@count = report.moderation_comments.all.count @response[:count] = report.moderation_comments.all.count
end end
def destroy_comment def destroy_comment
params.require :comment params.require :comment
@status = :err @response[:status] = :err
@success = false
comment = ModerationComment.find(params[:comment]) comment = ModerationComment.find(params[:comment])
unless current_user == comment.user unless current_user == comment.user
@status = :nopriv @response[:status] = :nopriv
@message = I18n.t('messages.moderation.destroy_comment.nopriv') @response[:message] = I18n.t('messages.moderation.destroy_comment.nopriv')
@success = false
return return
end end
comment.destroy comment.destroy
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.moderation.destroy_comment.okay') @response[:message] = I18n.t('messages.moderation.destroy_comment.okay')
@success = true @response[:success] = true
end end
def ban def ban
@status = :err @response[:status] = :err
@message = I18n.t('messages.moderation.ban.error') @response[:message] = I18n.t('messages.moderation.ban.error')
@success = false
params.require :user params.require :user
params.require :ban params.require :ban
@ -128,32 +114,30 @@ class Ajax::ModerationController < ApplicationController
buntil = DateTime.strptime params[:until], "%m/%d/%Y %I:%M %p" unless unban || perma buntil = DateTime.strptime params[:until], "%m/%d/%Y %I:%M %p" unless unban || perma
if !unban && target.has_role?(:administrator) if !unban && target.has_role?(:administrator)
@status = :nopriv @response[:status] = :nopriv
@message = I18n.t('messages.moderation.ban.nopriv') @response[:message] = I18n.t('messages.moderation.ban.nopriv')
@success = false
return return
end end
if unban if unban
target.unban target.unban
@message = I18n.t('messages.moderation.ban.unban') @response[:message] = I18n.t('messages.moderation.ban.unban')
@success = true @response[:success] = true
elsif perma elsif perma
target.ban nil, reason target.ban nil, reason
@message = I18n.t('messages.moderation.ban.perma') @response[:message] = I18n.t('messages.moderation.ban.perma')
else else
target.ban buntil, reason target.ban buntil, reason
@message = I18n.t('messages.moderation.ban.temp', date: buntil.to_s) @response[:message] = I18n.t('messages.moderation.ban.temp', date: buntil.to_s)
end end
target.save! target.save!
@status = :okay @response[:status] = :okay
@success = target.banned? == !unban @response[:success] = target.banned? == !unban
end end
def privilege def privilege
@status = :err @response[:status] = :err
@success = false
params.require :user params.require :user
params.require :type params.require :type
@ -163,17 +147,16 @@ class Ajax::ModerationController < ApplicationController
target_user = User.find_by_screen_name(params[:user]) target_user = User.find_by_screen_name(params[:user])
@message = I18n.t('messages.moderation.privilege.nope') @response[:message] = I18n.t('messages.moderation.privilege.nope')
return unless %w(moderator admin).include? params[:type].downcase return unless %w(moderator admin).include? params[:type].downcase
unless current_user.has_role?(:administrator) unless current_user.has_role?(:administrator)
@status = :nopriv @response[:status] = :nopriv
@message = I18n.t('messages.moderation.privilege.nopriv') @response[:message] = I18n.t('messages.moderation.privilege.nopriv')
@success = false
return return
end end
@checked = status @response[:checked] = status
type = params[:type].downcase type = params[:type].downcase
target_role = {"admin" => "administrator"}.fetch(type, type).to_sym target_role = {"admin" => "administrator"}.fetch(type, type).to_sym
@ -184,9 +167,9 @@ class Ajax::ModerationController < ApplicationController
end end
target_user.save! target_user.save!
@message = I18n.t('messages.moderation.privilege.checked', privilege: params[:type]) @response[:message] = I18n.t('messages.moderation.privilege.checked', privilege: params[:type])
@status = :okay @response[:status] = :okay
@success = true @response[:success] = true
end end
end end

View file

@ -1,36 +1,25 @@
class Ajax::QuestionController < ApplicationController class Ajax::QuestionController < AjaxController
include MarkdownHelper
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def destroy def destroy
params.require :question params.require :question
question = Question.find params[:question] question = Question.find params[:question]
if question.nil? if question.nil?
@status = :not_found @response[:status] = :not_found
@message = I18n.t('messages.question.destroy.not_found') @response[:message] = I18n.t('messages.question.destroy.not_found')
@success = false
return return
end end
if not (current_user.mod? or question.user == current_user) if not (current_user.mod? or question.user == current_user)
@status = :not_authorized @response[:status] = :not_authorized
@message = I18n.t('messages.question.destroy.not_authorized') @response[:message] = I18n.t('messages.question.destroy.not_authorized')
@success = false
return return
end end
question.destroy! question.destroy!
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.question.destroy.okay') @response[:message] = I18n.t('messages.question.destroy.okay')
@success = true @response[:success] = true
end end
def create def create
@ -43,9 +32,8 @@ class Ajax::QuestionController < ApplicationController
author_is_anonymous: params[:anonymousQuestion], author_is_anonymous: params[:anonymousQuestion],
user: current_user) user: current_user)
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid
@status = :rec_inv @response[:status] = :rec_inv
@message = I18n.t('messages.question.create.rec_inv') @response[:message] = I18n.t('messages.question.create.rec_inv')
@success = false
return return
end end
@ -63,41 +51,23 @@ class Ajax::QuestionController < ApplicationController
current_user.groups.find_by_name!(params[:rcpt].sub 'grp:', '') current_user.groups.find_by_name!(params[:rcpt].sub 'grp:', '')
QuestionWorker.perform_async params[:rcpt], current_user.id, question.id QuestionWorker.perform_async params[:rcpt], current_user.id, question.id
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
@status = :not_found @response[:status] = :not_found
@message = I18n.t('messages.question.create.not_found') @response[:message] = I18n.t('messages.question.create.not_found')
@success = false
return return
end end
end end
else else
if User.find(params[:rcpt]).nil? if User.find(params[:rcpt]).nil?
@status = :not_found @response[:status] = :not_found
@message = I18n.t('messages.question.create.not_found') @response[:message] = I18n.t('messages.question.create.not_found')
@success = false
return return
end end
Inbox.create!(user_id: params[:rcpt], question_id: question.id, new: true) Inbox.create!(user_id: params[:rcpt], question_id: question.id, new: true)
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.question.create.okay') @response[:message] = I18n.t('messages.question.create.okay')
@success = true @response[:success] = true
end
def preview
params.require :md
@message = I18n.t('messages.question.preview.fail')
begin
@markdown = markdown params[:md]
@message = I18n.t('messages.question.preview.okay')
rescue
@status = :fail
@success = false
return
end
@status = :okay
@success = true
end end
end end

View file

@ -1,25 +1,17 @@
class Ajax::ReportController < ApplicationController class Ajax::ReportController < AjaxController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def create def create
params.require :id params.require :id
params.require :type params.require :type
@status = :err @response[:status] = :err
@success = false
if current_user.nil? if current_user.nil?
@message = I18n.t('messages.report.create.login') @response[:message] = I18n.t('messages.report.create.login')
return return
end end
unless %w(answer comment question user).include? params[:type] unless %w(answer comment question user).include? params[:type]
@message = I18n.t('messages.report.create.unknown') @response[:message] = I18n.t('messages.report.create.unknown')
return return
end end
@ -39,14 +31,14 @@ class Ajax::ReportController < ApplicationController
end end
if object.nil? if object.nil?
@message = I18n.t('messages.report.create.not_found', parameter: params[:type]) @response[:message] = I18n.t('messages.report.create.not_found', parameter: params[:type])
return return
end end
current_user.report object, params[:reason] current_user.report object, params[:reason]
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.report.create.okay', parameter: params[:type]) @response[:message] = I18n.t('messages.report.create.okay', parameter: params[:type])
@success = true @response[:success] = true
end end
end end

View file

@ -1,11 +1,4 @@
class Ajax::SmileController < ApplicationController class Ajax::SmileController < AjaxController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def create def create
params.require :id params.require :id
@ -14,15 +7,14 @@ class Ajax::SmileController < ApplicationController
begin begin
current_user.smile answer current_user.smile answer
rescue rescue
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.smile.create.fail') @response[:message] = I18n.t('messages.smile.create.fail')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.smile.create.okay') @response[:message] = I18n.t('messages.smile.create.okay')
@success = true @response[:success] = true
end end
def destroy def destroy
@ -33,15 +25,14 @@ class Ajax::SmileController < ApplicationController
begin begin
current_user.unsmile answer current_user.unsmile answer
rescue rescue
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.smile.destroy.fail') @response[:message] = I18n.t('messages.smile.destroy.fail')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.smile.destroy.okay') @response[:message] = I18n.t('messages.smile.destroy.okay')
@success = true @response[:success] = true
end end
def create_comment def create_comment
@ -52,15 +43,14 @@ class Ajax::SmileController < ApplicationController
begin begin
current_user.smile_comment comment current_user.smile_comment comment
rescue rescue
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.smile.create_comment.fail') @response[:message] = I18n.t('messages.smile.create_comment.fail')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.smile.create_comment.okay') @response[:message] = I18n.t('messages.smile.create_comment.okay')
@success = true @response[:success] = true
end end
def destroy_comment def destroy_comment
@ -71,14 +61,13 @@ class Ajax::SmileController < ApplicationController
begin begin
current_user.unsmile_comment comment current_user.unsmile_comment comment
rescue rescue
@status = :fail @response[:status] = :fail
@message = I18n.t('messages.smile.destroy_comment.fail') @response[:message] = I18n.t('messages.smile.destroy_comment.fail')
@success = false
return return
end end
@status = :okay @response[:status] = :okay
@message = I18n.t('messages.smile.destroy_comment.okay') @response[:message] = I18n.t('messages.smile.destroy_comment.okay')
@success = true @response[:success] = true
end end
end end

View file

@ -1,25 +1,19 @@
class Ajax::SubscriptionController < ApplicationController class Ajax::SubscriptionController < AjaxController
before_action :authenticate_user! before_action :authenticate_user!
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def subscribe def subscribe
params.require :answer params.require :answer
@status = 418 @response[:status] = 418
@message = I18n.t('messages.subscription.torpedo') @response[:message] = I18n.t('messages.subscription.torpedo')
state = Subscription.subscribe(current_user, Answer.find(params[:answer])).nil? state = Subscription.subscribe(current_user, Answer.find(params[:answer])).nil?
@success = state == false @response[:success] = state == false
end end
def unsubscribe def unsubscribe
params.require :answer params.require :answer
@status = 418 @response[:status] = 418
@message = I18n.t('messages.subscription.torpedo') @response[:message] = I18n.t('messages.subscription.torpedo')
state = Subscription.unsubscribe(current_user, Answer.find(params[:answer])).nil? state = Subscription.unsubscribe(current_user, Answer.find(params[:answer])).nil?
@success = state == false @response[:success] = state == false
end end
end end

View file

@ -0,0 +1,57 @@
# frozen_string_literal: true
class AjaxController < ApplicationController
before_action :build_response
after_action :return_response
respond_to :json
rescue_from(ActiveRecord::RecordNotFound) do |e|
NewRelic::Agent.notice_error(e)
@response = {
success: false,
message: "Record not found",
status: :not_found
}
return_response
end
rescue_from(ActionController::ParameterMissing) do |e|
NewRelic::Agent.notice_error(e)
@response = {
success: false,
message: I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize),
status: :parameter_error
}
return_response
end
def find_active_announcements
# We do not need announcements here
end
private
def build_response
@response = {
success: false,
message: '',
status: 'unknown'
}
end
def return_response
# Q: Why don't we just use render(json:) here?
# A: Because otherwise Rails wants us to use views, which do not make much sense here.
#
# Q: Why do we always return 200?
# A: Because JQuery might not do things we want it to if we don't.
response.status = 200
response.headers["Content-Type"] = "application/json"
response.body = @response.to_json
end
end

View file

@ -1,2 +0,0 @@
json.partial! 'ajax/shared/status'
json.render @render if @render

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1,3 +0,0 @@
json.partial! 'ajax/shared/status'
json.render @render if @render
json.count @count if @count

View file

@ -1,2 +0,0 @@
json.partial! 'ajax/shared/status'
json.count @count if @count

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1,2 +0,0 @@
json.partial! 'ajax/shared/status'
json.render @render if @render

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1,2 +0,0 @@
json.partial! 'ajax/shared/status'
json.checked @checked

View file

@ -1,2 +0,0 @@
json.partial! 'ajax/shared/status'
json.render @render

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1,2 +0,0 @@
json.partial! 'ajax/shared/status'
json.markdown @markdown if @markdown

View file

@ -1,2 +0,0 @@
json.partial! 'ajax/shared/status'
json.render @render

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -1 +0,0 @@
json.partial! 'ajax/shared/status'

View file

@ -152,9 +152,6 @@ cs:
rec_inv: "Vaše otázka je příliš dlhá." rec_inv: "Vaše otázka je příliš dlhá."
not_found: "Skupina nebyla nalezena" not_found: "Skupina nebyla nalezena"
okay: "Otázka úspěšně zeptána." okay: "Otázka úspěšně zeptána."
preview:
fail: "Renderování kódu selhalo."
okay: "Úspěšně renderováno kód."
report: report:
create: create:
login: "přihlášení povinné" login: "přihlášení povinné"

View file

@ -154,9 +154,6 @@ de:
rec_inv: "Deine Frage ist zu lang." rec_inv: "Deine Frage ist zu lang."
not_found: "Gruppe nicht gefunden" not_found: "Gruppe nicht gefunden"
okay: "Frage erfolgreich gestellt." okay: "Frage erfolgreich gestellt."
preview:
fail: "Das Markdownrendern ist fehlgeschlagen."
okay: "Markdown erfolgreich gerendert."
report: report:
create: create:
login: "Login benötigt" login: "Login benötigt"
@ -455,4 +452,4 @@ de:
contributor: Contributor contributor: Contributor
blogger: Blogger blogger: Blogger
banned: Gebannt banned: Gebannt
translator: Übersetzer translator: Übersetzer

View file

@ -151,9 +151,6 @@ en:
rec_inv: "Your question is too long." rec_inv: "Your question is too long."
not_found: "Group not found" not_found: "Group not found"
okay: "Question asked successfully." okay: "Question asked successfully."
preview:
fail: "Failed to render markdown."
okay: "Successfully rendered markdown."
report: report:
create: create:
login: "login required" login: "login required"

View file

@ -151,9 +151,6 @@ en_dizzle:
rec_inv: "Yo crazy-ass question is too long." rec_inv: "Yo crazy-ass question is too long."
not_found: "Group not found" not_found: "Group not found"
okay: "Question asked successfully." okay: "Question asked successfully."
preview:
fail: "Failed ta render markdown."
okay: "Successfully rendered markdown."
report: report:
create: create:
login: "login required" login: "login required"

View file

@ -151,9 +151,6 @@ en_pirate:
rec_inv: "Your inquiry is too long." rec_inv: "Your inquiry is too long."
not_found: "Crew not found" not_found: "Crew not found"
okay: "Inquiry asked successfully." okay: "Inquiry asked successfully."
preview:
fail: "Failed to render markdown."
okay: "Successfully rendered markdown."
report: report:
create: create:
login: "login required" login: "login required"

View file

@ -154,9 +154,6 @@ fr:
rec_inv: "Votre question est trop longue." rec_inv: "Votre question est trop longue."
not_found: "Le groupe est introuvable." not_found: "Le groupe est introuvable."
okay: "La question a été posée avec succès." okay: "La question a été posée avec succès."
preview:
fail: "Échec de rendu du markdown."
okay: "Markdown rendu avec succès."
report: report:
create: create:
login: "connexion requise" login: "connexion requise"

View file

@ -151,9 +151,6 @@ it:
rec_inv: "La tua domanda è troppo lunga." rec_inv: "La tua domanda è troppo lunga."
not_found: "Gruppo non trovato" not_found: "Gruppo non trovato"
okay: "Domanda chiesta." okay: "Domanda chiesta."
preview:
fail: "Impossibile renderizzare markdown."
okay: "Markdown renderizzato."
report: report:
create: create:
login: "devi prima fare il login" login: "devi prima fare il login"

View file

@ -153,9 +153,6 @@ ja:
rec_inv: 質問が長過ぎます。 rec_inv: 質問が長過ぎます。
not_found: グループが見つかりません not_found: グループが見つかりません
okay: 質問の投稿に成功しました。 okay: 質問の投稿に成功しました。
preview:
fail: 書式処理に失敗しました。
okay: 書式処理に成功しました。
report: report:
create: create:
login: ログインが必要です login: ログインが必要です

View file

@ -106,7 +106,6 @@ Rails.application.routes.draw do
match '/create_group', to: 'group#create', via: :post, as: :create_group match '/create_group', to: 'group#create', via: :post, as: :create_group
match '/destroy_group', to: 'group#destroy', via: :post, as: :destroy_group match '/destroy_group', to: 'group#destroy', via: :post, as: :destroy_group
match '/group_membership', to: 'group#membership', via: :post, as: :group_membership match '/group_membership', to: 'group#membership', via: :post, as: :group_membership
match '/preview', to: "question#preview", via: :post, as: :preview
match '/subscribe', to: 'subscription#subscribe', via: :post, as: :subscribe_answer match '/subscribe', to: 'subscription#subscribe', via: :post, as: :subscribe_answer
match '/unsubscribe', to: 'subscription#unsubscribe', via: :post, as: :unsubscribe_answer match '/unsubscribe', to: 'subscription#unsubscribe', via: :post, as: :unsubscribe_answer
end end