From 574681dc474e57763aaf9845bd730e4f67f793a8 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 21 Jul 2017 14:24:28 -0400 Subject: [PATCH] FIX: Show emoji in inline oneboxes --- app/helpers/application_helper.rb | 4 +--- app/helpers/topics_helper.rb | 2 +- app/mailers/user_notifications.rb | 2 +- app/models/emoji.rb | 6 ++++++ app/serializers/topic_view_serializer.rb | 6 +++--- lib/inline_oneboxer.rb | 5 ++++- spec/components/inline_oneboxer_spec.rb | 9 +++++++++ 7 files changed, 25 insertions(+), 9 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 356e38745b4..944ab7f8ef2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -230,9 +230,7 @@ module ApplicationHelper end def gsub_emoji_to_unicode(str) - if str - str.gsub(/:([\w\-+]*(?::t\d)?):/) { |name| Emoji.lookup_unicode($1) || name } - end + Emoji.gsub_emoji_to_unicode(str) end def application_logo_url diff --git a/app/helpers/topics_helper.rb b/app/helpers/topics_helper.rb index ad96e94177e..c3f0cb96459 100644 --- a/app/helpers/topics_helper.rb +++ b/app/helpers/topics_helper.rb @@ -2,7 +2,7 @@ module TopicsHelper include ApplicationHelper def render_topic_title(topic) - link_to(gsub_emoji_to_unicode(topic.title),topic.relative_url) + link_to(Emoji.gsub_emoji_to_unicode(topic.title),topic.relative_url) end def categories_breadcrumb(topic) diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 3fa3f96a701..73a5370e514 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -426,7 +426,7 @@ class UserNotifications < ActionMailer::Base end email_opts = { - topic_title: gsub_emoji_to_unicode(title), + topic_title: Emoji.gsub_emoji_to_unicode(title), topic_title_url_encoded: title ? URI.encode(title) : title, message: message, url: post.url(without_slug: SiteSetting.private_email?), diff --git a/app/models/emoji.rb b/app/models/emoji.rb index 616f522a6b9..513ad8478ff 100644 --- a/app/models/emoji.rb +++ b/app/models/emoji.rb @@ -159,6 +159,12 @@ class Emoji end.join end + def self.gsub_emoji_to_unicode(str) + if str + str.gsub(/:([\w\-+]*(?::t\d)?):/) { |name| Emoji.lookup_unicode($1) || name } + end + end + def self.lookup_unicode(name) @reverse_map ||= begin map = {} diff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rb index f9a548372ed..23682bd629c 100644 --- a/app/serializers/topic_view_serializer.rb +++ b/app/serializers/topic_view_serializer.rb @@ -95,8 +95,8 @@ class TopicViewSerializer < ApplicationSerializer end if object.suggested_topics.try(:topics).present? - result[:suggested_topics] = object.suggested_topics.topics.map do |topic| - SuggestedTopicSerializer.new(topic, scope: scope, root: false) + result[:suggested_topics] = object.suggested_topics.topics.map do |t| + SuggestedTopicSerializer.new(t, scope: scope, root: false) end end @@ -277,7 +277,7 @@ class TopicViewSerializer < ApplicationSerializer end def unicode_title - gsub_emoji_to_unicode(object.topic.title) + Emoji.gsub_emoji_to_unicode(object.topic.title) end def include_pm_with_non_human_user? diff --git a/lib/inline_oneboxer.rb b/lib/inline_oneboxer.rb index 89903e615b7..0f3ec89be9b 100644 --- a/lib/inline_oneboxer.rb +++ b/lib/inline_oneboxer.rb @@ -26,7 +26,10 @@ class InlineOneboxer # Only public topics if Guardian.new.can_see?(topic) - onebox = { url: url, title: topic.title } + onebox = { + url: url, + title: Emoji.gsub_emoji_to_unicode(topic.title) + } Rails.cache.write(cache_key(url), onebox, expires_in: 1.day) return onebox end diff --git a/spec/components/inline_oneboxer_spec.rb b/spec/components/inline_oneboxer_spec.rb index d923f8807ab..b8577b0d9dd 100644 --- a/spec/components/inline_oneboxer_spec.rb +++ b/spec/components/inline_oneboxer_spec.rb @@ -53,6 +53,15 @@ describe InlineOneboxer do expect(InlineOneboxer.lookup(nil)).to be_nil expect(InlineOneboxer.lookup("/test")).to be_nil end + + it "will return the fancy title" do + topic = Fabricate(:topic, title: "Hello :pizza: with an emoji") + onebox = InlineOneboxer.lookup(topic.url) + expect(onebox).to be_present + expect(onebox[:url]).to eq(topic.url) + expect(onebox[:title]).to eq("Hello 🍕 with an emoji") + end + end