Attach attachments

This commit is contained in:
rinpatch 2019-01-15 21:17:56 +03:00
parent ff6c9a5c96
commit 410fd9d774
2 changed files with 31 additions and 8 deletions

View File

@ -39,6 +39,7 @@ def get_by_ap_id(ap_id) do
def get_by_id(id) do
Repo.get(Activity, id)
end
# TODO:
# Go through these and fix them everywhere.
# Wrong name, only returns create activities

View File

@ -8,6 +8,7 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
@impl Provider
def build_tags(%{activity: activity, user: user}) do
with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do
attachments = build_attachments(activity)
[
{:meta,
[
@ -16,11 +17,11 @@ def build_tags(%{activity: activity, user: user}) do
], []},
{: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"], []}
]
] ++ if attachments == [] do [
{:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
{:meta, [property: "og:image:width", content: 120], []},
{:meta, [property: "og:image:height", content: 120], []} ] else attachments end
end
end
@ -35,7 +36,7 @@ def build_tags(%{user: user}) do
], []},
{: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", content: attachment_url(User.avatar_url(user))], []},
{:meta, [property: "og:image:width", content: 120], []},
{:meta, [property: "og:image:height", content: 120], []},
{:meta, [property: "twitter:card", content: "summary"], []}
@ -43,6 +44,27 @@ def build_tags(%{user: user}) do
end
end
defp build_attachments(activity) do
Enum.reduce(activity.data["object"]["attachment"], [], fn attachment, acc ->
rendered_tags =
Enum.map(attachment["url"], fn url ->
media_type =
Enum.find(["image", "audio", "video"], fn media_type ->
String.starts_with?(url["mediaType"], media_type)
end)
if media_type do
{:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
else
nil
end
end)
Enum.reject(rendered_tags, &is_nil/1)
acc ++ rendered_tags
end)
end
defp scrub_html_and_truncate(content) do
content
# html content comes from DB already encoded, decode first and scrub after
@ -52,8 +74,8 @@ defp scrub_html_and_truncate(content) do
|> Formatter.truncate()
end
defp user_avatar_url(user) do
User.avatar_url(user) |> MediaProxy.url()
defp attachment_url(url) do
MediaProxy.url(url)
end
defp user_name_string(user) do