Merge remote-tracking branch 'remotes/origin/develop' into automatic-authentication...
[akkoma] / lib / pleroma / web / mastodon_api / controllers / scheduled_activity_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.Plugs.OAuthScopesPlug
11 alias Pleroma.ScheduledActivity
12 alias Pleroma.Web.MastodonAPI.MastodonAPI
13
14 plug(:assign_scheduled_activity when action != :index)
15
16 @oauth_read_actions [:show, :index]
17
18 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in @oauth_read_actions)
19 plug(OAuthScopesPlug, %{scopes: ["write:statuses"]} when action not in @oauth_read_actions)
20
21 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
22
23 @doc "GET /api/v1/scheduled_statuses"
24 def index(%{assigns: %{user: user}} = conn, params) do
25 with scheduled_activities <- MastodonAPI.get_scheduled_activities(user, params) do
26 conn
27 |> add_link_headers(scheduled_activities)
28 |> render("index.json", scheduled_activities: scheduled_activities)
29 end
30 end
31
32 @doc "GET /api/v1/scheduled_statuses/:id"
33 def show(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
34 render(conn, "show.json", scheduled_activity: scheduled_activity)
35 end
36
37 @doc "PUT /api/v1/scheduled_statuses/:id"
38 def update(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, params) do
39 with {:ok, scheduled_activity} <- ScheduledActivity.update(scheduled_activity, params) do
40 render(conn, "show.json", scheduled_activity: scheduled_activity)
41 end
42 end
43
44 @doc "DELETE /api/v1/scheduled_statuses/:id"
45 def delete(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
46 with {:ok, scheduled_activity} <- ScheduledActivity.delete(scheduled_activity) do
47 render(conn, "show.json", scheduled_activity: scheduled_activity)
48 end
49 end
50
51 defp assign_scheduled_activity(%{assigns: %{user: user}, params: %{"id" => id}} = conn, _) do
52 case ScheduledActivity.get(user, id) do
53 %ScheduledActivity{} = activity -> assign(conn, :scheduled_activity, activity)
54 nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
55 end
56 end
57 end