Add ability to follow hashtags (#336)
[akkoma] / lib / pleroma / web / mastodon_api / controllers / announcement_controller.ex
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
5 defmodule Pleroma.Web.MastodonAPI.AnnouncementController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper,
9 only: [
10 json_response: 3
11 ]
12
13 alias Pleroma.Announcement
14 alias Pleroma.Web.Plugs.OAuthScopesPlug
15
16 plug(Pleroma.Web.ApiSpec.CastAndValidate)
17
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.
20 plug(
21 OAuthScopesPlug,
22 %{scopes: [], op: :&}
23 when action in [:index]
24 )
25
26 # Same as in MastodonAPI specs
27 plug(
28 OAuthScopesPlug,
29 %{scopes: ["write:accounts"]}
30 when action in [:mark_read]
31 )
32
33 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
34
35 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AnnouncementOperation
36
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)
40 end
41
42 def index(conn, _params) do
43 render(conn, "index.json", announcements: all_visible(), user: nil)
44 end
45
46 defp all_visible do
47 Announcement.list_all_visible()
48 end
49
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, %{})
55 else
56 _ ->
57 {:error, :not_found}
58 end
59 end
60 end