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

Improve federation of custom emoji #9

Merged
merged 2 commits into from
Dec 17, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions app/lib/emoji_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
class EmojiFormatter
include RoutingHelper

DISALLOWED_BOUNDING_REGEX = /[[:alnum:]:]/

attr_reader :html, :custom_emojis, :options

# @param [ActiveSupport::SafeBuffer] html
Expand Down Expand Up @@ -39,15 +37,14 @@ def to_s
if inside_shortname && text[i] == ':'
inside_shortname = false
shortcode = text[shortname_start_index + 1..i - 1]
char_after = text[i + 1]

next unless (char_after.nil? || !DISALLOWED_BOUNDING_REGEX.match?(char_after)) && (emoji = emoji_map[shortcode])
next unless (emoji = emoji_map[shortcode])

result << Nokogiri::XML::Text.new(text[last_index..shortname_start_index - 1], tree.document) if shortname_start_index.positive?
result << Nokogiri::HTML.fragment(tag_for_emoji(shortcode, emoji))

last_index = i + 1
elsif text[i] == ':' && (i.zero? || !DISALLOWED_BOUNDING_REGEX.match?(text[i - 1]))
elsif text[i] == ':'
inside_shortname = true
shortname_start_index = i
end
Expand Down
6 changes: 2 additions & 4 deletions app/models/custom_emoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ class CustomEmoji < ApplicationRecord
LOCAL_LIMIT = (ENV['MAX_EMOJI_SIZE'] || 256.kilobytes).to_i
LIMIT = [LOCAL_LIMIT, (ENV['MAX_REMOTE_EMOJI_SIZE'] || 256.kilobytes).to_i].max

SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_]{2,}'
SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_-]{2,}'

SCAN_RE = /(?<=[^[:alnum:]:]|\n|^)
:(#{SHORTCODE_RE_FRAGMENT}):
(?=[^[:alnum:]:]|$)/x
SCAN_RE = /:(#{SHORTCODE_RE_FRAGMENT}):/x
SHORTCODE_ONLY_RE = /\A#{SHORTCODE_RE_FRAGMENT}\z/

IMAGE_MIME_TYPES = %w(image/png image/gif image/webp).freeze
Expand Down
8 changes: 0 additions & 8 deletions spec/lib/emoji_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ def preformat_text(str)
end
end

context 'when given text with concatenated emoji shortcodes' do
let(:text) { preformat_text(':coolcat::coolcat:') }

it 'does not touch the shortcodes' do
expect(subject).to match(/:coolcat::coolcat:/)
end
end

context 'when given text with an emoji shortcode at the end' do
let(:text) { preformat_text('Beep boop :coolcat:') }

Expand Down