UX: Replace heuristic solution for extracting root domain.

This commit is contained in:
Guo Xiang Tan 2017-11-29 16:24:27 +08:00
parent 3e9c3f6f13
commit 8491c5fba5
5 changed files with 29 additions and 8 deletions

View File

@ -28,6 +28,7 @@ end
gem 'mail' gem 'mail'
gem 'mime-types', require: 'mime/types/columnar' gem 'mime-types', require: 'mime/types/columnar'
gem 'mini_mime' gem 'mini_mime'
gem 'mini_suffix'
gem 'hiredis' gem 'hiredis'
gem 'redis', require: ["redis", "redis/connection/hiredis"] gem 'redis', require: ["redis", "redis/connection/hiredis"]

View File

@ -177,6 +177,8 @@ GEM
mini_portile2 (2.3.0) mini_portile2 (2.3.0)
mini_racer (0.1.11) mini_racer (0.1.11)
libv8 (~> 5.7) libv8 (~> 5.7)
mini_suffix (0.1.0)
ffi (~> 1.9)
minitest (5.10.3) minitest (5.10.3)
mocha (1.2.1) mocha (1.2.1)
metaclass (~> 0.0.1) metaclass (~> 0.0.1)
@ -451,6 +453,7 @@ DEPENDENCIES
mime-types mime-types
mini_mime mini_mime
mini_racer mini_racer
mini_suffix
minitest minitest
mocha mocha
mock_redis mock_redis

View File

@ -168,17 +168,16 @@ createWidget('topic-map-expanded', {
const result = [avatars]; const result = [avatars];
if (attrs.topicLinks) { if (attrs.topicLinks) {
const toShow = state.allLinksShown ? attrs.topicLinks : attrs.topicLinks.slice(0, LINKS_SHOWN); const toShow = state.allLinksShown ? attrs.topicLinks : attrs.topicLinks.slice(0, LINKS_SHOWN);
const links = toShow.map(l => {
const links = toShow.map(l => {
let host = ''; let host = '';
if (l.title && l.title.length) { if (l.title && l.title.length) {
const domain = l.domain; const rootDomain = l.root_domain;
if (domain && domain.length) {
const s = domain.split('.'); if (rootDomain && rootDomain.length) {
if (s[0] === 'www') s.shift(); host = h('span.domain', rootDomain);
host = h('span.domain', s.join('.'));
} }
} }

View File

@ -8,7 +8,8 @@ class TopicLinkSerializer < ApplicationSerializer
:reflection, :reflection,
:clicks, :clicks,
:user_id, :user_id,
:domain :domain,
:root_domain,
def url def url
object['url'] object['url']
@ -50,4 +51,8 @@ class TopicLinkSerializer < ApplicationSerializer
object['domain'] object['domain']
end end
def root_domain
MiniSuffix.domain(domain)
end
end end

View File

@ -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