2018-12-14 17:41:55 +01:00
|
|
|
defmodule MobilizonWeb.API.Events do
|
|
|
|
@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
|
2018-12-14 17:41:55 +01:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
2019-09-22 09:24:18 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub.Activity
|
2019-10-25 17:43:37 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub.Utils
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Create an event
|
|
|
|
"""
|
2019-07-30 10:35:29 +02: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),
|
|
|
|
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
|
|
|
|
"""
|
2019-09-04 18:24:31 +02: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),
|
|
|
|
args <-
|
|
|
|
Map.update(args, :picture, nil, fn picture ->
|
|
|
|
process_picture(picture, organizer_actor)
|
|
|
|
end) do
|
|
|
|
ActivityPub.update(:event, 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
|
|
|
|
|
|
|
|
If the event is deleted by
|
|
|
|
"""
|
|
|
|
def delete_event(%Event{} = event, federate \\ true) do
|
|
|
|
ActivityPub.delete(event, federate)
|
|
|
|
end
|
2019-10-25 17:43:37 +02: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:
|
|
|
|
picture |> Map.get(:file) |> Utils.make_picture_data(description: Map.get(picture, :name)),
|
|
|
|
actor_id: actor_id
|
|
|
|
}
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|