[#1364] Improved control over generation / sending of notifications. Fixed blocking...
[akkoma] / lib / pleroma / thread_mute.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 import Ecto.Changeset
13 import Ecto.Query
14
15 schema "thread_mutes" do
16 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
17 field(:context, :string)
18 end
19
20 def changeset(mute, params \\ %{}) do
21 mute
22 |> cast(params, [:user_id, :context])
23 |> foreign_key_constraint(:user_id)
24 |> unique_constraint(:user_id, name: :unique_index)
25 end
26
27 def query(user_id, context) do
28 {:ok, user_id} = FlakeId.Ecto.CompatType.dump(user_id)
29
30 ThreadMute
31 |> where(user_id: ^user_id)
32 |> where(context: ^context)
33 end
34
35 def muters_query(context) do
36 ThreadMute
37 |> join(:inner, [tm], u in assoc(tm, :user))
38 |> where([tm], tm.context == ^context)
39 |> select([tm, u], u.ap_id)
40 end
41
42 def muter_ap_ids(context, ap_ids \\ nil)
43
44 def muter_ap_ids(context, ap_ids) when context not in [nil, ""] do
45 context
46 |> muters_query()
47 |> maybe_filter_on_ap_id(ap_ids)
48 |> Repo.all()
49 end
50
51 def muter_ap_ids(_context, _ap_ids), do: []
52
53 defp maybe_filter_on_ap_id(query, ap_ids) when is_list(ap_ids) do
54 where(query, [tm, u], u.ap_id in ^ap_ids)
55 end
56
57 defp maybe_filter_on_ap_id(query, _ap_ids), do: query
58
59 def add_mute(user_id, context) do
60 %ThreadMute{}
61 |> changeset(%{user_id: user_id, context: context})
62 |> Repo.insert()
63 end
64
65 def remove_mute(user_id, context) do
66 query(user_id, context)
67 |> Repo.delete_all()
68 end
69
70 def check_muted(user_id, context) do
71 query(user_id, context)
72 |> Repo.all()
73 end
74 end