FIX: Duplicate link shouldn't happen on edit

This commit is contained in:
Robin Ward 2016-06-08 17:20:32 -04:00
parent 3e3538d603
commit 9a81115c1c
4 changed files with 18 additions and 11 deletions

View File

@ -111,17 +111,19 @@ export default Ember.Controller.extend({
}, },
afterRefresh($preview) { afterRefresh($preview) {
const linkLookup = this.get('linkLookup'); const linkLookup = this.get('linkLookup');
if (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) => { $links.each((idx, l) => {
const href = $(l).prop('href'); const href = $(l).prop('href');
if (href && href.length) { if (href && href.length) {
const [warn, info] = linkLookup.check(href); const [warn, info] = linkLookup.check(post, href);
if (warn) { if (warn) {
console.log(info);
const body = I18n.t('composer.duplicate_link', { const body = I18n.t('composer.duplicate_link', {
domain: info.domain, domain: info.domain,
username: info.username, username: info.username,
@ -482,7 +484,7 @@ export default Ember.Controller.extend({
// Given a potential instance and options, set the model for this composer. // Given a potential instance and options, set the model for this composer.
_setModel(composerModel, opts) { _setModel(composerModel, opts) {
this.set('linkList', null); this.set('linkLookup', null);
if (opts.draft) { if (opts.draft) {
composerModel = loadDraft(this.store, opts); composerModel = loadDraft(this.store, opts);

View File

@ -1,24 +1,29 @@
const _warned = {}; const _warned = {};
const NO_RESULT = [false, null];
export default class LinkLookup { export default class LinkLookup {
constructor(links) { constructor(links) {
this._links = links; this._links = links;
} }
check(href) { check(post, href) {
if (_warned[href]) { return [false, null]; } if (_warned[href]) { return NO_RESULT; }
const normalized = href.replace(/^https?:\/\//, ''); const normalized = href.replace(/^https?:\/\//, '').replace(/\/$/, '');
if (_warned[normalized]) { return [false, null]; } if (_warned[normalized]) { return NO_RESULT; }
const linkInfo = this._links[normalized]; const linkInfo = this._links[normalized];
if (linkInfo) { if (linkInfo) {
// Skip edits to the same URL
if (post && post.get('url') === linkInfo.post_url) { return NO_RESULT; }
_warned[href] = true; _warned[href] = true;
_warned[normalized] = true; _warned[normalized] = true;
return [true, linkInfo]; return [true, linkInfo];
} }
return [false, null]; return NO_RESULT;
} }
}; };

View File

@ -8,7 +8,7 @@ class ComposerMessagesController < ApplicationController
finder = ComposerMessagesFinder.new(current_user, params.slice(:composer_action, :topic_id, :post_id)) finder = ComposerMessagesFinder.new(current_user, params.slice(:composer_action, :topic_id, :post_id))
json = { composer_messages: [finder.find].compact } 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 topic = Topic.where(id: params[:topic_id]).first
if guardian.can_see?(topic) if guardian.can_see?(topic)
json[:extras] = {duplicate_lookup: TopicLink.duplicate_lookup(topic)} json[:extras] = {duplicate_lookup: TopicLink.duplicate_lookup(topic)}

View File

@ -229,7 +229,7 @@ class TopicLink < ActiveRecord::Base
lookup = {} lookup = {}
results.each do |tl| results.each do |tl|
normalized = tl.url.downcase.sub(/^https?:\/\//, '') normalized = tl.url.downcase.sub(/^https?:\/\//, '').sub(/\/$/, '')
lookup[normalized] = { domain: tl.domain, lookup[normalized] = { domain: tl.domain,
username: tl.post.user.username_lower, username: tl.post.user.username_lower,
post_url: tl.post.url, post_url: tl.post.url,