Correctly handle event update
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -2,8 +2,7 @@ defmodule MobilizonWeb.API.Events do
|
||||
@moduledoc """
|
||||
API for Events
|
||||
"""
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
|
||||
alias MobilizonWeb.API.Utils
|
||||
@@ -16,13 +15,13 @@ defmodule MobilizonWeb.API.Events do
|
||||
with %{
|
||||
title: title,
|
||||
physical_address: physical_address,
|
||||
visibility: visibility,
|
||||
picture: picture,
|
||||
content_html: content_html,
|
||||
tags: tags,
|
||||
to: to,
|
||||
cc: cc,
|
||||
begins_on: begins_on,
|
||||
ends_on: ends_on,
|
||||
category: category,
|
||||
options: options
|
||||
} <- prepare_args(args),
|
||||
@@ -34,7 +33,13 @@ defmodule MobilizonWeb.API.Events do
|
||||
content_html,
|
||||
picture,
|
||||
tags,
|
||||
%{begins_on: begins_on, physical_address: physical_address, category: category, options: options}
|
||||
%{
|
||||
begins_on: begins_on,
|
||||
ends_on: ends_on,
|
||||
physical_address: physical_address,
|
||||
category: category,
|
||||
options: options
|
||||
}
|
||||
) do
|
||||
ActivityPub.create(%{
|
||||
to: ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
@@ -48,30 +53,28 @@ defmodule MobilizonWeb.API.Events do
|
||||
@doc """
|
||||
Update an event
|
||||
"""
|
||||
@spec update_event(map()) :: {:ok, Activity.t(), Event.t()} | any()
|
||||
@spec update_event(map(), Event.t()) :: {:ok, Activity.t(), Event.t()} | any()
|
||||
def update_event(
|
||||
%{
|
||||
organizer_actor: organizer_actor,
|
||||
event: event
|
||||
} = args
|
||||
organizer_actor: organizer_actor
|
||||
} = args,
|
||||
%Event{} = event
|
||||
) do
|
||||
with %{
|
||||
with args <- Map.put(args, :tags, Map.get(args, :tags, [])),
|
||||
%{
|
||||
title: title,
|
||||
physical_address: physical_address,
|
||||
visibility: visibility,
|
||||
picture: picture,
|
||||
content_html: content_html,
|
||||
tags: tags,
|
||||
to: to,
|
||||
cc: cc,
|
||||
begins_on: begins_on,
|
||||
ends_on: ends_on,
|
||||
category: category,
|
||||
options: options
|
||||
} <-
|
||||
prepare_args(
|
||||
args
|
||||
|> update_args(event)
|
||||
),
|
||||
prepare_args(Map.merge(event, args)),
|
||||
event <-
|
||||
ActivityPubUtils.make_event_data(
|
||||
organizer_actor.url,
|
||||
@@ -82,34 +85,24 @@ defmodule MobilizonWeb.API.Events do
|
||||
tags,
|
||||
%{
|
||||
begins_on: begins_on,
|
||||
ends_on: ends_on,
|
||||
physical_address: physical_address,
|
||||
category: Map.get(args, :category),
|
||||
category: category,
|
||||
options: options
|
||||
}
|
||||
},
|
||||
event.uuid,
|
||||
event.url
|
||||
) do
|
||||
ActivityPub.update(%{
|
||||
to: ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
actor: organizer_actor,
|
||||
actor: organizer_actor.url,
|
||||
cc: [],
|
||||
object: event,
|
||||
local: true
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
defp update_args(args, event) do
|
||||
%{
|
||||
title: Map.get(args, :title, event.title),
|
||||
description: Map.get(args, :description, event.description),
|
||||
tags: Map.get(args, :tags, event.tags),
|
||||
physical_address: Map.get(args, :physical_address, event.physical_address),
|
||||
visibility: Map.get(args, :visibility, event.visibility),
|
||||
physical_address: Map.get(args, :physical_address, event.physical_address),
|
||||
begins_on: Map.get(args, :begins_on, event.begins_on),
|
||||
category: Map.get(args, :category, event.category),
|
||||
options: Map.get(args, :options, event.options)
|
||||
}
|
||||
end
|
||||
|
||||
defp prepare_args(
|
||||
%{
|
||||
organizer_actor: organizer_actor,
|
||||
@@ -118,8 +111,7 @@ defmodule MobilizonWeb.API.Events do
|
||||
options: options,
|
||||
tags: tags,
|
||||
begins_on: begins_on,
|
||||
category: category,
|
||||
options: options
|
||||
category: category
|
||||
} = args
|
||||
) do
|
||||
with physical_address <- Map.get(args, :physical_address, nil),
|
||||
@@ -131,13 +123,13 @@ defmodule MobilizonWeb.API.Events do
|
||||
%{
|
||||
title: title,
|
||||
physical_address: physical_address,
|
||||
visibility: visibility,
|
||||
picture: picture,
|
||||
content_html: content_html,
|
||||
tags: tags,
|
||||
to: to,
|
||||
cc: cc,
|
||||
begins_on: begins_on,
|
||||
ends_on: Map.get(args, :ends_on, nil),
|
||||
category: category,
|
||||
options: options
|
||||
}
|
||||
|
||||
@@ -18,13 +18,13 @@ defmodule MobilizonWeb.API.Groups do
|
||||
preferred_username: title,
|
||||
summary: summary,
|
||||
creator_actor_id: creator_actor_id,
|
||||
avatar: avatar,
|
||||
banner: banner
|
||||
avatar: _avatar,
|
||||
banner: _banner
|
||||
} = args
|
||||
) do
|
||||
with {:is_owned, true, actor} <- User.owns_actor(user, creator_actor_id),
|
||||
{:existing_group, nil} <- {:existing_group, Actors.get_group_by_title(title)},
|
||||
title <- String.trim(title),
|
||||
{:existing_group, nil} <- {:existing_group, Actors.get_group_by_title(title)},
|
||||
visibility <- Map.get(args, :visibility, :public),
|
||||
{content_html, tags, to, cc} <-
|
||||
Utils.prepare_content(actor, summary, visibility, [], nil),
|
||||
|
||||
@@ -193,7 +193,9 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
}
|
||||
} = _resolution
|
||||
) do
|
||||
with {:is_owned, true, organizer_actor} <- User.owns_actor(user, organizer_actor_id),
|
||||
# See https://github.com/absinthe-graphql/absinthe/issues/490
|
||||
with args <- Map.put(args, :options, args[:options] || %{}),
|
||||
{:is_owned, true, organizer_actor} <- User.owns_actor(user, organizer_actor_id),
|
||||
{:ok, args} <- save_attached_picture(args),
|
||||
{:ok, args} <- save_physical_address(args),
|
||||
args_with_organizer <- Map.put(args, :organizer_actor, organizer_actor),
|
||||
@@ -230,10 +232,13 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
}
|
||||
} = _resolution
|
||||
) do
|
||||
with {:ok, %Event{} = event} <- Mobilizon.Events.get_event(event_id),
|
||||
# See https://github.com/absinthe-graphql/absinthe/issues/490
|
||||
with args <- Map.put(args, :options, args[:options] || %{}),
|
||||
{:ok, %Event{} = event} <- Mobilizon.Events.get_event_full(event_id),
|
||||
{:is_owned, true, organizer_actor} <- User.owns_actor(user, event.organizer_actor_id),
|
||||
{:ok, args} <- save_attached_picture(args),
|
||||
{:ok, args} <- save_physical_address(args),
|
||||
args <- Map.put(args, :organizer_actor, organizer_actor),
|
||||
{
|
||||
:ok,
|
||||
%Activity{
|
||||
@@ -243,11 +248,14 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
},
|
||||
%Event{} = event
|
||||
} <-
|
||||
MobilizonWeb.API.Events.update_event(args) do
|
||||
MobilizonWeb.API.Events.update_event(args, event) do
|
||||
{:ok, event}
|
||||
else
|
||||
{:error, :event_not_found} ->
|
||||
{:error, "Event not found"}
|
||||
|
||||
{:is_owned, _} ->
|
||||
{:error, "User doesn't own actor"}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -229,14 +229,14 @@ defmodule MobilizonWeb.Schema.EventType do
|
||||
arg(:organizer_actor_id, non_null(:id))
|
||||
arg(:category, :string, default_value: "meeting")
|
||||
arg(:physical_address, :address_input)
|
||||
arg(:options, :event_options_input, default_value: %{})
|
||||
arg(:options, :event_options_input)
|
||||
|
||||
resolve(&Event.create_event/3)
|
||||
end
|
||||
|
||||
@desc "Update an event"
|
||||
field :update_event, type: :event do
|
||||
arg(:event_id, non_null(:integer))
|
||||
arg(:event_id, non_null(:id))
|
||||
|
||||
arg(:title, :string)
|
||||
arg(:description, :string)
|
||||
@@ -246,6 +246,7 @@ defmodule MobilizonWeb.Schema.EventType do
|
||||
arg(:status, :integer)
|
||||
arg(:public, :boolean)
|
||||
arg(:visibility, :event_visibility)
|
||||
arg(:organizer_actor_id, :id)
|
||||
|
||||
arg(:tags, list_of(:string), description: "The list of tags associated to the event")
|
||||
|
||||
@@ -259,6 +260,7 @@ defmodule MobilizonWeb.Schema.EventType do
|
||||
arg(:phone_address, :string)
|
||||
arg(:category, :string)
|
||||
arg(:physical_address, :address_input)
|
||||
arg(:options, :event_options_input)
|
||||
|
||||
resolve(&Event.update_event/3)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user