aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/web/mastodon_api/controllers/announcement_controller.ex
blob: 080af96d5184b2fec169cb73fcbe8c5673c0ffdb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.MastodonAPI.AnnouncementController do
  use Pleroma.Web, :controller

  import Pleroma.Web.ControllerHelper,
    only: [
      json_response: 3
    ]

  alias Pleroma.Announcement
  alias Pleroma.Web.Plugs.OAuthScopesPlug

  plug(Pleroma.Web.ApiSpec.CastAndValidate)

  # Mastodon docs say this only requires a user token, no scopes needed
  # As the op `|` requires at least one scope to be present, we use `&` here.
  plug(
    OAuthScopesPlug,
    %{scopes: [], op: :&}
    when action in [:index]
  )

  # Same as in MastodonAPI specs
  plug(
    OAuthScopesPlug,
    %{scopes: ["write:accounts"]}
    when action in [:mark_read]
  )

  action_fallback(Pleroma.Web.MastodonAPI.FallbackController)

  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AnnouncementOperation

  @doc "GET /api/v1/announcements"
  def index(%{assigns: %{user: user}} = conn, _params) do
    render(conn, "index.json", announcements: all_visible(), user: user)
  end

  def index(conn, _params) do
    render(conn, "index.json", announcements: all_visible(), user: nil)
  end

  defp all_visible do
    Announcement.list_all_visible()
  end

  @doc "POST /api/v1/announcements/:id/dismiss"
  def mark_read(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
    with announcement when not is_nil(announcement) <- Announcement.get_by_id(id),
         {:ok, _} <- Announcement.mark_read_by(announcement, user) do
      json_response(conn, :ok, %{})
    else
      _ ->
        {:error, :not_found}
    end
  end
end