logo

pleroma

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

announcement_controller.ex (1697B)


  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.AnnouncementController do
  5. use Pleroma.Web, :controller
  6. import Pleroma.Web.ControllerHelper,
  7. only: [
  8. json_response: 3
  9. ]
  10. alias Pleroma.Announcement
  11. alias Pleroma.Web.Plugs.OAuthScopesPlug
  12. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  13. # Mastodon docs say this only requires a user token, no scopes needed
  14. # As the op `|` requires at least one scope to be present, we use `&` here.
  15. plug(
  16. OAuthScopesPlug,
  17. %{scopes: [], op: :&}
  18. when action in [:index]
  19. )
  20. # Same as in MastodonAPI specs
  21. plug(
  22. OAuthScopesPlug,
  23. %{scopes: ["write:accounts"]}
  24. when action in [:mark_read]
  25. )
  26. action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
  27. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AnnouncementOperation
  28. @doc "GET /api/v1/announcements"
  29. def index(%{assigns: %{user: user}} = conn, _params) do
  30. render(conn, "index.json", announcements: all_visible(), user: user)
  31. end
  32. def index(conn, _params) do
  33. render(conn, "index.json", announcements: all_visible(), user: nil)
  34. end
  35. defp all_visible do
  36. Announcement.list_all_visible()
  37. end
  38. @doc "POST /api/v1/announcements/:id/dismiss"
  39. def mark_read(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
  40. with announcement when not is_nil(announcement) <- Announcement.get_by_id(id),
  41. {:ok, _} <- Announcement.mark_read_by(announcement, user) do
  42. json_response(conn, :ok, %{})
  43. else
  44. _ ->
  45. {:error, :not_found}
  46. end
  47. end
  48. end