diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2022-03-15 04:11:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-15 04:11:29 +0100 |
commit | e6ffbfb5e7684880e9252f72f9cb00b151c28d10 (patch) | |
tree | 82ccc383bc0a9614c95d682b76da6bb8d6435c38 /app/models | |
parent | eebafe24a8000ac77bb54660170f5b47bfc5b3a3 (diff) |
Add `types` param to `GET /api/v1/notifications` in REST API (#17767)
* Add `types` param to `GET /api/v1/notifications` in REST API * Improve tests
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/notification.rb | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/app/models/notification.rb b/app/models/notification.rb index 9bf296386..ba94b54d1 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -63,13 +63,6 @@ class Notification < ApplicationRecord scope :without_suspended, -> { joins(:from_account).merge(Account.without_suspended) } - scope :browserable, ->(exclude_types = [], account_id = nil) { - scope = all - scope = where(from_account_id: account_id) if account_id.present? - scope = scope.where(type: TYPES - exclude_types.map(&:to_sym)) unless exclude_types.empty? - scope - } - def type @type ||= (super || LEGACY_TYPE_CLASS_MAP[activity_type]).to_sym end @@ -90,6 +83,23 @@ class Notification < ApplicationRecord end class << self + def browserable(types: [], exclude_types: [], from_account_id: nil) + requested_types = begin + if types.empty? + TYPES + else + types.map(&:to_sym) & TYPES + end + end + + requested_types -= exclude_types.map(&:to_sym) + + all.tap do |scope| + scope.merge!(where(from_account_id: from_account_id)) if from_account_id.present? + scope.merge!(where(type: requested_types)) unless requested_types.size == TYPES.size + end + end + def preload_cache_collection_target_statuses(notifications, &_block) notifications.group_by(&:type).each do |type, grouped_notifications| associations = TARGET_STATUS_INCLUDES_BY_TYPE[type] |