|
|
|
@ -6,25 +6,22 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionController do
|
|
|
|
|
@moduledoc "The module represents functions to manage user subscriptions."
|
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
|
|
alias Pleroma.Web.MastodonAPI.PushSubscriptionView, as: View
|
|
|
|
|
alias Pleroma.Web.Push
|
|
|
|
|
alias Pleroma.Web.Push.Subscription
|
|
|
|
|
|
|
|
|
|
action_fallback(:errors)
|
|
|
|
|
|
|
|
|
|
plug(Pleroma.Plugs.OAuthScopesPlug, %{scopes: ["push"]})
|
|
|
|
|
|
|
|
|
|
plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
|
|
|
|
|
plug(:restrict_push_enabled)
|
|
|
|
|
|
|
|
|
|
# Creates PushSubscription
|
|
|
|
|
# POST /api/v1/push/subscription
|
|
|
|
|
#
|
|
|
|
|
def create(%{assigns: %{user: user, token: token}} = conn, params) do
|
|
|
|
|
with true <- Push.enabled(),
|
|
|
|
|
{:ok, _} <- Subscription.delete_if_exists(user, token),
|
|
|
|
|
with {:ok, _} <- Subscription.delete_if_exists(user, token),
|
|
|
|
|
{:ok, subscription} <- Subscription.create(user, token, params) do
|
|
|
|
|
view = View.render("push_subscription.json", subscription: subscription)
|
|
|
|
|
json(conn, view)
|
|
|
|
|
render(conn, "show.json", subscription: subscription)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -32,10 +29,8 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionController do
|
|
|
|
|
# GET /api/v1/push/subscription
|
|
|
|
|
#
|
|
|
|
|
def get(%{assigns: %{user: user, token: token}} = conn, _params) do
|
|
|
|
|
with true <- Push.enabled(),
|
|
|
|
|
{:ok, subscription} <- Subscription.get(user, token) do
|
|
|
|
|
view = View.render("push_subscription.json", subscription: subscription)
|
|
|
|
|
json(conn, view)
|
|
|
|
|
with {:ok, subscription} <- Subscription.get(user, token) do
|
|
|
|
|
render(conn, "show.json", subscription: subscription)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -43,10 +38,8 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionController do
|
|
|
|
|
# PUT /api/v1/push/subscription
|
|
|
|
|
#
|
|
|
|
|
def update(%{assigns: %{user: user, token: token}} = conn, params) do
|
|
|
|
|
with true <- Push.enabled(),
|
|
|
|
|
{:ok, subscription} <- Subscription.update(user, token, params) do
|
|
|
|
|
view = View.render("push_subscription.json", subscription: subscription)
|
|
|
|
|
json(conn, view)
|
|
|
|
|
with {:ok, subscription} <- Subscription.update(user, token, params) do
|
|
|
|
|
render(conn, "show.json", subscription: subscription)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -54,11 +47,20 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionController do
|
|
|
|
|
# DELETE /api/v1/push/subscription
|
|
|
|
|
#
|
|
|
|
|
def delete(%{assigns: %{user: user, token: token}} = conn, _params) do
|
|
|
|
|
with true <- Push.enabled(),
|
|
|
|
|
{:ok, _response} <- Subscription.delete(user, token),
|
|
|
|
|
with {:ok, _response} <- Subscription.delete(user, token),
|
|
|
|
|
do: json(conn, %{})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
# fallback action
|
|
|
|
|
#
|
|
|
|
|
def errors(conn, {:error, :not_found}) do
|
|
|
|
|