Browse Source

New API: GET /api/v1/custom_emojis to get a server's custom emojis (#5051)

pull/4/head
Eugen Rochko 6 years ago
committed by GitHub
parent
commit
293972f716
7 changed files with 43 additions and 12 deletions
  1. +1
    -1
      app/controllers/admin/custom_emojis_controller.rb
  2. +9
    -0
      app/controllers/api/v1/custom_emojis_controller.rb
  3. +2
    -0
      app/models/custom_emoji.rb
  4. +11
    -0
      app/serializers/rest/custom_emoji_serializer.rb
  5. +1
    -11
      app/serializers/rest/status_serializer.rb
  6. +1
    -0
      config/routes.rb
  7. +18
    -0
      spec/controllers/api/v1/custom_emojis_controller_spec.rb

+ 1
- 1
app/controllers/admin/custom_emojis_controller.rb View File

@ -3,7 +3,7 @@
module Admin
class CustomEmojisController < BaseController
def index
@custom_emojis = CustomEmoji.where(domain : nil)
@custom_emojis = CustomEmoji.local
end
def new

+ 9
- 0
app/controllers/api/v1/custom_emojis_controller.rb View File

@ -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

+ 2
- 0
app/models/custom_emoji.rb View File

@ -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

+ 11
- 0
app/serializers/rest/custom_emoji_serializer.rb View File

@ -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

+ 1
- 11
app/serializers/rest/status_serializer.rb View File

@ -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

+ 1
- 0
config/routes.rb View File

@ -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

+ 18
- 0
spec/controllers/api/v1/custom_emojis_controller_spec.rb View File

@ -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

Loading…
Cancel
Save