2018-12-14 16:41:55 +00:00
|
|
|
defmodule MobilizonWeb.API.Events do
|
|
|
|
@moduledoc """
|
|
|
|
API for Events
|
|
|
|
"""
|
2019-09-04 16:24:31 +00:00
|
|
|
alias Mobilizon.Events.Event
|
2018-12-14 16:41:55 +00:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
|
2019-09-22 07:24:18 +00:00
|
|
|
alias Mobilizon.Service.ActivityPub.Activity
|
2019-07-26 09:30:28 +00:00
|
|
|
alias MobilizonWeb.API.Utils
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-03-19 10:16:03 +00:00
|
|
|
@doc """
|
|
|
|
Create an event
|
|
|
|
"""
|
2019-07-30 08:35:29 +00:00
|
|
|
@spec create_event(map()) :: {:ok, Activity.t(), Event.t()} | any()
|
2019-09-02 15:23:00 +00:00
|
|
|
def create_event(%{organizer_actor: organizer_actor} = args) do
|
|
|
|
with %{
|
|
|
|
title: title,
|
|
|
|
physical_address: physical_address,
|
|
|
|
picture: picture,
|
|
|
|
content_html: content_html,
|
|
|
|
tags: tags,
|
|
|
|
to: to,
|
|
|
|
cc: cc,
|
|
|
|
begins_on: begins_on,
|
2019-09-04 16:24:31 +00:00
|
|
|
ends_on: ends_on,
|
2019-09-02 15:23:00 +00:00
|
|
|
category: category,
|
2019-09-20 16:22:03 +00:00
|
|
|
join_options: join_options,
|
2019-09-02 15:23:00 +00:00
|
|
|
options: options
|
|
|
|
} <- prepare_args(args),
|
|
|
|
event <-
|
|
|
|
ActivityPubUtils.make_event_data(
|
|
|
|
organizer_actor.url,
|
|
|
|
%{to: to, cc: cc},
|
|
|
|
title,
|
|
|
|
content_html,
|
|
|
|
picture,
|
|
|
|
tags,
|
2019-09-04 16:24:31 +00:00
|
|
|
%{
|
|
|
|
begins_on: begins_on,
|
|
|
|
ends_on: ends_on,
|
|
|
|
physical_address: physical_address,
|
|
|
|
category: category,
|
2019-09-20 16:22:03 +00:00
|
|
|
options: options,
|
|
|
|
join_options: join_options
|
2019-09-04 16:24:31 +00:00
|
|
|
}
|
2019-09-02 15:23:00 +00:00
|
|
|
) do
|
|
|
|
ActivityPub.create(%{
|
|
|
|
to: ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
actor: organizer_actor,
|
|
|
|
object: event,
|
|
|
|
local: true
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Update an event
|
|
|
|
"""
|
2019-09-04 16:24:31 +00:00
|
|
|
@spec update_event(map(), Event.t()) :: {:ok, Activity.t(), Event.t()} | any()
|
2019-09-02 15:23:00 +00:00
|
|
|
def update_event(
|
2018-12-14 16:41:55 +00:00
|
|
|
%{
|
2019-09-04 16:24:31 +00:00
|
|
|
organizer_actor: organizer_actor
|
|
|
|
} = args,
|
|
|
|
%Event{} = event
|
2019-09-02 15:23:00 +00:00
|
|
|
) do
|
2019-09-04 16:24:31 +00:00
|
|
|
with args <- Map.put(args, :tags, Map.get(args, :tags, [])),
|
|
|
|
%{
|
2019-09-02 15:23:00 +00:00
|
|
|
title: title,
|
|
|
|
physical_address: physical_address,
|
|
|
|
picture: picture,
|
|
|
|
content_html: content_html,
|
|
|
|
tags: tags,
|
|
|
|
to: to,
|
|
|
|
cc: cc,
|
|
|
|
begins_on: begins_on,
|
2019-09-04 16:24:31 +00:00
|
|
|
ends_on: ends_on,
|
2019-09-02 15:23:00 +00:00
|
|
|
category: category,
|
2019-09-20 16:22:03 +00:00
|
|
|
join_options: join_options,
|
2019-09-02 15:23:00 +00:00
|
|
|
options: options
|
|
|
|
} <-
|
2019-09-04 16:24:31 +00:00
|
|
|
prepare_args(Map.merge(event, args)),
|
2018-12-14 16:41:55 +00:00
|
|
|
event <-
|
|
|
|
ActivityPubUtils.make_event_data(
|
2019-09-02 15:23:00 +00:00
|
|
|
organizer_actor.url,
|
2019-07-30 08:35:29 +00:00
|
|
|
%{to: to, cc: cc},
|
2018-12-14 16:41:55 +00:00
|
|
|
title,
|
|
|
|
content_html,
|
2019-05-22 12:12:11 +00:00
|
|
|
picture,
|
2018-12-14 16:41:55 +00:00
|
|
|
tags,
|
2019-08-28 09:28:27 +00:00
|
|
|
%{
|
|
|
|
begins_on: begins_on,
|
2019-09-04 16:24:31 +00:00
|
|
|
ends_on: ends_on,
|
2019-08-28 09:28:27 +00:00
|
|
|
physical_address: physical_address,
|
2019-09-04 16:24:31 +00:00
|
|
|
category: category,
|
2019-09-20 16:22:03 +00:00
|
|
|
join_options: join_options,
|
2019-08-28 09:28:27 +00:00
|
|
|
options: options
|
2019-09-04 16:24:31 +00:00
|
|
|
},
|
|
|
|
event.uuid,
|
|
|
|
event.url
|
2018-12-14 16:41:55 +00:00
|
|
|
) do
|
2019-09-02 15:23:00 +00:00
|
|
|
ActivityPub.update(%{
|
2018-12-14 16:41:55 +00:00
|
|
|
to: ["https://www.w3.org/ns/activitystreams#Public"],
|
2019-09-04 16:24:31 +00:00
|
|
|
actor: organizer_actor.url,
|
|
|
|
cc: [],
|
2018-12-14 16:41:55 +00:00
|
|
|
object: event,
|
|
|
|
local: true
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2019-09-02 15:23:00 +00:00
|
|
|
|
|
|
|
defp prepare_args(
|
|
|
|
%{
|
|
|
|
organizer_actor: organizer_actor,
|
|
|
|
title: title,
|
|
|
|
description: description,
|
|
|
|
options: options,
|
|
|
|
tags: tags,
|
|
|
|
begins_on: begins_on,
|
2019-09-20 16:22:03 +00:00
|
|
|
category: category,
|
|
|
|
join_options: join_options
|
2019-09-02 15:23:00 +00:00
|
|
|
} = args
|
|
|
|
) do
|
|
|
|
with physical_address <- Map.get(args, :physical_address, nil),
|
|
|
|
title <- String.trim(title),
|
|
|
|
visibility <- Map.get(args, :visibility, :public),
|
|
|
|
picture <- Map.get(args, :picture, nil),
|
|
|
|
{content_html, tags, to, cc} <-
|
|
|
|
Utils.prepare_content(organizer_actor, description, visibility, tags, nil) do
|
|
|
|
%{
|
|
|
|
title: title,
|
|
|
|
physical_address: physical_address,
|
|
|
|
picture: picture,
|
|
|
|
content_html: content_html,
|
|
|
|
tags: tags,
|
|
|
|
to: to,
|
|
|
|
cc: cc,
|
|
|
|
begins_on: begins_on,
|
2019-09-04 16:24:31 +00:00
|
|
|
ends_on: Map.get(args, :ends_on, nil),
|
2019-09-02 15:23:00 +00:00
|
|
|
category: category,
|
2019-09-20 16:22:03 +00:00
|
|
|
join_options: join_options,
|
2019-09-02 15:23:00 +00:00
|
|
|
options: options
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2019-09-09 07:31:08 +00: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
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|