diff --git a/app/controllers/ajax/question_controller.rb b/app/controllers/ajax/question_controller.rb index a817e9b4..e35a9e7a 100644 --- a/app/controllers/ajax/question_controller.rb +++ b/app/controllers/ajax/question_controller.rb @@ -1,4 +1,6 @@ -require 'errors' +# frozen_string_literal: true + +require "errors" class Ajax::QuestionController < AjaxController def destroy @@ -72,9 +74,11 @@ class Ajax::QuestionController < AjaxController return end + # rubocop:disable Style/IfUnlessModifier unless MuteRule.where(user: target_user).any? { |rule| rule.applies_to? question } Inbox.create!(user_id: target_user.id, question_id: question.id, new: true) end + # rubocop:enable Style/IfUnlessModifier end @response[:status] = :okay diff --git a/app/models/user.rb b/app/models/user.rb index 32781196..3de6a619 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -106,8 +106,10 @@ class User < ApplicationRecord # @param question [Question] the question to answer # @param content [String] the answer content def answer(question, content) + # rubocop:disable Style/RedundantSelf raise Errors::AnsweringOtherBlockedSelf if question.user.blocking?(self) raise Errors::AnsweringSelfBlockedOther if self.blocking?(question.user) + # rubocop:enable Style/RedundantSelf Answer.create!(content: content, user: self, @@ -123,8 +125,10 @@ class User < ApplicationRecord # smiles an answer # @param answer [Answer] the answer to smile def smile(answer) + # rubocop:disable Style/RedundantSelf raise Errors::ReactingSelfBlockedOther if self.blocking?(answer.user) raise Errors::ReactingOtherBlockedSelf if answer.user.blocking?(self) + # rubocop:enable Style/RedundantSelf Smile.create!(user: self, answer: answer) end @@ -156,8 +160,10 @@ class User < ApplicationRecord end def comment(answer, content) + # rubocop:disable Style/RedundantSelf raise Errors::CommentingSelfBlockedOther if self.blocking?(answer.user) raise Errors::CommentingOtherBlockedSelf if answer.user.blocking?(self) + # rubocop:enable Style/RedundantSelf Comment.create!(user: self, answer: answer, content: content) end diff --git a/app/models/user/relationship/block.rb b/app/models/user/relationship/block.rb index 6ed16347..93af5956 100644 --- a/app/models/user/relationship/block.rb +++ b/app/models/user/relationship/block.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'errors' +require "errors" class User module Relationship @@ -22,11 +22,7 @@ class User def block(target_user) raise Errors::BlockingSelf if target_user == self - unfollow(target_user) if following?(target_user) - target_user.unfollow(self) if target_user.following?(self) - target_user.inboxes.joins(:question).where(question: { user_id: id }).destroy_all - inboxes.joins(:question).where(questions: { user_id: target_user.id, author_is_anonymous: false }).destroy_all - ListMember.joins(:list).where(list: { user_id: target_user.id }, user_id: id).destroy_all + unfollow_and_remove(target_user) create_relationship(active_block_relationships, target_user) end @@ -39,6 +35,16 @@ class User def blocking?(target_user) relationship_active?(blocked_users, target_user) end + + private + + def unfollow_and_remove(target_user) + unfollow(target_user) if following?(target_user) + target_user.unfollow(self) if target_user.following?(self) + target_user.inboxes.joins(:question).where(question: { user_id: id }).destroy_all + inboxes.joins(:question).where(questions: { user_id: target_user.id, author_is_anonymous: false }).destroy_all + ListMember.joins(:list).where(list: { user_id: target_user.id }, user_id: id).destroy_all + end end end end diff --git a/app/models/user/relationship/follow.rb b/app/models/user/relationship/follow.rb index fd4701ba..9f13bd7d 100644 --- a/app/models/user/relationship/follow.rb +++ b/app/models/user/relationship/follow.rb @@ -21,8 +21,10 @@ class User # Follow an user def follow(target_user) raise Errors::FollowingSelf if target_user == self + # rubocop:disable Style/RedundantSelf raise Errors::FollowingOtherBlockedSelf if target_user.blocking?(self) raise Errors::FollowingSelfBlockedOther if self.blocking?(target_user) + # rubocop:enable Style/RedundantSelf create_relationship(active_follow_relationships, target_user) end diff --git a/spec/controllers/ajax/answer_controller_spec.rb b/spec/controllers/ajax/answer_controller_spec.rb index 8adc591b..2fdce45a 100644 --- a/spec/controllers/ajax/answer_controller_spec.rb +++ b/spec/controllers/ajax/answer_controller_spec.rb @@ -148,7 +148,7 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do let(:expected_response) do { "success" => false, - "status" => "answering_other_blocked_self", + "status" => "answering_other_blocked_self", "message" => I18n.t("errors.answering_other_blocked_self") } end @@ -164,7 +164,7 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do let(:expected_response) do { "success" => false, - "status" => "answering_self_blocked_other", + "status" => "answering_self_blocked_other", "message" => I18n.t("errors.answering_self_blocked_other") } end