logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

user_relationship.ex (8044B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.UserRelationship do
  5. use Ecto.Schema
  6. import Ecto.Changeset
  7. import Ecto.Query
  8. alias Ecto.Changeset
  9. alias Pleroma.FollowingRelationship
  10. alias Pleroma.Repo
  11. alias Pleroma.User
  12. alias Pleroma.UserRelationship
  13. @type t :: %__MODULE__{}
  14. schema "user_relationships" do
  15. belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
  16. belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
  17. field(:relationship_type, Pleroma.UserRelationship.Type)
  18. field(:expires_at, :utc_datetime)
  19. timestamps(updated_at: false)
  20. end
  21. for relationship_type <- Keyword.keys(Pleroma.UserRelationship.Type.__enum_map__()) do
  22. # `def create_block/3`, `def create_mute/3`, `def create_reblog_mute/3`,
  23. # `def create_notification_mute/3`, `def create_inverse_subscription/3`,
  24. # `def endorsement/3`
  25. def unquote(:"create_#{relationship_type}")(source, target, expires_at \\ nil),
  26. do: create(unquote(relationship_type), source, target, expires_at)
  27. # `def delete_block/2`, `def delete_mute/2`, `def delete_reblog_mute/2`,
  28. # `def delete_notification_mute/2`, `def delete_inverse_subscription/2`,
  29. # `def delete_endorsement/2`
  30. def unquote(:"delete_#{relationship_type}")(source, target),
  31. do: delete(unquote(relationship_type), source, target)
  32. # `def block_exists?/2`, `def mute_exists?/2`, `def reblog_mute_exists?/2`,
  33. # `def notification_mute_exists?/2`, `def inverse_subscription_exists?/2`,
  34. # `def inverse_endorsement_exists?/2`
  35. def unquote(:"#{relationship_type}_exists?")(source, target),
  36. do: exists?(unquote(relationship_type), source, target)
  37. # `def get_block_expire_date/2`, `def get_mute_expire_date/2`,
  38. # `def get_reblog_mute_expire_date/2`, `def get_notification_mute_exists?/2`,
  39. # `def get_inverse_subscription_expire_date/2`, `def get_inverse_endorsement_expire_date/2`
  40. def unquote(:"get_#{relationship_type}_expire_date")(source, target),
  41. do: get_expire_date(unquote(relationship_type), source, target)
  42. end
  43. def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
  44. def user_relationship_mappings, do: Pleroma.UserRelationship.Type.__enum_map__()
  45. def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
  46. user_relationship
  47. |> cast(params, [:relationship_type, :source_id, :target_id, :expires_at])
  48. |> validate_required([:relationship_type, :source_id, :target_id])
  49. |> unique_constraint(:relationship_type,
  50. name: :user_relationships_source_id_relationship_type_target_id_index
  51. )
  52. |> validate_not_self_relationship()
  53. end
  54. def exists?(relationship_type, %User{} = source, %User{} = target) do
  55. UserRelationship
  56. |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
  57. |> Repo.exists?()
  58. end
  59. def get_expire_date(relationship_type, %User{} = source, %User{} = target) do
  60. %UserRelationship{expires_at: expires_at} =
  61. UserRelationship
  62. |> where(
  63. relationship_type: ^relationship_type,
  64. source_id: ^source.id,
  65. target_id: ^target.id
  66. )
  67. |> Repo.one!()
  68. expires_at
  69. end
  70. def create(relationship_type, %User{} = source, %User{} = target, expires_at \\ nil) do
  71. %UserRelationship{}
  72. |> changeset(%{
  73. relationship_type: relationship_type,
  74. source_id: source.id,
  75. target_id: target.id,
  76. expires_at: expires_at
  77. })
  78. |> Repo.insert(
  79. on_conflict: {:replace_all_except, [:id, :inserted_at]},
  80. conflict_target: [:source_id, :relationship_type, :target_id],
  81. returning: true
  82. )
  83. end
  84. def delete(relationship_type, %User{} = source, %User{} = target) do
  85. attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
  86. case Repo.get_by(UserRelationship, attrs) do
  87. %UserRelationship{} = existing_record -> Repo.delete(existing_record)
  88. nil -> {:ok, nil}
  89. end
  90. end
  91. def dictionary(
  92. source_users,
  93. target_users,
  94. source_to_target_rel_types \\ nil,
  95. target_to_source_rel_types \\ nil
  96. )
  97. def dictionary(
  98. _source_users,
  99. _target_users,
  100. [] = _source_to_target_rel_types,
  101. [] = _target_to_source_rel_types
  102. ) do
  103. []
  104. end
  105. def dictionary(
  106. source_users,
  107. target_users,
  108. source_to_target_rel_types,
  109. target_to_source_rel_types
  110. )
  111. when is_list(source_users) and is_list(target_users) do
  112. source_user_ids = User.binary_id(source_users)
  113. target_user_ids = User.binary_id(target_users)
  114. get_rel_type_codes = fn rel_type -> user_relationship_mappings()[rel_type] end
  115. source_to_target_rel_types =
  116. Enum.map(source_to_target_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
  117. target_to_source_rel_types =
  118. Enum.map(target_to_source_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
  119. __MODULE__
  120. |> where(
  121. fragment(
  122. "(source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?)) OR \
  123. (source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?))",
  124. ^source_user_ids,
  125. ^target_user_ids,
  126. ^source_to_target_rel_types,
  127. ^target_user_ids,
  128. ^source_user_ids,
  129. ^target_to_source_rel_types
  130. )
  131. )
  132. |> select([ur], [ur.relationship_type, ur.source_id, ur.target_id])
  133. |> Repo.all()
  134. end
  135. def exists?(dictionary, rel_type, source, target, func) do
  136. cond do
  137. is_nil(source) or is_nil(target) ->
  138. false
  139. dictionary ->
  140. [rel_type, source.id, target.id] in dictionary
  141. true ->
  142. func.(source, target)
  143. end
  144. end
  145. @doc ":relationships option for StatusView / AccountView / NotificationView"
  146. def view_relationships_option(reading_user, actors, opts \\ [])
  147. def view_relationships_option(nil = _reading_user, _actors, _opts) do
  148. %{user_relationships: [], following_relationships: []}
  149. end
  150. def view_relationships_option(%User{} = reading_user, actors, opts) do
  151. {source_to_target_rel_types, target_to_source_rel_types} =
  152. case opts[:subset] do
  153. :source_mutes ->
  154. # Used for statuses rendering (FE needs `muted` flag for each status when statuses load)
  155. {[:mute], []}
  156. nil ->
  157. {[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
  158. unknown ->
  159. raise "Unsupported :subset option value: #{inspect(unknown)}"
  160. end
  161. user_relationships =
  162. UserRelationship.dictionary(
  163. [reading_user],
  164. actors,
  165. source_to_target_rel_types,
  166. target_to_source_rel_types
  167. )
  168. following_relationships =
  169. case opts[:subset] do
  170. :source_mutes ->
  171. []
  172. nil ->
  173. FollowingRelationship.all_between_user_sets([reading_user], actors)
  174. unknown ->
  175. raise "Unsupported :subset option value: #{inspect(unknown)}"
  176. end
  177. %{user_relationships: user_relationships, following_relationships: following_relationships}
  178. end
  179. defp validate_not_self_relationship(%Changeset{} = changeset) do
  180. changeset
  181. |> validate_source_id_target_id_inequality()
  182. |> validate_target_id_source_id_inequality()
  183. end
  184. defp validate_source_id_target_id_inequality(%Changeset{} = changeset) do
  185. validate_change(changeset, :source_id, fn _, source_id ->
  186. if source_id == get_field(changeset, :target_id) do
  187. [source_id: "can't be equal to target_id"]
  188. else
  189. []
  190. end
  191. end)
  192. end
  193. defp validate_target_id_source_id_inequality(%Changeset{} = changeset) do
  194. validate_change(changeset, :target_id, fn _, target_id ->
  195. if target_id == get_field(changeset, :source_id) do
  196. [target_id: "can't be equal to source_id"]
  197. else
  198. []
  199. end
  200. end)
  201. end
  202. end