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