2020-01-26 21:11:16 +01:00
|
|
|
defmodule Mobilizon.GraphQL.API.Events do
|
2018-12-14 17:41:55 +01:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
API for Events.
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-09-23 10:26:23 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-10-25 17:43:37 +02:00
|
|
|
alias Mobilizon.Events.Event
|
2020-01-22 02:14:42 +01:00
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Federation.ActivityPub.{Activity, Utils}
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.GraphQL.API.Utils, as: APIUtils
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Create an event
|
|
|
|
"""
|
2020-01-26 21:11:16 +01:00
|
|
|
@spec create_event(map) :: {:ok, Activity.t(), Event.t()} | any
|
2019-10-25 17:43:37 +02:00
|
|
|
def create_event(args) do
|
|
|
|
with organizer_actor <- Map.get(args, :organizer_actor),
|
2020-11-26 11:41:13 +01:00
|
|
|
args <- extract_pictures_from_event_body(args, organizer_actor),
|
2019-10-25 17:43:37 +02:00
|
|
|
args <-
|
|
|
|
Map.update(args, :picture, nil, fn picture ->
|
|
|
|
process_picture(picture, organizer_actor)
|
|
|
|
end) do
|
|
|
|
# For now we don't federate drafts but it will be needed if we want to edit them as groups
|
|
|
|
ActivityPub.create(:event, args, args.draft == false)
|
2019-09-02 17:23:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Update an event
|
|
|
|
"""
|
2020-01-26 21:11:16 +01:00
|
|
|
@spec update_event(map, Event.t()) :: {:ok, Activity.t(), Event.t()} | any
|
2019-10-25 17:43:37 +02:00
|
|
|
def update_event(args, %Event{} = event) do
|
|
|
|
with organizer_actor <- Map.get(args, :organizer_actor),
|
2020-11-26 11:41:13 +01:00
|
|
|
args <- extract_pictures_from_event_body(args, organizer_actor),
|
2019-10-25 17:43:37 +02:00
|
|
|
args <-
|
|
|
|
Map.update(args, :picture, nil, fn picture ->
|
|
|
|
process_picture(picture, organizer_actor)
|
|
|
|
end) do
|
2020-07-09 17:24:28 +02:00
|
|
|
ActivityPub.update(event, args, Map.get(args, :draft, false) == false)
|
2019-09-02 17:23:00 +02:00
|
|
|
end
|
|
|
|
end
|
2019-09-09 09:31:08 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Trigger the deletion of an event
|
|
|
|
"""
|
2020-07-09 17:24:28 +02:00
|
|
|
def delete_event(%Event{} = event, %Actor{} = actor, federate \\ true) do
|
|
|
|
ActivityPub.delete(event, actor, federate)
|
2019-09-09 09:31:08 +02:00
|
|
|
end
|
2019-10-25 17:43:37 +02:00
|
|
|
|
|
|
|
defp process_picture(nil, _), do: nil
|
2020-11-26 11:41:13 +01:00
|
|
|
defp process_picture(%{media_id: _picture_id} = args, _), do: args
|
2019-10-25 17:43:37 +02:00
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
defp process_picture(%{media: media}, %Actor{id: actor_id}) do
|
2019-10-25 17:43:37 +02:00
|
|
|
%{
|
|
|
|
file:
|
2020-11-26 11:41:13 +01:00
|
|
|
media
|
2020-01-26 20:34:25 +01:00
|
|
|
|> Map.get(:file)
|
2020-11-26 11:41:13 +01:00
|
|
|
|> Utils.make_media_data(description: Map.get(media, :name)),
|
2019-10-25 17:43:37 +02:00
|
|
|
actor_id: actor_id
|
|
|
|
}
|
|
|
|
end
|
2020-11-26 11:41:13 +01:00
|
|
|
|
|
|
|
@spec extract_pictures_from_event_body(map(), Actor.t()) :: map()
|
|
|
|
defp extract_pictures_from_event_body(
|
|
|
|
%{description: description} = args,
|
|
|
|
%Actor{id: organizer_actor_id}
|
|
|
|
) do
|
|
|
|
pictures = APIUtils.extract_pictures_from_body(description, organizer_actor_id)
|
|
|
|
Map.put(args, :media, pictures)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp extract_pictures_from_event_body(args, _), do: args
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|