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

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

View file

@ -8,6 +8,7 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
@impl Provider @impl Provider
def build_tags(%{activity: activity, user: user}) do def build_tags(%{activity: activity, user: user}) do
with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do
attachments = build_attachments(activity)
[ [
{:meta, {:meta,
[ [
@ -16,11 +17,11 @@ def build_tags(%{activity: activity, user: user}) do
], []}, ], []},
{:meta, [property: "og:url", content: activity.data["id"]], []}, {:meta, [property: "og:url", content: activity.data["id"]], []},
{:meta, [property: "og:description", content: truncated_content], []}, {: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"], []} {: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
end end
@ -35,7 +36,7 @@ def build_tags(%{user: user}) do
], []}, ], []},
{:meta, [property: "og:url", content: User.profile_url(user)], []}, {:meta, [property: "og:url", content: User.profile_url(user)], []},
{:meta, [property: "og:description", content: truncated_bio], []}, {: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:width", content: 120], []},
{:meta, [property: "og:image:height", content: 120], []}, {:meta, [property: "og:image:height", content: 120], []},
{:meta, [property: "twitter:card", content: "summary"], []} {:meta, [property: "twitter:card", content: "summary"], []}
@ -43,6 +44,27 @@ def build_tags(%{user: user}) do
end end
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 defp scrub_html_and_truncate(content) do
content content
# html content comes from DB already encoded, decode first and scrub after # 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() |> Formatter.truncate()
end end
defp user_avatar_url(user) do defp attachment_url(url) do
User.avatar_url(user) |> MediaProxy.url() MediaProxy.url(url)
end end
defp user_name_string(user) do defp user_name_string(user) do