diff --git a/app/models/emoji.rb b/app/models/emoji.rb index 51eae0c706a..6eb570fce4b 100644 --- a/app/models/emoji.rb +++ b/app/models/emoji.rb @@ -173,6 +173,19 @@ class Emoji @unicode_replacements end + def self.lookup_unicode(name) + @reverse_map ||= begin + map = {} + db['emojis'].each do |e| + next if e['name'] == 'tm' + code = replacement_code(e['code']) + map[e['name']] = code if code + end + map + end + @reverse_map[name] + end + def self.unicode_replacements_json @unicode_replacements_json ||= unicode_replacements.to_json end diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index 5fc0c8aaf91..83a06aeaa12 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -382,7 +382,7 @@ class PostAlerter post_number: original_post.post_number, topic_title: original_post.topic.title, topic_id: original_post.topic.id, - excerpt: original_post.excerpt(400, text_entities: true, strip_links: true), + excerpt: original_post.excerpt(400, text_entities: true, strip_links: true, remap_emoji: true), username: original_username, post_url: post_url } diff --git a/lib/excerpt_parser.rb b/lib/excerpt_parser.rb index 4a19c337a82..2c62b937198 100644 --- a/lib/excerpt_parser.rb +++ b/lib/excerpt_parser.rb @@ -14,6 +14,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document @markdown_images = options[:markdown_images] == true @keep_newlines = options[:keep_newlines] == true @keep_emoji_images = options[:keep_emoji_images] == true + @remap_emoji = options[:remap_emoji] == true @start_excerpt = false end @@ -51,7 +52,11 @@ class ExcerptParser < Nokogiri::XML::SAX::Document attributes = Hash[*attributes.flatten] if attributes["class"] == 'emoji' - if @keep_emoji_images + if @remap_emoji + title = (attributes["alt"] || "").gsub(":", "") + title = Emoji.lookup_unicode(title) || attributes["alt"] + return characters(title) + elsif @keep_emoji_images return include_tag(name, attributes) else return characters(attributes["alt"]) diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index eb840cbcd8a..109181333fa 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -265,6 +265,11 @@ HTML expect(PrettyText.excerpt(emoji_image, 100, { keep_emoji_images: true })).to match_html(emoji_image) end + it "should have an option to remap emoji to code points" do + emoji_image = "I :heart: you :unknown: " + expect(PrettyText.excerpt(emoji_image, 100, { remap_emoji: true })).to match_html("I ❤ you :unknown:") + end + it "should have an option to preserve emoji codes" do emoji_code = ":heart:" expect(PrettyText.excerpt(emoji_code, 100)).to eq(":heart:") diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb index 93380605430..8c2915b64c2 100644 --- a/spec/services/post_alerter_spec.rb +++ b/spec/services/post_alerter_spec.rb @@ -327,7 +327,7 @@ describe PostAlerter do end describe "push_notification" do - let(:mention_post) { create_post_with_alerts(user: user, raw: 'Hello @eviltrout')} + let(:mention_post) { create_post_with_alerts(user: user, raw: 'Hello @eviltrout :heart:')} let(:topic) { mention_post.topic } it "correctly pushes notifications if configured correctly" do @@ -344,8 +344,6 @@ describe PostAlerter do push_url: "https://site2.com/push") end - - body = nil headers = nil @@ -368,7 +366,7 @@ describe PostAlerter do 'post_number' => 1, 'topic_title' => topic.title, 'topic_id' => topic.id, - 'excerpt' => 'Hello @eviltrout', + 'excerpt' => 'Hello @eviltrout ❤', 'username' => user.username, 'url' => UrlHelper.absolute(mention_post.url), 'client_id' => 'xxx0' @@ -378,7 +376,7 @@ describe PostAlerter do 'post_number' => 1, 'topic_title' => topic.title, 'topic_id' => topic.id, - 'excerpt' => 'Hello @eviltrout', + 'excerpt' => 'Hello @eviltrout ❤', 'username' => user.username, 'url' => UrlHelper.absolute(mention_post.url), 'client_id' => 'xxx1'