aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrinpatch <rinpatch@sdf.org>2019-01-15 21:17:56 +0300
committerrinpatch <rinpatch@sdf.org>2019-01-15 21:17:56 +0300
commit410fd9d774fb3437a38adfe405ff45d4950b51a9 (patch)
tree0b877febba39eea7d271c68f3b1c2563f011cdda
parentff6c9a5c961647b64c3c9fcc05932093595e9588 (diff)
downloadpleroma-410fd9d774fb3437a38adfe405ff45d4950b51a9.tar.gz
Attach attachments
-rw-r--r--lib/pleroma/activity.ex3
-rw-r--r--lib/pleroma/web/metadata/opengraph.ex36
2 files changed, 31 insertions, 8 deletions
diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex
index 47562306d..8fd0311d2 100644
--- a/lib/pleroma/activity.ex
+++ b/lib/pleroma/activity.ex
@@ -35,10 +35,11 @@ defmodule Pleroma.Activity do
)
)
end
-
+
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
diff --git a/lib/pleroma/web/metadata/opengraph.ex b/lib/pleroma/web/metadata/opengraph.ex
index 6d86c0ee6..2eac04ae7 100644
--- a/lib/pleroma/web/metadata/opengraph.ex
+++ b/lib/pleroma/web/metadata/opengraph.ex
@@ -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 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph 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 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph 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 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph 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 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph 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