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.Web.ThreadMute do
7 alias Pleroma.Web.ThreadMute
8 alias Pleroma.{Activity, Repo, User}
11 schema "thread_mutes" do
12 belongs_to(:user, User, type: Pleroma.FlakeId)
13 field(:context, :string)
16 def changeset(mute, params \\ %{}) do
18 |> Ecto.Changeset.cast(params, [:user_id, :context])
19 |> Ecto.Changeset.foreign_key_constraint(:user_id)
20 |> Ecto.Changeset.unique_constraint(:user_id, name: :unique_index)
23 def query(user, context) do
24 user_id = Pleroma.FlakeId.from_string(user.id)
27 |> Ecto.Query.where(user_id: ^user_id)
28 |> Ecto.Query.where(context: ^context)
31 def add_mute(user, id) do
32 activity = Activity.get_by_id(id)
35 changeset(%ThreadMute{}, %{user_id: user.id, context: activity.data["context"]}),
36 {:ok, _} <- Repo.insert(changeset) do
39 {:error, _} -> {:error, "conversation is already muted"}
43 def remove_mute(user, id) do
44 activity = Activity.get_by_id(id)
46 query(user, activity.data["context"])
52 def muted?(%{id: nil} = _user, _), do: false
54 def muted?(user, activity) do
55 with query <- query(user, activity.data["context"]),
56 [] <- Repo.all(query) do