summaryrefslogtreecommitdiff
path: root/lib/pleroma/thread_mute.ex
blob: 5d06cf030a31da27c5cfe884f8c5480c20d8b4ab (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 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

  import Ecto.Changeset
  import Ecto.Query

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

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

  def query(user_id, context) do
    user_binary_id = User.binary_id(user_id)

    ThreadMute
    |> where(user_id: ^user_binary_id)
    |> where(context: ^context)
  end

  def muters_query(context) do
    ThreadMute
    |> join(:inner, [tm], u in assoc(tm, :user))
    |> where([tm], tm.context == ^context)
    |> select([tm, u], u.ap_id)
  end

  def muter_ap_ids(context, ap_ids \\ nil)

  # Note: applies to fake activities (ActivityPub.Utils.get_notified_from_object/1 etc.)
  def muter_ap_ids(context, _ap_ids) when is_nil(context), do: []

  def muter_ap_ids(context, ap_ids) do
    context
    |> muters_query()
    |> maybe_filter_on_ap_id(ap_ids)
    |> Repo.all()
  end

  defp maybe_filter_on_ap_id(query, ap_ids) when is_list(ap_ids) do
    where(query, [tm, u], u.ap_id in ^ap_ids)
  end

  defp maybe_filter_on_ap_id(query, _ap_ids), do: query

  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 exists?(user_id, context) do
    query(user_id, context)
    |> Repo.exists?()
  end
end