From 21afdf6d9966265de95df860d90d250c76bdbe08 Mon Sep 17 00:00:00 2001 From: raeno Date: Mon, 10 Dec 2018 16:25:33 +0400 Subject: [PATCH] Insert meta tags into static index.html on the fly for OStatus#notice --- lib/pleroma/web/oembed/oembed_controller.ex | 11 +++++++++++ lib/pleroma/web/ostatus/ostatus.ex | 15 +++++++++++++++ lib/pleroma/web/ostatus/ostatus_controller.ex | 14 +++++++++++--- lib/pleroma/web/router.ex | 10 ++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 lib/pleroma/web/oembed/oembed_controller.ex diff --git a/lib/pleroma/web/oembed/oembed_controller.ex b/lib/pleroma/web/oembed/oembed_controller.ex new file mode 100644 index 000000000..e9030049e --- /dev/null +++ b/lib/pleroma/web/oembed/oembed_controller.ex @@ -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 diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index c6440c20e..8ec92d5f1 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -26,6 +26,16 @@ def is_representable?(%Activity{data: data}) 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) + " Enum.join("\r\n") + end + def feed_path(user) do "#{user.ap_id}/feed.atom" end @@ -42,6 +52,11 @@ def remote_follow_path 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) diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index 9dfcf0f95..635189619 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -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 @@ def notice(conn, %{"id" => id}) 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 @@ def notice(conn, %{"id" => id}) 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, "", links) + conn + |> put_resp_content_type("text/html") + |> send_resp(200, response) + end + defp represent_activity( conn, "activity+json", diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 9c06fac4f..3239249f9 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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)