Commit 5a46d37a authored by vaartis's avatar vaartis

Update the mute implementation to the current codebase

Make it part of the info thing (and do a migration to ensure it's there)
parent 465b547c
...@@ -889,21 +889,27 @@ defmodule Pleroma.User do ...@@ -889,21 +889,27 @@ defmodule Pleroma.User do
end end
def mute(muter, %User{ap_id: ap_id}) do def mute(muter, %User{ap_id: ap_id}) do
mutes = muter.info["mutes"] || [] info_cng =
new_mutes = Enum.uniq([ap_id | mutes]) muter.info
new_info = Map.put(muter.info, "mutes", new_mutes) |> User.Info.add_to_mutes(ap_id)
cng =
change(muter)
|> put_embed(:info, info_cng)
User.info_changeset(muter, %{info: new_info}) update_and_set_cache(cng)
|> update_and_set_cache()
end end
def unmute(user, %{ap_id: ap_id}) do def unmute(muter, %{ap_id: ap_id}) do
mutes = user.info["mutes"] || [] info_cng =
new_mutes = List.delete(mutes, ap_id) muter.info
new_info = Map.put(user.info, "mutes", new_mutes) |> User.Info.remove_from_mutes(ap_id)
User.info_changeset(user, %{info: new_info}) cng =
|> update_and_set_cache() change(muter)
|> put_embed(:info, info_cng)
update_and_set_cache(cng)
end end
def block(blocker, %User{ap_id: ap_id} = blocked) do def block(blocker, %User{ap_id: ap_id} = blocked) do
...@@ -948,7 +954,7 @@ defmodule Pleroma.User do ...@@ -948,7 +954,7 @@ defmodule Pleroma.User do
update_and_set_cache(cng) update_and_set_cache(cng)
end 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 def blocks?(user, %{ap_id: ap_id}) do
blocks = user.info.blocks blocks = user.info.blocks
...@@ -961,6 +967,9 @@ defmodule Pleroma.User do ...@@ -961,6 +967,9 @@ defmodule Pleroma.User do
end) end)
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), def blocked_users(user),
do: Repo.all(from(u in User, where: u.ap_id in ^user.info.blocks)) do: Repo.all(from(u in User, where: u.ap_id in ^user.info.blocks))
......
...@@ -19,6 +19,7 @@ defmodule Pleroma.User.Info do ...@@ -19,6 +19,7 @@ defmodule Pleroma.User.Info do
field(:default_scope, :string, default: "public") field(:default_scope, :string, default: "public")
field(:blocks, {:array, :string}, default: []) field(:blocks, {:array, :string}, default: [])
field(:domain_blocks, {:array, :string}, default: []) field(:domain_blocks, {:array, :string}, default: [])
field(:mutes, {:array, :string}, default: [])
field(:deactivated, :boolean, default: false) field(:deactivated, :boolean, default: false)
field(:no_rich_text, :boolean, default: false) field(:no_rich_text, :boolean, default: false)
field(:ap_enabled, :boolean, default: false) field(:ap_enabled, :boolean, default: false)
...@@ -74,6 +75,14 @@ defmodule Pleroma.User.Info do ...@@ -74,6 +75,14 @@ defmodule Pleroma.User.Info do
|> validate_required([:follower_count]) |> validate_required([:follower_count])
end end
def set_mutes(info, mutes) do
params = %{mutes: mutes}
info
|> cast(params, [:mutes])
|> validate_required([:mutes])
end
def set_blocks(info, blocks) do def set_blocks(info, blocks) do
params = %{blocks: blocks} params = %{blocks: blocks}
...@@ -82,6 +91,14 @@ defmodule Pleroma.User.Info do ...@@ -82,6 +91,14 @@ defmodule Pleroma.User.Info do
|> validate_required([:blocks]) |> validate_required([:blocks])
end 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 def add_to_block(info, blocked) do
set_blocks(info, Enum.uniq([blocked | info.blocks])) set_blocks(info, Enum.uniq([blocked | info.blocks]))
end end
......
...@@ -577,7 +577,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do ...@@ -577,7 +577,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_reblogs(query, _), do: query defp restrict_reblogs(query, _), do: query
defp restrict_muted(query, %{"muting_user" => %User{info: info}}) do defp restrict_muted(query, %{"muting_user" => %User{info: info}}) do
mutes = info["mutes"] || [] mutes = info.mutes
from( from(
activity in query, activity in query,
......
...@@ -769,22 +769,34 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do ...@@ -769,22 +769,34 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def mute(%{assigns: %{user: muter}} = conn, %{"id" => id}) do def mute(%{assigns: %{user: muter}} = conn, %{"id" => id}) do
with %User{} = muted <- Repo.get(User, id), with %User{} = muted <- Repo.get(User, id),
{:ok, muter} <- User.mute(muter, muted) do {: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
end end
def unmute(%{assigns: %{user: muter}} = conn, %{"id" => id}) do def unmute(%{assigns: %{user: muter}} = conn, %{"id" => id}) do
with %User{} = muted <- Repo.get(User, id), with %User{} = muted <- Repo.get(User, id),
{:ok, muter} <- User.unmute(muter, muted) do {: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
end end
# TODO: Use proper query
def mutes(%{assigns: %{user: user}} = conn, _) do def mutes(%{assigns: %{user: user}} = conn, _) do
with muted_users <- user.info["mutes"] || [], with muted_accounts <- User.muted_users(user) do
accounts <- Enum.map(muted_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do res = AccountView.render("accounts.json", users: muted_accounts, for: user, as: :user)
res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
json(conn, res) json(conn, res)
end end
end end
......
...@@ -48,7 +48,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do ...@@ -48,7 +48,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
followed_by: User.following?(target, user), followed_by: User.following?(target, user),
blocking: User.blocks?(user, target), blocking: User.blocks?(user, target),
muting: User.mutes?(user, target), muting: User.mutes?(user, target),
muting: false,
muting_notifications: false, muting_notifications: false,
requested: requested, requested: requested,
domain_blocking: false, domain_blocking: false,
......
defmodule Pleroma.Repo.Migrations.CreateUserMutes do
use Ecto.Migration
def change do
execute "UPDATE users SET info = jsonb_set(info, '{mutes}', '[]'::jsonb);"
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment