]> cat aescling's git repositories - mastodon.git/commitdiff
Change REST API to return empty data for suspended accounts (#14765)
authorEugen Rochko <eugen@zeonfederated.com>
Fri, 11 Sep 2020 13:16:29 +0000 (15:16 +0200)
committerGitHub <noreply@github.com>
Fri, 11 Sep 2020 13:16:29 +0000 (15:16 +0200)
22 files changed:
app/controllers/activitypub/outboxes_controller.rb
app/controllers/api/v1/accounts/featured_tags_controller.rb
app/controllers/api/v1/accounts/follower_accounts_controller.rb
app/controllers/api/v1/accounts/following_accounts_controller.rb
app/controllers/api/v1/accounts/identity_proofs_controller.rb
app/controllers/api/v1/accounts/lists_controller.rb
app/controllers/api/v1/accounts/relationships_controller.rb
app/controllers/api/v1/accounts/statuses_controller.rb
app/controllers/api/v1/accounts_controller.rb
app/controllers/api/v1/blocks_controller.rb
app/controllers/api/v1/endorsements_controller.rb
app/controllers/api/v1/follow_requests_controller.rb
app/controllers/api/v1/lists/accounts_controller.rb
app/controllers/api/v1/mutes_controller.rb
app/controllers/api/v1/notifications_controller.rb
app/controllers/api/v1/statuses/favourited_by_accounts_controller.rb
app/controllers/api/v1/statuses/reblogged_by_accounts_controller.rb
app/models/notification.rb
app/policies/status_policy.rb
app/serializers/rest/account_serializer.rb
lib/paperclip/attachment_extensions.rb
lib/paperclip/url_generator_extensions.rb

index e066860bfedbb93e7f55c3a8dbb0a917f8a94ddd..5fd735ad6af3d6467f08c6659aa21f95e0e4943e 100644 (file)
@@ -57,9 +57,8 @@ class ActivityPub::OutboxesController < ActivityPub::BaseController
   def set_statuses
     return unless page_requested?
 
-    @statuses = @account.statuses.permitted_for(@account, signed_request_account)
     @statuses = cache_collection_paginated_by_id(
-      @statuses,
+      @account.statuses.permitted_for(@account, signed_request_account),
       Status,
       LIMIT,
       params_slice(:max_id, :min_id, :since_id)
index d6277261d4503e35d90d99d688bf336bbe0e2ff2..014d7195671fad5ea1525c6f2277a2824171f294 100644 (file)
@@ -17,6 +17,6 @@ class Api::V1::Accounts::FeaturedTagsController < Api::BaseController
   end
 
   def set_featured_tags
-    @featured_tags = @account.featured_tags
+    @featured_tags = @account.suspended? ? @account.featured_tags : []
   end
 end
index 2277067c9f45dd35800fd1ec9af3ad41eb463106..a665863ebf4816993aca4fdac80701ca3dd8babc 100644 (file)
@@ -25,7 +25,7 @@ class Api::V1::Accounts::FollowerAccountsController < Api::BaseController
   end
 
   def hide_results?
-    (@account.hides_followers? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
+    @account.suspended? || (@account.hides_followers? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
   end
 
   def default_accounts
index 93d4bd3a4a1efcf1c1ba5c076285f23c3708b356..7d885a212f2c349ad1490f0053f8654ae9bd32da 100644 (file)
@@ -25,7 +25,7 @@ class Api::V1::Accounts::FollowingAccountsController < Api::BaseController
   end
 
   def hide_results?
-    (@account.hides_following? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
+    @account.suspended? || (@account.hides_following? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
   end
 
   def default_accounts
index 8dad6fee9627488efca4b3b99395ff8b96f8dc8b..4b5f6902c7df95345cbbf3677d977f639b040813 100644 (file)
@@ -5,7 +5,7 @@ class Api::V1::Accounts::IdentityProofsController < Api::BaseController
   before_action :set_account
 
   def index
-    @proofs = @account.identity_proofs.active
+    @proofs = @account.suspended? ? [] : @account.identity_proofs.active
     render json: @proofs, each_serializer: REST::IdentityProofSerializer
   end
 
index ccb751f8f7dc64601e6cf3a23427be6ae24a9b70..c92f1f8a08d10ce54f6984a548f229056d71ae7c 100644 (file)
@@ -6,7 +6,7 @@ class Api::V1::Accounts::ListsController < Api::BaseController
   before_action :set_account
 
   def index
-    @lists = @account.lists.where(account: current_account)
+    @lists = @account.suspended? ? [] : @account.lists.where(account: current_account)
     render json: @lists, each_serializer: REST::ListSerializer
   end
 
index 1d3992a285770c93eb3c8a10696f2ba6faf8bbd8..503f85c97d79fca46fdf15009a04867e339ede46 100644 (file)
@@ -5,7 +5,7 @@ class Api::V1::Accounts::RelationshipsController < Api::BaseController
   before_action :require_user!
 
   def index
-    accounts = Account.where(id: account_ids).select('id')
+    accounts = Account.without_suspended.where(id: account_ids).select('id')
     # .where doesn't guarantee that our results are in the same order
     # we requested them, so return the "right" order to the requestor.
     @accounts = accounts.index_by(&:id).values_at(*account_ids).compact
index 85a9133e3ac833de42a28bd4f0a72baa96d9d508..92ccb80615da26ad94dbebf6edccefe7c7c14fa0 100644 (file)
@@ -18,7 +18,7 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
   end
 
   def load_statuses
-    cached_account_statuses
+    @account.suspended? ? [] : cached_account_statuses
   end
 
   def cached_account_statuses
index 0080faf33071c5d36cfd7949c03f2da70e681407..61dcb87c23d5a8b0db4263fffa841e310fd1ce50 100644 (file)
@@ -9,7 +9,6 @@ class Api::V1::AccountsController < Api::BaseController
 
   before_action :require_user!, except: [:show, :create]
   before_action :set_account, except: [:create]
-  before_action :check_account_suspension, only: [:show]
   before_action :check_enabled_registrations, only: [:create]
 
   skip_before_action :require_authenticated_user!, only: :create
@@ -73,10 +72,6 @@ class Api::V1::AccountsController < Api::BaseController
     AccountRelationshipsPresenter.new([@account.id], current_user.account_id, options)
   end
 
-  def check_account_suspension
-    gone if @account.suspended?
-  end
-
   def account_params
     params.permit(:username, :email, :password, :agreement, :locale, :reason)
   end
index a2baeef900cbf6a84aceef8d871948d781e62ba6..586cdfca9d4e6b71be8a25debf30389a33ff3f80 100644 (file)
@@ -18,6 +18,8 @@ class Api::V1::BlocksController < Api::BaseController
 
   def paginated_blocks
     @paginated_blocks ||= Block.eager_load(target_account: :account_stat)
+                               .joins(:target_account)
+                               .merge(Account.without_suspended)
                                .where(account: current_account)
                                .paginate_by_max_id(
                                  limit_param(DEFAULT_ACCOUNTS_LIMIT),
index c87dbc4ce836a7de5d70b5ed170d2415da2ff98e..9e80f468a780251ba20c3726597bd5c6c2abfe6b 100644 (file)
@@ -25,7 +25,7 @@ class Api::V1::EndorsementsController < Api::BaseController
   end
 
   def endorsed_accounts
-    current_account.endorsed_accounts.includes(:account_stat)
+    current_account.endorsed_accounts.includes(:account_stat).without_suspended
   end
 
   def insert_pagination_headers
index 0ee6e531f07a4abacea52ccae938a14fdd92deb1..0420b7bef921f159f2c53b84c2008fc1ad02ea58 100644 (file)
@@ -37,7 +37,7 @@ class Api::V1::FollowRequestsController < Api::BaseController
   end
 
   def default_accounts
-    Account.includes(:follow_requests, :account_stat).references(:follow_requests)
+    Account.without_suspended.includes(:follow_requests, :account_stat).references(:follow_requests)
   end
 
   def paginated_follow_requests
index 23078263e7abdd70843a62906245afd35774fb3b..b66ea9bfe609d8b988fdce90bfcf070830b292b9 100644 (file)
@@ -37,9 +37,9 @@ class Api::V1::Lists::AccountsController < Api::BaseController
 
   def load_accounts
     if unlimited?
-      @list.accounts.includes(:account_stat).all
+      @list.accounts.without_suspended.includes(:account_stat).all
     else
-      @list.accounts.includes(:account_stat).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
+      @list.accounts.without_suspended.includes(:account_stat).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
     end
   end
 
index 65439fe9bc1505e9005b1d8719ab4b1483df1541..805d0dee2abcb5211ca09c940d78aaa63291635a 100644 (file)
@@ -18,6 +18,8 @@ class Api::V1::MutesController < Api::BaseController
 
   def paginated_mutes
     @paginated_mutes ||= Mute.eager_load(:target_account)
+                             .joins(:target_account)
+                             .merge(Account.without_suspended)
                              .where(account: current_account)
                              .paginate_by_max_id(
                                limit_param(DEFAULT_ACCOUNTS_LIMIT),
index 9d03cb879d60e8f8de0c3749679f8f1d3e498d16..522c35ba5483e5fb89f0c1003dc2519e7ecb2b94 100644 (file)
@@ -14,7 +14,7 @@ class Api::V1::NotificationsController < Api::BaseController
   end
 
   def show
-    @notification = current_account.notifications.find(params[:id])
+    @notification = current_account.notifications.without_suspended.find(params[:id])
     render json: @notification, serializer: REST::NotificationSerializer
   end
 
@@ -40,7 +40,7 @@ class Api::V1::NotificationsController < Api::BaseController
   end
 
   def browserable_account_notifications
-    current_account.notifications.browserable(exclude_types, from_account)
+    current_account.notifications.without_suspended.browserable(exclude_types, from_account)
   end
 
   def target_statuses_from_notifications
index 8229786d6cccaba018e9535b1be0be87533ce192..2b614a83756a251f94371e414733ba8539fc2c0f 100644 (file)
@@ -22,6 +22,7 @@ class Api::V1::Statuses::FavouritedByAccountsController < Api::BaseController
 
   def default_accounts
     Account
+      .without_suspended
       .includes(:favourites, :account_stat)
       .references(:favourites)
       .where(favourites: { status_id: @status.id })
index 6c9e49d903a4124770b81a21b64668873c796ab7..24db30fcc015d9e018b73afea62eba8c4a222c62 100644 (file)
@@ -21,7 +21,7 @@ class Api::V1::Statuses::RebloggedByAccountsController < Api::BaseController
   end
 
   def default_accounts
-    Account.includes(:statuses, :account_stat).references(:statuses)
+    Account.without_suspended.includes(:statuses, :account_stat).references(:statuses)
   end
 
   def paginated_statuses
index ad7528f505c035f8702fe9e7fdf53af2f5c18ac4..4d7a392b1b7d3e59544f202d83a54c9726798065 100644 (file)
@@ -41,8 +41,11 @@ class Notification < ApplicationRecord
   validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] }
   validates :activity_type, inclusion: { in: TYPE_CLASS_MAP.values }
 
+  scope :without_suspended, -> { joins(:from_account).merge(Account.without_suspended) }
+
   scope :browserable, ->(exclude_types = [], account_id = nil) {
     types = TYPE_CLASS_MAP.values - activity_types_from_types(exclude_types)
+
     if account_id.nil?
       where(activity_type: types)
     else
index 3d4e50d3719730cc65540f58180a103fc3a284f9..bcf9c3395ca974291ad738c45fcae14f05702bce 100644 (file)
@@ -12,6 +12,8 @@ class StatusPolicy < ApplicationPolicy
   end
 
   def show?
+    return false if author.suspended?
+
     if requires_mention?
       owned? || mention_exists?
     elsif private?
index 0db1916b0740640cf0ef64eb155da9ab2d5579e6..189a62d0eec82b5395687117f5501cde8a085fd6 100644 (file)
@@ -8,8 +8,11 @@ class REST::AccountSerializer < ActiveModel::Serializer
              :followers_count, :following_count, :statuses_count, :last_status_at
 
   has_one :moved_to_account, key: :moved, serializer: REST::AccountSerializer, if: :moved_and_not_nested?
+
   has_many :emojis, serializer: REST::CustomEmojiSerializer
 
+  attribute :suspended, if: :suspended?
+
   class FieldSerializer < ActiveModel::Serializer
     attributes :name, :value, :verified_at
 
@@ -29,7 +32,7 @@ class REST::AccountSerializer < ActiveModel::Serializer
   end
 
   def note
-    Formatter.instance.simplified_format(object)
+    object.suspended? ? '' : Formatter.instance.simplified_format(object)
   end
 
   def url
@@ -37,26 +40,60 @@ class REST::AccountSerializer < ActiveModel::Serializer
   end
 
   def avatar
-    full_asset_url(object.avatar_original_url)
+    full_asset_url(object.suspended? ? object.avatar.default_url : object.avatar_original_url)
   end
 
   def avatar_static
-    full_asset_url(object.avatar_static_url)
+    full_asset_url(object.suspended? ? object.avatar.default_url : object.avatar_static_url)
   end
 
   def header
-    full_asset_url(object.header_original_url)
+    full_asset_url(object.suspended? ? object.header.default_url : object.header_original_url)
   end
 
   def header_static
-    full_asset_url(object.header_static_url)
-  end
-
-  def moved_and_not_nested?
-    object.moved? && object.moved_to_account.moved_to_account_id.nil?
+    full_asset_url(object.suspended? ? object.header.default_url : object.header_static_url)
   end
 
   def last_status_at
     object.last_status_at&.to_date&.iso8601
   end
+
+  def display_name
+    object.suspended? ? '' : object.display_name
+  end
+
+  def locked
+    object.suspended? ? false : object.locked
+  end
+
+  def bot
+    object.suspended? ? false : object.bot
+  end
+
+  def discoverable
+    object.suspended? ? false : object.discoverable
+  end
+
+  def moved_to_account
+    object.suspended? ? nil : object.moved_to_account
+  end
+
+  def emojis
+    object.suspended? ? [] : object.emojis
+  end
+
+  def fields
+    object.suspended? ? [] : object.fields
+  end
+
+  def suspended
+    object.suspended?
+  end
+
+  delegate :suspended?, to: :object
+
+  def moved_and_not_nested?
+    object.moved? && object.moved_to_account.moved_to_account_id.nil?
+  end
 end
index 93df0a326f07c57dc5774c14bcd480e47a76e49e..752e79e65ed8ff6f8df03c85964250188ef43aeb 100644 (file)
@@ -35,6 +35,10 @@ module Paperclip
 
       formats.include?(other_extension.delete('.')) && File.basename(other_filename, other_extension) == File.basename(original_filename, File.extname(original_filename))
     end
+
+    def default_url(style_name = default_style)
+      @url_generator.for_as_default(style_name)
+    end
   end
 end
 
index 1079efdbc4f231b7a3f4b07faf56be8c6037d4bb..e1d6df2c299cdeaf5abb332f741d1dc67db4038b 100644 (file)
@@ -11,6 +11,10 @@ module Paperclip
         Addressable::URI.parse(url).normalize.to_str.gsub(escape_regex) { |m| "%#{m.ord.to_s(16).upcase}" }
       end
     end
+
+    def for_as_default(style_name)
+      attachment_options[:interpolator].interpolate(default_url, @attachment, style_name)
+    end
   end
 end