2018-11-06 10:30:27 +01:00
|
|
|
defmodule MobilizonWeb.Resolvers.Event do
|
2019-01-03 14:59:59 +01:00
|
|
|
@moduledoc """
|
|
|
|
Handles the event-related GraphQL calls
|
|
|
|
"""
|
2018-12-14 17:41:55 +01:00
|
|
|
alias Mobilizon.Activity
|
2019-07-30 10:35:29 +02:00
|
|
|
alias Mobilizon.Addresses
|
|
|
|
alias Mobilizon.Addresses.Address
|
2019-04-11 18:25:32 +02:00
|
|
|
alias Mobilizon.Events
|
2019-02-01 15:38:35 +01:00
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
2019-05-22 14:12:11 +02:00
|
|
|
alias Mobilizon.Media.Picture
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Users.User
|
2019-05-28 10:51:02 +02:00
|
|
|
alias MobilizonWeb.Resolvers.Person
|
2018-11-07 16:09:28 +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
|
2018-12-14 11:23:36 +01:00
|
|
|
{:ok, Mobilizon.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
|
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
def find_event(_parent, %{uuid: uuid}, _resolution) do
|
|
|
|
case Mobilizon.Events.get_event_full_by_uuid(uuid) do
|
|
|
|
nil ->
|
|
|
|
{:error, "Event with UUID #{uuid} not found"}
|
|
|
|
|
|
|
|
event ->
|
2019-05-28 10:51:02 +02:00
|
|
|
{:ok, Map.put(event, :organizer_actor, Person.proxify_pictures(event.organizer_actor))}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
@doc """
|
|
|
|
List participant for event (separate request)
|
|
|
|
"""
|
2019-03-01 17:11:28 +01:00
|
|
|
def list_participants_for_event(_parent, %{uuid: uuid, page: page, limit: limit}, _resolution) do
|
|
|
|
{:ok, Mobilizon.Events.list_participants_for_event(uuid, page, limit)}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
@doc """
|
|
|
|
List participants for event (through an event request)
|
|
|
|
"""
|
2019-01-21 15:08:22 +01:00
|
|
|
def list_participants_for_event(%Event{uuid: uuid}, _args, _resolution) do
|
|
|
|
{:ok, Mobilizon.Events.list_participants_for_event(uuid, 1, 10)}
|
2018-11-12 23:30:47 +01:00
|
|
|
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-02 17:23:00 +02:00
|
|
|
[Events.get_actor_upcoming_public_event(organizer_actor, uuid)]
|
|
|
|
|> 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 =
|
|
|
|
(events ++
|
|
|
|
Events.find_similar_events_by_common_tags(
|
|
|
|
tags,
|
2019-04-12 17:00:55 +02:00
|
|
|
@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
|
|
|
|
(events ++
|
|
|
|
Events.list_events(1, @number_of_related_events, :begins_on, :asc, true, true))
|
|
|
|
|> 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)
|
|
|
|
|
2019-02-01 15:38:35 +01:00
|
|
|
@doc """
|
|
|
|
Join an event for an actor
|
|
|
|
"""
|
|
|
|
def actor_join_event(
|
|
|
|
_parent,
|
|
|
|
%{actor_id: actor_id, event_id: event_id},
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) do
|
|
|
|
with {:is_owned, true, actor} <- User.owns_actor(user, actor_id),
|
2019-08-14 17:45:11 +02:00
|
|
|
{:has_event, {:ok, %Event{} = event}} <-
|
|
|
|
{:has_event, Mobilizon.Events.get_event_full(event_id)},
|
2019-02-01 15:38:35 +01:00
|
|
|
{:error, :participant_not_found} <- Mobilizon.Events.get_participant(event_id, actor_id),
|
2019-08-14 17:45:11 +02:00
|
|
|
{:ok, _activity, participant} <- MobilizonWeb.API.Participations.join(event, actor),
|
2019-02-01 15:38:35 +01:00
|
|
|
participant <-
|
|
|
|
Map.put(participant, :event, event)
|
2019-05-28 10:51:02 +02:00
|
|
|
|> Map.put(:actor, Person.proxify_pictures(actor)) do
|
2019-02-01 15:38:35 +01:00
|
|
|
{:ok, participant}
|
|
|
|
else
|
2019-08-14 17:45:11 +02:00
|
|
|
{:has_event, _} ->
|
|
|
|
{:error, "Event with this ID #{inspect(event_id)} doesn't exist"}
|
|
|
|
|
2019-02-01 15:38:35 +01:00
|
|
|
{:is_owned, false} ->
|
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
|
|
|
|
|
|
|
{:error, :event_not_found} ->
|
|
|
|
{:error, "Event id not found"}
|
|
|
|
|
|
|
|
{:ok, %Participant{}} ->
|
|
|
|
{:error, "You are already a participant of this event"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def actor_join_event(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to join an event"}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Leave an event for an actor
|
|
|
|
"""
|
|
|
|
def actor_leave_event(
|
|
|
|
_parent,
|
|
|
|
%{actor_id: actor_id, event_id: event_id},
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) do
|
2019-08-14 17:45:11 +02:00
|
|
|
with {:is_owned, true, actor} <- User.owns_actor(user, actor_id),
|
|
|
|
{:has_event, {:ok, %Event{} = event}} <-
|
|
|
|
{:has_event, Mobilizon.Events.get_event_full(event_id)},
|
|
|
|
{:ok, _activity, _participant} <- MobilizonWeb.API.Participations.leave(event, actor) do
|
2019-09-02 17:23:00 +02:00
|
|
|
{
|
|
|
|
:ok,
|
|
|
|
%{
|
|
|
|
event: %{
|
|
|
|
id: event_id
|
|
|
|
},
|
|
|
|
actor: %{
|
|
|
|
id: actor_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-01 15:38:35 +01:00
|
|
|
else
|
2019-08-14 17:45:11 +02:00
|
|
|
{:has_event, _} ->
|
|
|
|
{:error, "Event with this ID #{inspect(event_id)} doesn't exist"}
|
|
|
|
|
2019-02-01 15:38:35 +01:00
|
|
|
{:is_owned, false} ->
|
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
|
|
|
|
2019-02-07 17:57:49 +01:00
|
|
|
{:only_organizer, true} ->
|
|
|
|
{:error, "You can't leave event because you're the only event creator participant"}
|
|
|
|
|
2019-02-01 15:38:35 +01:00
|
|
|
{:error, :participant_not_found} ->
|
|
|
|
{:error, "Participant not found"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def actor_leave_event(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to leave an event"}
|
|
|
|
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,
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: user
|
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) 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] || %{}),
|
|
|
|
{:is_owned, true, organizer_actor} <- User.owns_actor(user, organizer_actor_id),
|
2019-09-02 17:23:00 +02:00
|
|
|
{:ok, args} <- save_attached_picture(args),
|
2019-07-30 10:35:29 +02:00
|
|
|
{:ok, args} <- save_physical_address(args),
|
2019-09-02 17:23:00 +02:00
|
|
|
args_with_organizer <- Map.put(args, :organizer_actor, organizer_actor),
|
|
|
|
{
|
|
|
|
:ok,
|
|
|
|
%Activity{
|
|
|
|
data: %{
|
|
|
|
"object" => %{"type" => "Event"} = _object
|
|
|
|
}
|
|
|
|
},
|
|
|
|
%Event{} = event
|
|
|
|
} <-
|
|
|
|
MobilizonWeb.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
|
|
|
|
{:is_owned, false} ->
|
|
|
|
{:error, "Organizer actor id is not owned by the user"}
|
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
|
|
|
|
{:error, "You need to be logged-in to create events"}
|
|
|
|
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,
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: user
|
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) 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] || %{}),
|
|
|
|
{:ok, %Event{} = event} <- Mobilizon.Events.get_event_full(event_id),
|
2019-09-02 17:23:00 +02:00
|
|
|
{:is_owned, true, organizer_actor} <- User.owns_actor(user, event.organizer_actor_id),
|
|
|
|
{:ok, args} <- save_attached_picture(args),
|
|
|
|
{:ok, args} <- save_physical_address(args),
|
2019-09-04 18:24:31 +02:00
|
|
|
args <- Map.put(args, :organizer_actor, organizer_actor),
|
2019-09-02 17:23:00 +02:00
|
|
|
{
|
|
|
|
:ok,
|
|
|
|
%Activity{
|
|
|
|
data: %{
|
|
|
|
"object" => %{"type" => "Event"} = _object
|
|
|
|
}
|
|
|
|
},
|
|
|
|
%Event{} = event
|
|
|
|
} <-
|
2019-09-04 18:24:31 +02:00
|
|
|
MobilizonWeb.API.Events.update_event(args, event) do
|
2019-09-02 17:23:00 +02:00
|
|
|
{:ok, event}
|
|
|
|
else
|
|
|
|
{:error, :event_not_found} ->
|
|
|
|
{:error, "Event not found"}
|
2019-09-04 18:24:31 +02:00
|
|
|
|
|
|
|
{:is_owned, _} ->
|
|
|
|
{:error, "User doesn't own actor"}
|
2019-09-02 17:23:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_event(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to update an event"}
|
|
|
|
end
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
# If we have an attached picture, just transmit it. It will be handled by
|
|
|
|
# Mobilizon.Service.ActivityPub.Utils.make_picture_data/1
|
2019-07-04 17:41:06 +02:00
|
|
|
# However, we need to pass it's actor ID
|
2019-05-22 14:12:11 +02:00
|
|
|
@spec save_attached_picture(map()) :: {:ok, map()}
|
2019-07-04 17:41:06 +02:00
|
|
|
defp save_attached_picture(
|
2019-09-02 17:23:00 +02:00
|
|
|
%{
|
|
|
|
picture: %{
|
|
|
|
picture: %{file: %Plug.Upload{} = _picture} = all_pic
|
|
|
|
}
|
|
|
|
} = args
|
2019-07-04 17:41:06 +02:00
|
|
|
) do
|
|
|
|
{:ok, Map.put(args, :picture, Map.put(all_pic, :actor_id, args.organizer_actor_id))}
|
|
|
|
end
|
2019-05-22 14:12:11 +02:00
|
|
|
|
|
|
|
# Otherwise if we use a previously uploaded picture we need to fetch it from database
|
|
|
|
@spec save_attached_picture(map()) :: {:ok, map()}
|
2019-09-02 17:23:00 +02:00
|
|
|
defp save_attached_picture(
|
|
|
|
%{
|
|
|
|
picture: %{
|
|
|
|
picture_id: picture_id
|
|
|
|
}
|
|
|
|
} = args
|
|
|
|
) do
|
2019-05-22 14:12:11 +02:00
|
|
|
with %Picture{} = picture <- Mobilizon.Media.get_picture(picture_id) do
|
|
|
|
{:ok, Map.put(args, :picture, picture)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec save_attached_picture(map()) :: {:ok, map()}
|
|
|
|
defp save_attached_picture(args), do: {:ok, args}
|
|
|
|
|
2019-07-30 10:35:29 +02:00
|
|
|
@spec save_physical_address(map()) :: {:ok, map()}
|
2019-09-02 17:23:00 +02:00
|
|
|
defp save_physical_address(
|
|
|
|
%{
|
|
|
|
physical_address: %{
|
|
|
|
url: physical_address_url
|
|
|
|
}
|
|
|
|
} = args
|
|
|
|
)
|
2019-08-22 15:57:44 +02:00
|
|
|
when not is_nil(physical_address_url) do
|
2019-07-30 10:35:29 +02:00
|
|
|
with %Address{} = address <- Addresses.get_address_by_url(physical_address_url),
|
2019-08-22 15:57:44 +02:00
|
|
|
args <- Map.put(args, :physical_address, address.url) do
|
2019-07-30 10:35:29 +02:00
|
|
|
{:ok, args}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-22 15:57:44 +02:00
|
|
|
@spec save_physical_address(map()) :: {:ok, map()}
|
|
|
|
defp save_physical_address(%{physical_address: address} = args) do
|
|
|
|
with {:ok, %Address{} = address} <- Addresses.create_address(address),
|
|
|
|
args <- Map.put(args, :physical_address, address.url) do
|
|
|
|
{:ok, args}
|
|
|
|
end
|
|
|
|
end
|
2019-07-30 10:35:29 +02:00
|
|
|
|
|
|
|
@spec save_physical_address(map()) :: {:ok, map()}
|
|
|
|
defp save_physical_address(args), do: {:ok, args}
|
|
|
|
|
2019-01-25 17:06:57 +01:00
|
|
|
@doc """
|
|
|
|
Delete an event
|
|
|
|
"""
|
2019-09-02 17:23:00 +02:00
|
|
|
def delete_event(
|
|
|
|
_parent,
|
|
|
|
%{event_id: event_id, actor_id: actor_id},
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) do
|
2019-01-25 17:06:57 +01:00
|
|
|
with {:ok, %Event{} = event} <- Mobilizon.Events.get_event(event_id),
|
2019-02-01 15:38:35 +01:00
|
|
|
{:is_owned, true, _} <- User.owns_actor(user, actor_id),
|
2019-01-25 17:06:57 +01:00
|
|
|
{:event_can_be_managed, true} <- Event.can_event_be_managed_by(event, actor_id),
|
|
|
|
event <- Mobilizon.Events.delete_event!(event) do
|
|
|
|
{:ok, %{id: event.id}}
|
|
|
|
else
|
|
|
|
{:error, :event_not_found} ->
|
|
|
|
{:error, "Event not found"}
|
|
|
|
|
|
|
|
{:is_owned, false} ->
|
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
|
|
|
|
|
|
|
{:event_can_be_managed, false} ->
|
|
|
|
{:error, "You cannot delete this event"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_event(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to delete an event"}
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|