X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=inline;f=lib%2Fpleroma%2Fthread_mute.ex;h=5d06cf030a31da27c5cfe884f8c5480c20d8b4ab;hb=593c0851d9a62977f5a6abd22df991b0d73da657;hp=cc815430a39f576530a3f23d9b6f0ff5cd1accd5;hpb=658f30c0b3cc5403d1172a07adba8bdcd79eb82c;p=akkoma
diff --git a/lib/pleroma/thread_mute.ex b/lib/pleroma/thread_mute.ex
index cc815430a..5d06cf030 100644
--- a/lib/pleroma/thread_mute.ex
+++ b/lib/pleroma/thread_mute.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ThreadMute do
@@ -9,7 +9,8 @@ defmodule Pleroma.ThreadMute do
alias Pleroma.ThreadMute
alias Pleroma.User
- require Ecto.Query
+ import Ecto.Changeset
+ import Ecto.Query
schema "thread_mutes" do
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
@@ -18,19 +19,44 @@ defmodule Pleroma.ThreadMute do
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)
+ |> cast(params, [:user_id, :context])
+ |> foreign_key_constraint(:user_id)
+ |> unique_constraint(:user_id, name: :unique_index)
end
def query(user_id, context) do
- {:ok, user_id} = FlakeId.Ecto.CompatType.dump(user_id)
+ user_binary_id = User.binary_id(user_id)
ThreadMute
- |> Ecto.Query.where(user_id: ^user_id)
- |> Ecto.Query.where(context: ^context)
+ |> 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})
@@ -42,8 +68,8 @@ defmodule Pleroma.ThreadMute do
|> Repo.delete_all()
end
- def check_muted(user_id, context) do
+ def exists?(user_id, context) do
query(user_id, context)
- |> Repo.all()
+ |> Repo.exists?()
end
end