2021-01-20 18:16:44 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Followers do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the followers-related GraphQL calls.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Mobilizon.Users.Guards
|
2021-09-10 11:35:32 +02:00
|
|
|
alias Mobilizon.Actors
|
2021-01-20 18:16:44 +01:00
|
|
|
alias Mobilizon.Actors.{Actor, Follower}
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actions
|
2021-01-20 18:16:44 +01:00
|
|
|
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: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_user: %User{role: user_role},
|
|
|
|
current_actor: %Actor{id: actor_id}
|
2021-01-20 18:16:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
if Actors.is_moderator?(actor_id, group_id) or is_moderator(user_role) do
|
2021-01-20 18:16:44 +01:00
|
|
|
{:ok,
|
|
|
|
Actors.list_paginated_followers_for_actor(group, Map.get(args, :approved), page, limit)}
|
|
|
|
else
|
2021-09-10 11:35:32 +02:00
|
|
|
{:error, :unauthorized}
|
2021-01-20 18:16:44 +01:00
|
|
|
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: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_actor: %Actor{id: actor_id}
|
2021-01-20 18:16:44 +01:00
|
|
|
}
|
|
|
|
}) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with %Follower{target_actor: %Actor{type: :Group, id: group_id}} = follower <-
|
2021-01-20 18:16:44 +01:00
|
|
|
Actors.get_follower(follower_id),
|
|
|
|
{:member, true} <-
|
|
|
|
{:member, Actors.is_moderator?(actor_id, group_id)},
|
|
|
|
{:ok, _activity, %Follower{} = follower} <-
|
|
|
|
(if approved do
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Accept.accept(:follow, follower)
|
2021-01-20 18:16:44 +01:00
|
|
|
else
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Reject.reject(:follow, follower)
|
2021-01-20 18:16:44 +01:00
|
|
|
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
|