2019-03-06 13:20:12 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-03-06 13:20:12 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.SubscriptionController do
|
|
|
|
@moduledoc "The module represents functions to manage user subscriptions."
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
alias Pleroma.Web.Push
|
|
|
|
alias Pleroma.Web.Push.Subscription
|
|
|
|
|
|
|
|
action_fallback(:errors)
|
|
|
|
|
2020-05-05 12:43:00 +00:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
plug(:restrict_push_enabled)
|
2020-06-24 06:57:27 +00:00
|
|
|
plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["push"]})
|
2020-04-21 13:29:19 +00:00
|
|
|
|
2020-05-05 12:43:00 +00:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.SubscriptionOperation
|
2019-10-06 14:12:17 +00:00
|
|
|
|
2019-03-06 13:20:12 +00:00
|
|
|
# Creates PushSubscription
|
|
|
|
# POST /api/v1/push/subscription
|
|
|
|
#
|
2020-05-05 12:43:00 +00:00
|
|
|
def create(%{assigns: %{user: user, token: token}, body_params: params} = conn, _) do
|
2020-04-15 18:59:25 +00:00
|
|
|
with {:ok, _} <- Subscription.delete_if_exists(user, token),
|
2019-03-06 13:20:12 +00:00
|
|
|
{:ok, subscription} <- Subscription.create(user, token, params) do
|
2020-04-15 18:59:25 +00:00
|
|
|
render(conn, "show.json", subscription: subscription)
|
2019-03-06 13:20:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Gets PushSubscription
|
|
|
|
# GET /api/v1/push/subscription
|
|
|
|
#
|
2020-05-05 12:43:00 +00:00
|
|
|
def show(%{assigns: %{user: user, token: token}} = conn, _params) do
|
2020-04-15 18:59:25 +00:00
|
|
|
with {:ok, subscription} <- Subscription.get(user, token) do
|
|
|
|
render(conn, "show.json", subscription: subscription)
|
2019-03-06 13:20:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Updates PushSubscription
|
|
|
|
# PUT /api/v1/push/subscription
|
|
|
|
#
|
2020-05-05 12:43:00 +00:00
|
|
|
def update(%{assigns: %{user: user, token: token}, body_params: params} = conn, _) do
|
2020-04-15 18:59:25 +00:00
|
|
|
with {:ok, subscription} <- Subscription.update(user, token, params) do
|
|
|
|
render(conn, "show.json", subscription: subscription)
|
2019-03-06 13:20:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Deletes PushSubscription
|
|
|
|
# DELETE /api/v1/push/subscription
|
|
|
|
#
|
|
|
|
def delete(%{assigns: %{user: user, token: token}} = conn, _params) do
|
2020-04-15 18:59:25 +00:00
|
|
|
with {:ok, _response} <- Subscription.delete(user, token),
|
2019-03-06 13:20:12 +00:00
|
|
|
do: json(conn, %{})
|
|
|
|
end
|
|
|
|
|
2020-04-15 18:59:25 +00:00
|
|
|
defp restrict_push_enabled(conn, _) do
|
|
|
|
if Push.enabled() do
|
|
|
|
conn
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> render_error(:forbidden, "Web push subscription is disabled on this Pleroma instance")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-06 13:20:12 +00:00
|
|
|
# fallback action
|
|
|
|
#
|
|
|
|
def errors(conn, {:error, :not_found}) do
|
|
|
|
conn
|
2019-07-10 09:25:58 +00:00
|
|
|
|> put_status(:not_found)
|
2020-05-05 12:43:00 +00:00
|
|
|
|> json(%{error: dgettext("errors", "Record not found")})
|
2019-03-06 13:20:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def errors(conn, _) do
|
2019-08-26 12:16:40 +00:00
|
|
|
Pleroma.Web.MastodonAPI.FallbackController.call(conn, nil)
|
2019-03-06 13:20:12 +00:00
|
|
|
end
|
|
|
|
end
|