diff --git a/app/workers/question_worker.rb b/app/workers/question_worker.rb index 9e7a3e2b..be82dfb4 100644 --- a/app/workers/question_worker.rb +++ b/app/workers/question_worker.rb @@ -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}" diff --git a/config/initializers/rpush.rb b/config/initializers/rpush.rb index e8908d7f..0aaf129c 100644 --- a/config/initializers/rpush.rb +++ b/config/initializers/rpush.rb @@ -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] diff --git a/lib/use_case/question/create.rb b/lib/use_case/question/create.rb index 36ef0205..4426c680 100644 --- a/lib/use_case/question/create.rb +++ b/lib/use_case/question/create.rb @@ -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,