519d2998d27bdc262ea755280f58965fbed380c7
[akkoma] / lib / pleroma / user_relationship.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.UserRelationship do
6 use Ecto.Schema
7
8 import Ecto.Changeset
9 import Ecto.Query
10
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.UserRelationship
14
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)
19
20 timestamps(updated_at: false)
21 end
22
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)
27
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)
31
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)
35 end
36
37 def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
38
39 def user_relationship_mappings, do: UserRelationshipTypeEnum.__enum_map__()
40
41 def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
42 user_relationship
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
47 )
48 |> validate_not_self_relationship()
49 end
50
51 def exists?(relationship_type, %User{} = source, %User{} = target) do
52 UserRelationship
53 |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
54 |> Repo.exists?()
55 end
56
57 def create(relationship_type, %User{} = source, %User{} = target) do
58 %UserRelationship{}
59 |> changeset(%{
60 relationship_type: relationship_type,
61 source_id: source.id,
62 target_id: target.id
63 })
64 |> Repo.insert(
65 on_conflict: {:replace_all_except, [:id]},
66 conflict_target: [:source_id, :relationship_type, :target_id]
67 )
68 end
69
70 def delete(relationship_type, %User{} = source, %User{} = target) do
71 attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
72
73 case Repo.get_by(UserRelationship, attrs) do
74 %UserRelationship{} = existing_record -> Repo.delete(existing_record)
75 nil -> {:ok, nil}
76 end
77 end
78
79 def dictionary(
80 source_users,
81 target_users,
82 source_to_target_rel_types \\ nil,
83 target_to_source_rel_types \\ nil
84 )
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)
88
89 get_rel_type_codes = fn rel_type -> user_relationship_mappings()[rel_type] end
90
91 source_to_target_rel_types =
92 Enum.map(source_to_target_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
93
94 target_to_source_rel_types =
95 Enum.map(target_to_source_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
96
97 __MODULE__
98 |> where(
99 fragment(
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(?))",
102 ^source_user_ids,
103 ^target_user_ids,
104 ^source_to_target_rel_types,
105 ^target_user_ids,
106 ^source_user_ids,
107 ^target_to_source_rel_types
108 )
109 )
110 |> select([ur], [ur.relationship_type, ur.source_id, ur.target_id])
111 |> Repo.all()
112 end
113
114 def exists?(dictionary, rel_type, source, target, func) do
115 cond do
116 is_nil(source) or is_nil(target) ->
117 false
118
119 dictionary ->
120 [rel_type, source.id, target.id] in dictionary
121
122 true ->
123 func.(source, target)
124 end
125 end
126
127 defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
128 changeset
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"]
132 else
133 []
134 end
135 end)
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"]
139 else
140 []
141 end
142 end)
143 end
144 end