2019-03-06 06:20:12 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-01 22:08:45 -07:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-03-06 06:20:12 -07: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
|
|
|
|
|
2020-01-20 03:53:14 -07:00
|
|
|
alias Pleroma.Web.MastodonAPI.PushSubscriptionView, as: View
|
2019-03-06 06:20:12 -07:00
|
|
|
alias Pleroma.Web.Push
|
|
|
|
alias Pleroma.Web.Push.Subscription
|
|
|
|
|
|
|
|
action_fallback(:errors)
|
|
|
|
|
2019-09-15 09:22:08 -06:00
|
|
|
plug(Pleroma.Plugs.OAuthScopesPlug, %{scopes: ["push"]})
|
|
|
|
|
2019-10-06 08:12:17 -06:00
|
|
|
plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
|
|
|
|
|
2019-03-06 06:20:12 -07:00
|
|
|
# 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),
|
|
|
|
{:ok, subscription} <- Subscription.create(user, token, params) do
|
|
|
|
view = View.render("push_subscription.json", subscription: subscription)
|
|
|
|
json(conn, view)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Gets PushSubscription
|
|
|
|
# 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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Updates PushSubscription
|
|
|
|
# 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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Deletes PushSubscription
|
|
|
|
# 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),
|
|
|
|
do: json(conn, %{})
|
|
|
|
end
|
|
|
|
|
|
|
|
# fallback action
|
|
|
|
#
|
|
|
|
def errors(conn, {:error, :not_found}) do
|
|
|
|
conn
|
2019-07-10 03:25:58 -06:00
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(dgettext("errors", "Not found"))
|
2019-03-06 06:20:12 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def errors(conn, _) do
|
2019-08-26 06:16:40 -06:00
|
|
|
Pleroma.Web.MastodonAPI.FallbackController.call(conn, nil)
|
2019-03-06 06:20:12 -07:00
|
|
|
end
|
|
|
|
end
|