1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UserMute do
13 alias Pleroma.UserMute
15 schema "user_mutes" do
16 belongs_to(:muter, User, type: FlakeId.Ecto.CompatType)
17 belongs_to(:mutee, User, type: FlakeId.Ecto.CompatType)
19 timestamps(updated_at: false)
22 def changeset(%UserMute{} = user_mute, params \\ %{}) do
24 |> cast(params, [:muter_id, :mutee_id])
25 |> validate_required([:muter_id, :mutee_id])
26 |> unique_constraint(:mutee_id, name: :user_mutes_muter_id_mutee_id_index)
27 |> validate_not_self_mute()
30 def exists?(%User{} = muter, %User{} = mutee) do
32 |> where(muter_id: ^muter.id, mutee_id: ^mutee.id)
36 def create(%User{} = muter, %User{} = mutee) do
38 |> changeset(%{muter_id: muter.id, mutee_id: mutee.id})
40 on_conflict: :replace_all_except_primary_key,
41 conflict_target: [:muter_id, :mutee_id]
45 def delete(%User{} = muter, %User{} = mutee) do
46 attrs = %{muter_id: muter.id, mutee_id: mutee.id}
48 case Repo.get_by(UserMute, attrs) do
49 %UserMute{} = existing_record -> Repo.delete(existing_record)
54 defp validate_not_self_mute(%Ecto.Changeset{} = changeset) do
56 |> validate_change(:mutee_id, fn _, mutee_id ->
57 if mutee_id == get_field(changeset, :muter_id) do
58 [mutee_id: "can't be equal to muter_id"]
63 |> validate_change(:muter_id, fn _, muter_id ->
64 if muter_id == get_field(changeset, :mutee_id) do
65 [muter_id: "can't be equal to mutee_id"]