2019-02-05 12:35:24 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ThreadMute do
|
|
|
|
use Ecto.Schema
|
2019-02-09 16:47:57 +00:00
|
|
|
alias Pleroma.Web.ThreadMute
|
2019-02-08 12:17:11 +00:00
|
|
|
alias Pleroma.{Activity, Repo, User}
|
|
|
|
require Ecto.Query
|
2019-02-05 12:35:24 +00:00
|
|
|
|
|
|
|
schema "thread_mutes" do
|
2019-02-07 21:25:07 +00:00
|
|
|
belongs_to(:user, User, type: Pleroma.FlakeId)
|
2019-02-05 12:35:24 +00:00
|
|
|
field(:context, :string)
|
|
|
|
end
|
|
|
|
|
2019-02-09 19:52:11 +00:00
|
|
|
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
|
|
|
|
|
2019-02-10 08:31:20 +00:00
|
|
|
def query(user, context) do
|
|
|
|
user_id = Pleroma.FlakeId.from_string(user.id)
|
|
|
|
|
|
|
|
ThreadMute
|
|
|
|
|> Ecto.Query.where(user_id: ^user_id)
|
|
|
|
|> Ecto.Query.where(context: ^context)
|
|
|
|
end
|
|
|
|
|
2019-02-05 12:35:24 +00:00
|
|
|
def add_mute(user, id) do
|
2019-02-09 16:47:57 +00:00
|
|
|
activity = Activity.get_by_id(id)
|
2019-02-09 19:52:11 +00:00
|
|
|
|
2019-02-10 08:31:20 +00:00
|
|
|
with changeset <-
|
|
|
|
changeset(%ThreadMute{}, %{user_id: user.id, context: activity.data["context"]}),
|
|
|
|
{:ok, _} <- Repo.insert(changeset) do
|
|
|
|
{:ok, activity}
|
|
|
|
else
|
2019-02-09 19:52:11 +00:00
|
|
|
{:error, _} -> {:error, "conversation is already muted"}
|
|
|
|
end
|
2019-02-05 12:35:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_mute(user, id) do
|
2019-02-09 16:47:57 +00:00
|
|
|
activity = Activity.get_by_id(id)
|
2019-02-08 12:21:34 +00:00
|
|
|
|
2019-02-10 08:31:20 +00:00
|
|
|
query(user, activity.data["context"])
|
2019-02-08 12:21:34 +00:00
|
|
|
|> Repo.delete_all()
|
2019-02-09 16:47:57 +00:00
|
|
|
|
|
|
|
{:ok, activity}
|
|
|
|
end
|
|
|
|
|
2019-02-10 08:31:20 +00:00
|
|
|
def muted?(%{id: nil} = _user, _), do: false
|
2019-02-09 16:47:57 +00:00
|
|
|
|
2019-02-10 08:31:20 +00:00
|
|
|
def muted?(user, activity) do
|
|
|
|
with query <- query(user, activity.data["context"]),
|
|
|
|
[] <- Repo.all(query) do
|
|
|
|
false
|
|
|
|
else
|
2019-02-09 16:47:57 +00:00
|
|
|
_ -> true
|
|
|
|
end
|
2019-02-05 12:35:24 +00:00
|
|
|
end
|
|
|
|
end
|