Skip to content

Commit

Permalink
Revamp notification policy options (mastodon#31343)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire authored Aug 9, 2024
1 parent e29c401 commit cbdd8ed
Show file tree
Hide file tree
Showing 16 changed files with 442 additions and 109 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v1/notifications/policies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class Api::V1::Notifications::PoliciesController < Api::BaseController
before_action :set_policy

def show
render json: @policy, serializer: REST::NotificationPolicySerializer
render json: @policy, serializer: REST::V1::NotificationPolicySerializer
end

def update
@policy.update!(resource_params)
render json: @policy, serializer: REST::NotificationPolicySerializer
render json: @policy, serializer: REST::V1::NotificationPolicySerializer
end

private
Expand Down
38 changes: 38 additions & 0 deletions app/controllers/api/v2/notifications/policies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

class Api::V2::Notifications::PoliciesController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:notifications' }, only: :show
before_action -> { doorkeeper_authorize! :write, :'write:notifications' }, only: :update

before_action :require_user!
before_action :set_policy

def show
render json: @policy, serializer: REST::NotificationPolicySerializer
end

def update
@policy.update!(resource_params)
render json: @policy, serializer: REST::NotificationPolicySerializer
end

private

def set_policy
@policy = NotificationPolicy.find_or_initialize_by(account: current_account)

with_read_replica do
@policy.summarize!
end
end

def resource_params
params.permit(
:for_not_following,
:for_not_followers,
:for_new_accounts,
:for_private_mentions,
:for_limited_accounts
)
end
end
47 changes: 39 additions & 8 deletions app/models/notification_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
#
# Table name: notification_policies
#
# id :bigint(8) not null, primary key
# account_id :bigint(8) not null
# filter_not_following :boolean default(FALSE), not null
# filter_not_followers :boolean default(FALSE), not null
# filter_new_accounts :boolean default(FALSE), not null
# filter_private_mentions :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint(8) not null, primary key
# account_id :bigint(8) not null
# created_at :datetime not null
# updated_at :datetime not null
# for_not_following :integer default("accept"), not null
# for_not_followers :integer default("accept"), not null
# for_new_accounts :integer default("accept"), not null
# for_private_mentions :integer default("filter"), not null
# for_limited_accounts :integer default("filter"), not null
#

class NotificationPolicy < ApplicationRecord
self.ignored_columns += %w(
filter_not_following
filter_not_followers
filter_new_accounts
filter_private_mentions
)

belongs_to :account

has_many :notification_requests, primary_key: :account_id, foreign_key: :account_id, dependent: nil, inverse_of: false
Expand All @@ -23,11 +31,34 @@ class NotificationPolicy < ApplicationRecord

MAX_MEANINGFUL_COUNT = 100

enum :for_not_following, { accept: 0, filter: 1, drop: 2 }, suffix: :not_following
enum :for_not_followers, { accept: 0, filter: 1, drop: 2 }, suffix: :not_followers
enum :for_new_accounts, { accept: 0, filter: 1, drop: 2 }, suffix: :new_accounts
enum :for_private_mentions, { accept: 0, filter: 1, drop: 2 }, suffix: :private_mentions
enum :for_limited_accounts, { accept: 0, filter: 1, drop: 2 }, suffix: :limited_accounts

def summarize!
@pending_requests_count = pending_notification_requests.first
@pending_notifications_count = pending_notification_requests.last
end

# Compat helpers with V1
def filter_not_following=(value)
self.for_not_following = value ? :filter : :accept
end

def filter_not_followers=(value)
self.for_not_followers = value ? :filter : :accept
end

def filter_new_accounts=(value)
self.for_new_accounts = value ? :filter : :accept
end

def filter_private_mentions=(value)
self.for_private_mentions = value ? :filter : :accept
end

private

def pending_notification_requests
Expand Down
9 changes: 5 additions & 4 deletions app/serializers/rest/notification_policy_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
class REST::NotificationPolicySerializer < ActiveModel::Serializer
# Please update `app/javascript/mastodon/api_types/notification_policies.ts` when making changes to the attributes

attributes :filter_not_following,
:filter_not_followers,
:filter_new_accounts,
:filter_private_mentions,
attributes :for_not_following,
:for_not_followers,
:for_new_accounts,
:for_private_mentions,
:for_limited_accounts,
:summary

def summary
Expand Down
32 changes: 32 additions & 0 deletions app/serializers/rest/v1/notification_policy_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

class REST::V1::NotificationPolicySerializer < ActiveModel::Serializer
attributes :filter_not_following,
:filter_not_followers,
:filter_new_accounts,
:filter_private_mentions,
:summary

def summary
{
pending_requests_count: object.pending_requests_count.to_i,
pending_notifications_count: object.pending_notifications_count.to_i,
}
end

def filter_not_following
!object.accept_not_following?
end

def filter_not_followers
!object.accept_not_followers?
end

def filter_new_accounts
!object.accept_new_accounts?
end

def filter_private_mentions
!object.accept_private_mentions?
end
end
Loading

0 comments on commit cbdd8ed

Please sign in to comment.