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-04 18:24:31 +02:00
|
|
|
alias Mobilizon.Events.Event
|
2019-09-23 10:26:23 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
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-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
|
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
alias MobilizonWeb.API.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-09-02 17:23:00 +02:00
|
|
|
def create_event(%{organizer_actor: organizer_actor} = args) do
|
2019-09-23 10:26:23 +02:00
|
|
|
with args <- prepare_args(args),
|
2019-09-02 17:23:00 +02:00
|
|
|
event <-
|
|
|
|
ActivityPubUtils.make_event_data(
|
2019-09-23 10:26:23 +02:00
|
|
|
args.organizer_actor.url,
|
|
|
|
%{to: args.to, cc: args.cc},
|
|
|
|
args.title,
|
|
|
|
args.content_html,
|
|
|
|
args.picture,
|
|
|
|
args.tags,
|
|
|
|
args.metadata
|
2019-09-02 17:23:00 +02:00
|
|
|
) do
|
|
|
|
ActivityPub.create(%{
|
2019-09-23 10:26:23 +02:00
|
|
|
to: args.to,
|
2019-09-02 17:23:00 +02:00
|
|
|
actor: organizer_actor,
|
|
|
|
object: event,
|
2019-10-02 17:59:07 +02:00
|
|
|
# For now we don't federate drafts but it will be needed if we want to edit them as groups
|
|
|
|
local: args.metadata.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-09-02 17:23:00 +02:00
|
|
|
def update_event(
|
2018-12-14 17:41:55 +01:00
|
|
|
%{
|
2019-09-04 18:24:31 +02:00
|
|
|
organizer_actor: organizer_actor
|
|
|
|
} = args,
|
|
|
|
%Event{} = event
|
2019-09-02 17:23:00 +02:00
|
|
|
) do
|
2019-09-04 18:24:31 +02:00
|
|
|
with args <- Map.put(args, :tags, Map.get(args, :tags, [])),
|
2019-09-23 10:26:23 +02:00
|
|
|
args <- prepare_args(Map.merge(event, args)),
|
2018-12-14 17:41:55 +01:00
|
|
|
event <-
|
|
|
|
ActivityPubUtils.make_event_data(
|
2019-09-23 10:26:23 +02:00
|
|
|
args.organizer_actor.url,
|
|
|
|
%{to: args.to, cc: args.cc},
|
|
|
|
args.title,
|
|
|
|
args.content_html,
|
|
|
|
args.picture,
|
|
|
|
args.tags,
|
|
|
|
args.metadata,
|
2019-09-04 18:24:31 +02:00
|
|
|
event.uuid,
|
|
|
|
event.url
|
2018-12-14 17:41:55 +01:00
|
|
|
) do
|
2019-09-02 17:23:00 +02:00
|
|
|
ActivityPub.update(%{
|
2019-09-23 10:26:23 +02:00
|
|
|
to: args.to,
|
2019-09-04 18:24:31 +02:00
|
|
|
actor: organizer_actor.url,
|
|
|
|
cc: [],
|
2018-12-14 17:41:55 +01:00
|
|
|
object: event,
|
2019-10-02 17:59:07 +02:00
|
|
|
local: args.metadata.draft == false
|
2018-12-14 17:41:55 +01:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2019-09-02 17:23:00 +02:00
|
|
|
|
2019-09-23 10:26:23 +02:00
|
|
|
defp prepare_args(args) do
|
|
|
|
with %Actor{} = organizer_actor <- Map.get(args, :organizer_actor),
|
|
|
|
title <- args |> Map.get(:title, "") |> String.trim(),
|
2019-09-02 17:23:00 +02:00
|
|
|
visibility <- Map.get(args, :visibility, :public),
|
2019-09-23 10:26:23 +02:00
|
|
|
description <- Map.get(args, :description),
|
|
|
|
tags <- Map.get(args, :tags),
|
2019-09-02 17:23:00 +02:00
|
|
|
{content_html, tags, to, cc} <-
|
|
|
|
Utils.prepare_content(organizer_actor, description, visibility, tags, nil) do
|
|
|
|
%{
|
|
|
|
title: title,
|
|
|
|
content_html: content_html,
|
2019-09-23 10:26:23 +02:00
|
|
|
picture: Map.get(args, :picture),
|
2019-09-02 17:23:00 +02:00
|
|
|
tags: tags,
|
2019-09-23 10:26:23 +02:00
|
|
|
organizer_actor: organizer_actor,
|
2019-09-02 17:23:00 +02:00
|
|
|
to: to,
|
|
|
|
cc: cc,
|
2019-09-23 10:26:23 +02:00
|
|
|
metadata: %{
|
|
|
|
begins_on: Map.get(args, :begins_on),
|
|
|
|
ends_on: Map.get(args, :ends_on),
|
|
|
|
physical_address: Map.get(args, :physical_address),
|
|
|
|
category: Map.get(args, :category),
|
|
|
|
options: Map.get(args, :options),
|
|
|
|
join_options: Map.get(args, :join_options),
|
|
|
|
status: Map.get(args, :status),
|
|
|
|
online_address: Map.get(args, :online_address),
|
2019-10-02 17:59:07 +02:00
|
|
|
phone_address: Map.get(args, :phone_address),
|
|
|
|
draft: Map.get(args, :draft)
|
2019-09-23 10:26:23 +02:00
|
|
|
}
|
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
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|