Allow anonymous questions to be returned when the user has disabled anonymous questions

Fixes #267
This commit is contained in:
Karina Kwiatek 2022-06-15 17:30:48 +02:00 committed by Karina Kwiatek
parent a37b6135ec
commit cca8795b01
3 changed files with 14 additions and 3 deletions

View File

@ -66,8 +66,8 @@ class Ajax::AnswerController < AjaxController
end
if answer.user == current_user
Inbox.create!(user: answer.user, question: answer.question, new: true)
end # TODO: decide what happens with the question
Inbox.create!(user: answer.user, question: answer.question, new: true, returning: true)
end
answer.destroy
@response[:status] = :okay

View File

@ -2,8 +2,13 @@ class Inbox < ApplicationRecord
belongs_to :user
belongs_to :question
attr_accessor :returning
before_create do
raise "User does not want to receive anonymous questions" if self.question.author_is_anonymous and self.question.author_name != 'justask' and !self.user.privacy_allow_anonymous_questions?
raise "User does not want to receive anonymous questions" if !returning &&
question.author_is_anonymous &&
(question.author_name != "justask") &&
!user.privacy_allow_anonymous_questions?
end
def answer(answer_content, user)

View File

@ -268,6 +268,12 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do
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
it "returns the question back to the user's inbox when the user has anonymous questions disabled" do
user.privacy_allow_anonymous_questions = false
user.save
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