Clean up marking notifications as read when viewing an answer

This commit is contained in:
Karina Kwiatek 2023-02-17 01:18:33 +01:00
parent d7997db492
commit 0771c689ea
2 changed files with 13 additions and 12 deletions

View File

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

View File

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