Refactor ostatus_controller, extract metatags redirection to Redirector itself. Set 'html' as default type for ostatus links
This commit is contained in:
parent
49c4f7d604
commit
018516d3f3
3 changed files with 77 additions and 45 deletions
|
@ -4,10 +4,10 @@ defmodule Pleroma.Web.Metadata do
|
|||
alias Pleroma.{User, Activity}
|
||||
alias Pleroma.Web.MediaProxy
|
||||
|
||||
def build_tags(activity, user, url) do
|
||||
def build_tags(request_url, params) do
|
||||
Enum.concat([
|
||||
if(meta_enabled?(:opengraph), do: opengraph_tags(activity, user), else: []),
|
||||
if(meta_enabled?(:oembed), do: oembed_links(url), else: [])
|
||||
if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: []),
|
||||
if(meta_enabled?(:oembed), do: oembed_links(request_url), else: [])
|
||||
])
|
||||
|> Enum.map(&to_tag/1)
|
||||
|> Enum.map(&HTML.safe_to_string/1)
|
||||
|
@ -19,6 +19,52 @@ def meta_enabled?(type) do
|
|||
Keyword.get(config, type, false)
|
||||
end
|
||||
|
||||
# opengraph for single status
|
||||
defp opengraph_tags(%{activity: activity, user: user}) do
|
||||
with truncated_content = Formatter.truncate(activity.data["object"]["content"]) do
|
||||
[
|
||||
{:meta,
|
||||
[
|
||||
property: "og:title",
|
||||
content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) post ##{activity.id}"
|
||||
], []},
|
||||
{:meta, [property: "og:url", content: activity.data["id"]], []},
|
||||
{:meta, [property: "og:description", content: truncated_content],
|
||||
[]},
|
||||
{:meta, [property: "og:image", content: user_avatar_url(user)], []},
|
||||
{:meta, [property: "og:image:width", content: 120], []},
|
||||
{:meta, [property: "og:image:height", content: 120], []},
|
||||
{:meta, [property: "twitter:card", content: "summary"], []}
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
# opengraph for user card
|
||||
defp opengraph_tags(%{user: user}) do
|
||||
with truncated_bio = Formatter.truncate(user.bio) do
|
||||
[
|
||||
{:meta,
|
||||
[
|
||||
property: "og:title",
|
||||
content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile"
|
||||
], []},
|
||||
{:meta, [property: "og:url", content: User.profile_url(user)], []},
|
||||
{:meta, [property: "og:description", content: truncated_bio], []},
|
||||
{:meta, [property: "og:image", content: user_avatar_url(user)], []},
|
||||
{:meta, [property: "og:image:width", content: 120], []},
|
||||
{:meta, [property: "og:image:height", content: 120], []},
|
||||
{:meta, [property: "twitter:card", content: "summary"], []}
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
defp oembed_links(url) do
|
||||
Enum.map(["xml", "json"], fn format ->
|
||||
href = HTML.raw(oembed_path(url, format))
|
||||
{ :link, [ type: ["application/#{format}+oembed"], href: href, rel: 'alternate'], [] }
|
||||
end)
|
||||
end
|
||||
|
||||
def to_tag(data) do
|
||||
with {name, attrs, _content = []} <- data do
|
||||
HTML.Tag.tag(name, attrs)
|
||||
|
@ -31,36 +77,16 @@ def to_tag(data) do
|
|||
end
|
||||
end
|
||||
|
||||
defp oembed_links(url) do
|
||||
Enum.map(["xml", "json"], fn format ->
|
||||
href = HTML.raw(oembed_path(url, format))
|
||||
{ :link, [ type: ["application/#{format}+oembed"], href: href, rel: 'alternate'], [] }
|
||||
end)
|
||||
end
|
||||
|
||||
defp opengraph_tags(activity, user) do
|
||||
with image = User.avatar_url(user) |> MediaProxy.url(),
|
||||
truncated_content = Formatter.truncate(activity.data["object"]["content"]),
|
||||
domain = Pleroma.Config.get([:instance, :domain], "UNKNOWN_DOMAIN") do
|
||||
[
|
||||
{:meta,
|
||||
[
|
||||
property: "og:title",
|
||||
content: "#{user.name} (@#{user.nickname}@#{domain}) post ##{activity.id}"
|
||||
], []},
|
||||
{:meta, [property: "og:url", content: activity.data["id"]], []},
|
||||
{:meta, [property: "og:description", content: truncated_content],
|
||||
[]},
|
||||
{:meta, [property: "og:image", content: image], []},
|
||||
{:meta, [property: "og:image:width", content: 120], []},
|
||||
{:meta, [property: "og:image:height", content: 120], []},
|
||||
{:meta, [property: "twitter:card", content: "summary"], []}
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
defp oembed_path(url, format) do
|
||||
query = URI.encode_query(%{url: url, format: format})
|
||||
"#{Web.base_url()}/oembed?#{query}"
|
||||
end
|
||||
|
||||
defp user_avatar_url(user) do
|
||||
User.avatar_url(user) |> MediaProxy.url()
|
||||
end
|
||||
|
||||
def pleroma_domain do
|
||||
Pleroma.Config.get([:instance, :domain], "UNKNOWN_DOMAIN")
|
||||
end
|
||||
end
|
|
@ -4,7 +4,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do
|
|||
alias Pleroma.{User, Activity, Object}
|
||||
alias Pleroma.Web.OStatus.{FeedRepresenter, ActivityRepresenter}
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Web.{OStatus, Federator, Metadata}
|
||||
alias Pleroma.Web.{OStatus, Federator}
|
||||
alias Pleroma.Web.XML
|
||||
alias Pleroma.Web.ActivityPub.ObjectView
|
||||
alias Pleroma.Web.ActivityPub.ActivityPubController
|
||||
|
@ -14,9 +14,13 @@ defmodule Pleroma.Web.OStatus.OStatusController do
|
|||
action_fallback(:errors)
|
||||
|
||||
def feed_redirect(conn, %{"nickname" => nickname}) do
|
||||
case get_format(conn) do
|
||||
format = get_format(conn)
|
||||
IO.puts(format)
|
||||
case format do
|
||||
"html" ->
|
||||
Fallback.RedirectController.redirector(conn, nil)
|
||||
with %User{} = user <- User.get_cached_by_nickname(nickname) do
|
||||
Fallback.RedirectController.redirector_with_meta(conn, %{user: user})
|
||||
end
|
||||
|
||||
"activity+json" ->
|
||||
ActivityPubController.call(conn, :user)
|
||||
|
@ -134,7 +138,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" ->
|
||||
serve_static_with_meta(conn, activity, user)
|
||||
Fallback.RedirectController.redirector_with_meta(conn, %{activity: activity, user: user})
|
||||
|
||||
_ ->
|
||||
represent_activity(conn, format, activity, user)
|
||||
|
@ -151,15 +155,6 @@ def notice(conn, %{"id" => id}) do
|
|||
end
|
||||
end
|
||||
|
||||
defp serve_static_with_meta(conn, activity, user) do
|
||||
{:ok, index_content } = File.read(Application.app_dir(:pleroma, "priv/static/index.html"))
|
||||
tags = Metadata.build_tags(activity, user, request_url(conn))
|
||||
response = String.replace(index_content, "<!--server-generated-meta-->", tags)
|
||||
conn
|
||||
|> put_resp_content_type("text/html")
|
||||
|> send_resp(200, response)
|
||||
end
|
||||
|
||||
defp represent_activity(
|
||||
conn,
|
||||
"activity+json",
|
||||
|
|
|
@ -351,7 +351,7 @@ defmodule Pleroma.Web.Router do
|
|||
end
|
||||
|
||||
pipeline :ostatus do
|
||||
plug(:accepts, ["xml", "atom", "html", "activity+json"])
|
||||
plug(:accepts, ["html", "xml", "atom", "activity+json"])
|
||||
end
|
||||
|
||||
pipeline :oembed do
|
||||
|
@ -444,6 +444,7 @@ defmodule Pleroma.Web.Router do
|
|||
|
||||
defmodule Fallback.RedirectController do
|
||||
use Pleroma.Web, :controller
|
||||
alias Pleroma.Web.Metadata
|
||||
|
||||
def redirector(conn, _params) do
|
||||
conn
|
||||
|
@ -451,6 +452,16 @@ def redirector(conn, _params) do
|
|||
|> send_file(200, Application.app_dir(:pleroma, "priv/static/index.html"))
|
||||
end
|
||||
|
||||
def redirector_with_meta(conn, params) do
|
||||
{:ok, index_content } = File.read(Application.app_dir(:pleroma, "priv/static/index.html"))
|
||||
tags = Metadata.build_tags(request_url(conn), params)
|
||||
response = String.replace(index_content, "<!--server-generated-meta-->", tags)
|
||||
|
||||
conn
|
||||
|> put_resp_content_type("text/html")
|
||||
|> send_resp(200, response)
|
||||
end
|
||||
|
||||
def registration_page(conn, params) do
|
||||
redirector(conn, params)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue