2021-05-06 10:27:04 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.PushSubscription do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the push subscriptions-related GraphQL calls.
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Storage.Page
|
2021-06-03 13:00:49 +00:00
|
|
|
alias Mobilizon.Users
|
2021-05-06 10:27:04 +00:00
|
|
|
alias Mobilizon.Users.{PushSubscription, User}
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
List all of an user's registered push subscriptions
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec list_user_push_subscriptions(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(PushSubscription.t())} | {:error, :unauthenticated}
|
2021-05-06 10:27:04 +00:00
|
|
|
def list_user_push_subscriptions(_parent, %{page: page, limit: limit}, %{
|
|
|
|
context: %{current_user: %User{id: user_id}}
|
|
|
|
}) do
|
|
|
|
%Page{} = page = Users.list_user_push_subscriptions(user_id, page, limit)
|
|
|
|
{:ok, page}
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_user_push_subscriptions(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Register a push subscription
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec register_push_subscription(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, String.t()} | {:error, String.t()}
|
2021-05-06 10:27:04 +00:00
|
|
|
def register_push_subscription(_parent, args, %{
|
|
|
|
context: %{current_user: %User{id: user_id}}
|
|
|
|
}) do
|
2021-05-06 16:39:59 +00:00
|
|
|
case Users.create_push_subscription(Map.put(args, :user_id, user_id)) do
|
|
|
|
{:ok, %PushSubscription{}} ->
|
|
|
|
{:ok, "OK"}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
require Logger
|
|
|
|
Logger.error(inspect(err))
|
|
|
|
{:error, "Something went wrong"}
|
|
|
|
end
|
2021-05-06 10:27:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec unregister_push_subscription(map(), map(), map()) ::
|
|
|
|
{:ok, PushSubscription.t()} | {:error, :unauthorized} | {:error, :not_found}
|
2021-05-06 16:39:59 +00:00
|
|
|
def unregister_push_subscription(_parent, %{endpoint: push_subscription_endpoint}, %{
|
2021-05-06 10:27:04 +00:00
|
|
|
context: %{current_user: %User{id: user_id}}
|
|
|
|
}) do
|
|
|
|
with %PushSubscription{user: %User{id: push_subscription_user_id}} = push_subscription <-
|
2021-05-06 16:39:59 +00:00
|
|
|
Users.get_push_subscription_by_endpoint(push_subscription_endpoint),
|
2021-05-06 10:27:04 +00:00
|
|
|
{:user_owns_push_subscription, true} <-
|
2021-05-06 16:39:59 +00:00
|
|
|
{:user_owns_push_subscription, push_subscription_user_id == user_id},
|
|
|
|
{:ok, %PushSubscription{}} <- Users.delete_push_subscription(push_subscription) do
|
|
|
|
{:ok, "OK"}
|
2021-05-06 10:27:04 +00:00
|
|
|
else
|
|
|
|
{:user_owns_push_subscription, false} ->
|
|
|
|
{:error, :unauthorized}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, :not_found}
|
2021-05-06 16:39:59 +00:00
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
require Logger
|
|
|
|
Logger.error(inspect(err))
|
|
|
|
{:error, "Something went wrong"}
|
2021-05-06 10:27:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|