2018-12-14 17:41:55 +01:00
|
|
|
defmodule MobilizonWeb.API.Events do
|
|
|
|
@moduledoc """
|
|
|
|
API for Events
|
|
|
|
"""
|
2019-07-30 10:35:29 +02:00
|
|
|
alias Mobilizon.Addresses
|
2018-12-14 17:41:55 +01:00
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
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-07-30 16:40:59 +02:00
|
|
|
@visibility %{"PUBLIC" => :public, "PRIVATE" => :private}
|
|
|
|
|
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()
|
2018-12-14 17:41:55 +01:00
|
|
|
def create_event(
|
|
|
|
%{
|
|
|
|
title: title,
|
|
|
|
description: description,
|
2019-01-21 15:08:22 +01:00
|
|
|
organizer_actor_id: organizer_actor_id,
|
2018-12-14 17:41:55 +01:00
|
|
|
begins_on: begins_on,
|
2019-07-26 11:30:28 +02:00
|
|
|
category: category,
|
|
|
|
tags: tags
|
2018-12-14 17:41:55 +01:00
|
|
|
} = args
|
|
|
|
) do
|
2019-07-04 17:41:06 +02:00
|
|
|
with %Actor{url: url} = actor <-
|
|
|
|
Actors.get_local_actor_with_everything(organizer_actor_id),
|
2019-07-30 10:35:29 +02:00
|
|
|
physical_address <- Map.get(args, :physical_address, nil),
|
2018-12-14 17:41:55 +01:00
|
|
|
title <- String.trim(title),
|
2019-07-23 13:49:22 +02:00
|
|
|
visibility <- Map.get(args, :visibility, :public),
|
2019-05-22 14:12:11 +02:00
|
|
|
picture <- Map.get(args, :picture, nil),
|
2019-07-26 11:30:28 +02:00
|
|
|
{content_html, tags, to, cc} <-
|
|
|
|
Utils.prepare_content(actor, description, visibility, tags, nil),
|
2018-12-14 17:41:55 +01:00
|
|
|
event <-
|
|
|
|
ActivityPubUtils.make_event_data(
|
|
|
|
url,
|
2019-07-30 10:35:29 +02:00
|
|
|
%{to: to, cc: cc},
|
2018-12-14 17:41:55 +01:00
|
|
|
title,
|
|
|
|
content_html,
|
2019-05-22 14:12:11 +02:00
|
|
|
picture,
|
2018-12-14 17:41:55 +01:00
|
|
|
tags,
|
2019-07-30 10:35:29 +02:00
|
|
|
%{begins_on: begins_on, physical_address: physical_address, category: category}
|
2018-12-14 17:41:55 +01:00
|
|
|
) do
|
|
|
|
ActivityPub.create(%{
|
|
|
|
to: ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
actor: actor,
|
|
|
|
object: event,
|
|
|
|
local: true
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2019-07-30 10:35:29 +02:00
|
|
|
|
|
|
|
defp get_physical_address(address_id) when is_number(address_id),
|
|
|
|
do: Addresses.get_address!(address_id)
|
|
|
|
|
|
|
|
defp get_physical_address(address_id) when is_binary(address_id) do
|
|
|
|
with {address_id, ""} <- Integer.parse(address_id) do
|
|
|
|
get_physical_address(address_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_physical_address(nil), do: nil
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|