From 2169b1ecbc9d285f52f7be95e021e334a70c1881 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 8 Jun 2016 12:35:11 -0400 Subject: [PATCH] FIX: Duplicate link message should link to the post --- .../discourse/controllers/composer.js.es6 | 5 +++-- app/models/topic_link.rb | 21 ++++++++----------- config/locales/client.en.yml | 2 +- spec/models/topic_link_spec.rb | 1 + 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6 index cea29105395..9631040a3f4 100644 --- a/app/assets/javascripts/discourse/controllers/composer.js.es6 +++ b/app/assets/javascripts/discourse/controllers/composer.js.es6 @@ -121,11 +121,12 @@ export default Ember.Controller.extend({ const [warn, info] = linkLookup.check(href); if (warn) { + console.log(info); const body = I18n.t('composer.duplicate_link', { domain: info.domain, username: info.username, - ago: relativeAge(moment(info.posted_at).toDate(), { format: 'medium' }), - href + post_url: info.post_url, + ago: relativeAge(moment(info.posted_at).toDate(), { format: 'medium' }) }); this.appEvents.trigger('composer-messages:create', { extraClass: 'custom-body', diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb index c1e54cd4c9e..0f19ba7d3fa 100644 --- a/app/models/topic_link.rb +++ b/app/models/topic_link.rb @@ -216,21 +216,18 @@ class TopicLink < ActiveRecord::Base end def self.duplicate_lookup(topic) - builder = SqlBuilder.new("SELECT tl.url, tl.domain, u.username_lower, p.created_at - FROM topic_links AS tl - INNER JOIN posts AS p ON p.id = tl.post_id - INNER JOIN users AS u ON p.user_id = u.id - /*where*/ - ORDER BY p.created_at DESC - LIMIT 200") - builder.where('tl.topic_id = :topic_id', topic_id: topic.id) + results = TopicLink + .includes(:post => :user) + .where(topic_id: topic.id).limit(200) lookup = {} - - builder.exec.to_a.each do |row| - normalized = row['url'].downcase.sub(/^https?:\/\//, '') - lookup[normalized] = {domain: row['domain'], username: row['username_lower'], posted_at: row['created_at']} + results.each do |tl| + normalized = tl.url.downcase.sub(/^https?:\/\//, '') + lookup[normalized] = { domain: tl.domain, + username: tl.post.user.username_lower, + post_url: tl.post.url, + posted_at: tl.post.created_at } end lookup diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index ce457927e74..a7d58b669dc 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -989,7 +989,7 @@ en: drafts_offline: "drafts offline" group_mentioned: "By mentioning {{group}}, you are about to notify {{count}} people." - duplicate_link: "It looks like your link to {{domain}} was already posted in the topic by @{{username}} in an earlier reply on {{ago}}" + duplicate_link: "It looks like your link to {{domain}} was already posted in the topic by @{{username}} in an earlier reply on {{ago}}" error: title_missing: "Title is required" diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb index 7a7109d58cd..bb5afa16c75 100644 --- a/spec/models/topic_link_spec.rb +++ b/spec/models/topic_link_spec.rb @@ -286,6 +286,7 @@ http://b.com/#{'a'*500} expect(ch[:domain]).to eq('www.codinghorror.com') expect(ch[:username]).to eq(post.username) expect(ch[:posted_at]).to be_present + expect(ch[:post_url]).to be_present end it 'has the correct results' do