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.AdminAPI.AnnouncementController do
6 use Pleroma.Web, :controller
8 alias Pleroma.Announcement
9 alias Pleroma.Web.ControllerHelper
10 alias Pleroma.Web.Plugs.OAuthScopesPlug
12 plug(Pleroma.Web.ApiSpec.CastAndValidate)
13 plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action in [:create, :delete, :change])
14 plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action in [:index, :show])
15 action_fallback(Pleroma.Web.AdminAPI.FallbackController)
17 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.AnnouncementOperation
19 defp default_limit, do: 20
21 def index(conn, params) do
22 limit = Map.get(params, :limit, default_limit())
23 offset = Map.get(params, :offset, 0)
25 announcements = Announcement.list_paginated(%{limit: limit, offset: offset})
27 render(conn, "index.json", announcements: announcements)
30 def show(conn, %{id: id} = _params) do
31 announcement = Announcement.get_by_id(id)
33 if is_nil(announcement) do
36 render(conn, "show.json", announcement: announcement)
40 def create(%{body_params: params} = conn, _params) do
41 with {:ok, announcement} <- Announcement.add(change_params(params)) do
42 render(conn, "show.json", announcement: announcement)
49 def change_params(orig_params) do
52 |> Pleroma.Maps.put_if_present("content", orig_params, &Map.fetch(&1, :content))
53 |> Pleroma.Maps.put_if_present("all_day", orig_params, &Map.fetch(&1, :all_day))
56 |> Map.merge(%{data: data})
59 def change(%{body_params: params} = conn, %{id: id} = _params) do
60 with announcement <- Announcement.get_by_id(id),
61 {:exists, true} <- {:exists, not is_nil(announcement)},
62 {:ok, announcement} <- Announcement.update(announcement, change_params(params)) do
63 render(conn, "show.json", announcement: announcement)
73 def delete(conn, %{id: id} = _params) do
74 case Announcement.delete_by_id(id) do
77 |> ControllerHelper.json_response(:ok, %{})