Skip to content

Commit

Permalink
Add base class for api/v1/timelines/* controllers (mastodon#27840)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored Nov 14, 2023
1 parent b2c5b20 commit 7e1a77e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 80 deletions.
33 changes: 33 additions & 0 deletions app/controllers/api/v1/timelines/base_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

class Api::V1::Timelines::BaseController < Api::BaseController
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }

private

def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end

def pagination_max_id
@statuses.last.id
end

def pagination_since_id
@statuses.first.id
end

def next_path_params
permitted_params.merge(max_id: pagination_max_id)
end

def prev_path_params
permitted_params.merge(min_id: pagination_since_id)
end

def permitted_params
params
.slice(*self.class::PERMITTED_PARAMS)
.permit(*self.class::PERMITTED_PARAMS)
end
end
25 changes: 5 additions & 20 deletions app/controllers/api/v1/timelines/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

class Api::V1::Timelines::HomeController < Api::BaseController
class Api::V1::Timelines::HomeController < Api::V1::Timelines::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:statuses' }, only: [:show]
before_action :require_user!, only: [:show]
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }

PERMITTED_PARAMS = %i(local limit).freeze

def show
with_read_replica do
Expand Down Expand Up @@ -40,27 +41,11 @@ def account_home_feed
HomeFeed.new(current_account)
end

def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end

def pagination_params(core_params)
params.slice(:local, :limit).permit(:local, :limit).merge(core_params)
end

def next_path
api_v1_timelines_home_url pagination_params(max_id: pagination_max_id)
api_v1_timelines_home_url next_path_params
end

def prev_path
api_v1_timelines_home_url pagination_params(min_id: pagination_since_id)
end

def pagination_max_id
@statuses.last.id
end

def pagination_since_id
@statuses.first.id
api_v1_timelines_home_url prev_path_params
end
end
24 changes: 4 additions & 20 deletions app/controllers/api/v1/timelines/list_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

class Api::V1::Timelines::ListController < Api::BaseController
class Api::V1::Timelines::ListController < Api::V1::Timelines::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:lists' }
before_action :require_user!
before_action :set_list
before_action :set_statuses

after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
PERMITTED_PARAMS = %i(limit).freeze

def show
render json: @statuses,
Expand Down Expand Up @@ -41,27 +41,11 @@ def list_feed
ListFeed.new(@list)
end

def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end

def pagination_params(core_params)
params.slice(:limit).permit(:limit).merge(core_params)
end

def next_path
api_v1_timelines_list_url params[:id], pagination_params(max_id: pagination_max_id)
api_v1_timelines_list_url params[:id], next_path_params
end

def prev_path
api_v1_timelines_list_url params[:id], pagination_params(min_id: pagination_since_id)
end

def pagination_max_id
@statuses.last.id
end

def pagination_since_id
@statuses.first.id
api_v1_timelines_list_url params[:id], prev_path_params
end
end
25 changes: 5 additions & 20 deletions app/controllers/api/v1/timelines/public_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

class Api::V1::Timelines::PublicController < Api::BaseController
class Api::V1::Timelines::PublicController < Api::V1::Timelines::BaseController
before_action :require_user!, only: [:show], if: :require_auth?
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }

PERMITTED_PARAMS = %i(local remote limit only_media).freeze

def show
cache_if_unauthenticated!
Expand Down Expand Up @@ -42,27 +43,11 @@ def public_feed
)
end

def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end

def pagination_params(core_params)
params.slice(:local, :remote, :limit, :only_media).permit(:local, :remote, :limit, :only_media).merge(core_params)
end

def next_path
api_v1_timelines_public_url pagination_params(max_id: pagination_max_id)
api_v1_timelines_public_url next_path_params
end

def prev_path
api_v1_timelines_public_url pagination_params(min_id: pagination_since_id)
end

def pagination_max_id
@statuses.last.id
end

def pagination_since_id
@statuses.first.id
api_v1_timelines_public_url prev_path_params
end
end
25 changes: 5 additions & 20 deletions app/controllers/api/v1/timelines/tag_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

class Api::V1::Timelines::TagController < Api::BaseController
class Api::V1::Timelines::TagController < Api::V1::Timelines::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:statuses' }, only: :show, if: :require_auth?
before_action :load_tag
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }

PERMITTED_PARAMS = %i(local limit only_media).freeze

def show
cache_if_unauthenticated!
Expand Down Expand Up @@ -51,27 +52,11 @@ def tag_feed
)
end

def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end

def pagination_params(core_params)
params.slice(:local, :limit, :only_media).permit(:local, :limit, :only_media).merge(core_params)
end

def next_path
api_v1_timelines_tag_url params[:id], pagination_params(max_id: pagination_max_id)
api_v1_timelines_tag_url params[:id], next_path_params
end

def prev_path
api_v1_timelines_tag_url params[:id], pagination_params(min_id: pagination_since_id)
end

def pagination_max_id
@statuses.last.id
end

def pagination_since_id
@statuses.first.id
api_v1_timelines_tag_url params[:id], prev_path_params
end
end

0 comments on commit 7e1a77e

Please sign in to comment.