diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 6a74687f..1a1a0066 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -13,6 +13,18 @@ class Inbox < ApplicationRecord !user.privacy_allow_anonymous_questions? end + after_create do + user.touch(:inbox_updated_at) + end + + after_update do + user.touch(:inbox_updated_at) + end + + after_destroy do + user.touch(:inbox_updated_at) + end + def answer(answer_content, user) raise Errors::AnsweringOtherBlockedSelf if question.user&.blocking?(user) raise Errors::AnsweringSelfBlockedOther if user.blocking?(question.user) diff --git a/app/models/notification.rb b/app/models/notification.rb index 3e15eae4..e36cad0a 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -1,9 +1,21 @@ # frozen_string_literal: true class Notification < ApplicationRecord - belongs_to :recipient, class_name: "User", touch: :notifications_updated_at + belongs_to :recipient, class_name: "User" belongs_to :target, polymorphic: true + after_create do + recipient.touch(:notifications_updated_at) + end + + after_update do + recipient.touch(:notifications_updated_at) + end + + after_destroy do + recipient.touch(:notifications_updated_at) + end + class << self include CursorPaginatable @@ -45,7 +57,7 @@ class Notification < ApplicationRecord n = notification_type.new(target:, recipient:, - new: true) + new: true,) n.save! n end diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 0b05a1b9..2e8e7b3c 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -39,7 +39,12 @@ class Subscription < ApplicationRecord { target_id: source.id, target_type: Comment, recipient_id: s.user_id, new: true, type: Notification::Commented, created_at: source.created_at, updated_at: source.created_at } end - Notification.insert_all!(notifications) unless notifications.empty? # rubocop:disable Rails/SkipsModelValidations + return if notifications.empty? + + # rubocop:disable Rails/SkipsModelValidations + Notification.insert_all!(notifications) + User.where(id: notifications.pluck(:recipient_id)).touch_all(:notifications_updated_at) + # rubocop:enable Rails/SkipsModelValidations end def denotify(source, target)