2022-03-07 22:06:07 -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-07 22:06:07 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.AdminAPI.AnnouncementController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
alias Pleroma.Announcement
|
|
|
|
alias Pleroma.Web.ControllerHelper
|
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
|
|
|
|
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
2022-03-08 18:55:41 -07:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action in [:create, :delete, :change])
|
2022-03-07 22:06:07 -07:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action in [:index, :show])
|
|
|
|
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
|
|
|
|
|
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.AnnouncementOperation
|
|
|
|
|
2022-03-08 19:26:05 -07:00
|
|
|
defp default_limit, do: 20
|
|
|
|
|
|
|
|
def index(conn, params) do
|
|
|
|
limit = Map.get(params, :limit, default_limit())
|
|
|
|
offset = Map.get(params, :offset, 0)
|
|
|
|
|
|
|
|
announcements = Announcement.list_paginated(%{limit: limit, offset: offset})
|
2022-03-07 22:06:07 -07:00
|
|
|
|
|
|
|
render(conn, "index.json", announcements: announcements)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, %{id: id} = _params) do
|
|
|
|
announcement = Announcement.get_by_id(id)
|
|
|
|
|
|
|
|
if is_nil(announcement) do
|
|
|
|
{:error, :not_found}
|
|
|
|
else
|
|
|
|
render(conn, "show.json", announcement: announcement)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-08 17:12:01 -07:00
|
|
|
def create(%{body_params: params} = conn, _params) do
|
2022-03-08 18:55:41 -07:00
|
|
|
with {:ok, announcement} <- Announcement.add(change_params(params)) do
|
|
|
|
render(conn, "show.json", announcement: announcement)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
{:error, 400}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_params(orig_params) do
|
2022-03-08 17:12:01 -07:00
|
|
|
data =
|
|
|
|
%{}
|
2022-03-08 18:55:41 -07:00
|
|
|
|> Pleroma.Maps.put_if_present("content", orig_params, &Map.fetch(&1, :content))
|
|
|
|
|> Pleroma.Maps.put_if_present("all_day", orig_params, &Map.fetch(&1, :all_day))
|
2022-03-08 17:12:01 -07:00
|
|
|
|
2022-03-08 18:55:41 -07:00
|
|
|
orig_params
|
|
|
|
|> Map.merge(%{data: data})
|
|
|
|
end
|
2022-03-07 22:06:07 -07:00
|
|
|
|
2022-03-08 18:55:41 -07:00
|
|
|
def change(%{body_params: params} = conn, %{id: id} = _params) do
|
|
|
|
with announcement <- Announcement.get_by_id(id),
|
|
|
|
{:exists, true} <- {:exists, not is_nil(announcement)},
|
|
|
|
{:ok, announcement} <- Announcement.update(announcement, change_params(params)) do
|
2022-03-07 22:06:07 -07:00
|
|
|
render(conn, "show.json", announcement: announcement)
|
|
|
|
else
|
2022-03-08 18:55:41 -07:00
|
|
|
{:exists, false} ->
|
|
|
|
{:error, :not_found}
|
|
|
|
|
2022-03-07 22:06:07 -07:00
|
|
|
_ ->
|
|
|
|
{:error, 400}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, %{id: id} = _params) do
|
|
|
|
case Announcement.delete_by_id(id) do
|
|
|
|
:ok ->
|
|
|
|
conn
|
|
|
|
|> ControllerHelper.json_response(:ok, %{})
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:error, :not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|