2019-02-11 02:59:51 -08:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 12:38:50 -08:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-02-11 02:59:51 -08:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.ThreadMute do
|
|
|
|
use Ecto.Schema
|
2019-03-04 18:52:23 -08:00
|
|
|
|
2019-03-04 17:45:07 -08:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.ThreadMute
|
2019-03-04 18:52:23 -08:00
|
|
|
alias Pleroma.User
|
|
|
|
|
2020-03-15 11:00:12 -07:00
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
2019-02-11 02:59:51 -08:00
|
|
|
|
|
|
|
schema "thread_mutes" do
|
2019-09-18 07:54:31 -07:00
|
|
|
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
2019-02-11 02:59:51 -08:00
|
|
|
field(:context, :string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def changeset(mute, params \\ %{}) do
|
|
|
|
mute
|
2020-03-15 11:00:12 -07:00
|
|
|
|> cast(params, [:user_id, :context])
|
|
|
|
|> foreign_key_constraint(:user_id)
|
|
|
|
|> unique_constraint(:user_id, name: :unique_index)
|
2019-02-11 02:59:51 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def query(user_id, context) do
|
2020-03-25 07:01:45 -07:00
|
|
|
user_binary_id = User.binary_id(user_id)
|
2019-02-11 02:59:51 -08:00
|
|
|
|
|
|
|
ThreadMute
|
2020-03-25 23:43:58 -07:00
|
|
|
|> where(user_id: ^user_binary_id)
|
2020-03-15 11:00:12 -07:00
|
|
|
|> where(context: ^context)
|
2019-02-11 02:59:51 -08:00
|
|
|
end
|
|
|
|
|
2020-03-15 11:00:12 -07:00
|
|
|
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)
|
|
|
|
|
2020-03-25 03:05:00 -07:00
|
|
|
# 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
|
2020-03-15 11:00:12 -07:00
|
|
|
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
|
|
|
|
|
2019-02-11 02:59:51 -08:00
|
|
|
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
|
|
|
|
|
2020-03-26 22:01:03 -07:00
|
|
|
def exists?(user_id, context) do
|
2019-02-11 02:59:51 -08:00
|
|
|
query(user_id, context)
|
2020-03-26 22:01:03 -07:00
|
|
|
|> Repo.exists?()
|
2019-02-11 02:59:51 -08:00
|
|
|
end
|
|
|
|
end
|