1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.ScheduledActivityController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
10 alias Pleroma.Plugs.OAuthScopesPlug
11 alias Pleroma.ScheduledActivity
12 alias Pleroma.Web.MastodonAPI.MastodonAPI
14 plug(:assign_scheduled_activity when action != :index)
16 @oauth_read_actions [:show, :index]
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)
21 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
23 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
25 @doc "GET /api/v1/scheduled_statuses"
26 def index(%{assigns: %{user: user}} = conn, params) do
27 with scheduled_activities <- MastodonAPI.get_scheduled_activities(user, params) do
29 |> add_link_headers(scheduled_activities)
30 |> render("index.json", scheduled_activities: scheduled_activities)
34 @doc "GET /api/v1/scheduled_statuses/:id"
35 def show(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
36 render(conn, "show.json", scheduled_activity: scheduled_activity)
39 @doc "PUT /api/v1/scheduled_statuses/:id"
40 def update(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, params) do
41 with {:ok, scheduled_activity} <- ScheduledActivity.update(scheduled_activity, params) do
42 render(conn, "show.json", scheduled_activity: scheduled_activity)
46 @doc "DELETE /api/v1/scheduled_statuses/:id"
47 def delete(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
48 with {:ok, scheduled_activity} <- ScheduledActivity.delete(scheduled_activity) do
49 render(conn, "show.json", scheduled_activity: scheduled_activity)
53 defp assign_scheduled_activity(%{assigns: %{user: user}, params: %{"id" => id}} = conn, _) do
54 case ScheduledActivity.get(user, id) do
55 %ScheduledActivity{} = activity -> assign(conn, :scheduled_activity, activity)
56 nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()