Insert meta tags into static index.html on the fly for OStatus#notice

This commit is contained in:
raeno 2018-12-10 16:25:33 +04:00
parent b19ee62252
commit 21afdf6d99
4 changed files with 47 additions and 3 deletions

View file

@ -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

View file

@ -26,6 +26,16 @@ def is_representable?(%Activity{data: data}) do
end end
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 def feed_path(user) do
"#{user.ap_id}/feed.atom" "#{user.ap_id}/feed.atom"
end end
@ -42,6 +52,11 @@ def remote_follow_path do
"#{Web.base_url()}/ostatus_subscribe?acct={uri}" "#{Web.base_url()}/ostatus_subscribe?acct={uri}"
end 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 def handle_incoming(xml_string) do
with doc when doc != :error <- parse_document(xml_string) do with doc when doc != :error <- parse_document(xml_string) do
entries = :xmerl_xpath.string('//entry', doc) entries = :xmerl_xpath.string('//entry', doc)

View file

@ -9,6 +9,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do
alias Pleroma.Web.ActivityPub.ObjectView alias Pleroma.Web.ActivityPub.ObjectView
alias Pleroma.Web.ActivityPub.ActivityPubController alias Pleroma.Web.ActivityPub.ActivityPubController
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Router.Helpers, as: Routes
plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming]) plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
action_fallback(:errors) 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 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
case format = get_format(conn) do case format = get_format(conn) do
"html" -> "html" ->
conn serve_static_with_meta(conn, activity)
|> put_resp_content_type("text/html")
|> send_file(200, Application.app_dir(:pleroma, "priv/static/index.html"))
_ -> _ ->
represent_activity(conn, format, activity, user) represent_activity(conn, format, activity, user)
@ -153,6 +152,15 @@ def notice(conn, %{"id" => id}) do
end end
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( defp represent_activity(
conn, conn,
"activity+json", "activity+json",

View file

@ -354,6 +354,10 @@ defmodule Pleroma.Web.Router do
plug(:accepts, ["xml", "atom", "html", "activity+json"]) plug(:accepts, ["xml", "atom", "html", "activity+json"])
end end
pipeline :oembed do
plug(:accepts, ["json", "xml"])
end
scope "/", Pleroma.Web do scope "/", Pleroma.Web do
pipe_through(:ostatus) pipe_through(:ostatus)
@ -369,6 +373,12 @@ defmodule Pleroma.Web.Router do
post("/push/subscriptions/:id", Websub.WebsubController, :websub_incoming) post("/push/subscriptions/:id", Websub.WebsubController, :websub_incoming)
end end
scope "/", Pleroma.Web do
pipe_through(:oembed)
get("/oembed", OEmbed.OEmbedController, :url)
end
pipeline :activitypub do pipeline :activitypub do
plug(:accepts, ["activity+json"]) plug(:accepts, ["activity+json"])
plug(Pleroma.Web.Plugs.HTTPSignaturePlug) plug(Pleroma.Web.Plugs.HTTPSignaturePlug)