Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode custom emojis as resolveable objects in ActivityPub #5243

Merged
merged 2 commits into from Oct 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/controllers/accounts_controller.rb
Expand Up @@ -26,7 +26,10 @@ def show
end

format.json do
render json: @account, serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
render json: @account,
serializer: ActivityPub::ActorSerializer,
adapter: ActivityPub::Adapter,
content_type: 'application/activity+json'
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions app/controllers/emojis_controller.rb
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class EmojisController < ApplicationController
before_action :set_emoji

def show
respond_to do |format|
format.json do
render json: @emoji,
serializer: ActivityPub::EmojiSerializer,
adapter: ActivityPub::Adapter,
content_type: 'application/activity+json'
end
end
end

private

def set_emoji
@emoji = CustomEmoji.local.find(params[:id])
end
end
5 changes: 4 additions & 1 deletion app/controllers/follower_accounts_controller.rb
Expand Up @@ -10,7 +10,10 @@ def index
format.html

format.json do
render json: collection_presenter, serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
render json: collection_presenter,
serializer: ActivityPub::CollectionSerializer,
adapter: ActivityPub::Adapter,
content_type: 'application/activity+json'
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/following_accounts_controller.rb
Expand Up @@ -10,7 +10,10 @@ def index
format.html

format.json do
render json: collection_presenter, serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
render json: collection_presenter,
serializer: ActivityPub::CollectionSerializer,
adapter: ActivityPub::Adapter,
content_type: 'application/activity+json'
end
end
end
Expand Down
10 changes: 8 additions & 2 deletions app/controllers/statuses_controller.rb
Expand Up @@ -21,13 +21,19 @@ def show
end

format.json do
render json: @status, serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
render json: @status,
serializer: ActivityPub::NoteSerializer,
adapter: ActivityPub::Adapter,
content_type: 'application/activity+json'
end
end
end

def activity
render json: @status, serializer: ActivityPub::ActivitySerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
render json: @status,
serializer: ActivityPub::ActivitySerializer,
adapter: ActivityPub::Adapter,
content_type: 'application/activity+json'
end

def embed
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/tags_controller.rb
Expand Up @@ -12,7 +12,10 @@ def show
format.html

format.json do
render json: collection_presenter, serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
render json: collection_presenter,
serializer: ActivityPub::CollectionSerializer,
adapter: ActivityPub::Adapter,
content_type: 'application/activity+json'
end
end
end
Expand Down
12 changes: 8 additions & 4 deletions app/lib/activitypub/activity/create.rb
Expand Up @@ -86,15 +86,19 @@ def process_mention(tag, status)
end

def process_emoji(tag, _status)
return if tag['name'].blank? || tag['href'].blank?
return if skip_download?
return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?

shortcode = tag['name'].delete(':')
image_url = tag['icon']['url']
uri = tag['id']
updated = tag['updated']
emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)

return if !emoji.nil? || skip_download?
return unless emoji.nil? || emoji.updated_at >= updated

emoji = CustomEmoji.new(domain: @account.domain, shortcode: shortcode)
emoji.image_remote_url = tag['href']
emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
emoji.image_remote_url = image_url
emoji.save
end

Expand Down
2 changes: 2 additions & 0 deletions app/lib/activitypub/tag_manager.rb
Expand Up @@ -33,6 +33,8 @@ def uri_for(target)
when :note, :comment, :activity
return activity_account_status_url(target.account, target) if target.reblog?
account_status_url(target.account, target)
when :emoji
emoji_url(target)
end
end

Expand Down
6 changes: 6 additions & 0 deletions app/models/custom_emoji.rb
Expand Up @@ -13,6 +13,8 @@
# created_at :datetime not null
# updated_at :datetime not null
# disabled :boolean default(FALSE), not null
# uri :string
# image_remote_url :string
#

class CustomEmoji < ApplicationRecord
Expand All @@ -37,6 +39,10 @@ def local?
domain.nil?
end

def object_type
:emoji
end

class << self
def from_text(text, domain)
return [] if text.blank?
Expand Down
18 changes: 2 additions & 16 deletions app/serializers/activitypub/actor_serializer.rb
Expand Up @@ -10,20 +10,6 @@ class ActivityPub::ActorSerializer < ActiveModel::Serializer

has_one :public_key, serializer: ActivityPub::PublicKeySerializer

class ImageSerializer < ActiveModel::Serializer
include RoutingHelper

attributes :type, :url

def type
'Image'
end

def url
full_asset_url(object.url(:original))
end
end

class EndpointsSerializer < ActiveModel::Serializer
include RoutingHelper

Expand All @@ -36,8 +22,8 @@ def shared_inbox

has_one :endpoints, serializer: EndpointsSerializer

has_one :icon, serializer: ImageSerializer, if: :avatar_exists?
has_one :image, serializer: ImageSerializer, if: :header_exists?
has_one :icon, serializer: ActivityPub::ImageSerializer, if: :avatar_exists?
has_one :image, serializer: ActivityPub::ImageSerializer, if: :header_exists?

def id
account_url(object)
Expand Down
29 changes: 29 additions & 0 deletions app/serializers/activitypub/emoji_serializer.rb
@@ -0,0 +1,29 @@
# frozen_string_literal: true

class ActivityPub::EmojiSerializer < ActiveModel::Serializer
include RoutingHelper

attributes :id, :type, :name, :updated

has_one :icon, serializer: ActivityPub::ImageSerializer

def id
ActivityPub::TagManager.instance.uri_for(object)
end

def type
'Emoji'
end

def icon
object.image
end

def updated
object.updated_at.iso8601
end

def name
":#{object.shortcode}:"
end
end
19 changes: 19 additions & 0 deletions app/serializers/activitypub/image_serializer.rb
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class ActivityPub::ImageSerializer < ActiveModel::Serializer
include RoutingHelper

attributes :type, :media_type, :url

def type
'Image'
end

def url
full_asset_url(object.url(:original))
end

def media_type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this new? is there a practical implication to this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's just nice

object.content_type
end
end
17 changes: 1 addition & 16 deletions app/serializers/activitypub/note_serializer.rb
Expand Up @@ -142,21 +142,6 @@ def name
end
end

class CustomEmojiSerializer < ActiveModel::Serializer
include RoutingHelper

attributes :type, :href, :name

def type
'Emoji'
end

def href
full_asset_url(object.image.url)
end

def name
":#{object.shortcode}:"
end
class CustomEmojiSerializer < ActivityPub::EmojiSerializer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between this and the Emoji serializer? do we need both?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AMS derives serializer name from class name, virtual_tags is array of various objects, some of which are instances of CustomEmoji, but what we need is called ActivityPub::EmojiSerializer. It's easier to do this than define an AMS method that changes how serializer names are derived.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the def object_type; emoji; end thing mean this isn't relevant? having a hard time finding the AMS docs on this, sorry.....

end
end
5 changes: 3 additions & 2 deletions config/routes.rb
Expand Up @@ -96,8 +96,9 @@
resources :sessions, only: [:destroy]
end

resources :media, only: [:show]
resources :tags, only: [:show]
resources :media, only: [:show]
resources :tags, only: [:show]
resources :emojis, only: [:show]

get '/media_proxy/:id/(*any)', to: 'media_proxy#show', as: :media_proxy

Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20171006142024_add_uri_to_custom_emojis.rb
@@ -0,0 +1,6 @@
class AddUriToCustomEmojis < ActiveRecord::Migration[5.1]
def change
add_column :custom_emojis, :uri, :string
add_column :custom_emojis, :image_remote_url, :string
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171005171936) do
ActiveRecord::Schema.define(version: 20171006142024) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -99,6 +99,8 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "disabled", default: false, null: false
t.string "uri"
t.string "image_remote_url"
t.index ["shortcode", "domain"], name: "index_custom_emojis_on_shortcode_and_domain", unique: true
end

Expand Down
10 changes: 7 additions & 3 deletions spec/lib/activitypub/activity/create_spec.rb
Expand Up @@ -290,7 +290,9 @@
tag: [
{
type: 'Emoji',
href: 'http://example.com/emoji.png',
icon: {
url: 'http://example.com/emoji.png',
},
name: 'tinking',
},
],
Expand All @@ -314,7 +316,9 @@
tag: [
{
type: 'Emoji',
href: 'http://example.com/emoji.png',
icon: {
url: 'http://example.com/emoji.png',
},
},
],
}
Expand All @@ -326,7 +330,7 @@
end
end

context 'with emojis missing href' do
context 'with emojis missing icon' do
let(:object_json) do
{
id: 'bar',
Expand Down