Merge branch '1149-oban-job-queue' into 'develop'
[akkoma] / lib / pleroma / thread_mute.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ThreadMute do
6 use Ecto.Schema
7
8 alias Pleroma.Repo
9 alias Pleroma.ThreadMute
10 alias Pleroma.User
11
12 require Ecto.Query
13
14 schema "thread_mutes" do
15 belongs_to(:user, User, type: Pleroma.FlakeId)
16 field(:context, :string)
17 end
18
19 def changeset(mute, params \\ %{}) do
20 mute
21 |> Ecto.Changeset.cast(params, [:user_id, :context])
22 |> Ecto.Changeset.foreign_key_constraint(:user_id)
23 |> Ecto.Changeset.unique_constraint(:user_id, name: :unique_index)
24 end
25
26 def query(user_id, context) do
27 user_id = Pleroma.FlakeId.from_string(user_id)
28
29 ThreadMute
30 |> Ecto.Query.where(user_id: ^user_id)
31 |> Ecto.Query.where(context: ^context)
32 end
33
34 def add_mute(user_id, context) do
35 %ThreadMute{}
36 |> changeset(%{user_id: user_id, context: context})
37 |> Repo.insert()
38 end
39
40 def remove_mute(user_id, context) do
41 query(user_id, context)
42 |> Repo.delete_all()
43 end
44
45 def check_muted(user_id, context) do
46 query(user_id, context)
47 |> Repo.all()
48 end
49 end