Skip to content

Commit

Permalink
feat: Add last_activity_at field to Notification model (#8585)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhsin-k committed Dec 20, 2023
1 parent a80eff9 commit b5071e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/models/notification.rb
Expand Up @@ -3,6 +3,7 @@
# Table name: notifications
#
# id :bigint not null, primary key
# last_activity_at :datetime
# notification_type :integer not null
# primary_actor_type :string not null
# read_at :datetime
Expand All @@ -18,6 +19,7 @@
# Indexes
#
# index_notifications_on_account_id (account_id)
# index_notifications_on_last_activity_at (last_activity_at)
# index_notifications_on_user_id (user_id)
# uniq_primary_actor_per_account_notifications (primary_actor_type,primary_actor_id)
# uniq_secondary_actor_per_account_notifications (secondary_actor_type,secondary_actor_id)
Expand All @@ -41,6 +43,7 @@ class Notification < ApplicationRecord

enum notification_type: NOTIFICATION_TYPES

before_create :set_last_activity_at
after_create_commit :process_notification_delivery, :dispatch_create_event
after_destroy_commit :dispatch_destroy_event

Expand Down Expand Up @@ -137,4 +140,8 @@ def dispatch_create_event
def dispatch_destroy_event
Rails.configuration.dispatcher.dispatch(NOTIFICATION_DELETED, Time.zone.now, notification: self)
end

def set_last_activity_at
self.last_activity_at = created_at
end
end
@@ -0,0 +1,6 @@
class AddLastActivityAtToNotifications < ActiveRecord::Migration[7.0]
def change
add_column :notifications, :last_activity_at, :datetime, default: -> { 'CURRENT_TIMESTAMP' }
add_index :notifications, :last_activity_at
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_12_19_000743) do
ActiveRecord::Schema[7.0].define(version: 2023_12_19_073832) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -731,7 +731,9 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "snoozed_until"
t.datetime "last_activity_at", default: -> { "CURRENT_TIMESTAMP" }
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"
t.index ["secondary_actor_type", "secondary_actor_id"], name: "uniq_secondary_actor_per_account_notifications"
t.index ["user_id"], name: "index_notifications_on_user_id"
Expand Down

0 comments on commit b5071e6

Please sign in to comment.