logo

pleroma

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

user_relationship.ex (8279B)


  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 datetime_impl do
  46. Application.get_env(:pleroma, :datetime_impl, Pleroma.DateTime.Impl)
  47. end
  48. def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
  49. user_relationship
  50. |> cast(params, [:relationship_type, :source_id, :target_id, :expires_at, :inserted_at])
  51. |> validate_required([:relationship_type, :source_id, :target_id])
  52. |> unique_constraint(:relationship_type,
  53. name: :user_relationships_source_id_relationship_type_target_id_index
  54. )
  55. |> validate_not_self_relationship()
  56. end
  57. @spec exists?(any(), Pleroma.User.t(), Pleroma.User.t()) :: boolean()
  58. def exists?(relationship_type, %User{} = source, %User{} = target) do
  59. UserRelationship
  60. |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
  61. |> Repo.exists?()
  62. end
  63. def get_expire_date(relationship_type, %User{} = source, %User{} = target) do
  64. %UserRelationship{expires_at: expires_at} =
  65. UserRelationship
  66. |> where(
  67. relationship_type: ^relationship_type,
  68. source_id: ^source.id,
  69. target_id: ^target.id
  70. )
  71. |> Repo.one!()
  72. expires_at
  73. end
  74. def create(relationship_type, %User{} = source, %User{} = target, expires_at \\ nil) do
  75. %UserRelationship{}
  76. |> changeset(%{
  77. relationship_type: relationship_type,
  78. source_id: source.id,
  79. target_id: target.id,
  80. expires_at: expires_at,
  81. inserted_at: datetime_impl().utc_now()
  82. })
  83. |> Repo.insert(
  84. on_conflict: {:replace_all_except, [:id, :inserted_at]},
  85. conflict_target: [:source_id, :relationship_type, :target_id],
  86. returning: true
  87. )
  88. end
  89. def delete(relationship_type, %User{} = source, %User{} = target) do
  90. attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
  91. case Repo.get_by(UserRelationship, attrs) do
  92. %UserRelationship{} = existing_record -> Repo.delete(existing_record)
  93. nil -> {:ok, nil}
  94. end
  95. end
  96. def dictionary(
  97. source_users,
  98. target_users,
  99. source_to_target_rel_types \\ nil,
  100. target_to_source_rel_types \\ nil
  101. )
  102. def dictionary(
  103. _source_users,
  104. _target_users,
  105. [] = _source_to_target_rel_types,
  106. [] = _target_to_source_rel_types
  107. ) do
  108. []
  109. end
  110. def dictionary(
  111. source_users,
  112. target_users,
  113. source_to_target_rel_types,
  114. target_to_source_rel_types
  115. )
  116. when is_list(source_users) and is_list(target_users) do
  117. source_user_ids = User.binary_id(source_users)
  118. target_user_ids = User.binary_id(target_users)
  119. get_rel_type_codes = fn rel_type -> user_relationship_mappings()[rel_type] end
  120. source_to_target_rel_types =
  121. Enum.map(source_to_target_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
  122. target_to_source_rel_types =
  123. Enum.map(target_to_source_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
  124. __MODULE__
  125. |> where(
  126. fragment(
  127. "(source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?)) OR \
  128. (source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?))",
  129. ^source_user_ids,
  130. ^target_user_ids,
  131. ^source_to_target_rel_types,
  132. ^target_user_ids,
  133. ^source_user_ids,
  134. ^target_to_source_rel_types
  135. )
  136. )
  137. |> select([ur], [ur.relationship_type, ur.source_id, ur.target_id])
  138. |> Repo.all()
  139. end
  140. def exists?(dictionary, rel_type, source, target, func) do
  141. cond do
  142. is_nil(source) or is_nil(target) ->
  143. false
  144. dictionary ->
  145. [rel_type, source.id, target.id] in dictionary
  146. true ->
  147. func.(source, target)
  148. end
  149. end
  150. @doc ":relationships option for StatusView / AccountView / NotificationView"
  151. def view_relationships_option(reading_user, actors, opts \\ [])
  152. def view_relationships_option(nil = _reading_user, _actors, _opts) do
  153. %{user_relationships: [], following_relationships: []}
  154. end
  155. def view_relationships_option(%User{} = reading_user, actors, opts) do
  156. {source_to_target_rel_types, target_to_source_rel_types} =
  157. case opts[:subset] do
  158. :source_mutes ->
  159. # Used for statuses rendering (FE needs `muted` flag for each status when statuses load)
  160. {[:mute], []}
  161. nil ->
  162. {[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
  163. unknown ->
  164. raise "Unsupported :subset option value: #{inspect(unknown)}"
  165. end
  166. user_relationships =
  167. UserRelationship.dictionary(
  168. [reading_user],
  169. actors,
  170. source_to_target_rel_types,
  171. target_to_source_rel_types
  172. )
  173. following_relationships =
  174. case opts[:subset] do
  175. :source_mutes ->
  176. []
  177. nil ->
  178. FollowingRelationship.all_between_user_sets([reading_user], actors)
  179. unknown ->
  180. raise "Unsupported :subset option value: #{inspect(unknown)}"
  181. end
  182. %{user_relationships: user_relationships, following_relationships: following_relationships}
  183. end
  184. defp validate_not_self_relationship(%Changeset{} = changeset) do
  185. changeset
  186. |> validate_source_id_target_id_inequality()
  187. |> validate_target_id_source_id_inequality()
  188. end
  189. defp validate_source_id_target_id_inequality(%Changeset{} = changeset) do
  190. validate_change(changeset, :source_id, fn _, source_id ->
  191. if source_id == get_field(changeset, :target_id) do
  192. [source_id: "can't be equal to target_id"]
  193. else
  194. []
  195. end
  196. end)
  197. end
  198. defp validate_target_id_source_id_inequality(%Changeset{} = changeset) do
  199. validate_change(changeset, :target_id, fn _, target_id ->
  200. if target_id == get_field(changeset, :source_id) do
  201. [target_id: "can't be equal to source_id"]
  202. else
  203. []
  204. end
  205. end)
  206. end
  207. end