From 87b13c543039859007d9e2ba27c0236ab4092a9d Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Tue, 22 Dec 2020 03:54:57 +0100 Subject: [PATCH] Create Object.hashtags/1 wrapper --- lib/pleroma/object.ex | 4 +++ .../web/activity_pub/mrf/simple_policy.ex | 3 ++- .../web/activity_pub/transmogrifier.ex | 25 ++++++++----------- .../web/mastodon_api/views/status_view.ex | 2 +- .../templates/feed/feed/_activity.atom.eex | 2 +- .../web/templates/feed/feed/_activity.rss.eex | 2 +- .../feed/feed/_tag_activity.atom.eex | 2 +- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index 052ad413b..00d561d1b 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -344,4 +344,8 @@ defmodule Pleroma.Object do def self_replies(object, opts \\ []), do: replies(object, Keyword.put(opts, :self_only, true)) + + def hashtags(%{"hashtags" => hashtags}), do: hashtags || [] + def hashtags(%{"tag" => tags}), do: Enum.filter(tags, &is_bitstring(&1)) + def hashtags(_), do: [] end diff --git a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex index 2fa7b3194..8e0514dc8 100644 --- a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex @@ -8,6 +8,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do alias Pleroma.Config alias Pleroma.FollowingRelationship + alias Pleroma.Object alias Pleroma.User alias Pleroma.Web.ActivityPub.MRF @@ -76,7 +77,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do if MRF.subdomain_match?(media_nsfw, actor_host) do child_object = child_object - |> Map.put("hashtags", (child_object["hashtags"] || []) ++ ["nsfw"]) + |> Map.put("hashtags", Object.hashtags(child_object) ++ ["nsfw"]) |> Map.put("sensitive", true) Map.put(object, "object", child_object) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index d3dc637da..36ef6a454 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -863,23 +863,18 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do def maybe_fix_object_url(data), do: data def add_hashtags(object) do - tags = - ((object["hashtags"] || []) ++ (object["tag"] || [])) - |> Enum.map(fn - # Expand internal representation tags into AS2 tags. - tag when is_binary(tag) -> - %{ - "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", - "name" => "##{tag}", - "type" => "Hashtag" - } - - # Do not process tags which are already AS2 tag objects. - tag when is_map(tag) -> - tag + hashtags = + object + |> Object.hashtags() + |> Enum.map(fn tag -> + %{ + "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", + "name" => "##{tag}", + "type" => "Hashtag" + } end) - Map.put(object, "tag", tags) + Map.put(object, "tag", hashtags ++ (object["tag"] || [])) end # TODO These should be added on our side on insertion, it doesn't make much diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 6fc6272c2..b39f05d13 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -347,7 +347,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do media_attachments: attachments, poll: render(PollView, "show.json", object: object, for: opts[:for]), mentions: mentions, - tags: build_tags(object.data["hashtags"] || tags), + tags: build_tags(Object.hashtags(object.data)), application: %{ name: "Web", website: nil diff --git a/lib/pleroma/web/templates/feed/feed/_activity.atom.eex b/lib/pleroma/web/templates/feed/feed/_activity.atom.eex index 12a9545f3..cb18abb5d 100644 --- a/lib/pleroma/web/templates/feed/feed/_activity.atom.eex +++ b/lib/pleroma/web/templates/feed/feed/_activity.atom.eex @@ -22,7 +22,7 @@ <% end %> - <%= for hashtag <- @data["hashtags"] || [] do %> + <%= for hashtag <- Object.hashtags(@data) do %> <% end %> diff --git a/lib/pleroma/web/templates/feed/feed/_activity.rss.eex b/lib/pleroma/web/templates/feed/feed/_activity.rss.eex index 00872b4b7..6ef24d0ef 100644 --- a/lib/pleroma/web/templates/feed/feed/_activity.rss.eex +++ b/lib/pleroma/web/templates/feed/feed/_activity.rss.eex @@ -21,7 +21,7 @@ <%= @data["external_url"] %> <% end %> - <%= for hashtag <- @data["hashtags"] || [] do %> + <%= for hashtag <- Object.hashtags(@data) do %> <% end %> diff --git a/lib/pleroma/web/templates/feed/feed/_tag_activity.atom.eex b/lib/pleroma/web/templates/feed/feed/_tag_activity.atom.eex index 1377a6bbc..098e28205 100644 --- a/lib/pleroma/web/templates/feed/feed/_tag_activity.atom.eex +++ b/lib/pleroma/web/templates/feed/feed/_tag_activity.atom.eex @@ -41,7 +41,7 @@ <% end %> <% end %> - <%= for hashtag <- @data["hashtags"] || [] do %> + <%= for hashtag <- Object.hashtags(@data) do %> <% end %>