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.SubscriptionController do
6 @moduledoc "The module represents functions to manage user subscriptions."
7 use Pleroma.Web, :controller
9 alias Pleroma.Web.MastodonAPI.PushSubscriptionView, as: View
10 alias Pleroma.Web.Push
11 alias Pleroma.Web.Push.Subscription
13 action_fallback(:errors)
15 plug(Pleroma.Plugs.OAuthScopesPlug, %{scopes: ["push"]})
17 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
19 # Creates PushSubscription
20 # POST /api/v1/push/subscription
22 def create(%{assigns: %{user: user, token: token}} = conn, params) do
23 with true <- Push.enabled(),
24 {:ok, _} <- Subscription.delete_if_exists(user, token),
25 {:ok, subscription} <- Subscription.create(user, token, params) do
26 view = View.render("push_subscription.json", subscription: subscription)
31 # Gets PushSubscription
32 # GET /api/v1/push/subscription
34 def get(%{assigns: %{user: user, token: token}} = conn, _params) do
35 with true <- Push.enabled(),
36 {:ok, subscription} <- Subscription.get(user, token) do
37 view = View.render("push_subscription.json", subscription: subscription)
42 # Updates PushSubscription
43 # PUT /api/v1/push/subscription
45 def update(%{assigns: %{user: user, token: token}} = conn, params) do
46 with true <- Push.enabled(),
47 {:ok, subscription} <- Subscription.update(user, token, params) do
48 view = View.render("push_subscription.json", subscription: subscription)
53 # Deletes PushSubscription
54 # DELETE /api/v1/push/subscription
56 def delete(%{assigns: %{user: user, token: token}} = conn, _params) do
57 with true <- Push.enabled(),
58 {:ok, _response} <- Subscription.delete(user, token),
64 def errors(conn, {:error, :not_found}) do
66 |> put_status(:not_found)
67 |> json(dgettext("errors", "Not found"))
70 def errors(conn, _) do
71 Pleroma.Web.MastodonAPI.FallbackController.call(conn, nil)