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 @type t :: %__MODULE__{}
19 schema "user_relationships" do
20 belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
21 belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
22 field(:relationship_type, Pleroma.UserRelationship.Type)
23 field(:expires_at, :utc_datetime)
25 timestamps(updated_at: false)
28 for relationship_type <- Keyword.keys(Pleroma.UserRelationship.Type.__enum_map__()) do
29 # `def create_block/3`, `def create_mute/3`, `def create_reblog_mute/3`,
30 # `def create_notification_mute/3`, `def create_inverse_subscription/3`,
32 def unquote(:"create_#{relationship_type}")(source, target, expires_at \\ nil),
33 do: create(unquote(relationship_type), source, target, expires_at)
35 # `def delete_block/2`, `def delete_mute/2`, `def delete_reblog_mute/2`,
36 # `def delete_notification_mute/2`, `def delete_inverse_subscription/2`,
37 # `def delete_endorsement/2`
38 def unquote(:"delete_#{relationship_type}")(source, target),
39 do: delete(unquote(relationship_type), source, target)
41 # `def block_exists?/2`, `def mute_exists?/2`, `def reblog_mute_exists?/2`,
42 # `def notification_mute_exists?/2`, `def inverse_subscription_exists?/2`,
43 # `def inverse_endorsement_exists?/2`
44 def unquote(:"#{relationship_type}_exists?")(source, target),
45 do: exists?(unquote(relationship_type), source, target)
47 # `def get_block_expire_date/2`, `def get_mute_expire_date/2`,
48 # `def get_reblog_mute_expire_date/2`, `def get_notification_mute_exists?/2`,
49 # `def get_inverse_subscription_expire_date/2`, `def get_inverse_endorsement_expire_date/2`
50 def unquote(:"get_#{relationship_type}_expire_date")(source, target),
51 do: get_expire_date(unquote(relationship_type), source, target)
54 def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
56 def user_relationship_mappings, do: Pleroma.UserRelationship.Type.__enum_map__()
58 def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
60 |> cast(params, [:relationship_type, :source_id, :target_id, :expires_at])
61 |> validate_required([:relationship_type, :source_id, :target_id])
62 |> unique_constraint(:relationship_type,
63 name: :user_relationships_source_id_relationship_type_target_id_index
65 |> validate_not_self_relationship()
68 def exists?(relationship_type, %User{} = source, %User{} = target) do
70 |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
74 def get_expire_date(relationship_type, %User{} = source, %User{} = target) do
75 %UserRelationship{expires_at: expires_at} =
78 relationship_type: ^relationship_type,
79 source_id: ^source.id,
87 def create(relationship_type, %User{} = source, %User{} = target, expires_at \\ nil) do
90 relationship_type: relationship_type,
93 expires_at: expires_at
96 on_conflict: {:replace_all_except, [:id, :inserted_at]},
97 conflict_target: [:source_id, :relationship_type, :target_id],
102 def delete(relationship_type, %User{} = source, %User{} = target) do
103 attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
105 case Repo.get_by(UserRelationship, attrs) do
106 %UserRelationship{} = existing_record -> Repo.delete(existing_record)
114 source_to_target_rel_types \\ nil,
115 target_to_source_rel_types \\ nil
121 [] = _source_to_target_rel_types,
122 [] = _target_to_source_rel_types
130 source_to_target_rel_types,
131 target_to_source_rel_types
133 when is_list(source_users) and is_list(target_users) do
134 source_user_ids = User.binary_id(source_users)
135 target_user_ids = User.binary_id(target_users)
137 get_rel_type_codes = fn rel_type -> user_relationship_mappings()[rel_type] end
139 source_to_target_rel_types =
140 Enum.map(source_to_target_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
142 target_to_source_rel_types =
143 Enum.map(target_to_source_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
148 "(source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?)) OR \
149 (source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?))",
152 ^source_to_target_rel_types,
155 ^target_to_source_rel_types
158 |> select([ur], [ur.relationship_type, ur.source_id, ur.target_id])
162 def exists?(dictionary, rel_type, source, target, func) do
164 is_nil(source) or is_nil(target) ->
168 [rel_type, source.id, target.id] in dictionary
171 func.(source, target)
175 @doc ":relationships option for StatusView / AccountView / NotificationView"
176 def view_relationships_option(reading_user, actors, opts \\ [])
178 def view_relationships_option(nil = _reading_user, _actors, _opts) do
179 %{user_relationships: [], following_relationships: []}
182 def view_relationships_option(%User{} = reading_user, actors, opts) do
183 {source_to_target_rel_types, target_to_source_rel_types} =
184 case opts[:subset] do
186 # Used for statuses rendering (FE needs `muted` flag for each status when statuses load)
190 {[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
193 raise "Unsupported :subset option value: #{inspect(unknown)}"
197 UserRelationship.dictionary(
200 source_to_target_rel_types,
201 target_to_source_rel_types
204 following_relationships =
205 case opts[:subset] do
210 FollowingRelationship.all_between_user_sets([reading_user], actors)
213 raise "Unsupported :subset option value: #{inspect(unknown)}"
216 %{user_relationships: user_relationships, following_relationships: following_relationships}
219 defp validate_not_self_relationship(%Changeset{} = changeset) do
221 |> validate_source_id_target_id_inequality()
222 |> validate_target_id_source_id_inequality()
225 defp validate_source_id_target_id_inequality(%Changeset{} = changeset) do
226 validate_change(changeset, :source_id, fn _, source_id ->
227 if source_id == get_field(changeset, :target_id) do
228 [source_id: "can't be equal to target_id"]
235 defp validate_target_id_source_id_inequality(%Changeset{} = changeset) do
236 validate_change(changeset, :target_id, fn _, target_id ->
237 if target_id == get_field(changeset, :source_id) do
238 [target_id: "can't be equal to source_id"]