2020-01-26 20:11:16 +00:00
|
|
|
defmodule Mobilizon.GraphQL.API.Events do
|
2018-12-14 16:41:55 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
API for Events.
|
2018-12-14 16:41:55 +00:00
|
|
|
"""
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2019-09-23 08:26:23 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-10-25 15:43:37 +00:00
|
|
|
alias Mobilizon.Events.Event
|
2020-01-22 01:14:42 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Federation.ActivityPub.{Activity, Utils}
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-03-19 10:16:03 +00:00
|
|
|
@doc """
|
|
|
|
Create an event
|
|
|
|
"""
|
2020-01-26 20:11:16 +00:00
|
|
|
@spec create_event(map) :: {:ok, Activity.t(), Event.t()} | any
|
2019-10-25 15:43:37 +00:00
|
|
|
def create_event(args) do
|
|
|
|
with organizer_actor <- Map.get(args, :organizer_actor),
|
|
|
|
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 15:23:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Update an event
|
|
|
|
"""
|
2020-01-26 20:11:16 +00:00
|
|
|
@spec update_event(map, Event.t()) :: {:ok, Activity.t(), Event.t()} | any
|
2019-10-25 15:43:37 +00:00
|
|
|
def update_event(args, %Event{} = event) do
|
|
|
|
with organizer_actor <- Map.get(args, :organizer_actor),
|
|
|
|
args <-
|
|
|
|
Map.update(args, :picture, nil, fn picture ->
|
|
|
|
process_picture(picture, organizer_actor)
|
|
|
|
end) do
|
2020-07-09 15:24:28 +00:00
|
|
|
ActivityPub.update(event, args, Map.get(args, :draft, false) == false)
|
2019-09-02 15:23:00 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-09 07:31:08 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Trigger the deletion of an event
|
|
|
|
|
|
|
|
If the event is deleted by
|
|
|
|
"""
|
2020-07-09 15:24:28 +00:00
|
|
|
def delete_event(%Event{} = event, %Actor{} = actor, federate \\ true) do
|
|
|
|
ActivityPub.delete(event, actor, federate)
|
2019-09-09 07:31:08 +00:00
|
|
|
end
|
2019-10-25 15:43:37 +00:00
|
|
|
|
|
|
|
defp process_picture(nil, _), do: nil
|
|
|
|
defp process_picture(%{picture_id: _picture_id} = args, _), do: args
|
|
|
|
|
|
|
|
defp process_picture(%{picture: picture}, %Actor{id: actor_id}) do
|
|
|
|
%{
|
|
|
|
file:
|
2020-01-26 19:34:25 +00:00
|
|
|
picture
|
|
|
|
|> Map.get(:file)
|
|
|
|
|> Utils.make_picture_data(description: Map.get(picture, :name)),
|
2019-10-25 15:43:37 +00:00
|
|
|
actor_id: actor_id
|
|
|
|
}
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|