X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fuser_relationship.ex;h=393947942304d0a4356e9599a9fc91bad1552b2a;hb=8854770fc4e9079131a0897d5fb6c0ccccf98bc6;hp=5cb99ae5015d1fcbe381749365c96a4ce64d102f;hpb=3aaf3aa2c24c6b38d7e49e4861c1294c285db49b;p=akkoma
diff --git a/lib/pleroma/user_relationship.ex b/lib/pleroma/user_relationship.ex
index 5cb99ae50..393947942 100644
--- a/lib/pleroma/user_relationship.ex
+++ b/lib/pleroma/user_relationship.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.UserRelationship do
@@ -20,6 +20,20 @@ defmodule Pleroma.UserRelationship do
timestamps(updated_at: false)
end
+ for relationship_type <- Keyword.keys(UserRelationshipTypeEnum.__enum_map__()) do
+ # Definitions of `create_block/2`, `create_mute/2` etc.
+ def unquote(:"create_#{relationship_type}")(source, target),
+ do: create(unquote(relationship_type), source, target)
+
+ # Definitions of `delete_block/2`, `delete_mute/2` etc.
+ def unquote(:"delete_#{relationship_type}")(source, target),
+ do: delete(unquote(relationship_type), source, target)
+
+ # Definitions of `block_exists?/2`, `mute_exists?/2` etc.
+ def unquote(:"#{relationship_type}_exists?")(source, target),
+ do: exists?(unquote(relationship_type), source, target)
+ end
+
def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
user_relationship
|> cast(params, [:relationship_type, :source_id, :target_id])
@@ -36,10 +50,6 @@ defmodule Pleroma.UserRelationship do
|> Repo.exists?()
end
- def block_exists?(%User{} = blocker, %User{} = blockee), do: exists?(:block, blocker, blockee)
-
- def mute_exists?(%User{} = muter, %User{} = mutee), do: exists?(:mute, muter, mutee)
-
def create(relationship_type, %User{} = source, %User{} = target) do
%UserRelationship{}
|> changeset(%{
@@ -48,15 +58,11 @@ defmodule Pleroma.UserRelationship do
target_id: target.id
})
|> Repo.insert(
- on_conflict: :replace_all_except_primary_key,
+ on_conflict: {:replace_all_except, [:id]},
conflict_target: [:source_id, :relationship_type, :target_id]
)
end
- def create_block(%User{} = blocker, %User{} = blockee), do: create(:block, blocker, blockee)
-
- def create_mute(%User{} = muter, %User{} = mutee), do: create(:mute, muter, mutee)
-
def delete(relationship_type, %User{} = source, %User{} = target) do
attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
@@ -66,10 +72,6 @@ defmodule Pleroma.UserRelationship do
end
end
- def delete_block(%User{} = blocker, %User{} = blockee), do: delete(:block, blocker, blockee)
-
- def delete_mute(%User{} = muter, %User{} = mutee), do: delete(:mute, muter, mutee)
-
defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
changeset
|> validate_change(:target_id, fn _, target_id ->