From 7d16bb379d1463471faf264bb89e24d5b8a505ca Mon Sep 17 00:00:00 2001 From: nullkal Date: Tue, 19 Sep 2017 23:37:06 +0900 Subject: Use OrderedCollectionPage to return followers/following list (#4949) --- app/controllers/follower_accounts_controller.rb | 23 ++++++++++++++++++++--- app/controllers/following_accounts_controller.rb | 23 ++++++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/follower_accounts_controller.rb b/app/controllers/follower_accounts_controller.rb index 0e1949897..8eb4d2822 100644 --- a/app/controllers/follower_accounts_controller.rb +++ b/app/controllers/follower_accounts_controller.rb @@ -17,12 +17,29 @@ class FollowerAccountsController < ApplicationController private + def page_url(page) + account_followers_url(@account, page: page) unless page.nil? + end + def collection_presenter - ActivityPub::CollectionPresenter.new( - id: account_followers_url(@account), + page = ActivityPub::CollectionPresenter.new( + id: account_followers_url(@account, page: params.fetch(:page, 1)), type: :ordered, size: @account.followers_count, - items: @follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.account) } + items: @follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.account) }, + part_of: account_followers_url(@account), + next: page_url(@follows.next_page), + prev: page_url(@follows.prev_page) ) + if params[:page].present? + page + else + ActivityPub::CollectionPresenter.new( + id: account_followers_url(@account), + type: :ordered, + size: @account.followers_count, + first: page + ) + end end end diff --git a/app/controllers/following_accounts_controller.rb b/app/controllers/following_accounts_controller.rb index d4593093f..1ca6f0fe7 100644 --- a/app/controllers/following_accounts_controller.rb +++ b/app/controllers/following_accounts_controller.rb @@ -17,12 +17,29 @@ class FollowingAccountsController < ApplicationController private + def page_url(page) + account_following_index_url(@account, page: page) unless page.nil? + end + def collection_presenter - ActivityPub::CollectionPresenter.new( - id: account_following_index_url(@account), + page = ActivityPub::CollectionPresenter.new( + id: account_following_index_url(@account, page: params.fetch(:page, 1)), type: :ordered, size: @account.following_count, - items: @follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.target_account) } + items: @follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.target_account) }, + part_of: account_following_index_url(@account), + next: page_url(@follows.next_page), + prev: page_url(@follows.prev_page) ) + if params[:page].present? + page + else + ActivityPub::CollectionPresenter.new( + id: account_following_index_url(@account), + type: :ordered, + size: @account.following_count, + first: page + ) + end end end -- cgit From 293972f716476933df2b665ad755cafe4d29d82d Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 23 Sep 2017 01:57:23 +0200 Subject: New API: GET /api/v1/custom_emojis to get a server's custom emojis (#5051) --- app/controllers/admin/custom_emojis_controller.rb | 2 +- app/controllers/api/v1/custom_emojis_controller.rb | 9 +++++++++ app/models/custom_emoji.rb | 2 ++ app/serializers/rest/custom_emoji_serializer.rb | 11 +++++++++++ app/serializers/rest/status_serializer.rb | 12 +----------- config/routes.rb | 1 + .../api/v1/custom_emojis_controller_spec.rb | 18 ++++++++++++++++++ 7 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 app/controllers/api/v1/custom_emojis_controller.rb create mode 100644 app/serializers/rest/custom_emoji_serializer.rb create mode 100644 spec/controllers/api/v1/custom_emojis_controller_spec.rb (limited to 'app/controllers') diff --git a/app/controllers/admin/custom_emojis_controller.rb b/app/controllers/admin/custom_emojis_controller.rb index 572ad1ac2..d70514d9a 100644 --- a/app/controllers/admin/custom_emojis_controller.rb +++ b/app/controllers/admin/custom_emojis_controller.rb @@ -3,7 +3,7 @@ module Admin class CustomEmojisController < BaseController def index - @custom_emojis = CustomEmoji.where(domain: nil) + @custom_emojis = CustomEmoji.local end def new diff --git a/app/controllers/api/v1/custom_emojis_controller.rb b/app/controllers/api/v1/custom_emojis_controller.rb new file mode 100644 index 000000000..4dd77fb55 --- /dev/null +++ b/app/controllers/api/v1/custom_emojis_controller.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class Api::V1::CustomEmojisController < Api::BaseController + respond_to :json + + def index + render json: CustomEmoji.local, each_serializer: REST::CustomEmojiSerializer + end +end diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb index f4d3b16a0..aff9f8dfa 100644 --- a/app/models/custom_emoji.rb +++ b/app/models/custom_emoji.rb @@ -26,6 +26,8 @@ class CustomEmoji < ApplicationRecord validates_attachment :image, content_type: { content_type: 'image/png' }, presence: true, size: { in: 0..50.kilobytes } validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 } + scope :local, -> { where(domain: nil) } + include Remotable class << self diff --git a/app/serializers/rest/custom_emoji_serializer.rb b/app/serializers/rest/custom_emoji_serializer.rb new file mode 100644 index 000000000..b744dd4ec --- /dev/null +++ b/app/serializers/rest/custom_emoji_serializer.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class REST::CustomEmojiSerializer < ActiveModel::Serializer + include RoutingHelper + + attributes :shortcode, :url + + def url + full_asset_url(object.image.url) + end +end diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb index e0fd1c77e..ef3c325ba 100644 --- a/app/serializers/rest/status_serializer.rb +++ b/app/serializers/rest/status_serializer.rb @@ -17,7 +17,7 @@ class REST::StatusSerializer < ActiveModel::Serializer has_many :media_attachments, serializer: REST::MediaAttachmentSerializer has_many :mentions has_many :tags - has_many :emojis + has_many :emojis, serializer: REST::CustomEmojiSerializer def id object.id.to_s @@ -119,14 +119,4 @@ class REST::StatusSerializer < ActiveModel::Serializer tag_url(object) end end - - class CustomEmojiSerializer < ActiveModel::Serializer - include RoutingHelper - - attributes :shortcode, :url - - def url - full_asset_url(object.image.url) - end - end end diff --git a/config/routes.rb b/config/routes.rb index d38f5308a..cb7e84d7b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -188,6 +188,7 @@ Rails.application.routes.draw do end resources :streaming, only: [:index] + resources :custom_emojis, only: [:index] get '/search', to: 'search#index', as: :search diff --git a/spec/controllers/api/v1/custom_emojis_controller_spec.rb b/spec/controllers/api/v1/custom_emojis_controller_spec.rb new file mode 100644 index 000000000..9f3522812 --- /dev/null +++ b/spec/controllers/api/v1/custom_emojis_controller_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Api::V1::CustomEmojisController, type: :controller do + render_views + + describe 'GET #index' do + before do + Fabricate(:custom_emoji) + get :index + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + end +end -- cgit