2019-09-27 06:35:45 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-09-27 06:35:45 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.ScheduledActivityController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
|
|
|
|
|
|
|
|
alias Pleroma.ScheduledActivity
|
|
|
|
alias Pleroma.Web.MastodonAPI.MastodonAPI
|
2020-06-24 10:07:47 +00:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2019-09-27 06:35:45 +00:00
|
|
|
|
2019-10-02 17:42:40 +00:00
|
|
|
@oauth_read_actions [:show, :index]
|
|
|
|
|
2024-01-30 23:33:23 +00:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
|
2019-10-02 17:42:40 +00:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in @oauth_read_actions)
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["write:statuses"]} when action not in @oauth_read_actions)
|
2020-05-05 19:42:18 +00:00
|
|
|
plug(:assign_scheduled_activity when action != :index)
|
2019-10-02 17:42:40 +00:00
|
|
|
|
2019-09-27 06:35:45 +00:00
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ScheduledActivityOperation
|
|
|
|
|
2019-09-27 06:35:45 +00:00
|
|
|
@doc "GET /api/v1/scheduled_statuses"
|
2024-01-30 23:33:23 +00:00
|
|
|
def index(%{assigns: %{user: user}, private: %{open_api_spex: %{params: params}}} = conn, _) do
|
2020-05-05 19:42:18 +00:00
|
|
|
params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
|
|
|
|
|
2019-09-27 06:35:45 +00:00
|
|
|
with scheduled_activities <- MastodonAPI.get_scheduled_activities(user, params) do
|
|
|
|
conn
|
|
|
|
|> add_link_headers(scheduled_activities)
|
|
|
|
|> render("index.json", scheduled_activities: scheduled_activities)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/scheduled_statuses/:id"
|
|
|
|
def show(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
|
|
|
|
render(conn, "show.json", scheduled_activity: scheduled_activity)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "PUT /api/v1/scheduled_statuses/:id"
|
2024-01-30 23:33:23 +00:00
|
|
|
def update(
|
|
|
|
%{
|
|
|
|
assigns: %{scheduled_activity: scheduled_activity},
|
|
|
|
private: %{open_api_spex: %{body_params: params}}
|
|
|
|
} = conn,
|
|
|
|
_
|
|
|
|
) do
|
2019-09-27 06:35:45 +00:00
|
|
|
with {:ok, scheduled_activity} <- ScheduledActivity.update(scheduled_activity, params) do
|
|
|
|
render(conn, "show.json", scheduled_activity: scheduled_activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "DELETE /api/v1/scheduled_statuses/:id"
|
|
|
|
def delete(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
|
|
|
|
with {:ok, scheduled_activity} <- ScheduledActivity.delete(scheduled_activity) do
|
|
|
|
render(conn, "show.json", scheduled_activity: scheduled_activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-30 23:33:23 +00:00
|
|
|
defp assign_scheduled_activity(
|
|
|
|
%{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn,
|
|
|
|
_
|
|
|
|
) do
|
2019-09-27 06:35:45 +00:00
|
|
|
case ScheduledActivity.get(user, id) do
|
|
|
|
%ScheduledActivity{} = activity -> assign(conn, :scheduled_activity, activity)
|
|
|
|
nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|