Moved ask all + ask group to worker, validates user_id for ask person now

This commit is contained in:
Yuki 2015-07-27 12:29:56 +05:30
parent fcbfa6efdb
commit 92bbfef2df
5 changed files with 37 additions and 10 deletions

View File

@ -55,16 +55,12 @@ class Ajax::QuestionController < ApplicationController
if params[:rcpt] == 'followers' if params[:rcpt] == 'followers'
unless current_user.nil? unless current_user.nil?
current_user.followers.each do |f| QuestionWorker.perform_async params[:rcpt], current_user.id, question.id
Inbox.create!(user_id: f.id, question_id: question.id, new: true)
end
end end
elsif params[:rcpt].start_with? 'grp:' elsif params[:rcpt].start_with? 'grp:'
unless current_user.nil? unless current_user.nil?
begin current_user.groups.find_by_name!(params[:rcpt].sub 'grp:', '')
current_user.groups.find_by_name!(params[:rcpt].sub 'grp:', '').members.each do |m| QuestionWorker.perform_async params[:rcpt], current_user.id, question.id
Inbox.create!(user_id: m.user.id, question_id: question.id, new: true)
end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
@status = :not_found @status = :not_found
@message = I18n.t('messages.question.create.not_found') @message = I18n.t('messages.question.create.not_found')
@ -73,6 +69,13 @@ class Ajax::QuestionController < ApplicationController
end end
end end
else else
if User.find(params[:rcpt]).nil?
@status = :not_found
@message = I18n.t('messages.question.create.not_found')
@success = false
return
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

View File

@ -1,7 +1,7 @@
class DeletionWorker class DeletionWorker
include Sidekiq::Worker include Sidekiq::Worker
sidekiq_options queue: :deletion sidekiq_options queue: :deletion, retry: false
# @param resource_id [Integer] user id passed from Devise # @param resource_id [Integer] user id passed from Devise
def perform(resource_id) def perform(resource_id)

View File

@ -0,0 +1,23 @@
class QuestionWorker
include Sidekiq::Worker
sidekiq_options queue: :question, retry: false
# @param resource_id [Integer] user id passed from Devise
def perform(rcpt, user_id, question_id)
begin
user = User.find(user_id)
if params[:rcpt].start_with? 'grp:'
user.followers.each do |f|
Inbox.create!(user_id: fid, question_id: question_id, new: true)
end
else
current_user.groups.find_by_name!(params[:rcpt].sub 'grp:', '').members.each do |m|
Inbox.create!(user_id: m.user.id, question_id: question.id, new: true)
end
end
rescue => e
Rails.logger.error "failed to answer question: #{e.message}"
end
end
end

View File

@ -1,7 +1,7 @@
class ShareWorker class ShareWorker
include Sidekiq::Worker include Sidekiq::Worker
sidekiq_options queue: :share sidekiq_options queue: :share, retry: false
# @param user_id [Integer] the user id # @param user_id [Integer] the user id
# @param answer_id [Integer] the user id # @param answer_id [Integer] the user id
@ -15,4 +15,4 @@ class ShareWorker
end end
end end
end end
end end

View File

@ -11,3 +11,4 @@ production:
- mailer - mailer
- deletion - deletion
- rollbar - rollbar
- question