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
|
2023-10-17 14:41:31 +00:00
|
|
|
alias Mobilizon.Discussions.Comment
|
2019-10-25 15:43:37 +00:00
|
|
|
alias Mobilizon.Events.Event
|
2021-09-28 17:40:37 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Actions, Activity, Utils}
|
2020-11-26 10:41:13 +00:00
|
|
|
alias Mobilizon.GraphQL.API.Utils, as: APIUtils
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-03-19 10:16:03 +00:00
|
|
|
@doc """
|
|
|
|
Create an event
|
|
|
|
"""
|
2021-10-10 14:25:50 +00:00
|
|
|
@spec create_event(map) ::
|
|
|
|
{:ok, Activity.t(), Event.t()} | {:error, atom() | Ecto.Changeset.t()}
|
2019-10-25 15:43:37 +00:00
|
|
|
def create_event(args) do
|
2021-09-24 14:46:42 +00:00
|
|
|
# For now we don't federate drafts but it will be needed if we want to edit them as groups
|
2021-09-28 17:40:37 +00:00
|
|
|
Actions.Create.create(:event, prepare_args(args), should_federate(args))
|
2019-09-02 15:23:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Update an event
|
|
|
|
"""
|
2021-10-10 14:25:50 +00:00
|
|
|
@spec update_event(map, Event.t()) ::
|
|
|
|
{:ok, Activity.t(), Event.t()} | {:error, atom | Ecto.Changeset.t()}
|
2019-10-25 15:43:37 +00:00
|
|
|
def update_event(args, %Event{} = event) do
|
2021-09-28 17:40:37 +00:00
|
|
|
Actions.Update.update(event, prepare_args(args), should_federate(args))
|
2019-09-02 15:23:00 +00:00
|
|
|
end
|
2019-09-09 07:31:08 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Trigger the deletion of an event
|
|
|
|
"""
|
2021-10-05 13:29:06 +00:00
|
|
|
@spec delete_event(Event.t(), Actor.t()) :: {:ok, Activity.t(), Entity.t()} | any()
|
|
|
|
def delete_event(%Event{} = event, %Actor{} = actor) do
|
|
|
|
Actions.Delete.delete(event, actor, true)
|
2019-09-09 07:31:08 +00:00
|
|
|
end
|
2019-10-25 15:43:37 +00:00
|
|
|
|
2023-10-17 14:41:31 +00:00
|
|
|
@spec send_private_message_to_participants(map()) ::
|
|
|
|
{:ok, Activity.t(), Comment.t()} | {:error, atom() | Ecto.Changeset.t()}
|
|
|
|
def send_private_message_to_participants(args) do
|
|
|
|
Actions.Create.create(:comment, args, true)
|
|
|
|
end
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec prepare_args(map) :: map
|
|
|
|
defp prepare_args(args) do
|
|
|
|
organizer_actor = Map.get(args, :organizer_actor)
|
|
|
|
|
|
|
|
args
|
|
|
|
|> extract_pictures_from_event_body(organizer_actor)
|
|
|
|
|> Map.update(:picture, nil, fn picture ->
|
|
|
|
process_picture(picture, organizer_actor)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-10-25 15:43:37 +00:00
|
|
|
defp process_picture(nil, _), do: nil
|
2020-11-26 10:41:13 +00:00
|
|
|
defp process_picture(%{media_id: _picture_id} = args, _), do: args
|
2019-10-25 15:43:37 +00:00
|
|
|
|
2020-11-26 10:41:13 +00:00
|
|
|
defp process_picture(%{media: media}, %Actor{id: actor_id}) do
|
2024-04-10 12:36:21 +00:00
|
|
|
# case url
|
|
|
|
if Map.has_key?(media, :url) do
|
2021-06-10 07:36:25 +00:00
|
|
|
%{
|
2024-04-10 12:36:21 +00:00
|
|
|
file: %{"url" => media.url, "name" => media.name},
|
2021-06-10 07:36:25 +00:00
|
|
|
actor_id: actor_id
|
|
|
|
}
|
2024-04-10 12:36:21 +00:00
|
|
|
|
|
|
|
# case upload
|
|
|
|
else
|
|
|
|
with uploaded when is_map(uploaded) <-
|
|
|
|
media
|
|
|
|
|> Map.get(:file)
|
|
|
|
|> Utils.make_media_data(description: Map.get(media, :name)) do
|
|
|
|
%{
|
|
|
|
file: Map.take(uploaded, [:url, :name, :content_type, :size]),
|
|
|
|
metadata: Map.take(uploaded, [:width, :height, :blurhash]),
|
|
|
|
actor_id: actor_id
|
|
|
|
}
|
|
|
|
end
|
2021-06-10 07:36:25 +00:00
|
|
|
end
|
2019-10-25 15:43:37 +00:00
|
|
|
end
|
2020-11-26 10:41:13 +00:00
|
|
|
|
|
|
|
@spec extract_pictures_from_event_body(map(), Actor.t()) :: map()
|
|
|
|
defp extract_pictures_from_event_body(
|
|
|
|
%{description: description} = args,
|
|
|
|
%Actor{id: organizer_actor_id}
|
|
|
|
) do
|
|
|
|
pictures = APIUtils.extract_pictures_from_body(description, organizer_actor_id)
|
|
|
|
Map.put(args, :media, pictures)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp extract_pictures_from_event_body(args, _), do: args
|
2021-07-26 15:19:10 +00:00
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec should_federate(map()) :: boolean
|
2021-07-26 15:19:10 +00:00
|
|
|
defp should_federate(%{attributed_to_id: attributed_to_id}) when not is_nil(attributed_to_id),
|
|
|
|
do: true
|
|
|
|
|
|
|
|
defp should_federate(args), do: Map.get(args, :draft, false) == false
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|