Mastodon-compatible webhooks
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
parent
1efe48672d
commit
9d7bdab7a2
25 changed files with 1059 additions and 6 deletions
88
lib/pleroma/web/admin_api/controllers/webhook_controller.ex
Normal file
88
lib/pleroma/web/admin_api/controllers/webhook_controller.ex
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
# 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.WebhookController do
|
||||
use Pleroma.Web, :controller
|
||||
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
||||
alias Pleroma.Webhook
|
||||
|
||||
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
||||
|
||||
plug(
|
||||
OAuthScopesPlug,
|
||||
%{scopes: ["admin:write"]}
|
||||
when action in [:update, :create, :delete, :enable, :disable, :rotate_secret]
|
||||
)
|
||||
|
||||
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.WebhookOperation
|
||||
|
||||
def index(conn, _) do
|
||||
webhooks =
|
||||
Webhook
|
||||
|> Repo.all()
|
||||
|
||||
render(conn, "index.json", webhooks: webhooks)
|
||||
end
|
||||
|
||||
def show(conn, %{id: id}) do
|
||||
with %Webhook{} = webhook <- Webhook.get(id) do
|
||||
render(conn, "show.json", webhook: webhook)
|
||||
else
|
||||
nil -> {:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
def create(%{body_params: params} = conn, _) do
|
||||
with webhook <- Webhook.create(params) do
|
||||
render(conn, "show.json", webhook: webhook)
|
||||
end
|
||||
end
|
||||
|
||||
def update(%{body_params: params} = conn, %{id: id}) do
|
||||
with %Webhook{} = webhook <- Webhook.get(id),
|
||||
webhook <- Webhook.update(webhook, params) do
|
||||
render(conn, "show.json", webhook: webhook)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{id: id}) do
|
||||
with %Webhook{} = webhook <- Webhook.get(id),
|
||||
{:ok, webhook} <- Webhook.delete(webhook) do
|
||||
render(conn, "show.json", webhook: webhook)
|
||||
end
|
||||
end
|
||||
|
||||
def enable(conn, %{id: id}) do
|
||||
with %Webhook{} = webhook <- Webhook.get(id),
|
||||
{:ok, webhook} <- Webhook.set_enabled(webhook, true) do
|
||||
render(conn, "show.json", webhook: webhook)
|
||||
else
|
||||
nil -> {:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
def disable(conn, %{id: id}) do
|
||||
with %Webhook{} = webhook <- Webhook.get(id),
|
||||
{:ok, webhook} <- Webhook.set_enabled(webhook, false) do
|
||||
render(conn, "show.json", webhook: webhook)
|
||||
else
|
||||
nil -> {:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
def rotate_secret(conn, %{id: id}) do
|
||||
with %Webhook{} = webhook <- Webhook.get(id),
|
||||
{:ok, webhook} <- Webhook.rotate_secret(webhook) do
|
||||
render(conn, "show.json", webhook: webhook)
|
||||
else
|
||||
nil -> {:error, :not_found}
|
||||
end
|
||||
end
|
||||
end
|
||||
33
lib/pleroma/web/admin_api/views/webhook_view.ex
Normal file
33
lib/pleroma/web/admin_api/views/webhook_view.ex
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
# 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.WebhookView do
|
||||
use Pleroma.Web, :view
|
||||
|
||||
alias Pleroma.Web.CommonAPI.Utils
|
||||
|
||||
def render("index.json", %{webhooks: webhooks}) do
|
||||
render_many(webhooks, __MODULE__, "show.json")
|
||||
end
|
||||
|
||||
def render("show.json", %{webhook: webhook}) do
|
||||
%{
|
||||
id: webhook.id |> to_string(),
|
||||
url: webhook.url,
|
||||
events: webhook.events,
|
||||
secret: webhook.secret,
|
||||
enabled: webhook.enabled,
|
||||
created_at: Utils.to_masto_date(webhook.inserted_at),
|
||||
updated_at: Utils.to_masto_date(webhook.updated_at)
|
||||
}
|
||||
end
|
||||
|
||||
def render("event.json", %{type: type, object: object}) do
|
||||
%{
|
||||
type: type,
|
||||
created_at: Utils.to_masto_date(NaiveDateTime.utc_now()),
|
||||
object: object
|
||||
}
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue