Insert meta tags into static index.html on the fly for OStatus#notice
authorraeno <just.raeno@gmail.com>
Mon, 10 Dec 2018 12:25:33 +0000 (16:25 +0400)
committerraeno <just.raeno@gmail.com>
Thu, 13 Dec 2018 21:17:53 +0000 (22:17 +0100)
lib/pleroma/web/oembed/oembed_controller.ex [new file with mode: 0644]
lib/pleroma/web/ostatus/ostatus.ex
lib/pleroma/web/ostatus/ostatus_controller.ex
lib/pleroma/web/router.ex

diff --git a/lib/pleroma/web/oembed/oembed_controller.ex b/lib/pleroma/web/oembed/oembed_controller.ex
new file mode 100644 (file)
index 0000000..e903004
--- /dev/null
@@ -0,0 +1,11 @@
+defmodule Pleroma.Web.OEmbed.OEmbedController do
+  use Pleroma.Web, :controller
+
+  alias Pleroma.Repo
+
+  def url(conn, %{ "url" => uri} ) do
+    conn
+    |> put_resp_content_type("application/json")
+    |> json(%{ status: "success"} )
+  end
+end
\ No newline at end of file
index c6440c20e9daa1d1248494fb201fe7aed7785b6a..8ec92d5f1699c97105fa0b698c3289c44ec312bf 100644 (file)
@@ -26,6 +26,16 @@ defmodule Pleroma.Web.OStatus do
     end
   end
 
+  def metadata(url), do: oembed_links(url)
+
+  def oembed_links(url) do
+    Enum.map(["xml", "json"], fn format ->
+      href = oembed_path(url, format)
+      "<link rel=\"alternate\" type=\"application/#{format}+oembed\" href=\"#{href}\""
+    end)
+    |> Enum.join("\r\n")
+  end
+
   def feed_path(user) do
     "#{user.ap_id}/feed.atom"
   end
@@ -42,6 +52,11 @@ defmodule Pleroma.Web.OStatus do
     "#{Web.base_url()}/ostatus_subscribe?acct={uri}"
   end
 
+  def oembed_path(url, format) do
+    query = URI.encode_query(%{url: url, format: format})
+    "#{Web.base_url()}/oembed?#{query}"
+  end
+
   def handle_incoming(xml_string) do
     with doc when doc != :error <- parse_document(xml_string) do
       entries = :xmerl_xpath.string('//entry', doc)
index 9dfcf0f95a9f94a5d1bf9ff1d8503acd821b6fbf..6351896190eae3ed3346e57bca74d45c1c75a4d3 100644 (file)
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do
   alias Pleroma.Web.ActivityPub.ObjectView
   alias Pleroma.Web.ActivityPub.ActivityPubController
   alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Router.Helpers, as: Routes
 
   plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
   action_fallback(:errors)
@@ -134,9 +135,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do
          %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
       case format = get_format(conn) do
         "html" ->
-          conn
-          |> put_resp_content_type("text/html")
-          |> send_file(200, Application.app_dir(:pleroma, "priv/static/index.html"))
+          serve_static_with_meta(conn, activity)
 
         _ ->
           represent_activity(conn, format, activity, user)
@@ -153,6 +152,15 @@ defmodule Pleroma.Web.OStatus.OStatusController do
     end
   end
 
+  defp serve_static_with_meta(conn, activity) do
+    {:ok, index_content } = File.read(Application.app_dir(:pleroma, "priv/static/index.html"))
+    links = OStatus.metadata(request_url(conn))
+    response = String.replace(index_content, "<!--server-generated-meta-->", links)
+    conn
+    |> put_resp_content_type("text/html")
+    |> send_resp(200, response)
+  end
+
   defp represent_activity(
          conn,
          "activity+json",
index 9c06fac4f30bf9a5d3a53d207037f0f83e78c629..3239249f913bbb3cd5a9bf79ccfe8a68fc77717e 100644 (file)
@@ -354,6 +354,10 @@ defmodule Pleroma.Web.Router do
     plug(:accepts, ["xml", "atom", "html", "activity+json"])
   end
 
+  pipeline :oembed do
+    plug(:accepts, ["json", "xml"])
+  end
+
   scope "/", Pleroma.Web do
     pipe_through(:ostatus)
 
@@ -369,6 +373,12 @@ defmodule Pleroma.Web.Router do
     post("/push/subscriptions/:id", Websub.WebsubController, :websub_incoming)
   end
 
+  scope "/", Pleroma.Web do
+    pipe_through(:oembed)
+
+    get("/oembed", OEmbed.OEmbedController, :url)
+  end
+
   pipeline :activitypub do
     plug(:accepts, ["activity+json"])
     plug(Pleroma.Web.Plugs.HTTPSignaturePlug)