Remove "ask a group" feature

This commit is contained in:
Georg Gadinger 2020-05-25 18:33:09 +02:00
parent 61816aec5f
commit b58883e004
6 changed files with 44 additions and 107 deletions

View File

@ -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) ->

View File

@ -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?

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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