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)
+
+ cng =
+ change(muter)
+ |> put_embed(:info, info_cng)
- User.info_changeset(muter, %{info: new_info})
- |> update_and_set_cache()
+ 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
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
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))
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)
|> 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}
|> 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
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,
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
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,
--- /dev/null
+defmodule Pleroma.Repo.Migrations.CreateUserMutes do
+ use Ecto.Migration
+
+ def change do
+ execute "UPDATE users SET info = jsonb_set(info, '{mutes}', '[]'::jsonb);"
+ end
+end