From 1c00933953a0c459dd2ca7f8f311af09239ef48b Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Fri, 13 Nov 2020 16:05:23 +0300 Subject: [PATCH] added `User.tag_names` --- lib/pleroma/user.ex | 7 +++++++ lib/pleroma/web/activity_pub/mrf/tag_policy.ex | 12 +++--------- lib/pleroma/web/admin_api/views/account_view.ex | 3 +-- lib/pleroma/web/mastodon_api/views/account_view.ex | 4 +--- test/pleroma/user_test.exs | 14 ++++++++++++++ 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 9396815e9..690d751fd 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -2006,6 +2006,13 @@ defmodule Pleroma.User do def parse_bio(_, _), do: "" + def tag_names(%__MODULE__{} = user) do + {:ok, tags} = Repo.get_assoc(user, :tags) + Enum.map(tags, & &1.name) + end + + def tag_names(_), do: [] + def tag(user_identifiers, tags) when is_list(user_identifiers) do Repo.transaction(fn -> for user_identifier <- user_identifiers, do: tag(user_identifier, tags) diff --git a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex index dc445add2..e4d174fa9 100644 --- a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex @@ -32,13 +32,6 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do ] end - defp get_tags(%User{} = user) do - {:ok, tags} = Pleroma.Repo.get_assoc(user, :tags) - Enum.map(tags, & &1.name) - end - - defp get_tags(_), do: [] - defp process_tag( "mrf_tag:media-force-nsfw", %{ @@ -151,8 +144,9 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do defp process_tag(_, message), do: {:ok, message} def filter_message(actor, message) do - User.get_cached_by_ap_id(actor) - |> get_tags() + actor + |> User.get_cached_by_ap_id() + |> User.tag_names() |> Enum.reduce({:ok, message}, fn tag, {:ok, message} -> process_tag(tag, message) diff --git a/lib/pleroma/web/admin_api/views/account_view.ex b/lib/pleroma/web/admin_api/views/account_view.ex index ac8c952d9..d90b682e0 100644 --- a/lib/pleroma/web/admin_api/views/account_view.ex +++ b/lib/pleroma/web/admin_api/views/account_view.ex @@ -66,7 +66,6 @@ defmodule Pleroma.Web.AdminAPI.AccountView do avatar = User.avatar_url(user) |> MediaProxy.url() display_name = Pleroma.HTML.strip_tags(user.name || user.nickname) user = User.sanitize_html(user, FastSanitize.Sanitizer.StripTags) - {:ok, user_tags} = Pleroma.Repo.get_assoc(user, :tags) %{ "id" => user.id, @@ -77,7 +76,7 @@ defmodule Pleroma.Web.AdminAPI.AccountView do "is_active" => user.is_active, "local" => user.local, "roles" => roles(user), - "tags" => Enum.map(user_tags, & &1.name), + "tags" => User.tag_names(user), "is_confirmed" => user.is_confirmed, "is_approved" => user.is_approved, "url" => user.uri || user.ap_id, diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 7e5a5d732..ecb6d5d22 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -233,8 +233,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do nil end - {:ok, user_tags} = Pleroma.Repo.get_assoc(user, :tags) - %{ id: to_string(user.id), username: username_from_nickname(user.nickname), @@ -271,7 +269,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do ap_id: user.ap_id, also_known_as: user.also_known_as, is_confirmed: user.is_confirmed, - tags: Enum.map(user_tags, & &1.name), + tags: User.tag_names(user), hide_followers_count: user.hide_followers_count, hide_follows_count: user.hide_follows_count, hide_followers: user.hide_followers, diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 2e485d02c..c015f06b6 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -152,6 +152,20 @@ defmodule Pleroma.UserTest do end end + describe "tag_names/1" do + test "returns tag names of user" do + user = + insert(:user, %{ + tags: [ + build(:tag, name: "verify"), + build(:tag, name: "spam") + ] + }) + + assert User.tag_names(user) == ["verify", "spam"] + end + end + test "ap_id returns the activity pub id for the user" do user = UserBuilder.build()