From 9a81115c1c3f6e9400f01b242fcb8219bc27a042 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 8 Jun 2016 17:20:32 -0400 Subject: [PATCH] FIX: Duplicate link shouldn't happen on edit --- .../discourse/controllers/composer.js.es6 | 10 ++++++---- .../javascripts/discourse/lib/link-lookup.js.es6 | 15 ++++++++++----- app/controllers/composer_messages_controller.rb | 2 +- app/models/topic_link.rb | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6 index 9631040a3f4..c661294944f 100644 --- a/app/assets/javascripts/discourse/controllers/composer.js.es6 +++ b/app/assets/javascripts/discourse/controllers/composer.js.es6 @@ -111,17 +111,19 @@ export default Ember.Controller.extend({ }, afterRefresh($preview) { + const linkLookup = this.get('linkLookup'); if (linkLookup) { - const $links = $('a[href]', $preview); + const post = this.get('model.post'); + if (post && post.get('user_id') !== this.currentUser.id) { return; } + const $links = $('a[href]', $preview); $links.each((idx, l) => { const href = $(l).prop('href'); if (href && href.length) { - const [warn, info] = linkLookup.check(href); + const [warn, info] = linkLookup.check(post, href); if (warn) { - console.log(info); const body = I18n.t('composer.duplicate_link', { domain: info.domain, username: info.username, @@ -482,7 +484,7 @@ export default Ember.Controller.extend({ // Given a potential instance and options, set the model for this composer. _setModel(composerModel, opts) { - this.set('linkList', null); + this.set('linkLookup', null); if (opts.draft) { composerModel = loadDraft(this.store, opts); diff --git a/app/assets/javascripts/discourse/lib/link-lookup.js.es6 b/app/assets/javascripts/discourse/lib/link-lookup.js.es6 index 04451ffc82b..27f724762f8 100644 --- a/app/assets/javascripts/discourse/lib/link-lookup.js.es6 +++ b/app/assets/javascripts/discourse/lib/link-lookup.js.es6 @@ -1,24 +1,29 @@ const _warned = {}; +const NO_RESULT = [false, null]; + export default class LinkLookup { constructor(links) { this._links = links; } - check(href) { - if (_warned[href]) { return [false, null]; } + check(post, href) { + if (_warned[href]) { return NO_RESULT; } - const normalized = href.replace(/^https?:\/\//, ''); - if (_warned[normalized]) { return [false, null]; } + const normalized = href.replace(/^https?:\/\//, '').replace(/\/$/, ''); + if (_warned[normalized]) { return NO_RESULT; } const linkInfo = this._links[normalized]; if (linkInfo) { + // Skip edits to the same URL + if (post && post.get('url') === linkInfo.post_url) { return NO_RESULT; } + _warned[href] = true; _warned[normalized] = true; return [true, linkInfo]; } - return [false, null]; + return NO_RESULT; } }; diff --git a/app/controllers/composer_messages_controller.rb b/app/controllers/composer_messages_controller.rb index 57bebbf8d91..ff1a51d2bd2 100644 --- a/app/controllers/composer_messages_controller.rb +++ b/app/controllers/composer_messages_controller.rb @@ -8,7 +8,7 @@ class ComposerMessagesController < ApplicationController finder = ComposerMessagesFinder.new(current_user, params.slice(:composer_action, :topic_id, :post_id)) json = { composer_messages: [finder.find].compact } - if params[:composer_action] == "reply" && params[:topic_id].present? + if params[:topic_id].present? topic = Topic.where(id: params[:topic_id]).first if guardian.can_see?(topic) json[:extras] = {duplicate_lookup: TopicLink.duplicate_lookup(topic)} diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb index 44d6a41ac13..227b8937285 100644 --- a/app/models/topic_link.rb +++ b/app/models/topic_link.rb @@ -229,7 +229,7 @@ class TopicLink < ActiveRecord::Base lookup = {} results.each do |tl| - normalized = tl.url.downcase.sub(/^https?:\/\//, '') + normalized = tl.url.downcase.sub(/^https?:\/\//, '').sub(/\/$/, '') lookup[normalized] = { domain: tl.domain, username: tl.post.user.username_lower, post_url: tl.post.url,