2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Event do
|
2019-01-03 14:59:59 +01:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Handles the event-related GraphQL calls.
|
2019-01-03 14:59:59 +01:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2020-11-19 17:06:28 +01:00
|
|
|
alias Mobilizon.{Actors, Admin, Events, Users}
|
2019-09-07 19:54:11 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2020-02-07 16:28:49 +01:00
|
|
|
alias Mobilizon.Config
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Events.{Event, EventParticipantStats}
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Users.User
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2020-01-26 21:11:16 +01:00
|
|
|
alias Mobilizon.GraphQL.API
|
2020-01-26 20:34:25 +01:00
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Activity
|
2020-09-29 09:53:48 +02:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2018-12-21 11:45:55 +01:00
|
|
|
# We limit the max number of events that can be retrieved
|
|
|
|
@event_max_limit 100
|
2019-04-11 18:25:32 +02:00
|
|
|
@number_of_related_events 3
|
2018-12-21 11:45:55 +01:00
|
|
|
|
|
|
|
def list_events(_parent, %{page: page, limit: limit}, _resolution)
|
|
|
|
when limit < @event_max_limit do
|
2019-12-20 13:04:34 +01:00
|
|
|
{:ok, Events.list_events(page, limit)}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
2018-12-21 12:21:56 +01:00
|
|
|
def list_events(_parent, %{page: _page, limit: _limit}, _resolution) do
|
2018-12-21 11:45:55 +01:00
|
|
|
{:error, :events_max_limit_reached}
|
|
|
|
end
|
|
|
|
|
2019-10-08 18:13:06 +02:00
|
|
|
defp find_private_event(
|
|
|
|
_parent,
|
|
|
|
%{uuid: uuid},
|
|
|
|
%{context: %{current_user: %User{id: user_id}}} = _resolution
|
|
|
|
) do
|
2019-12-20 13:04:34 +01:00
|
|
|
case {:has_event, Events.get_own_event_by_uuid_with_preload(uuid, user_id)} do
|
2019-10-02 17:59:07 +02:00
|
|
|
{:has_event, %Event{} = event} ->
|
2020-12-15 17:17:42 +01:00
|
|
|
{:ok, event}
|
2019-10-02 17:59:07 +02:00
|
|
|
|
|
|
|
{:has_event, _} ->
|
2020-10-01 15:07:15 +02:00
|
|
|
{:error, :event_not_found}
|
2019-10-02 17:59:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-01 15:07:15 +02:00
|
|
|
defp find_private_event(_parent, _args, _resolution) do
|
|
|
|
{:error, :event_not_found}
|
2020-01-26 20:34:25 +01:00
|
|
|
end
|
2019-10-08 21:31:47 +02:00
|
|
|
|
2020-02-07 16:28:49 +01:00
|
|
|
def find_event(parent, %{uuid: uuid} = args, %{context: context} = resolution) do
|
|
|
|
with {:has_event, %Event{} = event} <-
|
|
|
|
{:has_event, Events.get_public_event_by_uuid_with_preload(uuid)},
|
|
|
|
{:access_valid, true} <-
|
|
|
|
{:access_valid, Map.has_key?(context, :current_user) || check_event_access(event)} do
|
2020-12-15 17:17:42 +01:00
|
|
|
{:ok, event}
|
2020-02-07 16:28:49 +01:00
|
|
|
else
|
2019-09-18 17:32:37 +02:00
|
|
|
{:has_event, _} ->
|
2019-10-08 18:13:06 +02:00
|
|
|
find_private_event(parent, args, resolution)
|
2020-02-07 16:28:49 +01:00
|
|
|
|
|
|
|
{:access_valid, _} ->
|
2020-10-01 15:07:15 +02:00
|
|
|
{:error, :event_not_found}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-09 14:07:49 +02:00
|
|
|
@spec check_event_access(Event.t()) :: boolean()
|
|
|
|
defp check_event_access(%Event{local: true}), do: true
|
2020-02-07 16:28:49 +01:00
|
|
|
|
2020-06-09 14:07:49 +02:00
|
|
|
defp check_event_access(%Event{url: url}) do
|
2020-02-07 16:28:49 +01:00
|
|
|
relay_actor_id = Config.relay_actor_id()
|
|
|
|
Events.check_if_event_has_instance_follow(url, relay_actor_id)
|
|
|
|
end
|
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
@doc """
|
|
|
|
List participants for event (through an event request)
|
|
|
|
"""
|
2019-09-20 18:22:03 +02:00
|
|
|
def list_participants_for_event(
|
2019-09-26 16:38:58 +02:00
|
|
|
%Event{id: event_id},
|
2020-11-19 17:06:28 +01:00
|
|
|
%{page: page, limit: limit, roles: roles},
|
2019-09-26 16:38:58 +02:00
|
|
|
%{context: %{current_user: %User{} = user}} = _resolution
|
2019-09-20 18:22:03 +02:00
|
|
|
) do
|
2020-11-19 17:06:28 +01:00
|
|
|
with %Actor{id: actor_id} <- Users.get_actor_for_user(user),
|
2019-09-26 16:38:58 +02:00
|
|
|
# Check that moderator has right
|
|
|
|
{:actor_approve_permission, true} <-
|
2019-12-20 13:04:34 +01:00
|
|
|
{:actor_approve_permission, Events.moderator_for_event?(event_id, actor_id)} do
|
2019-09-26 16:38:58 +02:00
|
|
|
roles =
|
|
|
|
case roles do
|
|
|
|
"" ->
|
|
|
|
[]
|
2019-09-20 18:22:03 +02:00
|
|
|
|
2019-09-26 16:38:58 +02:00
|
|
|
roles ->
|
|
|
|
roles
|
|
|
|
|> String.split(",")
|
|
|
|
|> Enum.map(&String.downcase/1)
|
|
|
|
|> Enum.map(&String.to_existing_atom/1)
|
|
|
|
end
|
|
|
|
|
2020-06-18 15:23:05 +02:00
|
|
|
participants = Events.list_participants_for_event(event_id, roles, page, limit)
|
|
|
|
{:ok, participants}
|
2019-09-26 16:38:58 +02:00
|
|
|
else
|
|
|
|
{:actor_approve_permission, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error,
|
|
|
|
dgettext("errors", "Provided moderator profile doesn't have permission on this event")}
|
2019-09-26 16:38:58 +02:00
|
|
|
end
|
|
|
|
end
|
2019-09-20 18:22:03 +02:00
|
|
|
|
2019-09-26 16:38:58 +02:00
|
|
|
def list_participants_for_event(_, _args, _resolution) do
|
2020-03-05 19:32:34 +01:00
|
|
|
{:ok, %{total: 0, elements: []}}
|
2018-11-12 23:30:47 +01:00
|
|
|
end
|
|
|
|
|
2020-06-15 19:41:11 +02:00
|
|
|
def stats_participants(
|
|
|
|
%Event{participant_stats: %EventParticipantStats{} = stats, id: event_id} = _event,
|
|
|
|
_args,
|
|
|
|
%{context: %{current_user: %User{id: user_id} = _user}} = _resolution
|
|
|
|
) do
|
|
|
|
if Events.is_user_moderator_for_event?(user_id, event_id) do
|
2020-06-18 16:24:00 +02:00
|
|
|
{:ok,
|
|
|
|
Map.put(
|
|
|
|
stats,
|
|
|
|
:going,
|
|
|
|
stats.participant + stats.moderator + stats.administrator + stats.creator
|
|
|
|
)}
|
2020-06-15 19:41:11 +02:00
|
|
|
else
|
2020-06-18 16:24:00 +02:00
|
|
|
{:ok, %{participant: stats.participant}}
|
2020-06-15 19:41:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-18 16:24:00 +02:00
|
|
|
def stats_participants(
|
|
|
|
%Event{participant_stats: %EventParticipantStats{participant: participant}},
|
|
|
|
_args,
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
{:ok, %EventParticipantStats{participant: participant}}
|
|
|
|
end
|
|
|
|
|
2020-06-15 19:41:11 +02:00
|
|
|
def stats_participants(_event, _args, _resolution) do
|
|
|
|
{:ok, %EventParticipantStats{}}
|
|
|
|
end
|
|
|
|
|
2019-04-11 18:25:32 +02:00
|
|
|
@doc """
|
|
|
|
List related events
|
|
|
|
"""
|
|
|
|
def list_related_events(
|
2019-04-12 17:00:55 +02:00
|
|
|
%Event{tags: tags, organizer_actor: organizer_actor, uuid: uuid},
|
2019-04-11 18:25:32 +02:00
|
|
|
_args,
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
# We get the organizer's next public event
|
|
|
|
events =
|
2019-09-16 02:07:44 +02:00
|
|
|
[Events.get_upcoming_public_event_for_actor(organizer_actor, uuid)]
|
2019-09-02 17:23:00 +02:00
|
|
|
|> Enum.filter(&is_map/1)
|
2019-04-11 18:25:32 +02:00
|
|
|
|
2019-04-12 17:00:55 +02:00
|
|
|
# We find similar events with the same tags
|
2019-04-11 18:25:32 +02:00
|
|
|
# uniq_by : It's possible event_from_same_actor is inside events_from_tags
|
|
|
|
events =
|
2019-09-07 19:54:11 +02:00
|
|
|
events
|
2019-09-16 02:07:44 +02:00
|
|
|
|> Enum.concat(Events.list_events_by_tags(tags, @number_of_related_events))
|
2019-04-11 18:25:32 +02:00
|
|
|
|> uniq_events()
|
|
|
|
|
|
|
|
# TODO: We should use tag_relations to find more appropriate events
|
|
|
|
|
|
|
|
# We've considered all recommended events, so we fetch the latest events
|
|
|
|
events =
|
|
|
|
if @number_of_related_events - length(events) > 0 do
|
2019-09-07 19:54:11 +02:00
|
|
|
events
|
2020-12-09 17:55:38 +01:00
|
|
|
|> Enum.concat(
|
|
|
|
Events.list_events(1, @number_of_related_events, :begins_on, :asc, true).elements
|
|
|
|
)
|
2019-04-11 18:25:32 +02:00
|
|
|
|> uniq_events()
|
|
|
|
else
|
|
|
|
events
|
|
|
|
end
|
|
|
|
|
|
|
|
events =
|
|
|
|
events
|
|
|
|
# We remove the same event from the results
|
|
|
|
|> Enum.filter(fn event -> event.uuid != uuid end)
|
|
|
|
# We return only @number_of_related_events right now
|
|
|
|
|> Enum.take(@number_of_related_events)
|
|
|
|
|
|
|
|
{:ok, events}
|
|
|
|
end
|
|
|
|
|
2019-04-12 17:00:55 +02:00
|
|
|
defp uniq_events(events), do: Enum.uniq_by(events, fn event -> event.uuid end)
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Create an event
|
|
|
|
"""
|
2019-09-02 17:23:00 +02:00
|
|
|
def create_event(
|
|
|
|
_parent,
|
|
|
|
%{organizer_actor_id: organizer_actor_id} = args,
|
2019-09-07 19:54:11 +02:00
|
|
|
%{context: %{current_user: user}} = _resolution
|
2019-09-02 17:23:00 +02:00
|
|
|
) do
|
2019-09-04 18:24:31 +02:00
|
|
|
# See https://github.com/absinthe-graphql/absinthe/issues/490
|
2020-11-19 17:06:28 +01:00
|
|
|
with {:is_owned, %Actor{} = organizer_actor} <- User.owns_actor(user, organizer_actor_id),
|
|
|
|
args <- Map.put(args, :options, args[:options] || %{}),
|
2019-09-02 17:23:00 +02:00
|
|
|
args_with_organizer <- Map.put(args, :organizer_actor, organizer_actor),
|
2019-09-21 23:59:07 +02:00
|
|
|
{:ok, %Activity{data: %{"object" => %{"type" => "Event"}}}, %Event{} = event} <-
|
2020-01-26 21:11:16 +01:00
|
|
|
API.Events.create_event(args_with_organizer) do
|
2019-07-30 10:35:29 +02:00
|
|
|
{:ok, event}
|
2019-09-02 17:23:00 +02:00
|
|
|
else
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Organizer profile is not owned by the user")}
|
2019-10-02 17:59:07 +02:00
|
|
|
|
2019-11-18 18:40:03 +01:00
|
|
|
{:error, _, %Ecto.Changeset{} = error, _} ->
|
|
|
|
{:error, error}
|
|
|
|
|
2019-10-02 17:59:07 +02:00
|
|
|
{:error, %Ecto.Changeset{} = error} ->
|
|
|
|
{:error, error}
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_event(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to create events")}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
2019-01-25 17:06:57 +01:00
|
|
|
|
2019-09-02 17:23:00 +02:00
|
|
|
@doc """
|
|
|
|
Update an event
|
|
|
|
"""
|
|
|
|
def update_event(
|
|
|
|
_parent,
|
|
|
|
%{event_id: event_id} = args,
|
2020-10-09 16:07:13 +02:00
|
|
|
%{context: %{current_user: %User{} = user}} = _resolution
|
2019-09-02 17:23:00 +02:00
|
|
|
) do
|
2019-09-04 18:24:31 +02:00
|
|
|
# See https://github.com/absinthe-graphql/absinthe/issues/490
|
|
|
|
with args <- Map.put(args, :options, args[:options] || %{}),
|
2019-09-21 23:59:07 +02:00
|
|
|
{:ok, %Event{} = event} <- Events.get_event_with_preload(event_id),
|
2020-10-09 18:12:35 +02:00
|
|
|
{:old_actor, {:is_owned, %Actor{}}} <-
|
|
|
|
{:old_actor, User.owns_actor(user, event.organizer_actor_id)},
|
|
|
|
new_organizer_actor_id <- args |> Map.get(:organizer_actor_id, event.organizer_actor_id),
|
|
|
|
{:new_actor, {:is_owned, %Actor{} = organizer_actor}} <-
|
|
|
|
{:new_actor, User.owns_actor(user, new_organizer_actor_id)},
|
2019-09-09 11:21:42 +02:00
|
|
|
args <- Map.put(args, :organizer_actor, organizer_actor),
|
2019-09-21 23:59:07 +02:00
|
|
|
{:ok, %Activity{data: %{"object" => %{"type" => "Event"}}}, %Event{} = event} <-
|
2020-01-26 21:11:16 +01:00
|
|
|
API.Events.update_event(args, event) do
|
2019-09-02 17:23:00 +02:00
|
|
|
{:ok, event}
|
|
|
|
else
|
|
|
|
{:error, :event_not_found} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Event not found")}
|
2019-09-04 18:24:31 +02:00
|
|
|
|
2020-10-09 18:12:35 +02:00
|
|
|
{:old_actor, _} ->
|
|
|
|
{:error, dgettext("errors", "You can't edit this event.")}
|
|
|
|
|
|
|
|
{:new_actor, _} ->
|
2020-10-09 19:29:12 +02:00
|
|
|
{:error, dgettext("errors", "You can't attribute this event to this profile.")}
|
2019-11-18 18:40:03 +01:00
|
|
|
|
|
|
|
{:error, _, %Ecto.Changeset{} = error, _} ->
|
|
|
|
{:error, error}
|
2019-09-02 17:23:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_event(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to update an event")}
|
2019-09-02 17:23:00 +02:00
|
|
|
end
|
|
|
|
|
2019-01-25 17:06:57 +01:00
|
|
|
@doc """
|
|
|
|
Delete an event
|
|
|
|
"""
|
2019-09-02 17:23:00 +02:00
|
|
|
def delete_event(
|
|
|
|
_parent,
|
2020-11-19 17:06:28 +01:00
|
|
|
%{event_id: event_id},
|
2019-09-21 23:59:07 +02:00
|
|
|
%{context: %{current_user: %User{role: role} = user}}
|
2019-09-02 17:23:00 +02:00
|
|
|
) do
|
2019-09-21 23:59:07 +02:00
|
|
|
with {:ok, %Event{local: is_local} = event} <- Events.get_event_with_preload(event_id),
|
2020-11-19 17:06:28 +01:00
|
|
|
%Actor{id: actor_id} = actor <- Users.get_actor_for_user(user) do
|
2019-09-09 09:31:08 +02:00
|
|
|
cond do
|
2019-09-21 23:59:07 +02:00
|
|
|
{:event_can_be_managed, true} == Event.can_be_managed_by(event, actor_id) ->
|
2020-07-09 17:24:28 +02:00
|
|
|
do_delete_event(event, actor)
|
2019-09-09 09:31:08 +02:00
|
|
|
|
|
|
|
role in [:moderator, :administrator] ->
|
2020-07-09 17:24:28 +02:00
|
|
|
with {:ok, res} <- do_delete_event(event, actor, !is_local),
|
2019-09-09 09:31:08 +02:00
|
|
|
%Actor{} = actor <- Actors.get_actor(actor_id) do
|
2020-01-23 21:59:50 +01:00
|
|
|
Admin.log_action(actor, "delete", event)
|
2019-09-21 23:59:07 +02:00
|
|
|
|
2019-09-09 09:31:08 +02:00
|
|
|
{:ok, res}
|
|
|
|
end
|
|
|
|
|
|
|
|
true ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You cannot delete this event")}
|
2019-09-09 09:31:08 +02:00
|
|
|
end
|
2019-01-25 17:06:57 +01:00
|
|
|
else
|
|
|
|
{:error, :event_not_found} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Event not found")}
|
2019-01-25 17:06:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_event(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to delete an event")}
|
2019-01-25 17:06:57 +01:00
|
|
|
end
|
2019-09-09 09:31:08 +02:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp do_delete_event(%Event{} = event, %Actor{} = actor, federate \\ true)
|
|
|
|
when is_boolean(federate) do
|
|
|
|
with {:ok, _activity, event} <- API.Events.delete_event(event, actor) do
|
2019-09-09 09:31:08 +02:00
|
|
|
{:ok, %{id: event.id}}
|
|
|
|
end
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|