Add notification policies and notification requests (#29366)
This commit is contained in:
parent
653ce43abe
commit
50b17f7e10
104 changed files with 1096 additions and 247 deletions
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddFilteredToNotifications < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
add_column :notifications, :filtered, :boolean, default: false, null: false
|
||||
end
|
||||
end
|
18
db/migrate/20240221195828_create_notification_requests.rb
Normal file
18
db/migrate/20240221195828_create_notification_requests.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class CreateNotificationRequests < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
create_table :notification_requests do |t|
|
||||
t.references :account, null: false, foreign_key: { on_delete: :cascade }, index: false
|
||||
t.references :from_account, null: false, foreign_key: { to_table: :accounts, on_delete: :cascade }
|
||||
t.references :last_status, null: false, foreign_key: { to_table: :statuses, on_delete: :nullify }
|
||||
t.bigint :notifications_count, null: false, default: 0
|
||||
t.boolean :dismissed, null: false, default: false
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :notification_requests, [:account_id, :from_account_id], unique: true
|
||||
add_index :notification_requests, [:account_id, :id], where: 'dismissed = false', order: { id: :desc }
|
||||
end
|
||||
end
|
|
@ -0,0 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class NotificationRequestIdsToTimestampIds < ActiveRecord::Migration[7.1]
|
||||
def up
|
||||
safety_assured do
|
||||
execute("ALTER TABLE notification_requests ALTER COLUMN id SET DEFAULT timestamp_id('notification_requests')")
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
execute('LOCK notification_requests')
|
||||
execute("SELECT setval('notification_requests_id_seq', (SELECT MAX(id) FROM notification_requests))")
|
||||
execute("ALTER TABLE notification_requests ALTER COLUMN id SET DEFAULT nextval('notification_requests_id_seq')")
|
||||
end
|
||||
end
|
12
db/migrate/20240222193403_create_notification_permissions.rb
Normal file
12
db/migrate/20240222193403_create_notification_permissions.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class CreateNotificationPermissions < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
create_table :notification_permissions do |t|
|
||||
t.references :account, null: false, foreign_key: true
|
||||
t.references :from_account, null: false, foreign_key: { to_table: :accounts }
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
15
db/migrate/20240222203722_create_notification_policies.rb
Normal file
15
db/migrate/20240222203722_create_notification_policies.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class CreateNotificationPolicies < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
create_table :notification_policies do |t|
|
||||
t.references :account, null: false, foreign_key: true, index: { unique: true }
|
||||
t.boolean :filter_not_following, null: false, default: false
|
||||
t.boolean :filter_not_followers, null: false, default: false
|
||||
t.boolean :filter_new_accounts, null: false, default: false
|
||||
t.boolean :filter_private_mentions, null: false, default: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddFilteredIndexOnNotifications < ActiveRecord::Migration[7.1]
|
||||
disable_ddl_transaction!
|
||||
|
||||
def change
|
||||
add_index :notifications, [:account_id, :id, :type], where: 'filtered = false', order: { id: :desc }, name: 'index_notifications_on_filtered', algorithm: :concurrently
|
||||
end
|
||||
end
|
|
@ -0,0 +1,46 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class MigrateInteractionSettingsToPolicy < ActiveRecord::Migration[7.1]
|
||||
disable_ddl_transaction!
|
||||
|
||||
# Dummy classes, to make migration possible across version changes
|
||||
class Account < ApplicationRecord
|
||||
has_one :user, inverse_of: :account
|
||||
has_one :notification_policy, inverse_of: :account
|
||||
end
|
||||
|
||||
class User < ApplicationRecord
|
||||
belongs_to :account
|
||||
end
|
||||
|
||||
class NotificationPolicy < ApplicationRecord
|
||||
belongs_to :account
|
||||
end
|
||||
|
||||
def up
|
||||
User.includes(account: :notification_policy).find_each do |user|
|
||||
deserialized_settings = Oj.load(user.attributes_before_type_cast['settings'])
|
||||
policy = user.account.notification_policy || user.account.build_notification_policy
|
||||
requires_new_policy = false
|
||||
|
||||
if deserialized_settings['interactions.must_be_follower']
|
||||
policy.filter_not_followers = true
|
||||
requires_new_policy = true
|
||||
end
|
||||
|
||||
if deserialized_settings['interactions.must_be_following']
|
||||
policy.filter_not_following = true
|
||||
requires_new_policy = true
|
||||
end
|
||||
|
||||
if deserialized_settings['interactions.must_be_following_dm']
|
||||
policy.filter_private_mentions = true
|
||||
requires_new_policy = true
|
||||
end
|
||||
|
||||
policy.save if requires_new_policy && policy.changed?
|
||||
end
|
||||
end
|
||||
|
||||
def down; end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue