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.Activities.Activity
|
|
|
|
alias Mobilizon.Actors.Actor
|
2021-03-23 15:18:03 +01:00
|
|
|
alias Mobilizon.Service.Activity, as: ActivityService
|
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
|
|
|
|
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id) or is_moderator(role)} do
|
|
|
|
%Page{total: total, elements: elements} =
|
2021-03-09 16:20:58 +01:00
|
|
|
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
|
|
|
|
|
|
|
elements =
|
|
|
|
Enum.map(elements, fn %Activity{} = activity ->
|
|
|
|
activity
|
|
|
|
|> Map.update(:subject_params, %{}, &transform_params/1)
|
2021-03-23 15:18:03 +01:00
|
|
|
|> Map.put(:object, ActivityService.object(activity))
|
2021-02-24 19:06:48 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
{:ok, %Page{total: total, elements: elements}}
|
|
|
|
else
|
|
|
|
{:member, false} ->
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_activity(_, _, _) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec transform_params(map()) :: list()
|
|
|
|
defp transform_params(params) do
|
|
|
|
Enum.map(params, fn {key, value} -> %{key: key, value: transform_value(value)} end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp transform_value(value) when is_list(value) do
|
|
|
|
Enum.join(value, ",")
|
|
|
|
end
|
|
|
|
|
|
|
|
defp transform_value(value), do: value
|
|
|
|
end
|