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
10 alias Pleroma.Web.Push.Subscription
11 alias Pleroma.Web.MastodonAPI.PushSubscriptionView, as: View
13 action_fallback(:errors)
15 plug(Pleroma.Plugs.OAuthScopesPlug, %{scopes: ["push"]})
17 # Creates PushSubscription
18 # POST /api/v1/push/subscription
20 def create(%{assigns: %{user: user, token: token}} = conn, params) do
21 with true <- Push.enabled(),
22 {:ok, _} <- Subscription.delete_if_exists(user, token),
23 {:ok, subscription} <- Subscription.create(user, token, params) do
24 view = View.render("push_subscription.json", subscription: subscription)
29 # Gets PushSubscription
30 # GET /api/v1/push/subscription
32 def get(%{assigns: %{user: user, token: token}} = conn, _params) do
33 with true <- Push.enabled(),
34 {:ok, subscription} <- Subscription.get(user, token) do
35 view = View.render("push_subscription.json", subscription: subscription)
40 # Updates PushSubscription
41 # PUT /api/v1/push/subscription
43 def update(%{assigns: %{user: user, token: token}} = conn, params) do
44 with true <- Push.enabled(),
45 {:ok, subscription} <- Subscription.update(user, token, params) do
46 view = View.render("push_subscription.json", subscription: subscription)
51 # Deletes PushSubscription
52 # DELETE /api/v1/push/subscription
54 def delete(%{assigns: %{user: user, token: token}} = conn, _params) do
55 with true <- Push.enabled(),
56 {:ok, _response} <- Subscription.delete(user, token),
62 def errors(conn, {:error, :not_found}) do
64 |> put_status(:not_found)
65 |> json(dgettext("errors", "Not found"))
68 def errors(conn, _) do
69 Pleroma.Web.MastodonAPI.FallbackController.call(conn, nil)