2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.FeedToken do
|
2019-03-08 18:52:27 +01:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Handles the feed tokens-related GraphQL calls.
|
2019-03-08 18:52:27 +01:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-03-08 18:52:27 +01:00
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Events.FeedToken
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 09:53:48 +02:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
require Logger
|
2019-03-08 18:52:27 +01:00
|
|
|
|
|
|
|
@doc """
|
2020-11-06 15:43:38 +01:00
|
|
|
Create an feed token for an user and optionally a defined actor
|
2019-03-08 18:52:27 +01:00
|
|
|
"""
|
2020-01-26 20:34:25 +01:00
|
|
|
@spec create_feed_token(any, map, map) :: {:ok, FeedToken.t()} | {:error, String.t()}
|
|
|
|
def create_feed_token(
|
|
|
|
_parent,
|
|
|
|
%{actor_id: actor_id},
|
|
|
|
%{context: %{current_user: %User{id: id} = user}}
|
|
|
|
) do
|
2019-09-07 19:54:11 +02:00
|
|
|
with {:is_owned, %Actor{}} <- User.owns_actor(user, actor_id),
|
2020-02-13 15:48:12 +01:00
|
|
|
{:ok, feed_token} <- Events.create_feed_token(%{user_id: id, actor_id: actor_id}) do
|
2021-03-26 19:01:55 +01:00
|
|
|
{:ok, to_short_uuid(feed_token)}
|
2019-03-08 18:52:27 +01:00
|
|
|
else
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2019-03-08 18:52:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
@spec create_feed_token(any, map, map) :: {:ok, FeedToken.t()}
|
|
|
|
def create_feed_token(_parent, %{}, %{context: %{current_user: %User{id: id}}}) do
|
2020-02-13 15:48:12 +01:00
|
|
|
with {:ok, feed_token} <- Events.create_feed_token(%{user_id: id}) do
|
2021-03-26 19:01:55 +01:00
|
|
|
{:ok, to_short_uuid(feed_token)}
|
2019-03-08 18:52:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
@spec create_feed_token(any, map, map) :: {:error, String.t()}
|
2019-03-08 18:52:27 +01:00
|
|
|
def create_feed_token(_parent, _args, %{}) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You are not allowed to create a feed token if not connected")}
|
2019-03-08 18:52:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Delete a feed token
|
|
|
|
"""
|
2020-01-26 20:34:25 +01:00
|
|
|
@spec delete_feed_token(any, map, map) :: {:ok, map} | {:error, String.t()}
|
|
|
|
def delete_feed_token(
|
|
|
|
_parent,
|
|
|
|
%{token: token},
|
|
|
|
%{context: %{current_user: %User{id: id} = _user}}
|
|
|
|
) do
|
2021-03-26 19:01:55 +01:00
|
|
|
with {:ok, token} <- ShortUUID.decode(token),
|
|
|
|
{:ok, token} <- Ecto.UUID.cast(token),
|
2019-03-08 18:52:27 +01:00
|
|
|
{:no_token, %FeedToken{actor: actor, user: %User{} = user} = feed_token} <-
|
|
|
|
{:no_token, Events.get_feed_token(token)},
|
|
|
|
{:token_from_user, true} <- {:token_from_user, id == user.id},
|
|
|
|
{:ok, _} <- Events.delete_feed_token(feed_token) do
|
|
|
|
res = %{user: %{id: id}}
|
|
|
|
res = if is_nil(actor), do: res, else: Map.put(res, :actor, %{id: actor.id})
|
|
|
|
{:ok, res}
|
|
|
|
else
|
|
|
|
{:error, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "No such feed token")}
|
2019-03-08 18:52:27 +01:00
|
|
|
|
|
|
|
:error ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Token is not a valid UUID")}
|
2019-03-08 18:52:27 +01:00
|
|
|
|
2021-03-26 19:01:55 +01:00
|
|
|
{:error, "Invalid input"} ->
|
|
|
|
{:error, dgettext("errors", "Token is not a valid UUID")}
|
|
|
|
|
2019-03-08 18:52:27 +01:00
|
|
|
{:no_token, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Token does not exist")}
|
2019-03-08 18:52:27 +01:00
|
|
|
|
|
|
|
{:token_from_user, false} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You don't have permission to delete this token")}
|
2019-03-08 18:52:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
@spec delete_feed_token(any, map, map) :: {:error, String.t()}
|
2019-03-08 18:52:27 +01:00
|
|
|
def delete_feed_token(_parent, _args, %{}) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You are not allowed to delete a feed token if not connected")}
|
2019-03-08 18:52:27 +01:00
|
|
|
end
|
2021-03-26 19:01:55 +01:00
|
|
|
|
|
|
|
defp to_short_uuid(%FeedToken{token: token} = feed_token) do
|
|
|
|
%FeedToken{feed_token | token: ShortUUID.encode!(token)}
|
|
|
|
end
|
2019-03-08 18:52:27 +01:00
|
|
|
end
|