]> cat aescling's git repositories - mastodon.git/commitdiff
New API: GET /api/v1/custom_emojis to get a server's custom emojis (#5051)
authorEugen Rochko <eugen@zeonfederated.com>
Fri, 22 Sep 2017 23:57:23 +0000 (01:57 +0200)
committerGitHub <noreply@github.com>
Fri, 22 Sep 2017 23:57:23 +0000 (01:57 +0200)
app/controllers/admin/custom_emojis_controller.rb
app/controllers/api/v1/custom_emojis_controller.rb [new file with mode: 0644]
app/models/custom_emoji.rb
app/serializers/rest/custom_emoji_serializer.rb [new file with mode: 0644]
app/serializers/rest/status_serializer.rb
config/routes.rb
spec/controllers/api/v1/custom_emojis_controller_spec.rb [new file with mode: 0644]

index 572ad1ac2e2bac459551e868a320adab4e088b51..d70514d9a97a41e80e94f1c16b2b9cb909a62ea1 100644 (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
diff --git a/app/controllers/api/v1/custom_emojis_controller.rb b/app/controllers/api/v1/custom_emojis_controller.rb
new file mode 100644 (file)
index 0000000..4dd77fb
--- /dev/null
@@ -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
index f4d3b16a031d429d5416844f872427f9f73bdbd0..aff9f8dfa3f7770ae4460aaf5598e8d564bba918 100644 (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
diff --git a/app/serializers/rest/custom_emoji_serializer.rb b/app/serializers/rest/custom_emoji_serializer.rb
new file mode 100644 (file)
index 0000000..b744dd4
--- /dev/null
@@ -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
index e0fd1c77e40fb0a3dd814ea0e4e2c7690e59405f..ef3c325ba6adb3a6763c8c2701dcf637cf8ee198 100644 (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
index d38f5308a23c095b622fdc69d3521045e887d899..cb7e84d7bde4ed69c3469d378b75a98077772b66 100644 (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
 
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 (file)
index 0000000..9f35228
--- /dev/null
@@ -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