Skip to content

Commit

Permalink
Refactor controllers for statuses, accounts, and more (#11249)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron committed Jul 8, 2019
1 parent f147764 commit 63c7fe8
Show file tree
Hide file tree
Showing 41 changed files with 299 additions and 289 deletions.
16 changes: 12 additions & 4 deletions app/controllers/about_controller.rb
Expand Up @@ -3,11 +3,11 @@
class AboutController < ApplicationController
layout 'public'

before_action :set_instance_presenter, only: [:show, :more, :terms]
before_action :set_body_classes, only: :show
before_action :set_instance_presenter
before_action :set_expires_in

def show
@hide_navbar = true
end
def show; end

def more; end

Expand All @@ -27,4 +27,12 @@ def new_user
def set_instance_presenter
@instance_presenter = InstancePresenter.new
end

def set_body_classes
@hide_navbar = true
end

def set_expires_in
expires_in 0, public: true
end
end
15 changes: 9 additions & 6 deletions app/controllers/accounts_controller.rb
Expand Up @@ -6,13 +6,13 @@ class AccountsController < ApplicationController
include AccountControllerConcern

before_action :set_cache_headers
before_action :set_body_classes

def show
respond_to do |format|
format.html do
mark_cacheable! unless user_signed_in?
expires_in 0, public: true unless user_signed_in?

@body_classes = 'with-modals'
@pinned_statuses = []
@endorsed_accounts = @account.endorsed_accounts.to_a.sample(4)

Expand All @@ -32,22 +32,25 @@ def show
end

format.rss do
mark_cacheable!
expires_in 0, public: true

@statuses = cache_collection(default_statuses.without_reblogs.without_replies.limit(PAGE_SIZE), Status)
render xml: RSS::AccountSerializer.render(@account, @statuses)
end

format.json do
render_cached_json(['activitypub', 'actor', @account], content_type: 'application/activity+json') do
ActiveModelSerializers::SerializableResource.new(@account, serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter)
end
expires_in 3.minutes, public: true
render json: @account, content_type: 'application/activity+json', serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter
end
end
end

private

def set_body_classes
@body_classes = 'with-modals'
end

def show_pinned_statuses?
[replies_requested?, media_requested?, tag_requested?, params[:max_id].present?, params[:min_id].present?].none?
end
Expand Down
16 changes: 3 additions & 13 deletions app/controllers/activitypub/collections_controller.rb
Expand Up @@ -2,29 +2,19 @@

class ActivityPub::CollectionsController < Api::BaseController
include SignatureVerification
include AccountOwnedConcern

before_action :set_account
before_action :set_size
before_action :set_statuses
before_action :set_cache_headers

def show
render_cached_json(['activitypub', 'collection', @account, params[:id]], content_type: 'application/activity+json') do
ActiveModelSerializers::SerializableResource.new(
collection_presenter,
serializer: ActivityPub::CollectionSerializer,
adapter: ActivityPub::Adapter,
skip_activities: true
)
end
expires_in 3.minutes, public: true
render json: collection_presenter, content_type: 'application/activity+json', serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, skip_activities: true
end

private

def set_account
@account = Account.find_local!(params[:account_username])
end

def set_statuses
@statuses = scope_for_collection
@statuses = cache_collection(@statuses, Status)
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/activitypub/inboxes_controller.rb
Expand Up @@ -3,8 +3,7 @@
class ActivityPub::InboxesController < Api::BaseController
include SignatureVerification
include JsonLdHelper

before_action :set_account
include AccountOwnedConcern

def create
if unknown_deleted_account?
Expand All @@ -27,8 +26,8 @@ def unknown_deleted_account?
false
end

def set_account
@account = Account.find_local!(params[:account_username]) if params[:account_username]
def account_required?
params[:account_username].present?
end

def body
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/activitypub/outboxes_controller.rb
Expand Up @@ -4,8 +4,8 @@ class ActivityPub::OutboxesController < Api::BaseController
LIMIT = 20

include SignatureVerification
include AccountOwnedConcern

before_action :set_account
before_action :set_statuses
before_action :set_cache_headers

Expand All @@ -17,10 +17,6 @@ def show

private

def set_account
@account = Account.find_local!(params[:account_username])
end

def outbox_presenter
if page_requested?
ActivityPub::CollectionPresenter.new(
Expand Down
68 changes: 68 additions & 0 deletions app/controllers/activitypub/replies_controller.rb
@@ -0,0 +1,68 @@
# frozen_string_literal: true

class ActivityPub::RepliesController < Api::BaseController
include SignatureAuthentication
include Authorization
include AccountOwnedConcern

DESCENDANTS_LIMIT = 60

before_action :set_status
before_action :set_cache_headers
before_action :set_replies

def index
render json: replies_collection_presenter, serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json', skip_activities: true
end

private

def set_status
@status = @account.statuses.find(params[:status_id])
authorize @status, :show?
rescue Mastodon::NotPermittedError
raise ActiveRecord::RecordNotFound
end

def set_replies
@replies = page_params[:other_accounts] ? Status.where.not(account_id: @account.id) : @account.statuses
@replies = @replies.where(in_reply_to_id: @status.id, visibility: [:public, :unlisted])
@replies = @replies.paginate_by_min_id(DESCENDANTS_LIMIT, params[:min_id])
end

def replies_collection_presenter
page = ActivityPub::CollectionPresenter.new(
id: account_status_replies_url(@account, @status, page_params),
type: :unordered,
part_of: account_status_replies_url(@account, @status),
next: next_page,
items: @replies.map { |status| status.local ? status : status.id }
)

return page if page_requested?

ActivityPub::CollectionPresenter.new(
id: account_status_replies_url(@account, @status),
type: :unordered,
first: page
)
end

def page_requested?
params[:page] == 'true'
end

def next_page
account_status_replies_url(
@account,
@status,
page: true,
min_id: @replies&.last&.id,
other_accounts: !(@replies&.last&.account_id == @account.id && @replies.size == DESCENDANTS_LIMIT)
)
end

def page_params
params_slice(:other_accounts, :min_id).merge(page: true)
end
end
17 changes: 4 additions & 13 deletions app/controllers/api/proofs_controller.rb
@@ -1,10 +1,9 @@
# frozen_string_literal: true

class Api::ProofsController < Api::BaseController
before_action :set_account
include AccountOwnedConcern

before_action :set_provider
before_action :check_account_approval
before_action :check_account_suspension

def index
render json: @account, serializer: @provider.serializer_class
Expand All @@ -16,15 +15,7 @@ def set_provider
@provider = ProofProvider.find(params[:provider]) || raise(ActiveRecord::RecordNotFound)
end

def set_account
@account = Account.find_local!(params[:username])
end

def check_account_approval
not_found if @account.user_pending?
end

def check_account_suspension
gone if @account.suspended?
def username_param
params[:username]
end
end
4 changes: 0 additions & 4 deletions app/controllers/application_controller.rb
Expand Up @@ -154,8 +154,4 @@ def render_cached_json(cache_key, **options)
def set_cache_headers
response.headers['Vary'] = 'Accept'
end

def mark_cacheable!
expires_in 0, public: true
end
end
34 changes: 3 additions & 31 deletions app/controllers/concerns/account_controller_concern.rb
Expand Up @@ -3,24 +3,19 @@
module AccountControllerConcern
extend ActiveSupport::Concern

include AccountOwnedConcern

FOLLOW_PER_PAGE = 12

included do
layout 'public'

before_action :set_account
before_action :check_account_approval
before_action :check_account_suspension
before_action :set_instance_presenter
before_action :set_link_headers
end

private

def set_account
@account = Account.find_local!(username_param)
end

def set_instance_presenter
@instance_presenter = InstancePresenter.new
end
Expand All @@ -29,27 +24,15 @@ def set_link_headers
response.headers['Link'] = LinkHeader.new(
[
webfinger_account_link,
atom_account_url_link,
actor_url_link,
]
)
end

def username_param
params[:account_username]
end

def webfinger_account_link
[
webfinger_account_url,
[%w(rel lrdd), %w(type application/xrd+xml)],
]
end

def atom_account_url_link
[
account_url(@account, format: 'atom'),
[%w(rel alternate), %w(type application/atom+xml)],
[%w(rel lrdd), %w(type application/jrd+json)],
]
end

Expand All @@ -63,15 +46,4 @@ def actor_url_link
def webfinger_account_url
webfinger_url(resource: @account.to_webfinger_s)
end

def check_account_approval
not_found if @account.user_pending?
end

def check_account_suspension
if @account.suspended?
expires_in(3.minutes, public: true)
gone
end
end
end
33 changes: 33 additions & 0 deletions app/controllers/concerns/account_owned_concern.rb
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module AccountOwnedConcern
extend ActiveSupport::Concern

included do
before_action :set_account, if: :account_required?
before_action :check_account_approval, if: :account_required?
before_action :check_account_suspension, if: :account_required?
end

private

def account_required?
true
end

def set_account
@account = Account.find_local!(username_param)
end

def username_param
params[:account_username]
end

def check_account_approval
not_found if @account.local? && @account.user_pending?
end

def check_account_suspension
expires_in(3.minutes, public: true) && gone if @account.suspended?
end
end

0 comments on commit 63c7fe8

Please sign in to comment.