1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-23 16:33:28 +00:00
mobilizon/lib/graphql/resolvers/feed_token.ex

89 lines
3 KiB
Elixir
Raw Normal View History

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