1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UserRelationship do
13 alias Pleroma.UserRelationship
15 schema "user_relationships" do
16 belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
17 belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
18 field(:relationship_type, UserRelationshipTypeEnum)
20 timestamps(updated_at: false)
23 for relationship_type <- Keyword.keys(UserRelationshipTypeEnum.__enum_map__()) do
24 # Definitions of `create_block/2`, `create_mute/2` etc.
25 def unquote(:"create_#{relationship_type}")(source, target),
26 do: create(unquote(relationship_type), source, target)
28 # Definitions of `delete_block/2`, `delete_mute/2` etc.
29 def unquote(:"delete_#{relationship_type}")(source, target),
30 do: delete(unquote(relationship_type), source, target)
32 # Definitions of `block_exists?/2`, `mute_exists?/2` etc.
33 def unquote(:"#{relationship_type}_exists?")(source, target),
34 do: exists?(unquote(relationship_type), source, target)
37 def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
39 def user_relationship_mappings, do: UserRelationshipTypeEnum.__enum_map__()
41 def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
43 |> cast(params, [:relationship_type, :source_id, :target_id])
44 |> validate_required([:relationship_type, :source_id, :target_id])
45 |> unique_constraint(:relationship_type,
46 name: :user_relationships_source_id_relationship_type_target_id_index
48 |> validate_not_self_relationship()
51 def exists?(relationship_type, %User{} = source, %User{} = target) do
53 |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
57 def create(relationship_type, %User{} = source, %User{} = target) do
60 relationship_type: relationship_type,
65 on_conflict: {:replace_all_except, [:id]},
66 conflict_target: [:source_id, :relationship_type, :target_id]
70 def delete(relationship_type, %User{} = source, %User{} = target) do
71 attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
73 case Repo.get_by(UserRelationship, attrs) do
74 %UserRelationship{} = existing_record -> Repo.delete(existing_record)
82 source_to_target_rel_types \\ nil,
83 target_to_source_rel_types \\ nil
85 when is_list(source_users) and is_list(target_users) do
86 source_user_ids = User.binary_id(source_users)
87 target_user_ids = User.binary_id(target_users)
89 get_rel_type_codes = fn rel_type -> user_relationship_mappings()[rel_type] end
91 source_to_target_rel_types =
92 Enum.map(source_to_target_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
94 target_to_source_rel_types =
95 Enum.map(target_to_source_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
100 "(source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?)) OR \
101 (source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?))",
104 ^source_to_target_rel_types,
107 ^target_to_source_rel_types
110 |> select([ur], [ur.relationship_type, ur.source_id, ur.target_id])
114 def exists?(dictionary, rel_type, source, target, func) do
116 is_nil(source) or is_nil(target) ->
120 [rel_type, source.id, target.id] in dictionary
123 func.(source, target)
127 defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
129 |> validate_change(:target_id, fn _, target_id ->
130 if target_id == get_field(changeset, :source_id) do
131 [target_id: "can't be equal to source_id"]
136 |> validate_change(:source_id, fn _, source_id ->
137 if source_id == get_field(changeset, :target_id) do
138 [source_id: "can't be equal to target_id"]