logo

pleroma

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

list.ex (3606B)


  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.List do
  5. use Ecto.Schema
  6. import Ecto.Query
  7. import Ecto.Changeset
  8. alias Pleroma.Activity
  9. alias Pleroma.Repo
  10. alias Pleroma.User
  11. schema "lists" do
  12. belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
  13. field(:title, :string)
  14. field(:following, {:array, :string}, default: [])
  15. field(:ap_id, :string)
  16. timestamps()
  17. end
  18. def title_changeset(list, attrs \\ %{}) do
  19. list
  20. |> cast(attrs, [:title])
  21. |> validate_required([:title])
  22. end
  23. def follow_changeset(list, attrs \\ %{}) do
  24. list
  25. |> cast(attrs, [:following])
  26. |> validate_required([:following])
  27. end
  28. def for_user(user, _opts) do
  29. query =
  30. from(
  31. l in Pleroma.List,
  32. where: l.user_id == ^user.id,
  33. order_by: [desc: l.id],
  34. limit: 50
  35. )
  36. Repo.all(query)
  37. end
  38. def get(id, %{id: user_id} = _user) do
  39. query =
  40. from(
  41. l in Pleroma.List,
  42. where: l.id == ^id,
  43. where: l.user_id == ^user_id
  44. )
  45. Repo.one(query)
  46. end
  47. def get_by_ap_id(ap_id) do
  48. Repo.get_by(__MODULE__, ap_id: ap_id)
  49. end
  50. def get_following(%Pleroma.List{following: following} = _list) do
  51. q =
  52. from(
  53. u in User,
  54. where: u.follower_address in ^following
  55. )
  56. {:ok, Repo.all(q)}
  57. end
  58. # Get lists the activity should be streamed to.
  59. def get_lists_from_activity(%Activity{actor: ap_id}) do
  60. actor = User.get_cached_by_ap_id(ap_id)
  61. query =
  62. from(
  63. l in Pleroma.List,
  64. where: fragment("? && ?", l.following, ^[actor.follower_address])
  65. )
  66. Repo.all(query)
  67. end
  68. # Get lists to which the account belongs.
  69. def get_lists_account_belongs(%User{} = owner, user) do
  70. Pleroma.List
  71. |> where([l], l.user_id == ^owner.id)
  72. |> where([l], fragment("? = ANY(?)", ^user.follower_address, l.following))
  73. |> Repo.all()
  74. end
  75. def rename(%Pleroma.List{} = list, title) do
  76. list
  77. |> title_changeset(%{title: title})
  78. |> Repo.update()
  79. end
  80. def create(title, %User{} = creator) do
  81. changeset = title_changeset(%Pleroma.List{user_id: creator.id}, %{title: title})
  82. if changeset.valid? do
  83. Repo.transaction(fn ->
  84. list = Repo.insert!(changeset)
  85. list
  86. |> change(ap_id: "#{creator.ap_id}/lists/#{list.id}")
  87. |> Repo.update!()
  88. end)
  89. else
  90. {:error, changeset}
  91. end
  92. end
  93. def follow(%Pleroma.List{id: id}, %User{} = followed) do
  94. list = Repo.get(Pleroma.List, id)
  95. %{following: following} = list
  96. update_follows(list, %{following: Enum.uniq([followed.follower_address | following])})
  97. end
  98. def unfollow(%Pleroma.List{id: id}, %User{} = unfollowed) do
  99. list = Repo.get(Pleroma.List, id)
  100. %{following: following} = list
  101. update_follows(list, %{following: List.delete(following, unfollowed.follower_address)})
  102. end
  103. def delete(%Pleroma.List{} = list) do
  104. Repo.delete(list)
  105. end
  106. def update_follows(%Pleroma.List{} = list, attrs) do
  107. list
  108. |> follow_changeset(attrs)
  109. |> Repo.update()
  110. end
  111. def memberships(%User{follower_address: follower_address}) do
  112. Pleroma.List
  113. |> where([l], ^follower_address in l.following)
  114. |> select([l], l.ap_id)
  115. |> Repo.all()
  116. end
  117. def memberships(_), do: []
  118. def member?(%Pleroma.List{following: following}, %User{follower_address: follower_address}) do
  119. Enum.member?(following, follower_address)
  120. end
  121. def member?(_, _), do: false
  122. end