Preloading of follow relations for timeline/statuses rendering (performance improveme...
[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 FlakeId.Ecto.CompatType
12 alias Pleroma.Repo
13 alias Pleroma.User
14 alias Pleroma.UserRelationship
15
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)
20
21 timestamps(updated_at: false)
22 end
23
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)
28
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)
32
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)
36 end
37
38 def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
39
40 def user_relationship_mappings, do: UserRelationshipTypeEnum.__enum_map__()
41
42 def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
43 user_relationship
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
48 )
49 |> validate_not_self_relationship()
50 end
51
52 def exists?(relationship_type, %User{} = source, %User{} = target) do
53 UserRelationship
54 |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
55 |> Repo.exists?()
56 end
57
58 def create(relationship_type, %User{} = source, %User{} = target) do
59 %UserRelationship{}
60 |> changeset(%{
61 relationship_type: relationship_type,
62 source_id: source.id,
63 target_id: target.id
64 })
65 |> Repo.insert(
66 on_conflict: {:replace_all_except, [:id]},
67 conflict_target: [:source_id, :relationship_type, :target_id]
68 )
69 end
70
71 def delete(relationship_type, %User{} = source, %User{} = target) do
72 attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
73
74 case Repo.get_by(UserRelationship, attrs) do
75 %UserRelationship{} = existing_record -> Repo.delete(existing_record)
76 nil -> {:ok, nil}
77 end
78 end
79
80 def dictionary(
81 source_users,
82 target_users,
83 source_to_target_rel_types \\ nil,
84 target_to_source_rel_types \\ nil
85 )
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
89 end
90
91 source_user_ids = Enum.map(source_users, &get_bin_ids.(&1))
92 target_user_ids = Enum.map(target_users, &get_bin_ids.(&1))
93
94 get_rel_type_codes = fn rel_type -> user_relationship_mappings()[rel_type] end
95
96 source_to_target_rel_types =
97 Enum.map(source_to_target_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
98
99 target_to_source_rel_types =
100 Enum.map(target_to_source_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
101
102 __MODULE__
103 |> where(
104 fragment(
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(?))",
107 ^source_user_ids,
108 ^target_user_ids,
109 ^source_to_target_rel_types,
110 ^target_user_ids,
111 ^source_user_ids,
112 ^target_to_source_rel_types
113 )
114 )
115 |> select([ur], [ur.relationship_type, ur.source_id, ur.target_id])
116 |> Repo.all()
117 end
118
119 def exists?(dictionary, rel_type, source, target, func) do
120 cond do
121 is_nil(source) or is_nil(target) ->
122 false
123
124 dictionary ->
125 [rel_type, source.id, target.id] in dictionary
126
127 true ->
128 func.(source, target)
129 end
130 end
131
132 defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
133 changeset
134 |> validate_change(:target_id, fn _, target_id ->
135 if target_id == get_field(changeset, :source_id) do
136 [target_id: "can't be equal to source_id"]
137 else
138 []
139 end
140 end)
141 |> validate_change(:source_id, fn _, source_id ->
142 if source_id == get_field(changeset, :target_id) do
143 [source_id: "can't be equal to target_id"]
144 else
145 []
146 end
147 end)
148 end
149 end