Merge branch 'master' into feature/bootstrap

This commit is contained in:
Andreas Nedbal 2020-04-29 02:49:07 +02:00
commit 45b11bddfd
53 changed files with 613 additions and 427 deletions

View File

@ -90,6 +90,7 @@ group :development, :test do
gem 'puma' gem 'puma'
gem 'rspec-rails', '~> 3.9' gem 'rspec-rails', '~> 3.9'
gem 'rspec-its', '~> 1.3' gem 'rspec-its', '~> 1.3'
gem "rspec-sidekiq", "~> 3.0"
gem 'factory_bot_rails', require: false gem 'factory_bot_rails', require: false
gem 'faker' gem 'faker'
gem 'capybara' gem 'capybara'

View File

@ -406,6 +406,9 @@ GEM
rspec-expectations (~> 3.9.0) rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0) rspec-mocks (~> 3.9.0)
rspec-support (~> 3.9.0) rspec-support (~> 3.9.0)
rspec-sidekiq (3.0.3)
rspec-core (~> 3.0, >= 3.0.0)
sidekiq (>= 2.4.0)
rspec-support (3.9.2) rspec-support (3.9.2)
ruby-progressbar (1.10.1) ruby-progressbar (1.10.1)
sanitize (5.1.0) sanitize (5.1.0)
@ -563,6 +566,7 @@ DEPENDENCIES
rolify (~> 5.2) rolify (~> 5.2)
rspec-its (~> 1.3) rspec-its (~> 1.3)
rspec-rails (~> 3.9) rspec-rails (~> 3.9)
rspec-sidekiq (~> 3.0)
ruby-progressbar ruby-progressbar
sanitize sanitize
sass-rails (~> 5.0) sass-rails (~> 5.0)

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
@ -50,10 +40,10 @@ class Ajax::AnswerController < ApplicationController
else else
current_user.answer question, params[:answer] current_user.answer question, params[:answer]
end end
rescue rescue => e
@status = :err NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.error') @response[:status] = :err
@success = false @response[:message] = I18n.t('messages.error')
return return
end end
@ -61,12 +51,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 +67,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 +77,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
@ -14,39 +7,37 @@ class Ajax::CommentController < ApplicationController
begin begin
current_user.comment(answer, params[:comment]) current_user.comment(answer, params[:comment])
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid => e
@status = :rec_inv NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.comment.create.rec_inv') @response[:status] = :rec_inv
@success = false @response[:message] = I18n.t('messages.comment.create.rec_inv')
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
@ -13,16 +6,16 @@ class Ajax::FriendController < ApplicationController
begin begin
current_user.follow target_user current_user.follow target_user
rescue rescue => e
@status = :fail NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.friend.create.fail') @response[:status] = :fail
@success = false @response[:message] = I18n.t('messages.friend.create.fail')
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
@ -32,15 +25,15 @@ class Ajax::FriendController < ApplicationController
begin begin
current_user.unfollow target_user current_user.unfollow target_user
rescue rescue => e
@status = :fail NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.friend.destroy.fail') @response[:status] = :fail
@success = false @response[:message] = I18n.t('messages.friend.destroy.fail')
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,19 @@
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 => e
@status = :toolong NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.group.create.noname') @response[:status] = :toolong
@response[:message] = I18n.t('messages.group.create.noname')
return return
end end
params.require :user params.require :user
@ -28,33 +21,35 @@ class Ajax::GroupController < ApplicationController
begin begin
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 => e
@status = :toolong NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.group.create.toolong') @response[:status] = :toolong
@response[:message] = I18n.t('messages.group.create.toolong')
return return
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound => e
@status = :notfound NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.group.create.notfound') @response[:status] = :notfound
@response[:message] = I18n.t('messages.group.create.notfound')
return return
rescue ActiveRecord::RecordNotUnique rescue ActiveRecord::RecordNotUnique => e
@status = :exists NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.group.create.exists') @response[:status] = :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: 'modal/group/item', locals: { group: group, user: target_user }) @response[:render] = render_to_string(partial: '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
@ -62,24 +57,24 @@ 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 => e
@status = :notfound NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.group.destroy.notfound') @response[:status] = :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
@ -91,9 +86,10 @@ 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 => e
@status = :notfound NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.group.membership.notfound') @response[:status] = :notfound
@response[:message] = I18n.t('messages.group.membership.notfound')
return return
end end
@ -101,15 +97,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,38 @@ 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 => e
@status = :err NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.error') @response[:status] = :err
@success = false @response[:message] = I18n.t('messages.error')
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 => e
@status = :err NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.error') @response[:status] = :err
@success = false @response[:message] = I18n.t('messages.error')
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
@ -76,16 +66,15 @@ class Ajax::InboxController < ApplicationController
@inbox = current_user.inboxes.joins(:question) @inbox = current_user.inboxes.joins(:question)
.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 => e
@status = :err NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.error') @response[:status] = :err
@success = false @response[:message] = I18n.t('messages.error')
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
@ -14,17 +7,17 @@ class Ajax::ModerationController < ApplicationController
begin begin
current_user.report_vote(report, params[:upvote]) current_user.report_vote(report, params[:upvote])
rescue rescue => e
@status = :fail NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.moderation.vote.fail') @response[:status] = :fail
@success = false @response[:message] = I18n.t('messages.moderation.vote.fail')
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
@ -34,17 +27,17 @@ class Ajax::ModerationController < ApplicationController
begin begin
current_user.report_unvote report current_user.report_unvote report
rescue rescue => e
@status = :fail NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.moderation.destroy_vote.fail') @response[:status] = :fail
@success = false @response[:message] = I18n.t('messages.moderation.destroy_vote.fail')
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
@ -55,16 +48,16 @@ class Ajax::ModerationController < ApplicationController
begin begin
report.deleted = true report.deleted = true
report.save report.save
rescue rescue => e
@status = :fail NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.moderation.destroy_report.fail') @response[:status] = :fail
@success = false @response[:message] = I18n.t('messages.moderation.destroy_report.fail')
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 +66,45 @@ 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 => e
@status = :rec_inv NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.moderation.create_comment.rec_inv') @response[:status] = :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 +118,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 +151,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 +171,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
@ -42,10 +31,10 @@ class Ajax::QuestionController < ApplicationController
question = Question.create!(content: params[:question], question = Question.create!(content: params[:question],
author_is_anonymous: params[:anonymousQuestion], author_is_anonymous: params[:anonymousQuestion],
user: current_user) user: current_user)
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid => e
@status = :rec_inv NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.question.create.rec_inv') @response[:status] = :rec_inv
@success = false @response[:message] = I18n.t('messages.question.create.rec_inv')
return return
end end
@ -62,42 +51,25 @@ class Ajax::QuestionController < ApplicationController
begin begin
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 => e
@status = :not_found NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.question.create.not_found') @response[:status] = :not_found
@success = false @response[:message] = I18n.t('messages.question.create.not_found')
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
@ -13,16 +6,16 @@ class Ajax::SmileController < ApplicationController
begin begin
current_user.smile answer current_user.smile answer
rescue rescue => e
@status = :fail NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.smile.create.fail') @response[:status] = :fail
@success = false @response[:message] = I18n.t('messages.smile.create.fail')
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
@ -32,16 +25,16 @@ class Ajax::SmileController < ApplicationController
begin begin
current_user.unsmile answer current_user.unsmile answer
rescue rescue => e
@status = :fail NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.smile.destroy.fail') @response[:status] = :fail
@success = false @response[:message] = I18n.t('messages.smile.destroy.fail')
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
@ -51,16 +44,16 @@ class Ajax::SmileController < ApplicationController
begin begin
current_user.smile_comment comment current_user.smile_comment comment
rescue rescue => e
@status = :fail NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.smile.create_comment.fail') @response[:status] = :fail
@success = false @response[:message] = I18n.t('messages.smile.create_comment.fail')
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
@ -70,15 +63,15 @@ class Ajax::SmileController < ApplicationController
begin begin
current_user.unsmile_comment comment current_user.unsmile_comment comment
rescue rescue => e
@status = :fail NewRelic::Agent.notice_error(e)
@message = I18n.t('messages.smile.destroy_comment.fail') @response[:status] = :fail
@success = false @response[:message] = I18n.t('messages.smile.destroy_comment.fail')
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: e.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,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,2 +0,0 @@
json.partial! 'ajax/shared/status'
json.count @count if @count

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.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.markdown @markdown if @markdown

View File

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

View File

@ -1,3 +0,0 @@
json.success @success
json.status @status
json.message @message

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

View File

@ -0,0 +1,290 @@
# coding: utf-8
# frozen_string_literal: true
require "rails_helper"
describe Ajax::AnswerController, type: :controller do
shared_examples "returns the expected response" do
it "returns the expected response" do
expect(JSON.parse(subject.body)).to match(expected_response)
end
end
let(:user) { FactoryBot.create(:user) }
let(:question) { FactoryBot.create(:question, user: FactoryBot.build(:user, privacy_allow_stranger_answers: asker_allows_strangers)) }
let(:asker_allows_strangers) { true }
describe "#create" do
let(:params) do
{
id: id,
answer: answer,
share: shared_services&.to_json,
inbox: inbox
}.compact
end
subject { post(:create, params: params) }
context "when user is signed in" do
shared_examples "creates the answer" do
it "creates an answer" do
expect { subject }.to(change { Answer.count }.by(1))
end
it "enqueues a job for sharing the answer to social networks" do
subject
expect(ShareWorker).to have_enqueued_sidekiq_job(user.id, Answer.last.id, shared_services)
end
include_examples "returns the expected response"
end
shared_examples "does not create the answer" do
it "does not create an answer" do
expect { subject }.not_to(change { Answer.count })
end
it "does not enqueue a job for sharing the answer to social networks" do
subject
expect(ShareWorker).not_to have_enqueued_sidekiq_job(anything, anything, anything)
end
include_examples "returns the expected response"
end
shared_examples "fails when answer content is empty" do
let(:expected_response) do
{
"success" => false,
# caught by rescue_from, so status is not peter_dinklage
"status" => "parameter_error",
"message" => anything
}
end
["", " ", "\r \n"].each do |a|
context "when answer is #{a.inspect}" do
let(:answer) { a }
include_examples "does not create the answer"
end
end
end
before(:each) { sign_in(user) }
context "when all parameters are given" do
let(:answer) { "Werfen Sie nicht länger das Fenster zum Geld hinaus!" }
let(:shared_services) { %w[twitter] }
context "when inbox is true" do
let(:id) { FactoryBot.create(:inbox, user: inbox_user, question: question).id }
let(:inbox) { true }
context "when the inbox entry belongs to the user" do
let(:inbox_user) { user }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
include_examples "creates the answer"
it_behaves_like "fails when answer content is empty"
end
context "when the inbox entry does not belong to the user" do
let(:inbox_user) { FactoryBot.create(:user) }
let(:expected_response) do
{
"success" => false,
"status" => "fail",
"message" => anything
}
end
include_examples "does not create the answer"
it_behaves_like "fails when answer content is empty"
end
end
context "when inbox is false" do
let(:id) { question.id }
let(:inbox) { false }
context "when question asker allows strangers to answer (i.e. question was not in inbox)" do
let(:asker_allows_strangers) { true }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything,
"render" => anything
}
end
include_examples "creates the answer"
it_behaves_like "fails when answer content is empty"
end
context "when question asker does not allow strangers to answer (i.e. question was not in inbox)" do
let(:asker_allows_strangers) { false }
let(:expected_response) do
{
"success" => false,
"status" => "privacy_stronk",
"message" => anything
}
end
include_examples "does not create the answer"
end
end
end
context "when some parameters are missing" do
let(:id) { nil }
let(:answer) { "Trollolo" }
let(:inbox) { nil }
let(:shared_services) { [] }
let(:expected_response) do
{
"success" => false,
# caught by rescue_from, so status is not peter_dinklage
"status" => "parameter_error",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
context "when user is not signed in" do
let(:id) { question.id }
let(:inbox) { false }
let(:shared_services) { %w[twitter] }
let(:answer) { "HACKED" }
let(:expected_response) do
{
"success" => false,
"status" => "err",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
describe "#destroy" do
let(:answer_user) { user }
let(:question) { FactoryBot.create(:question, user: FactoryBot.create(:user)) }
let(:answer) { FactoryBot.create(:answer, user: answer_user, question: question) }
let(:answer_id) { answer.id }
let(:params) do
{
answer: answer_id
}
end
subject { delete(:destroy, params: params) }
context "when user is signed in" do
shared_examples "deletes the answer" do
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
it "deletes the answer" do
answer # ensure we already have it in the db
expect { subject }.to(change { Answer.count }.by(-1))
end
include_examples "returns the expected response"
end
shared_examples "does not delete the answer" do
let(:expected_response) do
{
"success" => false,
"status" => "nopriv",
"message" => anything
}
end
it "does not delete the answer" do
answer # ensure we already have it in the db
expect { subject }.not_to(change { Answer.count })
end
include_examples "returns the expected response"
end
before(:each) { sign_in(user) }
context "when the answer exists and was made by the current user" do
include_examples "deletes the answer"
it "returns the question back to the user's inbox" do
expect { subject }.to(change { Inbox.where(question_id: answer.question.id, user_id: user.id).count }.by(1))
end
end
context "when the answer exists and was not made by the current user" do
let(:answer_user) { FactoryBot.create(:user) }
include_examples "does not delete the answer"
%i[moderator administrator].each do |privileged_role|
context "when the current user is a #{privileged_role}" do
around do |example|
user.add_role privileged_role
example.run
user.remove_role privileged_role
end
include_examples "deletes the answer"
end
end
end
context "when the answer does not exist" do
let(:answer_id) { "sonic_the_hedgehog" }
let(:expected_response) do
{
"success" => false,
"status" => anything,
"message" => anything
}
end
include_examples "returns the expected response"
end
end
context "when user is not signed in" do
let(:expected_response) do
{
"success" => false,
"status" => "nopriv",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
end

8
spec/factories/inbox.rb Normal file
View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
FactoryBot.define do
factory :inbox do
question { FactoryBot.build(:question) }
new { true }
end
end