Implement first pass of announcement admin api

CCBUG: https://git.pleroma.social/pleroma/pleroma/-/issues/2836
CCBUG: https://git.pleroma.social/pleroma/pleroma/-/issues/1470
This commit is contained in:
Tusooa Zhu 2022-03-08 00:06:07 -05:00
parent 4458db3201
commit d7af67012f
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
9 changed files with 392 additions and 0 deletions

View file

@ -0,0 +1,51 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Announcement do
use Ecto.Schema
import Ecto.Changeset, only: [cast: 3, validate_required: 2]
alias Pleroma.Repo
@type t :: %__MODULE__{}
@primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
schema "announcements" do
field(:data, :map)
timestamps()
end
def change(struct, params \\ %{}) do
struct
|> cast(params, [:data])
|> validate_required([:data])
end
def add(params) do
changeset = change(%__MODULE__{}, params)
Repo.insert(changeset)
end
def list_all do
__MODULE__
|> Repo.all()
end
def get_by_id(id) do
Repo.get_by(__MODULE__, id: id)
end
def delete_by_id(id) do
with announcement when not is_nil(announcement) <- get_by_id(id),
{:ok, _} <- Repo.delete(announcement) do
:ok
else
_ ->
:error
end
end
end

View file

@ -0,0 +1,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.AdminAPI.AnnouncementController do
use Pleroma.Web, :controller
alias Pleroma.Announcement
alias Pleroma.Web.ControllerHelper
alias Pleroma.Web.Plugs.OAuthScopesPlug
plug(Pleroma.Web.ApiSpec.CastAndValidate)
plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action in [:create, :delete])
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
def index(conn, _params) do
announcements = Announcement.list_all()
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
def create(%{body_params: %{content: content}} = conn, _params) do
add_params = %{
data: %{
"content" => content
}
}
with {:ok, announcement} <- Announcement.add(add_params) do
render(conn, "show.json", announcement: announcement)
else
_ ->
{: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

View file

@ -0,0 +1,20 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.AnnouncementView do
use Pleroma.Web, :view
def render("index.json", %{announcements: announcements}) do
render_many(announcements, __MODULE__, "show.json")
end
def render("show.json", %{announcement: announcement}) do
%{
id: announcement.id,
content: announcement.data["content"],
published_at: announcement.inserted_at,
updated_at: announcement.updated_at
}
end
end

View file

@ -0,0 +1,109 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ApiSpec.Admin.AnnouncementOperation do
alias OpenApiSpex.Operation
alias OpenApiSpex.Schema
alias Pleroma.Web.ApiSpec.Schemas.Announcement
alias Pleroma.Web.ApiSpec.Schemas.ApiError
import Pleroma.Web.ApiSpec.Helpers
def open_api_operation(action) do
operation = String.to_existing_atom("#{action}_operation")
apply(__MODULE__, operation, [])
end
def index_operation do
%Operation{
tags: ["Announcement managment"],
summary: "Retrieve a list of announcements",
operationId: "AdminAPI.AnnouncementController.index",
security: [%{"oAuth" => ["admin:read"]}],
responses: %{
200 => Operation.response("Response", "application/json", list_of_announcements()),
403 => Operation.response("Forbidden", "application/json", ApiError)
}
}
end
def show_operation do
%Operation{
tags: ["Announcement managment"],
summary: "Display one announcement",
operationId: "AdminAPI.AnnouncementController.show",
security: [%{"oAuth" => ["admin:read"]}],
parameters: [
Operation.parameter(
:id,
:path,
:string,
"announcement id"
)
| admin_api_params()
],
responses: %{
200 => Operation.response("Response", "application/json", Announcement),
403 => Operation.response("Forbidden", "application/json", ApiError),
404 => Operation.response("Not Found", "application/json", ApiError)
}
}
end
def delete_operation do
%Operation{
tags: ["Announcement managment"],
summary: "Delete one announcement",
operationId: "AdminAPI.AnnouncementController.delete",
security: [%{"oAuth" => ["admin:write"]}],
parameters: [
Operation.parameter(
:id,
:path,
:string,
"announcement id"
)
| admin_api_params()
],
responses: %{
200 => Operation.response("Response", "application/json", %Schema{type: :object}),
403 => Operation.response("Forbidden", "application/json", ApiError),
404 => Operation.response("Not Found", "application/json", ApiError)
}
}
end
def create_operation do
%Operation{
tags: ["Announcement managment"],
summary: "Create one announcement",
operationId: "AdminAPI.AnnouncementController.create",
security: [%{"oAuth" => ["admin:write"]}],
requestBody: request_body("Parameters", create_request(), required: true),
responses: %{
200 => Operation.response("Response", "application/json", Announcement),
400 => Operation.response("Bad Request", "application/json", ApiError),
403 => Operation.response("Forbidden", "application/json", ApiError)
}
}
end
def create_request do
%Schema{
title: "AnnouncementCreateRequest",
type: :object,
required: [:content],
properties: %{
content: %Schema{type: :string}
}
}
end
def list_of_announcements do
%Schema{
type: :array,
items: Announcement
}
end
end

View file

@ -0,0 +1,22 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ApiSpec.Schemas.Announcement do
alias OpenApiSpex.Schema
alias Pleroma.Web.ApiSpec.Schemas.FlakeID
require OpenApiSpex
OpenApiSpex.schema(%{
title: "Account",
description: "Response schema for an account",
type: :object,
properties: %{
id: FlakeID,
content: %Schema{type: :string},
published_at: %Schema{type: :string, format: "date-time"},
updated_at: %Schema{type: :string, format: "date-time"}
}
})
end

View file

@ -229,6 +229,11 @@ defmodule Pleroma.Web.Router do
post("/frontends/install", FrontendController, :install)
post("/backups", AdminAPIController, :create_backup)
get("/announcements", AnnouncementController, :index)
post("/announcements", AnnouncementController, :create)
get("/announcements/:id", AnnouncementController, :show)
delete("/announcements/:id", AnnouncementController, :delete)
end
# AdminAPI: admins and mods (staff) can perform these actions (if enabled by config)

View file

@ -0,0 +1,23 @@
defmodule Pleroma.Repo.Migrations.CreateAnnouncements do
use Ecto.Migration
def change do
create_if_not_exists table(:announcements, primary_key: false) do
add(:id, :uuid, primary_key: true)
add(:data, :map)
timestamps()
end
create_if_not_exists table(:announcement_read_relationships) do
add(:user_id, references(:users, type: :uuid, on_delete: :delete_all))
add(:announcement_id, references(:announcements, type: :uuid, on_delete: :delete_all))
timestamps(updated_at: false)
end
create_if_not_exists(
unique_index(:announcement_read_relationships, [:user_id, :announcement_id])
)
end
end

View file

@ -0,0 +1,94 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.AnnouncementControllerTest do
use Pleroma.Web.ConnCase
import Pleroma.Factory
setup do
admin = insert(:user, is_admin: true)
token = insert(:oauth_admin_token, user: admin)
conn =
build_conn()
|> assign(:user, admin)
|> assign(:token, token)
{:ok, %{admin: admin, token: token, conn: conn}}
end
describe "GET /api/v1/pleroma/admin/announcements" do
test "it lists all announcements", %{conn: conn} do
%{id: id} = insert(:announcement)
response =
conn
|> get("/api/v1/pleroma/admin/announcements")
|> json_response_and_validate_schema(:ok)
assert [%{"id" => ^id}] = response
end
end
describe "GET /api/v1/pleroma/admin/announcements/:id" do
test "it displays one announcement", %{conn: conn} do
%{id: id} = insert(:announcement)
response =
conn
|> get("/api/v1/pleroma/admin/announcements/#{id}")
|> json_response_and_validate_schema(:ok)
assert %{"id" => ^id} = response
end
test "it returns not found for non-existent id", %{conn: conn} do
%{id: id} = insert(:announcement)
_response =
conn
|> get("/api/v1/pleroma/admin/announcements/#{id}xxx")
|> json_response_and_validate_schema(:not_found)
end
end
describe "DELETE /api/v1/pleroma/admin/announcements/:id" do
test "it deletes specified announcement", %{conn: conn} do
%{id: id} = insert(:announcement)
_response =
conn
|> delete("/api/v1/pleroma/admin/announcements/#{id}")
|> json_response_and_validate_schema(:ok)
end
test "it returns not found for non-existent id", %{conn: conn} do
%{id: id} = insert(:announcement)
_response =
conn
|> get("/api/v1/pleroma/admin/announcements/#{id}xxx")
|> json_response_and_validate_schema(:not_found)
assert %{id: ^id} = Pleroma.Announcement.get_by_id(id)
end
end
describe "POST /api/v1/pleroma/admin/announcements" do
test "it creates an announcement", %{conn: conn} do
content = "test post announcement api"
response =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/pleroma/admin/announcements", %{
"content" => content
})
|> json_response_and_validate_schema(:ok)
assert %{"content" => ^content} = response
end
end
end

View file

@ -627,4 +627,12 @@ def filter_factory do
context: ["home"]
}
end
def announcement_factory do
%Pleroma.Announcement{
data: %{
"content" => "test announcement"
}
}
end
end