Cleanup SubscriptionController

This commit is contained in:
Egor Kislitsyn 2020-04-15 22:59:25 +04:00
parent b1c1d2e5e1
commit cf4ebba774
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
4 changed files with 31 additions and 26 deletions

View file

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

View file

@ -2,11 +2,11 @@
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.PushSubscriptionView do defmodule Pleroma.Web.MastodonAPI.SubscriptionView do
use Pleroma.Web, :view use Pleroma.Web, :view
alias Pleroma.Web.Push alias Pleroma.Web.Push
def render("push_subscription.json", %{subscription: subscription}) do def render("show.json", %{subscription: subscription}) do
%{ %{
id: to_string(subscription.id), id: to_string(subscription.id),
endpoint: subscription.endpoint, endpoint: subscription.endpoint,

View file

@ -35,7 +35,10 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
quote do quote do
vapid_details = Application.get_env(:web_push_encryption, :vapid_details, []) vapid_details = Application.get_env(:web_push_encryption, :vapid_details, [])
Application.put_env(:web_push_encryption, :vapid_details, []) Application.put_env(:web_push_encryption, :vapid_details, [])
assert "Something went wrong" == unquote(yield)
assert %{"error" => "Web push subscription is disabled on this Pleroma instance"} ==
unquote(yield)
Application.put_env(:web_push_encryption, :vapid_details, vapid_details) Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
end end
end end
@ -45,7 +48,7 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
assert_error_when_disable_push do assert_error_when_disable_push do
conn conn
|> post("/api/v1/push/subscription", %{}) |> post("/api/v1/push/subscription", %{})
|> json_response(500) |> json_response(403)
end end
end end
@ -74,7 +77,7 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
assert_error_when_disable_push do assert_error_when_disable_push do
conn conn
|> get("/api/v1/push/subscription", %{}) |> get("/api/v1/push/subscription", %{})
|> json_response(500) |> json_response(403)
end end
end end
@ -127,7 +130,7 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
assert_error_when_disable_push do assert_error_when_disable_push do
conn conn
|> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}}) |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
|> json_response(500) |> json_response(403)
end end
end end
@ -155,7 +158,7 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
assert_error_when_disable_push do assert_error_when_disable_push do
conn conn
|> delete("/api/v1/push/subscription", %{}) |> delete("/api/v1/push/subscription", %{})
|> json_response(500) |> json_response(403)
end end
end end

View file

@ -2,10 +2,10 @@
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.PushSubscriptionViewTest do defmodule Pleroma.Web.MastodonAPI.SubscriptionViewTest do
use Pleroma.DataCase use Pleroma.DataCase
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.Web.MastodonAPI.PushSubscriptionView, as: View alias Pleroma.Web.MastodonAPI.SubscriptionView, as: View
alias Pleroma.Web.Push alias Pleroma.Web.Push
test "Represent a subscription" do test "Represent a subscription" do
@ -18,6 +18,6 @@ defmodule Pleroma.Web.MastodonAPI.PushSubscriptionViewTest do
server_key: Keyword.get(Push.vapid_config(), :public_key) server_key: Keyword.get(Push.vapid_config(), :public_key)
} }
assert expected == View.render("push_subscription.json", %{subscription: subscription}) assert expected == View.render("show.json", %{subscription: subscription})
end end
end end