+
+ defp set_domain_blocks(user, domain_blocks) do
+ params = %{domain_blocks: domain_blocks}
+
+ user
+ |> cast(params, [:domain_blocks])
+ |> validate_required([:domain_blocks])
+ |> update_and_set_cache()
+ end
+
+ def block_domain(user, domain_blocked) do
+ set_domain_blocks(user, Enum.uniq([domain_blocked | user.domain_blocks]))
+ end
+
+ def unblock_domain(user, domain_blocked) do
+ set_domain_blocks(user, List.delete(user.domain_blocks, domain_blocked))
+ end
+
+ defp set_blocks(user, blocks) do
+ params = %{blocks: blocks}
+
+ user
+ |> cast(params, [:blocks])
+ |> validate_required([:blocks])
+ |> update_and_set_cache()
+ end
+
+ def add_to_block(user, blocked) do
+ set_blocks(user, Enum.uniq([blocked | user.blocks]))
+ end
+
+ def remove_from_block(user, blocked) do
+ set_blocks(user, List.delete(user.blocks, blocked))
+ end
+
+ defp set_mutes(user, mutes) do
+ params = %{mutes: mutes}
+
+ user
+ |> cast(params, [:mutes])
+ |> validate_required([:mutes])
+ |> update_and_set_cache()
+ end
+
+ def add_to_mutes(user, muted, notifications?) do
+ with {:ok, user} <- set_mutes(user, Enum.uniq([muted | user.mutes])) do
+ set_notification_mutes(
+ user,
+ Enum.uniq([muted | user.muted_notifications]),
+ notifications?
+ )
+ end
+ end
+
+ def remove_from_mutes(user, muted) do
+ with {:ok, user} <- set_mutes(user, List.delete(user.mutes, muted)) do
+ set_notification_mutes(
+ user,
+ List.delete(user.muted_notifications, muted),
+ true
+ )
+ end
+ end
+
+ defp set_notification_mutes(user, _muted_notifications, false = _notifications?) do
+ {:ok, user}
+ end
+
+ defp set_notification_mutes(user, muted_notifications, true = _notifications?) do
+ params = %{muted_notifications: muted_notifications}
+
+ user
+ |> cast(params, [:muted_notifications])
+ |> validate_required([:muted_notifications])
+ |> update_and_set_cache()
+ end
+
+ def add_reblog_mute(user, ap_id) do
+ params = %{muted_reblogs: user.muted_reblogs ++ [ap_id]}
+
+ user
+ |> cast(params, [:muted_reblogs])
+ |> update_and_set_cache()
+ end
+
+ def remove_reblog_mute(user, ap_id) do
+ params = %{muted_reblogs: List.delete(user.muted_reblogs, ap_id)}
+
+ user
+ |> cast(params, [:muted_reblogs])
+ |> update_and_set_cache()
+ end
+
+ def set_invisible(user, invisible) do
+ params = %{invisible: invisible}
+
+ user
+ |> cast(params, [:invisible])
+ |> validate_required([:invisible])
+ |> update_and_set_cache()
+ end