From 8491c5fba55bd680e8478b2a4cbbe43c50adb32b Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 29 Nov 2017 16:24:27 +0800 Subject: [PATCH] UX: Replace heuristic solution for extracting root domain. --- Gemfile | 1 + Gemfile.lock | 3 +++ .../javascripts/discourse/widgets/topic-map.js.es6 | 13 ++++++------- app/serializers/topic_link_serializer.rb | 7 ++++++- spec/serializers/topic_link_serializer_spec.rb | 13 +++++++++++++ 5 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 spec/serializers/topic_link_serializer_spec.rb diff --git a/Gemfile b/Gemfile index d83171e4381..93d7138d162 100644 --- a/Gemfile +++ b/Gemfile @@ -28,6 +28,7 @@ end gem 'mail' gem 'mime-types', require: 'mime/types/columnar' gem 'mini_mime' +gem 'mini_suffix' gem 'hiredis' gem 'redis', require: ["redis", "redis/connection/hiredis"] diff --git a/Gemfile.lock b/Gemfile.lock index a04773bdfeb..af4d8bfc113 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -177,6 +177,8 @@ GEM mini_portile2 (2.3.0) mini_racer (0.1.11) libv8 (~> 5.7) + mini_suffix (0.1.0) + ffi (~> 1.9) minitest (5.10.3) mocha (1.2.1) metaclass (~> 0.0.1) @@ -451,6 +453,7 @@ DEPENDENCIES mime-types mini_mime mini_racer + mini_suffix minitest mocha mock_redis diff --git a/app/assets/javascripts/discourse/widgets/topic-map.js.es6 b/app/assets/javascripts/discourse/widgets/topic-map.js.es6 index 6cfd0f55f07..fbf6074e0a2 100644 --- a/app/assets/javascripts/discourse/widgets/topic-map.js.es6 +++ b/app/assets/javascripts/discourse/widgets/topic-map.js.es6 @@ -168,17 +168,16 @@ createWidget('topic-map-expanded', { const result = [avatars]; if (attrs.topicLinks) { - const toShow = state.allLinksShown ? attrs.topicLinks : attrs.topicLinks.slice(0, LINKS_SHOWN); - const links = toShow.map(l => { + const links = toShow.map(l => { let host = ''; + if (l.title && l.title.length) { - const domain = l.domain; - if (domain && domain.length) { - const s = domain.split('.'); - if (s[0] === 'www') s.shift(); - host = h('span.domain', s.join('.')); + const rootDomain = l.root_domain; + + if (rootDomain && rootDomain.length) { + host = h('span.domain', rootDomain); } } diff --git a/app/serializers/topic_link_serializer.rb b/app/serializers/topic_link_serializer.rb index 1f64aec4be3..42367857c54 100644 --- a/app/serializers/topic_link_serializer.rb +++ b/app/serializers/topic_link_serializer.rb @@ -8,7 +8,8 @@ class TopicLinkSerializer < ApplicationSerializer :reflection, :clicks, :user_id, - :domain + :domain, + :root_domain, def url object['url'] @@ -50,4 +51,8 @@ class TopicLinkSerializer < ApplicationSerializer object['domain'] end + def root_domain + MiniSuffix.domain(domain) + end + end diff --git a/spec/serializers/topic_link_serializer_spec.rb b/spec/serializers/topic_link_serializer_spec.rb new file mode 100644 index 00000000000..9b61f55ac89 --- /dev/null +++ b/spec/serializers/topic_link_serializer_spec.rb @@ -0,0 +1,13 @@ +require 'rails_helper' + +describe TopicLinkSerializer do + + it "correctly serializes the topic link" do + post = Fabricate(:post, raw: 'https://meta.discourse.org/') + TopicLink.extract_from(post) + serialized = described_class.new(post.topic_links.first, root: false).as_json + + expect(serialized[:domain]).to eq("meta.discourse.org") + expect(serialized[:root_domain]).to eq("discourse.org") + end +end