logo

pleroma

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

mastodon_api.ex (3541B)


  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.Web.MastodonAPI.MastodonAPI do
  5. import Ecto.Query
  6. import Ecto.Changeset
  7. alias Pleroma.Notification
  8. alias Pleroma.Pagination
  9. alias Pleroma.ScheduledActivity
  10. alias Pleroma.User
  11. alias Pleroma.Web.CommonAPI
  12. @spec follow(User.t(), User.t(), map) :: {:ok, User.t()} | {:error, String.t()}
  13. def follow(follower, followed, params \\ %{}) do
  14. result =
  15. if not User.following?(follower, followed) do
  16. CommonAPI.follow(follower, followed)
  17. else
  18. {:ok, follower, followed, nil}
  19. end
  20. with {:ok, follower, _followed, _} <- result do
  21. options = cast_params(params)
  22. set_reblogs_visibility(options[:reblogs], result)
  23. set_subscription(options[:notify], result)
  24. {:ok, follower}
  25. end
  26. end
  27. defp set_reblogs_visibility(false, {:ok, follower, followed, _}) do
  28. CommonAPI.hide_reblogs(follower, followed)
  29. end
  30. defp set_reblogs_visibility(_, {:ok, follower, followed, _}) do
  31. CommonAPI.show_reblogs(follower, followed)
  32. end
  33. defp set_subscription(true, {:ok, follower, followed, _}) do
  34. User.subscribe(follower, followed)
  35. end
  36. defp set_subscription(false, {:ok, follower, followed, _}) do
  37. User.unsubscribe(follower, followed)
  38. end
  39. defp set_subscription(_, _), do: {:ok, nil}
  40. @spec get_followers(User.t(), map()) :: list(User.t())
  41. def get_followers(user, params \\ %{}) do
  42. user
  43. |> User.get_followers_query()
  44. |> Pagination.fetch_paginated(params)
  45. end
  46. def get_friends(user, params \\ %{}) do
  47. user
  48. |> User.get_friends_query()
  49. |> Pagination.fetch_paginated(params)
  50. end
  51. def get_notifications(user, params \\ %{}) do
  52. options =
  53. cast_params(params) |> Map.update(:include_types, [], fn include_types -> include_types end)
  54. options =
  55. if ("pleroma:report" not in options.include_types and
  56. User.privileged?(user, :reports_manage_reports)) or
  57. User.privileged?(user, :reports_manage_reports) do
  58. options
  59. else
  60. options
  61. |> Map.update(:exclude_types, ["pleroma:report"], fn current_exclude_types ->
  62. current_exclude_types ++ ["pleroma:report"]
  63. end)
  64. end
  65. user
  66. |> Notification.for_user_query(options)
  67. |> restrict(:types, options)
  68. |> restrict(:exclude_types, options)
  69. |> restrict(:account_ap_id, options)
  70. |> Pagination.fetch_paginated(params)
  71. end
  72. def get_scheduled_activities(user, params \\ %{}) do
  73. user
  74. |> ScheduledActivity.for_user_query()
  75. |> Pagination.fetch_paginated(params)
  76. end
  77. defp cast_params(params) do
  78. param_types = %{
  79. exclude_types: {:array, :string},
  80. types: {:array, :string},
  81. exclude_visibilities: {:array, :string},
  82. reblogs: :boolean,
  83. with_muted: :boolean,
  84. account_ap_id: :string,
  85. notify: :boolean
  86. }
  87. changeset = cast({%{}, param_types}, params, Map.keys(param_types))
  88. changeset.changes
  89. end
  90. defp restrict(query, :types, %{types: mastodon_types = [_ | _]}) do
  91. where(query, [n], n.type in ^mastodon_types)
  92. end
  93. defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
  94. where(query, [n], n.type not in ^mastodon_types)
  95. end
  96. defp restrict(query, :account_ap_id, %{account_ap_id: account_ap_id}) do
  97. where(query, [n, a], a.actor == ^account_ap_id)
  98. end
  99. defp restrict(query, _, _), do: query
  100. end