1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-27 18:31:07 +00:00
mobilizon/lib/graphql/resolvers/followers.ex
Thomas Citharel b5d9b82bdd
Refactor Mobilizon.Federation.ActivityPub and add typespecs
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2021-09-29 16:31:11 +02:00

62 lines
1.9 KiB
Elixir

defmodule Mobilizon.GraphQL.Resolvers.Followers do
@moduledoc """
Handles the followers-related GraphQL calls.
"""
import Mobilizon.Users.Guards
alias Mobilizon.Actors
alias Mobilizon.Actors.{Actor, Follower}
alias Mobilizon.Federation.ActivityPub.Actions
alias Mobilizon.Storage.Page
alias Mobilizon.Users.User
@spec find_followers_for_group(Actor.t(), map(), map()) :: {:ok, Page.t()}
def find_followers_for_group(
%Actor{id: group_id} = group,
%{page: page, limit: limit} = args,
%{
context: %{
current_user: %User{role: user_role},
current_actor: %Actor{id: actor_id}
}
}
) do
if Actors.is_moderator?(actor_id, group_id) or is_moderator(user_role) do
{:ok,
Actors.list_paginated_followers_for_actor(group, Map.get(args, :approved), page, limit)}
else
{:error, :unauthorized}
end
end
def find_followers_for_group(_, _, _), do: {:error, :unauthenticated}
@spec update_follower(any(), map(), map()) :: {:ok, Follower.t()} | {:error, any()}
def update_follower(_, %{id: follower_id, approved: approved}, %{
context: %{
current_actor: %Actor{id: actor_id}
}
}) do
with %Follower{target_actor: %Actor{type: :Group, id: group_id}} = follower <-
Actors.get_follower(follower_id),
{:member, true} <-
{:member, Actors.is_moderator?(actor_id, group_id)},
{:ok, _activity, %Follower{} = follower} <-
(if approved do
Actions.Accept.accept(:follow, follower)
else
Actions.Reject.reject(:follow, follower)
end) do
{:ok, follower}
else
{:member, _} ->
{:error, :unauthorized}
_ ->
{:error,
if(approved, do: "Unable to approve follower", else: "Unable to reject follower")}
end
end
def update_follower(_, _, _), do: {:error, :unauthenticated}
end