mirror of
https://github.com/discourse/discourse.git
synced 2025-03-23 01:23:09 +08:00
UX: Replace heuristic solution root domain extraction for topic featured link.
This commit is contained in:
parent
b1375ef44e
commit
9fbe1436b6
@ -1,4 +1,3 @@
|
|||||||
import { extractDomainFromUrl } from 'discourse/lib/utilities';
|
|
||||||
import { h } from 'virtual-dom';
|
import { h } from 'virtual-dom';
|
||||||
|
|
||||||
const _decorators = [];
|
const _decorators = [];
|
||||||
@ -8,23 +7,22 @@ export function addFeaturedLinkMetaDecorator(decorator) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function extractLinkMeta(topic) {
|
export function extractLinkMeta(topic) {
|
||||||
const href = topic.featured_link,
|
const href = topic.get('featured_link');
|
||||||
target = Discourse.User.currentProp('external_links_in_new_tab') ? '_blank' : '';
|
const target = Discourse.User.currentProp('external_links_in_new_tab') ? '_blank' : '';
|
||||||
|
|
||||||
if (!href) { return; }
|
if (!href) { return; }
|
||||||
|
|
||||||
let domain = extractDomainFromUrl(href);
|
const meta = {
|
||||||
if (!domain) { return; }
|
target: target,
|
||||||
|
href,
|
||||||
|
domain: topic.get('featured_link_root_domain'),
|
||||||
|
rel: 'nofollow'
|
||||||
|
};
|
||||||
|
|
||||||
// www appears frequently, so we truncate it
|
|
||||||
if (domain && domain.substr(0, 4) === 'www.') {
|
|
||||||
domain = domain.substring(4);
|
|
||||||
}
|
|
||||||
|
|
||||||
const meta = { target, href, domain, rel: 'nofollow' };
|
|
||||||
if (_decorators.length) {
|
if (_decorators.length) {
|
||||||
_decorators.forEach(cb => cb(meta));
|
_decorators.forEach(cb => cb(meta));
|
||||||
}
|
}
|
||||||
|
|
||||||
return meta;
|
return meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,4 +43,3 @@ export function topicFeaturedLinkNode(topic) {
|
|||||||
}, meta.domain);
|
}, meta.domain);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1266,6 +1266,10 @@ SQL
|
|||||||
result.ntuples != 0
|
result.ntuples != 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def featured_link_root_domain
|
||||||
|
MiniSuffix.domain(URI.parse(self.featured_link).hostname)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def update_category_topic_count_by(num)
|
def update_category_topic_count_by(num)
|
||||||
|
@ -11,7 +11,8 @@ class TopicListItemSerializer < ListableTopicSerializer
|
|||||||
:bookmarked_post_numbers,
|
:bookmarked_post_numbers,
|
||||||
:liked_post_numbers,
|
:liked_post_numbers,
|
||||||
:tags,
|
:tags,
|
||||||
:featured_link
|
:featured_link,
|
||||||
|
:featured_link_root_domain
|
||||||
|
|
||||||
has_many :posters, serializer: TopicPosterSerializer, embed: :objects
|
has_many :posters, serializer: TopicPosterSerializer, embed: :objects
|
||||||
has_many :participants, serializer: TopicPosterSerializer, embed: :objects
|
has_many :participants, serializer: TopicPosterSerializer, embed: :objects
|
||||||
@ -77,8 +78,8 @@ class TopicListItemSerializer < ListableTopicSerializer
|
|||||||
SiteSetting.topic_featured_link_enabled
|
SiteSetting.topic_featured_link_enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
def featured_link
|
def include_featured_link_root_domain?
|
||||||
object.featured_link
|
SiteSetting.topic_featured_link_enabled && object.featured_link.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -37,6 +37,7 @@ class TopicViewSerializer < ApplicationSerializer
|
|||||||
:user_id,
|
:user_id,
|
||||||
:pm_with_non_human_user?,
|
:pm_with_non_human_user?,
|
||||||
:featured_link,
|
:featured_link,
|
||||||
|
:featured_link_root_domain,
|
||||||
:pinned_globally,
|
:pinned_globally,
|
||||||
:pinned_at,
|
:pinned_at,
|
||||||
:pinned_until
|
:pinned_until
|
||||||
@ -261,6 +262,10 @@ class TopicViewSerializer < ApplicationSerializer
|
|||||||
SiteSetting.topic_featured_link_enabled
|
SiteSetting.topic_featured_link_enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def include_featured_link_root_domain?
|
||||||
|
SiteSetting.topic_featured_link_enabled && object.topic.featured_link
|
||||||
|
end
|
||||||
|
|
||||||
def include_unicode_title?
|
def include_unicode_title?
|
||||||
!!(object.topic.title =~ /:([\w\-+]*):/)
|
!!(object.topic.title =~ /:([\w\-+]*):/)
|
||||||
end
|
end
|
||||||
|
@ -2095,4 +2095,19 @@ describe Topic do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#featured_link_root_domain' do
|
||||||
|
let(:topic) { Fabricate.build(:topic) }
|
||||||
|
|
||||||
|
it 'should extract the root domain correctly' do
|
||||||
|
[
|
||||||
|
"https://meta.discourse.org",
|
||||||
|
"https://meta.discourse.org/",
|
||||||
|
"https://meta.discourse.org/?filter=test"
|
||||||
|
].each do |featured_link|
|
||||||
|
topic.featured_link = featured_link
|
||||||
|
expect(topic.featured_link_root_domain).to eq("discourse.org")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,18 +2,45 @@ require 'rails_helper'
|
|||||||
require_dependency 'post_action'
|
require_dependency 'post_action'
|
||||||
|
|
||||||
describe TopicListItemSerializer do
|
describe TopicListItemSerializer do
|
||||||
|
let(:topic) do
|
||||||
it "correctly serializes topic" do
|
|
||||||
date = Time.zone.now
|
date = Time.zone.now
|
||||||
|
|
||||||
topic = Topic.new
|
Fabricate.build(:topic,
|
||||||
topic.title = "test"
|
title: 'test',
|
||||||
topic.created_at = date - 2.minutes
|
created_at: date - 2.minutes,
|
||||||
topic.bumped_at = date
|
bumped_at: date,
|
||||||
topic.posters = []
|
posters: [],
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "correctly serializes topic" do
|
||||||
|
SiteSetting.topic_featured_link_enabled = true
|
||||||
serialized = TopicListItemSerializer.new(topic, scope: Guardian.new, root: false).as_json
|
serialized = TopicListItemSerializer.new(topic, scope: Guardian.new, root: false).as_json
|
||||||
|
|
||||||
expect(serialized[:title]).to eq("test")
|
expect(serialized[:title]).to eq("test")
|
||||||
expect(serialized[:bumped]).to eq(true)
|
expect(serialized[:bumped]).to eq(true)
|
||||||
|
expect(serialized[:featured_link]).to eq(nil)
|
||||||
|
expect(serialized[:featured_link_root_domain]).to eq(nil)
|
||||||
|
|
||||||
|
featured_link = 'http://meta.discourse.org'
|
||||||
|
topic.featured_link = featured_link
|
||||||
|
serialized = TopicListItemSerializer.new(topic, scope: Guardian.new, root: false).as_json
|
||||||
|
|
||||||
|
expect(serialized[:featured_link]).to eq(featured_link)
|
||||||
|
expect(serialized[:featured_link_root_domain]).to eq('discourse.org')
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when topic featured link is disable' do
|
||||||
|
before do
|
||||||
|
SiteSetting.topic_featured_link_enabled = false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not include the topic's featured link" do
|
||||||
|
topic.featured_link = 'http://meta.discourse.org'
|
||||||
|
serialized = TopicListItemSerializer.new(topic, scope: Guardian.new, root: false).as_json
|
||||||
|
|
||||||
|
expect(serialized[:featured_link]).to eq(nil)
|
||||||
|
expect(serialized[:featured_link_root_domain]).to eq(nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,35 @@ describe TopicViewSerializer do
|
|||||||
let(:topic) { Fabricate(:topic) }
|
let(:topic) { Fabricate(:topic) }
|
||||||
let(:user) { Fabricate(:user) }
|
let(:user) { Fabricate(:user) }
|
||||||
|
|
||||||
|
describe '#featured_link and #featured_link_root_domain' do
|
||||||
|
let(:featured_link) { 'http://meta.discourse.org' }
|
||||||
|
|
||||||
|
describe 'when topic featured link is disable' do
|
||||||
|
it 'should return the right attributes' do
|
||||||
|
topic.update!(featured_link: featured_link)
|
||||||
|
SiteSetting.topic_featured_link_enabled = false
|
||||||
|
|
||||||
|
topic_view = TopicView.new(topic.id, user)
|
||||||
|
json = described_class.new(topic_view, scope: Guardian.new(user), root: false).as_json
|
||||||
|
|
||||||
|
expect(json[:featured_link]).to eq(nil)
|
||||||
|
expect(json[:featured_link_root_domain]).to eq(nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when topic featured link is enabled' do
|
||||||
|
it 'should return the right attributes' do
|
||||||
|
topic.update!(featured_link: featured_link)
|
||||||
|
|
||||||
|
topic_view = TopicView.new(topic.id, user)
|
||||||
|
json = described_class.new(topic_view, scope: Guardian.new(user), root: false).as_json
|
||||||
|
|
||||||
|
expect(json[:featured_link]).to eq(featured_link)
|
||||||
|
expect(json[:featured_link_root_domain]).to eq('discourse.org')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#suggested_topics' do
|
describe '#suggested_topics' do
|
||||||
let(:topic2) { Fabricate(:topic) }
|
let(:topic2) { Fabricate(:topic) }
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user