1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AnnouncementController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper,
13 alias Pleroma.Announcement
14 alias Pleroma.Web.Plugs.OAuthScopesPlug
16 plug(Pleroma.Web.ApiSpec.CastAndValidate)
18 # Mastodon docs say this only requires a user token, no scopes needed
19 # As the op `|` requires at least one scope to be present, we use `&` here.
23 when action in [:index]
26 # Same as in MastodonAPI specs
29 %{scopes: ["write:accounts"]}
30 when action in [:mark_read]
33 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
35 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AnnouncementOperation
37 @doc "GET /api/v1/announcements"
38 def index(%{assigns: %{user: user}} = conn, _params) do
39 render(conn, "index.json", announcements: all_visible(), user: user)
42 def index(conn, _params) do
43 render(conn, "index.json", announcements: all_visible(), user: nil)
47 Announcement.list_all_visible()
50 @doc "POST /api/v1/announcements/:id/dismiss"
51 def mark_read(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
52 with announcement when not is_nil(announcement) <- Announcement.get_by_id(id),
53 {:ok, _} <- Announcement.mark_read_by(announcement, user) do
54 json_response(conn, :ok, %{})