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
11 alias FlakeId.Ecto.CompatType
14 alias Pleroma.UserRelationship
16 schema "user_relationships" do
17 belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
18 belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
19 field(:relationship_type, UserRelationshipTypeEnum)
21 timestamps(updated_at: false)
24 for relationship_type <- Keyword.keys(UserRelationshipTypeEnum.__enum_map__()) do
25 # Definitions of `create_block/2`, `create_mute/2` etc.
26 def unquote(:"create_#{relationship_type}")(source, target),
27 do: create(unquote(relationship_type), source, target)
29 # Definitions of `delete_block/2`, `delete_mute/2` etc.
30 def unquote(:"delete_#{relationship_type}")(source, target),
31 do: delete(unquote(relationship_type), source, target)
33 # Definitions of `block_exists?/2`, `mute_exists?/2` etc.
34 def unquote(:"#{relationship_type}_exists?")(source, target),
35 do: exists?(unquote(relationship_type), source, target)
38 def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
40 def user_relationship_mappings, do: UserRelationshipTypeEnum.__enum_map__()
42 def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
44 |> cast(params, [:relationship_type, :source_id, :target_id])
45 |> validate_required([:relationship_type, :source_id, :target_id])
46 |> unique_constraint(:relationship_type,
47 name: :user_relationships_source_id_relationship_type_target_id_index
49 |> validate_not_self_relationship()
52 def exists?(relationship_type, %User{} = source, %User{} = target) do
54 |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
58 def create(relationship_type, %User{} = source, %User{} = target) do
61 relationship_type: relationship_type,
66 on_conflict: {:replace_all_except, [:id]},
67 conflict_target: [:source_id, :relationship_type, :target_id]
71 def delete(relationship_type, %User{} = source, %User{} = target) do
72 attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
74 case Repo.get_by(UserRelationship, attrs) do
75 %UserRelationship{} = existing_record -> Repo.delete(existing_record)
83 source_to_target_rel_types \\ nil,
84 target_to_source_rel_types \\ nil
86 when is_list(source_users) and is_list(target_users) do
87 get_bin_ids = fn user ->
88 with {:ok, bin_id} <- CompatType.dump(user.id), do: bin_id
91 source_user_ids = Enum.map(source_users, &get_bin_ids.(&1))
92 target_user_ids = Enum.map(target_users, &get_bin_ids.(&1))
94 get_rel_type_codes = fn rel_type -> user_relationship_mappings()[rel_type] end
96 source_to_target_rel_types =
97 Enum.map(source_to_target_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
99 target_to_source_rel_types =
100 Enum.map(target_to_source_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
105 "(source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?)) OR \
106 (source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?))",
109 ^source_to_target_rel_types,
112 ^target_to_source_rel_types
115 |> select([ur], [ur.relationship_type, ur.source_id, ur.target_id])
119 defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
121 |> validate_change(:target_id, fn _, target_id ->
122 if target_id == get_field(changeset, :source_id) do
123 [target_id: "can't be equal to source_id"]
128 |> validate_change(:source_id, fn _, source_id ->
129 if source_id == get_field(changeset, :target_id) do
130 [source_id: "can't be equal to target_id"]