summaryrefslogtreecommitdiff
path: root/lib/pleroma/thread_mute.ex
blob: cc815430a39f576530a3f23d9b6f0ff5cd1accd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.ThreadMute do
  use Ecto.Schema

  alias Pleroma.Repo
  alias Pleroma.ThreadMute
  alias Pleroma.User

  require Ecto.Query

  schema "thread_mutes" do
    belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
    field(:context, :string)
  end

  def changeset(mute, params \\ %{}) do
    mute
    |> Ecto.Changeset.cast(params, [:user_id, :context])
    |> Ecto.Changeset.foreign_key_constraint(:user_id)
    |> Ecto.Changeset.unique_constraint(:user_id, name: :unique_index)
  end

  def query(user_id, context) do
    {:ok, user_id} = FlakeId.Ecto.CompatType.dump(user_id)

    ThreadMute
    |> Ecto.Query.where(user_id: ^user_id)
    |> Ecto.Query.where(context: ^context)
  end

  def add_mute(user_id, context) do
    %ThreadMute{}
    |> changeset(%{user_id: user_id, context: context})
    |> Repo.insert()
  end

  def remove_mute(user_id, context) do
    query(user_id, context)
    |> Repo.delete_all()
  end

  def check_muted(user_id, context) do
    query(user_id, context)
    |> Repo.all()
  end
end