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-03-23 15:18:03 +01:00
|
|
|
alias Mobilizon.{Activities, Actors, Users}
|
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-03-09 16:20:58 +01:00
|
|
|
def group_activity(%Actor{type: :Group, id: group_id}, %{page: page, limit: limit} = args, %{
|
2021-02-24 19:06:48 +01:00
|
|
|
context: %{current_user: %User{role: role} = user}
|
|
|
|
}) do
|
2021-09-24 16:46:42 +02:00
|
|
|
case Users.get_actor_for_user(user) do
|
|
|
|
%Actor{id: actor_id} = _actor ->
|
|
|
|
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-24 16:46:42 +02:00
|
|
|
elements = Enum.map(elements, &Utils.transform_activity/1)
|
2021-02-24 19:06:48 +01:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
{:ok, %Page{total: total, elements: elements}}
|
|
|
|
else
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, :user_actor_not_found}
|
2021-02-24 19:06:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_activity(_, _, _) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
end
|