From cbadf9d3333d3840d21cfcd3bd4f33d3b5b90445 Mon Sep 17 00:00:00 2001
From: rinpatch <rinpatch@sdf.org>
Date: Sat, 2 Feb 2019 11:38:37 +0300
Subject: [PATCH 1/4] Fix rich media relative path

---
 lib/pleroma/web/mastodon_api/views/status_view.ex | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index d5b7e68c7..826563f74 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -184,6 +184,11 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
     page_url = rich_media[:url] || page_url
     page_url_data = URI.parse(page_url)
+
+    image_url =
+      URI.merge(page_url_data, URI.parse(rich_media[:image]))
+      |> to_string
+
     site_name = rich_media[:site_name] || page_url_data.host
 
     %{
@@ -191,7 +196,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       provider_name: site_name,
       provider_url: page_url_data.scheme <> "://" <> page_url_data.host,
       url: page_url,
-      image: rich_media[:image] |> MediaProxy.url(),
+      image: image_url |> MediaProxy.url(),
       title: rich_media[:title],
       description: rich_media[:description],
       pleroma: %{

From e4d18f328b738a2c3a953e721f0d350cfba20089 Mon Sep 17 00:00:00 2001
From: rinpatch <rinpatch@sdf.org>
Date: Sat, 2 Feb 2019 11:53:46 +0300
Subject: [PATCH 2/4] merge only if page_url is an absolute path

---
 lib/pleroma/web/mastodon_api/views/status_view.ex | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index 826563f74..aa38784a6 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -186,8 +186,12 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     page_url_data = URI.parse(page_url)
 
     image_url =
-      URI.merge(page_url_data, URI.parse(rich_media[:image]))
-      |> to_string
+      if %URI{host: nil} = page_url_data do
+        rich_media[:image]
+      else
+        URI.merge(page_url_data, URI.parse(rich_media[:image]))
+        |> to_string
+      end
 
     site_name = rich_media[:site_name] || page_url_data.host
 

From 833404f0f549a5c2ac58d43239217648cc354a6a Mon Sep 17 00:00:00 2001
From: rinpatch <rinpatch@sdf.org>
Date: Sat, 2 Feb 2019 12:04:18 +0300
Subject: [PATCH 3/4] Use with instead of if in the card

---
 lib/pleroma/web/mastodon_api/views/status_view.ex | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index aa38784a6..c8fde93ba 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -186,11 +186,12 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     page_url_data = URI.parse(page_url)
 
     image_url =
-      if %URI{host: nil} = page_url_data do
+      with %URI{host: nil} <- page_url_data do
         rich_media[:image]
       else
-        URI.merge(page_url_data, URI.parse(rich_media[:image]))
-        |> to_string
+        _ ->
+          URI.merge(page_url_data, URI.parse(rich_media[:image]))
+          |> to_string
       end
 
     site_name = rich_media[:site_name] || page_url_data.host

From 68d461b3a9f3be58af85a7ae98ace2ebcbc616f2 Mon Sep 17 00:00:00 2001
From: rinpatch <rinpatch@sdf.org>
Date: Sat, 2 Feb 2019 12:24:24 +0300
Subject: [PATCH 4/4] Check if rich media uri is relative

---
 .../web/mastodon_api/views/status_view.ex     | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index c8fde93ba..d1b11d4f1 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -182,17 +182,18 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   end
 
   def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
-    page_url = rich_media[:url] || page_url
-    page_url_data = URI.parse(page_url)
+    page_url_data =
+      if rich_media[:url] != nil do
+        URI.merge(URI.parse(page_url), URI.parse(rich_media[:url]))
+      else
+        page_url
+      end
+
+    page_url = page_url_data |> to_string
 
     image_url =
-      with %URI{host: nil} <- page_url_data do
-        rich_media[:image]
-      else
-        _ ->
-          URI.merge(page_url_data, URI.parse(rich_media[:image]))
-          |> to_string
-      end
+      URI.merge(page_url_data, URI.parse(rich_media[:image]))
+      |> to_string
 
     site_name = rich_media[:site_name] || page_url_data.host