Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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 Ecto.Changeset
12 alias Pleroma.FollowingRelationship
13 alias Pleroma.Repo
14 alias Pleroma.User
15 alias Pleroma.UserRelationship
16
17 schema "user_relationships" do
18 belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
19 belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
20 field(:relationship_type, Pleroma.UserRelationship.Type)
21
22 timestamps(updated_at: false)
23 end
24
25 for relationship_type <- Keyword.keys(Pleroma.UserRelationship.Type.__enum_map__()) do
26 # `def create_block/2`, `def create_mute/2`, `def create_reblog_mute/2`,
27 # `def create_notification_mute/2`, `def create_inverse_subscription/2`
28 def unquote(:"create_#{relationship_type}")(source, target),
29 do: create(unquote(relationship_type), source, target)
30
31 # `def delete_block/2`, `def delete_mute/2`, `def delete_reblog_mute/2`,
32 # `def delete_notification_mute/2`, `def delete_inverse_subscription/2`
33 def unquote(:"delete_#{relationship_type}")(source, target),
34 do: delete(unquote(relationship_type), source, target)
35
36 # `def block_exists?/2`, `def mute_exists?/2`, `def reblog_mute_exists?/2`,
37 # `def notification_mute_exists?/2`, `def inverse_subscription_exists?/2`
38 def unquote(:"#{relationship_type}_exists?")(source, target),
39 do: exists?(unquote(relationship_type), source, target)
40 end
41
42 def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
43
44 def user_relationship_mappings, do: Pleroma.UserRelationship.Type.__enum_map__()
45
46 def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
47 user_relationship
48 |> cast(params, [:relationship_type, :source_id, :target_id])
49 |> validate_required([:relationship_type, :source_id, :target_id])
50 |> unique_constraint(:relationship_type,
51 name: :user_relationships_source_id_relationship_type_target_id_index
52 )
53 |> validate_not_self_relationship()
54 end
55
56 def exists?(relationship_type, %User{} = source, %User{} = target) do
57 UserRelationship
58 |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
59 |> Repo.exists?()
60 end
61
62 def create(relationship_type, %User{} = source, %User{} = target) do
63 %UserRelationship{}
64 |> changeset(%{
65 relationship_type: relationship_type,
66 source_id: source.id,
67 target_id: target.id
68 })
69 |> Repo.insert(
70 on_conflict: {:replace_all_except, [:id]},
71 conflict_target: [:source_id, :relationship_type, :target_id]
72 )
73 end
74
75 def delete(relationship_type, %User{} = source, %User{} = target) do
76 attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
77
78 case Repo.get_by(UserRelationship, attrs) do
79 %UserRelationship{} = existing_record -> Repo.delete(existing_record)
80 nil -> {:ok, nil}
81 end
82 end
83
84 def dictionary(
85 source_users,
86 target_users,
87 source_to_target_rel_types \\ nil,
88 target_to_source_rel_types \\ nil
89 )
90 when is_list(source_users) and is_list(target_users) do
91 source_user_ids = User.binary_id(source_users)
92 target_user_ids = User.binary_id(target_users)
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 @doc ":relationships option for StatusView / AccountView / NotificationView"
133 def view_relationships_option(reading_user, actors, opts \\ [])
134
135 def view_relationships_option(nil = _reading_user, _actors, _opts) do
136 %{user_relationships: [], following_relationships: []}
137 end
138
139 def view_relationships_option(%User{} = reading_user, actors, opts) do
140 {source_to_target_rel_types, target_to_source_rel_types} =
141 if opts[:source_mutes_only] do
142 # This option is used for rendering statuses (FE needs `muted` flag for each one anyways)
143 {[:mute], []}
144 else
145 {[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
146 end
147
148 user_relationships =
149 UserRelationship.dictionary(
150 [reading_user],
151 actors,
152 source_to_target_rel_types,
153 target_to_source_rel_types
154 )
155
156 following_relationships = FollowingRelationship.all_between_user_sets([reading_user], actors)
157
158 %{user_relationships: user_relationships, following_relationships: following_relationships}
159 end
160
161 defp validate_not_self_relationship(%Changeset{} = changeset) do
162 changeset
163 |> validate_source_id_target_id_inequality()
164 |> validate_target_id_source_id_inequality()
165 end
166
167 defp validate_source_id_target_id_inequality(%Changeset{} = changeset) do
168 validate_change(changeset, :source_id, fn _, source_id ->
169 if source_id == get_field(changeset, :target_id) do
170 [source_id: "can't be equal to target_id"]
171 else
172 []
173 end
174 end)
175 end
176
177 defp validate_target_id_source_id_inequality(%Changeset{} = changeset) do
178 validate_change(changeset, :target_id, fn _, target_id ->
179 if target_id == get_field(changeset, :source_id) do
180 [target_id: "can't be equal to source_id"]
181 else
182 []
183 end
184 end)
185 end
186 end