diff --git a/app/jobs/notification/reopen_snoozed_notifications_job.rb b/app/jobs/notification/reopen_snoozed_notifications_job.rb index 89b245187..612a5645d 100644 --- a/app/jobs/notification/reopen_snoozed_notifications_job.rb +++ b/app/jobs/notification/reopen_snoozed_notifications_job.rb @@ -2,9 +2,17 @@ class Notification::ReopenSnoozedNotificationsJob < ApplicationJob queue_as :low def perform - # rubocop:disable Rails/SkipsModelValidations - Notification.where(snoozed_until: 3.days.ago..Time.current) - .update_all(snoozed_until: nil, updated_at: Time.current, last_activity_at: Time.current) - # rubocop:enable Rails/SkipsModelValidations + Notification.where(snoozed_until: 3.days.ago..Time.current).find_each do |notification| + # Ensure the meta field is a hash and merge the last_snoozed_at key + updated_meta = (notification.meta || {}).merge('last_snoozed_at' => notification.snoozed_until) + + # Update the notification with the new values + notification.update( + snoozed_until: nil, + updated_at: Time.current, + last_activity_at: Time.current, + meta: updated_meta + ) + end end end diff --git a/app/models/notification.rb b/app/models/notification.rb index e0b31b457..f8c6e3d5b 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -4,6 +4,7 @@ # # id :bigint not null, primary key # last_activity_at :datetime +# meta :jsonb # notification_type :integer not null # primary_actor_type :string not null # read_at :datetime diff --git a/db/migrate/20240214092129_add_meta_to_notifications.rb b/db/migrate/20240214092129_add_meta_to_notifications.rb new file mode 100644 index 000000000..e0f93f276 --- /dev/null +++ b/db/migrate/20240214092129_add_meta_to_notifications.rb @@ -0,0 +1,5 @@ +class AddMetaToNotifications < ActiveRecord::Migration[7.0] + def change + add_column :notifications, :meta, :jsonb, default: {} + end +end diff --git a/db/schema.rb b/db/schema.rb index 7d2372554..f9064a969 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_02_07_103014) do +ActiveRecord::Schema[7.0].define(version: 2024_02_14_092129) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "pg_trgm" @@ -750,6 +750,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_07_103014) do t.datetime "updated_at", null: false t.datetime "snoozed_until" t.datetime "last_activity_at", default: -> { "CURRENT_TIMESTAMP" } + t.jsonb "meta", default: {} t.index ["account_id"], name: "index_notifications_on_account_id" t.index ["last_activity_at"], name: "index_notifications_on_last_activity_at" t.index ["primary_actor_type", "primary_actor_id"], name: "uniq_primary_actor_per_account_notifications" diff --git a/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb b/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb index 2b210ef2b..0ea3b3885 100644 --- a/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb +++ b/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb @@ -13,10 +13,15 @@ RSpec.describe Notification::ReopenSnoozedNotificationsJob do context 'when called' do it 'reopens snoozed notifications whose snooze until has passed' do described_class.perform_now + time_after_job = Time.current expect(snoozed_till_5_minutes_ago.reload.snoozed_until).to be_nil expect(snoozed_till_tomorrow.reload.snoozed_until.to_date).to eq 1.day.from_now.to_date expect(snoozed_indefinitely.reload.snoozed_until).to be_nil + expect(snoozed_till_5_minutes_ago.reload.last_activity_at).to be_within(1.second).of(time_after_job) + + last_snoozed_at = Time.zone.parse(snoozed_till_5_minutes_ago.reload.meta['last_snoozed_at']) + expect(last_snoozed_at).to be <= time_after_job end end end