2022-03-08 10:07:21 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2022-03-08 10:07:21 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.AnnouncementController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2022-03-08 14:59:20 -07:00
|
|
|
import Pleroma.Web.ControllerHelper,
|
|
|
|
only: [
|
|
|
|
json_response: 3
|
|
|
|
]
|
2022-03-08 10:07:21 -07:00
|
|
|
|
|
|
|
alias Pleroma.Announcement
|
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
|
|
|
|
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
2022-04-02 00:25:13 -06:00
|
|
|
# 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.
|
2022-03-08 10:07:21 -07:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2022-04-02 00:25:13 -06:00
|
|
|
%{scopes: [], op: :&}
|
|
|
|
when action in [:index]
|
2022-03-08 10:07:21 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
# Same as in MastodonAPI specs
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2022-04-02 00:25:13 -06:00
|
|
|
%{scopes: ["write:accounts"]}
|
2022-03-08 10:07:21 -07:00
|
|
|
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
|
2022-03-08 17:17:49 -07:00
|
|
|
Announcement.list_all_visible()
|
2022-03-08 10:07:21 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/announcements/:id/dismiss"
|
2022-03-08 14:59:20 -07:00
|
|
|
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
|
2022-03-08 10:07:21 -07:00
|
|
|
end
|
|
|
|
end
|