From b58883e0043d6d4523cb5b69e62d1feed5347193 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Mon, 25 May 2020 18:33:09 +0200 Subject: [PATCH] Remove "ask a group" feature --- app/assets/javascripts/questionbox/all.coffee | 9 +-- app/controllers/ajax/question_controller.rb | 19 +------ app/views/modal/_ask.haml | 8 --- app/workers/question_worker.rb | 28 ++++----- .../ajax/question_controller_spec.rb | 57 +------------------ spec/workers/question_worker_spec.rb | 30 ++++++++++ 6 files changed, 44 insertions(+), 107 deletions(-) create mode 100644 spec/workers/question_worker_spec.rb diff --git a/app/assets/javascripts/questionbox/all.coffee b/app/assets/javascripts/questionbox/all.coffee index ea884283..7e3160a0 100644 --- a/app/assets/javascripts/questionbox/all.coffee +++ b/app/assets/javascripts/questionbox/all.coffee @@ -3,18 +3,11 @@ $(document).on "click", "button[name=qb-all-ask]", -> btn.button "loading" $("textarea[name=qb-all-question]").attr "readonly", "readonly" - rcptSelect = ($ 'select[name=qb-all-rcpt]') - - rcpt = if rcptSelect.length > 0 - rcptSelect.first().val() - else - 'followers' - $.ajax url: '/ajax/ask' type: 'POST' data: - rcpt: rcpt + rcpt: 'followers' question: $("textarea[name=qb-all-question]").val() anonymousQuestion: false success: (data, status, jqxhr) -> diff --git a/app/controllers/ajax/question_controller.rb b/app/controllers/ajax/question_controller.rb index ec465915..ebe43d61 100644 --- a/app/controllers/ajax/question_controller.rb +++ b/app/controllers/ajax/question_controller.rb @@ -27,7 +27,7 @@ class Ajax::QuestionController < AjaxController params.require :anonymousQuestion params.require :rcpt - is_never_anonymous = user_signed_in? && (params[:rcpt].start_with?('grp:') || params[:rcpt] == 'followers') + is_never_anonymous = user_signed_in? && params[:rcpt] == 'followers' begin question = Question.create!(content: params[:question], @@ -50,22 +50,7 @@ class Ajax::QuestionController < AjaxController end if params[:rcpt] == 'followers' - unless current_user.nil? - QuestionWorker.perform_async params[:rcpt], current_user.id, question.id - end - elsif params[:rcpt].start_with? 'grp:' - unless current_user.nil? - begin - current_user.lists.find_by_name!(params[:rcpt].sub 'grp:', '') - QuestionWorker.perform_async params[:rcpt], current_user.id, question.id - rescue ActiveRecord::RecordNotFound => e - NewRelic::Agent.notice_error(e) - question.delete - @response[:status] = :not_found - @response[:message] = I18n.t('messages.question.create.not_found') - return - end - end + QuestionWorker.perform_async(current_user.id, question.id) unless current_user.nil? else u = User.find_by_id(params[:rcpt]) if u.nil? diff --git a/app/views/modal/_ask.haml b/app/views/modal/_ask.haml index c834d9ae..59adb393 100644 --- a/app/views/modal/_ask.haml +++ b/app/views/modal/_ask.haml @@ -9,13 +9,5 @@ .modal-body %textarea.form-control{ name: 'qb-all-question', placeholder: t('views.placeholder.question') } .modal-footer - - if current_user.lists.count.positive? - %label - = t 'views.modal.ask.choose' - %select.form-control{ name: 'qb-all-rcpt', autocomplete: :off } - %option{ value: 'followers', selected: true }= t('views.general.follower').pluralize(2) - %optlist{ label: t('views.list.title').pluralize(2) } - - current_user.lists.each do |list| - %option{ value: "grp:#{list.name}" }= list.display_name %button.btn.btn-default{ type: :button, data: { dismiss: :modal } }= t 'views.actions.cancel' %button.btn.btn-primary{ name: 'qb-all-ask', type: :button, data: { loading_text: t('views.modal.ask.loading') } }= t 'views.actions.ask' diff --git a/app/workers/question_worker.rb b/app/workers/question_worker.rb index 9e1c2fc5..c1253a5b 100644 --- a/app/workers/question_worker.rb +++ b/app/workers/question_worker.rb @@ -1,28 +1,20 @@ +# frozen_string_literal: true + class QuestionWorker include Sidekiq::Worker sidekiq_options queue: :question, retry: false - # @param rcpt [String] recipient # @param user_id [Integer] user id passed from Devise # @param question_id [Integer] newly created question id - def perform(rcpt, user_id, question_id) - begin - user = User.find(user_id) - if rcpt == 'followers' - user.followers.each do |f| - Inbox.create(user_id: f.id, question_id: question_id, new: true) - end - elsif rcpt.start_with? 'grp:' - user.lists.find_by_name!(rcpt.sub 'grp:', '').members.each do |m| - Inbox.create(user_id: m.user.id, question_id: question_id, new: true) - end - else - logger.info "unknown rcpt #{rcpt}" - end - rescue => e - logger.info "failed to ask question: #{e.message}" - NewRelic::Agent.notice_error(e) + def perform(user_id, question_id) + user = User.find(user_id) + + user.followers.each do |f| + Inbox.create(user_id: f.id, question_id: question_id, new: true) end + rescue StandardError => e + logger.info "failed to ask question: #{e.message}" + NewRelic::Agent.notice_error(e) end end diff --git a/spec/controllers/ajax/question_controller_spec.rb b/spec/controllers/ajax/question_controller_spec.rb index 8d27e61c..c722cdac 100644 --- a/spec/controllers/ajax/question_controller_spec.rb +++ b/spec/controllers/ajax/question_controller_spec.rb @@ -40,7 +40,7 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do it "enqueues a QuestionWorker job" do allow(QuestionWorker).to receive(:perform_async) subject - expect(QuestionWorker).to have_received(:perform_async).with(expected_rcpt, user.id, Question.last.id) + expect(QuestionWorker).to have_received(:perform_async).with(user.id, Question.last.id) end include_examples "returns the expected response" @@ -146,55 +146,6 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do end end - context "when rcpt is a list" do - let(:rcpt) { "grp:foobar" } - - context "when list exists" do - let(:list) { FactoryBot.create(:list, display_name: "FooBar", user: user) } - before { list } - - context "when anonymousQuestion is true" do - let(:anonymous_question) { "true" } - let(:expected_question_anonymous) { false } - - include_examples "creates the question", false - include_examples "enqueues a QuestionWorker job", "grp:foobar" - end - - context "when anonymousQuestion is false" do - let(:anonymous_question) { "false" } - let(:expected_question_anonymous) { false } - - include_examples "creates the question", false - include_examples "enqueues a QuestionWorker job", "grp:foobar" - end - end - - context "when list does not exist" do - let(:expected_response) do - { - "success" => false, - "status" => "not_found", - "message" => anything - } - end - - context "when anonymousQuestion is true" do - let(:anonymous_question) { "true" } - - include_examples "does not create the question", false - include_examples "does not enqueue a QuestionWorker job" - end - - context "when anonymousQuestion is false" do - let(:anonymous_question) { "false" } - - include_examples "does not create the question", false - include_examples "does not enqueue a QuestionWorker job" - end - end - end - context "when rcpt is a non-existent user" do let(:rcpt) { "tripmeister_eder" } let(:anonymous_question) { "false" } @@ -274,12 +225,6 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do include_examples "does not enqueue a QuestionWorker job" end - context "when rcpt is a list" do - let(:rcpt) { "grp:foobar" } - - include_examples "does not enqueue a QuestionWorker job" - end - context "when rcpt is a non-existent user" do let(:rcpt) { "tripmeister_eder" } let(:expected_response) do diff --git a/spec/workers/question_worker_spec.rb b/spec/workers/question_worker_spec.rb new file mode 100644 index 00000000..832276ac --- /dev/null +++ b/spec/workers/question_worker_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe QuestionWorker do + describe "#perform" do + let(:user) { FactoryBot.create(:user) } + let(:user_id) { user.id } + let(:question) { FactoryBot.create(:question, user: user) } + let(:question_id) { question.id } + + before do + 5.times do + other_user = FactoryBot.create(:user) + other_user.follow(user) + end + end + + subject { described_class.new.perform(user_id, question_id) } + + it "places the question in the inbox of the user's followers" do + expect { subject } + .to( + change { Inbox.where(user_id: user.followers.ids, question_id: question_id, new: true).count } + .from(0) + .to(5) + ) + end + end +end