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

View 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 @@ 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, "<!--server-generated-meta-->", links)
conn
|> put_resp_content_type("text/html")
|> send_resp(200, response)
end
defp represent_activity(
conn,
"activity+json",

View 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)