1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UserRelationship do
12 alias Pleroma.FollowingRelationship
15 alias Pleroma.UserRelationship
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 field(:expires_at, :utc_datetime)
23 timestamps(updated_at: false)
26 for relationship_type <- Keyword.keys(Pleroma.UserRelationship.Type.__enum_map__()) do
27 # `def create_block/3`, `def create_mute/3`, `def create_reblog_mute/3`,
28 # `def create_notification_mute/3`, `def create_inverse_subscription/3`,
30 def unquote(:"create_#{relationship_type}")(source, target, expires_at \\ nil),
31 do: create(unquote(relationship_type), source, target, expires_at)
33 # `def delete_block/2`, `def delete_mute/2`, `def delete_reblog_mute/2`,
34 # `def delete_notification_mute/2`, `def delete_inverse_subscription/2`,
35 # `def delete_endorsement/2`
36 def unquote(:"delete_#{relationship_type}")(source, target),
37 do: delete(unquote(relationship_type), source, target)
39 # `def block_exists?/2`, `def mute_exists?/2`, `def reblog_mute_exists?/2`,
40 # `def notification_mute_exists?/2`, `def inverse_subscription_exists?/2`,
41 # `def inverse_endorsement_exists?/2`
42 def unquote(:"#{relationship_type}_exists?")(source, target),
43 do: exists?(unquote(relationship_type), source, target)
45 # `def get_block_expire_date/2`, `def get_mute_expire_date/2`,
46 # `def get_reblog_mute_expire_date/2`, `def get_notification_mute_exists?/2`,
47 # `def get_inverse_subscription_expire_date/2`, `def get_inverse_endorsement_expire_date/2`
48 def unquote(:"get_#{relationship_type}_expire_date")(source, target),
49 do: get_expire_date(unquote(relationship_type), source, target)
52 def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
54 def user_relationship_mappings, do: Pleroma.UserRelationship.Type.__enum_map__()
56 def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
58 |> cast(params, [:relationship_type, :source_id, :target_id, :expires_at])
59 |> validate_required([:relationship_type, :source_id, :target_id])
60 |> unique_constraint(:relationship_type,
61 name: :user_relationships_source_id_relationship_type_target_id_index
63 |> validate_not_self_relationship()
66 def exists?(relationship_type, %User{} = source, %User{} = target) do
68 |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
72 def get_expire_date(relationship_type, %User{} = source, %User{} = target) do
73 %UserRelationship{expires_at: expires_at} =
76 relationship_type: ^relationship_type,
77 source_id: ^source.id,
85 def create(relationship_type, %User{} = source, %User{} = target, expires_at \\ nil) do
88 relationship_type: relationship_type,
91 expires_at: expires_at
94 on_conflict: {:replace_all_except, [:id, :inserted_at]},
95 conflict_target: [:source_id, :relationship_type, :target_id],
100 def delete(relationship_type, %User{} = source, %User{} = target) do
101 attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
103 case Repo.get_by(UserRelationship, attrs) do
104 %UserRelationship{} = existing_record -> Repo.delete(existing_record)
112 source_to_target_rel_types \\ nil,
113 target_to_source_rel_types \\ nil
119 [] = _source_to_target_rel_types,
120 [] = _target_to_source_rel_types
128 source_to_target_rel_types,
129 target_to_source_rel_types
131 when is_list(source_users) and is_list(target_users) do
132 source_user_ids = User.binary_id(source_users)
133 target_user_ids = User.binary_id(target_users)
135 get_rel_type_codes = fn rel_type -> user_relationship_mappings()[rel_type] end
137 source_to_target_rel_types =
138 Enum.map(source_to_target_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
140 target_to_source_rel_types =
141 Enum.map(target_to_source_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
146 "(source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?)) OR \
147 (source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?))",
150 ^source_to_target_rel_types,
153 ^target_to_source_rel_types
156 |> select([ur], [ur.relationship_type, ur.source_id, ur.target_id])
160 def exists?(dictionary, rel_type, source, target, func) do
162 is_nil(source) or is_nil(target) ->
166 [rel_type, source.id, target.id] in dictionary
169 func.(source, target)
173 @doc ":relationships option for StatusView / AccountView / NotificationView"
174 def view_relationships_option(reading_user, actors, opts \\ [])
176 def view_relationships_option(nil = _reading_user, _actors, _opts) do
177 %{user_relationships: [], following_relationships: []}
180 def view_relationships_option(%User{} = reading_user, actors, opts) do
181 {source_to_target_rel_types, target_to_source_rel_types} =
182 case opts[:subset] do
184 # Used for statuses rendering (FE needs `muted` flag for each status when statuses load)
188 {[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
191 raise "Unsupported :subset option value: #{inspect(unknown)}"
195 UserRelationship.dictionary(
198 source_to_target_rel_types,
199 target_to_source_rel_types
202 following_relationships =
203 case opts[:subset] do
208 FollowingRelationship.all_between_user_sets([reading_user], actors)
211 raise "Unsupported :subset option value: #{inspect(unknown)}"
214 %{user_relationships: user_relationships, following_relationships: following_relationships}
217 defp validate_not_self_relationship(%Changeset{} = changeset) do
219 |> validate_source_id_target_id_inequality()
220 |> validate_target_id_source_id_inequality()
223 defp validate_source_id_target_id_inequality(%Changeset{} = changeset) do
224 validate_change(changeset, :source_id, fn _, source_id ->
225 if source_id == get_field(changeset, :target_id) do
226 [source_id: "can't be equal to target_id"]
233 defp validate_target_id_source_id_inequality(%Changeset{} = changeset) do
234 validate_change(changeset, :target_id, fn _, target_id ->
235 if target_id == get_field(changeset, :source_id) do
236 [target_id: "can't be equal to source_id"]