total rebase
[anni] / lib / pleroma / web / mastodon_api / controllers / scheduled_activity_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.ScheduledActivityController do
6   use Pleroma.Web, :controller
7
8   import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
9
10   alias Pleroma.ScheduledActivity
11   alias Pleroma.Web.MastodonAPI.MastodonAPI
12   alias Pleroma.Web.Plugs.OAuthScopesPlug
13
14   @oauth_read_actions [:show, :index]
15
16   plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
17   plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in @oauth_read_actions)
18   plug(OAuthScopesPlug, %{scopes: ["write:statuses"]} when action not in @oauth_read_actions)
19   plug(:assign_scheduled_activity when action != :index)
20
21   action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
22
23   defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ScheduledActivityOperation
24
25   @doc "GET /api/v1/scheduled_statuses"
26   def index(%{assigns: %{user: user}, private: %{open_api_spex: %{params: params}}} = conn, _) do
27     params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
28
29     with scheduled_activities <- MastodonAPI.get_scheduled_activities(user, params) do
30       conn
31       |> add_link_headers(scheduled_activities)
32       |> render("index.json", scheduled_activities: scheduled_activities)
33     end
34   end
35
36   @doc "GET /api/v1/scheduled_statuses/:id"
37   def show(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
38     render(conn, "show.json", scheduled_activity: scheduled_activity)
39   end
40
41   @doc "PUT /api/v1/scheduled_statuses/:id"
42   def update(
43         %{
44           assigns: %{scheduled_activity: scheduled_activity},
45           private: %{open_api_spex: %{body_params: params}}
46         } = conn,
47         _
48       ) do
49     with {:ok, scheduled_activity} <- ScheduledActivity.update(scheduled_activity, params) do
50       render(conn, "show.json", scheduled_activity: scheduled_activity)
51     end
52   end
53
54   @doc "DELETE /api/v1/scheduled_statuses/:id"
55   def delete(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
56     with {:ok, scheduled_activity} <- ScheduledActivity.delete(scheduled_activity) do
57       render(conn, "show.json", scheduled_activity: scheduled_activity)
58     end
59   end
60
61   defp assign_scheduled_activity(
62          %{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn,
63          _
64        ) do
65     case ScheduledActivity.get(user, id) do
66       %ScheduledActivity{} = activity -> assign(conn, :scheduled_activity, activity)
67       nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
68     end
69   end
70 end