From 0771c689ead9cc52fe11c00a161016f73e64f68d Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 17 Feb 2023 01:18:33 +0100 Subject: [PATCH] Clean up marking notifications as read when viewing an answer --- app/controllers/answer_controller.rb | 23 ++++++++++++----------- app/views/answer/show.html.haml | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/app/controllers/answer_controller.rb b/app/controllers/answer_controller.rb index 110486d1..3d520116 100644 --- a/app/controllers/answer_controller.rb +++ b/app/controllers/answer_controller.rb @@ -11,17 +11,7 @@ class AnswerController < ApplicationController @answer = Answer.includes(comments: %i[user smiles], question: [:user], smiles: [:user]).find(params[:id]) @subscribed = Subscription.where(user: current_user, answer: @answer).pluck(:id) @display_all = true - - if user_signed_in? - notif = Notification.where(type: "Notification::QuestionAnswered", target_id: @answer.id, recipient_id: current_user.id, new: true).first - notif&.update(new: false) - notif = Notification.where(type: "Notification::Commented", target_id: @answer.comments.pluck(:id), recipient_id: current_user.id, new: true) - notif.update_all(new: false) unless notif.empty? - notif = Notification.where(type: "Notification::Smiled", target_id: @answer.smiles.pluck(:id), recipient_id: current_user.id, new: true) - notif.update_all(new: false) unless notif.empty? - notif = Notification.where(type: "Notification::CommentSmiled", target_id: @answer.comment_smiles.pluck(:id), recipient_id: current_user.id, new: true) - notif.update_all(new: false) unless notif.empty? - end + mark_notifications_as_read if user_signed_in? end def pin @@ -53,4 +43,15 @@ class AnswerController < ApplicationController end end end + + private + + def mark_notifications_as_read + Notification.where(recipient_id: current_user.id, new: true) + .and(Notification.where(type: "Notification::QuestionAnswered", target_id: @answer.id) + .or(Notification.where(type: "Notification::Commented", target_id: @answer.comments.pluck(:id))) + .or(Notification.where(type: "Notification::Smiled", target_id: @answer.smiles.pluck(:id))) + .or(Notification.where(type: "Notification::CommentSmiled", target_id: @answer.comment_smiles.pluck(:id)))) + .update_all(new: false) # rubocop:disable Rails/SkipsModelValidations + end end diff --git a/app/views/answer/show.html.haml b/app/views/answer/show.html.haml index 393b0dd5..8a50232f 100644 --- a/app/views/answer/show.html.haml +++ b/app/views/answer/show.html.haml @@ -1,4 +1,4 @@ - provide(:title, answer_title(@answer)) - provide(:og, answer_opengraph(@answer)) .container-lg.container--main - = render 'answerbox', a: @answer, display_all: @display_all, subscribed_answer_ids: @subscribed + = render "answerbox", a: @answer, display_all: @display_all, subscribed_answer_ids: @subscribed