From faaada6e8a6c704b0549549aa3e9795d1fd558f4 Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Wed, 19 Mar 2014 16:33:21 -0400
Subject: [PATCH] Consider URLs the same even with a trailing slash

---
 app/models/topic_embed.rb | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb
index 2be0005f8b3..60bc4de0fa1 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -6,10 +6,16 @@ class TopicEmbed < ActiveRecord::Base
   validates_presence_of :embed_url
   validates_presence_of :content_sha1
 
+  def self.normalize_url(url)
+    url.sub(/\/$/, '')
+  end
+
   # Import an article from a source (RSS/Atom/Other)
   def self.import(user, url, title, contents)
     return unless url =~ /^https?\:\/\//
 
+    url = normalize_url(url)
+
     if SiteSetting.embed_truncate
       contents = first_paragraph_from(contents)
     end
@@ -53,6 +59,7 @@ class TopicEmbed < ActiveRecord::Base
   def self.import_remote(user, url, opts=nil)
     require 'ruby-readability'
 
+    url = normalize_url(url)
     opts = opts || {}
     doc = Readability::Document.new(open(url).read,
                                         tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol],
@@ -63,6 +70,7 @@ class TopicEmbed < ActiveRecord::Base
 
   # Convert any relative URLs to absolute. RSS is annoying for this.
   def self.absolutize_urls(url, contents)
+    url = normalize_url(url)
     uri = URI(url)
     prefix = "#{uri.scheme}://#{uri.host}"
     prefix << ":#{uri.port}" if uri.port != 80 && uri.port != 443
@@ -84,6 +92,7 @@ class TopicEmbed < ActiveRecord::Base
   end
 
   def self.topic_id_for_embed(embed_url)
+    embed_url = normalize_url(embed_url)
     TopicEmbed.where(embed_url: embed_url).pluck(:topic_id).first
   end