Send push notifications on question create

This commit is contained in:
Karina Kwiatek 2022-09-11 20:25:11 +02:00
parent bae227be76
commit 8b98c278da
3 changed files with 20 additions and 0 deletions

View File

@ -10,6 +10,7 @@ class QuestionWorker
def perform(user_id, question_id)
user = User.find(user_id)
question = Question.find(question_id)
webpush_app = Rpush::App.find_by(name: "webpush")
user.followers.each do |f|
next if f.inbox_locked?
@ -18,6 +19,14 @@ class QuestionWorker
next if user.muting?(question.user)
Inbox.create(user_id: f.id, question_id: question_id, new: true)
f.web_push_subscriptions.each do |s|
n = Rpush::Webpush::Notification.new
n.app = webpush_app
n.registration_ids = [s.subscription.symbolize_keys]
n.data = { message: { title: "New question notif title", body: question.content }.to_json }
n.save!
end
end
rescue StandardError => e
logger.info "failed to ask question: #{e.message}"

View File

@ -134,3 +134,5 @@ Rpush.reflect do |on|
# on.error do |error|
# end
end
Rails.application.config.active_record.yaml_column_permitted_classes = [Symbol, Hash]

View File

@ -30,6 +30,15 @@ module UseCase
inbox = ::Inbox.create!(user: target_user, question: question, new: true)
webpush_app = Rpush::App.find_by(name: "webpush")
target_user.web_push_subscriptions.each do |s|
n = Rpush::Webpush::Notification.new
n.app = webpush_app
n.registration_ids = [s.subscription.symbolize_keys]
n.data = { message: { title: "New question notif title", body: question.content }.to_json }
n.save!
end
{
status: 201,
resource: question,