Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Change REST API to return empty data for suspended accounts (mastodon…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored Sep 11, 2020
1 parent e6d67f8 commit e6b272e
Show file tree
Hide file tree
Showing 22 changed files with 79 additions and 30 deletions.
3 changes: 1 addition & 2 deletions app/controllers/activitypub/outboxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ def prev_page
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def set_account
end

def set_featured_tags
@featured_tags = @account.featured_tags
@featured_tags = @account.suspended? ? @account.featured_tags : []
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def load_accounts
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def load_accounts
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/accounts/lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/accounts/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def set_account
end

def load_statuses
cached_account_statuses
@account.suspended? ? [] : cached_account_statuses
end

def cached_account_statuses
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/api/v1/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,10 +72,6 @@ def relationships(**options)
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
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api/v1/blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def load_accounts

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),
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/endorsements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def load_accounts
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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/follow_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def load_accounts
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
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/lists/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def set_list

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

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api/v1/mutes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def load_accounts

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),
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def index
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

Expand All @@ -40,7 +40,7 @@ def load_notifications
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def load_accounts

def default_accounts
Account
.without_suspended
.includes(:favourites, :account_stat)
.references(:favourites)
.where(favourites: { status_id: @status.id })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def load_accounts
end

def default_accounts
Account.includes(:statuses, :account_stat).references(:statuses)
Account.without_suspended.includes(:statuses, :account_stat).references(:statuses)
end

def paginated_statuses
Expand Down
3 changes: 3 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/policies/status_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def index?
end

def show?
return false if author.suspended?

if requires_mention?
owned? || mention_exists?
elsif private?
Expand Down
55 changes: 46 additions & 9 deletions app/serializers/rest/account_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -29,34 +32,68 @@ def acct
end

def note
Formatter.instance.simplified_format(object)
object.suspended? ? '' : Formatter.instance.simplified_format(object)
end

def url
ActivityPub::TagManager.instance.url_for(object)
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
4 changes: 4 additions & 0 deletions lib/paperclip/attachment_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def variant?(other_filename)

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

Expand Down
4 changes: 4 additions & 0 deletions lib/paperclip/url_generator_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def escape_url(url)
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

Expand Down

0 comments on commit e6b272e

Please sign in to comment.