2021-02-24 18:06:48 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Activity do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the activity-related GraphQL calls.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Mobilizon.Users.Guards
|
2021-09-10 09:35:32 +00:00
|
|
|
alias Mobilizon.{Activities, Actors}
|
2021-09-28 17:40:37 +00:00
|
|
|
alias Mobilizon.Activities.Activity
|
2021-02-24 18:06:48 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-05-06 16:39:59 +00:00
|
|
|
alias Mobilizon.Service.Activity.Utils
|
2021-02-24 18:06:48 +00:00
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec group_activity(Actor.t(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Activity.t())} | {:error, :unauthorized | :unauthenticated}
|
2021-03-09 15:20:58 +00:00
|
|
|
def group_activity(%Actor{type: :Group, id: group_id}, %{page: page, limit: limit} = args, %{
|
2021-09-10 09:35:32 +00:00
|
|
|
context: %{current_user: %User{role: role}, current_actor: %Actor{id: actor_id}}
|
2021-02-24 18:06:48 +00:00
|
|
|
}) do
|
2021-09-10 09:35:32 +00:00
|
|
|
if Actors.is_member?(actor_id, group_id) or is_moderator(role) do
|
|
|
|
%Page{total: total, elements: elements} =
|
|
|
|
Activities.list_group_activities_for_member(
|
|
|
|
group_id,
|
|
|
|
actor_id,
|
|
|
|
[type: Map.get(args, :type), author: Map.get(args, :author)],
|
|
|
|
page,
|
|
|
|
limit
|
|
|
|
)
|
2021-02-24 18:06:48 +00:00
|
|
|
|
2021-09-10 09:35:32 +00:00
|
|
|
elements = Enum.map(elements, &Utils.transform_activity/1)
|
2021-02-24 18:06:48 +00:00
|
|
|
|
2021-09-10 09:35:32 +00:00
|
|
|
{:ok, %Page{total: total, elements: elements}}
|
|
|
|
else
|
|
|
|
{:error, :unauthorized}
|
2021-02-24 18:06:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_activity(_, _, _) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
end
|