Update the mute implementation to the current codebase

Make it part of the info thing (and do a migration to ensure it's there)
This commit is contained in:
Ekaterina Vaartis 2019-02-19 23:09:16 +03:00
parent 465b547c90
commit 5a46d37af9
6 changed files with 64 additions and 20 deletions

View file

@ -889,21 +889,27 @@ def blocks_import(%User{} = blocker, blocked_identifiers) when is_list(blocked_i
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)
User.info_changeset(muter, %{info: new_info}) cng =
|> update_and_set_cache() change(muter)
|> put_embed(:info, info_cng)
update_and_set_cache(cng)
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 @@ def unblock(blocker, %{ap_id: ap_id}) 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 @@ def blocks?(user, %{ap_id: ap_id}) 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))

View file

@ -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 @@ def set_follower_count(info, number) 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 @@ def set_blocks(info, blocks) 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

View file

@ -577,7 +577,7 @@ defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val == "true" or
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,

View file

@ -769,22 +769,34 @@ def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) 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

View file

@ -48,7 +48,6 @@ def render("relationship.json", %{user: user, target: target}) 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,

View file

@ -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