From 5a46d37af9fb1914d795cb90d28356efcd0790d5 Mon Sep 17 00:00:00 2001 From: Ekaterina Vaartis Date: Tue, 19 Feb 2019 23:09:16 +0300 Subject: [PATCH] Update the mute implementation to the current codebase Make it part of the info thing (and do a migration to ensure it's there) --- lib/pleroma/user.ex | 33 ++++++++++++++-------- lib/pleroma/user/info.ex | 17 +++++++++++ lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- .../web/mastodon_api/mastodon_api_controller.ex | 24 ++++++++++++---- lib/pleroma/web/mastodon_api/views/account_view.ex | 1 - .../20190219192317_create_user_mutes.exs | 7 +++++ 6 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 priv/repo/migrations/20190219192317_create_user_mutes.exs diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index cfb44ebaa..35ba4ad99 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -889,21 +889,27 @@ defmodule Pleroma.User do end def mute(muter, %User{ap_id: ap_id}) do - mutes = muter.info["mutes"] || [] - new_mutes = Enum.uniq([ap_id | mutes]) - new_info = Map.put(muter.info, "mutes", new_mutes) + info_cng = + muter.info + |> User.Info.add_to_mutes(ap_id) - User.info_changeset(muter, %{info: new_info}) - |> update_and_set_cache() + cng = + change(muter) + |> put_embed(:info, info_cng) + + update_and_set_cache(cng) end - def unmute(user, %{ap_id: ap_id}) do - mutes = user.info["mutes"] || [] - new_mutes = List.delete(mutes, ap_id) - new_info = Map.put(user.info, "mutes", new_mutes) + def unmute(muter, %{ap_id: ap_id}) do + info_cng = + muter.info + |> User.Info.remove_from_mutes(ap_id) - User.info_changeset(user, %{info: new_info}) - |> update_and_set_cache() + cng = + change(muter) + |> put_embed(:info, info_cng) + + update_and_set_cache(cng) end def block(blocker, %User{ap_id: ap_id} = blocked) do @@ -948,7 +954,7 @@ defmodule Pleroma.User do update_and_set_cache(cng) end - def mutes?(user, %{ap_id: ap_id}), do: Enum.member?(user.info["mutes"] || [], ap_id) + def mutes?(user, %{ap_id: ap_id}), do: Enum.member?(user.info.mutes, ap_id) def blocks?(user, %{ap_id: ap_id}) do blocks = user.info.blocks @@ -961,6 +967,9 @@ defmodule Pleroma.User do end) end + def muted_users(user), + do: Repo.all(from(u in User, where: u.ap_id in ^user.info.mutes)) + def blocked_users(user), do: Repo.all(from(u in User, where: u.ap_id in ^user.info.blocks)) diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 9099d7fbb..00a0f6df3 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -19,6 +19,7 @@ defmodule Pleroma.User.Info do field(:default_scope, :string, default: "public") field(:blocks, {:array, :string}, default: []) field(:domain_blocks, {:array, :string}, default: []) + field(:mutes, {:array, :string}, default: []) field(:deactivated, :boolean, default: false) field(:no_rich_text, :boolean, default: false) field(:ap_enabled, :boolean, default: false) @@ -74,6 +75,14 @@ defmodule Pleroma.User.Info do |> validate_required([:follower_count]) end + def set_mutes(info, mutes) do + params = %{mutes: mutes} + + info + |> cast(params, [:mutes]) + |> validate_required([:mutes]) + end + def set_blocks(info, blocks) do params = %{blocks: blocks} @@ -82,6 +91,14 @@ defmodule Pleroma.User.Info do |> validate_required([:blocks]) end + def add_to_mutes(info, muted) do + set_mutes(info, Enum.uniq([muted | info.mutes])) + end + + def remove_from_mutes(info, muted) do + set_mutes(info, List.delete(info.mutes, muted)) + end + def add_to_block(info, blocked) do set_blocks(info, Enum.uniq([blocked | info.blocks])) end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 36e5e23bf..cb8a2139e 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -577,7 +577,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp restrict_reblogs(query, _), do: query defp restrict_muted(query, %{"muting_user" => %User{info: info}}) do - mutes = info["mutes"] || [] + mutes = info.mutes from( activity in query, diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 49b49be19..3a343f3d8 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -769,22 +769,34 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def mute(%{assigns: %{user: muter}} = conn, %{"id" => id}) do with %User{} = muted <- Repo.get(User, id), {:ok, muter} <- User.mute(muter, muted) do - render(conn, AccountView, "relationship.json", %{user: muter, target: muted}) + conn + |> put_view(AccountView) + |> render("relationship.json", %{user: muter, target: muted}) + else + {:error, message} -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Jason.encode!(%{"error" => message})) end end def unmute(%{assigns: %{user: muter}} = conn, %{"id" => id}) do with %User{} = muted <- Repo.get(User, id), {:ok, muter} <- User.unmute(muter, muted) do - render(conn, AccountView, "relationship.json", %{user: muter, target: muted}) + conn + |> put_view(AccountView) + |> render("relationship.json", %{user: muter, target: muted}) + else + {:error, message} -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Jason.encode!(%{"error" => message})) end end - # TODO: Use proper query def mutes(%{assigns: %{user: user}} = conn, _) do - with muted_users <- user.info["mutes"] || [], - accounts <- Enum.map(muted_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do - res = AccountView.render("accounts.json", users: accounts, for: user, as: :user) + with muted_accounts <- User.muted_users(user) do + res = AccountView.render("accounts.json", users: muted_accounts, for: user, as: :user) json(conn, res) end end diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 91b3e034f..8fdefdebd 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -48,7 +48,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do followed_by: User.following?(target, user), blocking: User.blocks?(user, target), muting: User.mutes?(user, target), - muting: false, muting_notifications: false, requested: requested, domain_blocking: false, diff --git a/priv/repo/migrations/20190219192317_create_user_mutes.exs b/priv/repo/migrations/20190219192317_create_user_mutes.exs new file mode 100644 index 000000000..304074567 --- /dev/null +++ b/priv/repo/migrations/20190219192317_create_user_mutes.exs @@ -0,0 +1,7 @@ +defmodule Pleroma.Repo.Migrations.CreateUserMutes do + use Ecto.Migration + + def change do + execute "UPDATE users SET info = jsonb_set(info, '{mutes}', '[]'::jsonb);" + end +end