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
|
|
|
|
|
2022-04-07 18:37:44 +02:00
|
|
|
@spec find_followers_for_group(Actor.t(), map(), map()) :: {:ok, Page.t(Follower.t())}
|
2021-01-20 18:16:44 +01:00
|
|
|
def find_followers_for_group(
|
|
|
|
%Actor{id: group_id} = group,
|
2021-11-06 10:09:54 +01:00
|
|
|
args,
|
2021-01-20 18:16:44 +01:00
|
|
|
%{
|
|
|
|
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-11-06 10:09:54 +01:00
|
|
|
followers = group_followers(group, args)
|
|
|
|
|
2021-09-10 11:35:32 +02:00
|
|
|
if Actors.is_moderator?(actor_id, group_id) or is_moderator(user_role) do
|
2021-11-06 10:09:54 +01:00
|
|
|
{:ok, followers}
|
2021-01-20 18:16:44 +01:00
|
|
|
else
|
2021-11-06 10:09:54 +01:00
|
|
|
{:ok, %Page{followers | elements: []}}
|
2021-01-20 18:16:44 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-06 10:09:54 +01:00
|
|
|
def find_followers_for_group(%Actor{} = group, args, _) do
|
|
|
|
followers = group_followers(group, args)
|
|
|
|
{:ok, %Page{followers | elements: []}}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp group_followers(group, %{page: page, limit: limit} = args) do
|
|
|
|
Actors.list_paginated_followers_for_actor(group, Map.get(args, :approved), page, limit)
|
|
|
|
end
|
2021-01-20 18:16:44 +01:00
|
|
|
|
|
|
|
@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}
|
2022-08-26 16:08:58 +02:00
|
|
|
|
|
|
|
def count_followers_for_group(%Actor{type: :Group} = group, _args, _resolution) do
|
|
|
|
{:ok, Actors.count_followers_for_actor(group)}
|
|
|
|
end
|
2021-01-20 18:16:44 +01:00
|
|
|
end
|