2020-01-26 19:34:25 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Event do
|
2019-01-03 13:59:59 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Handles the event-related GraphQL calls.
|
2019-01-03 13:59:59 +00:00
|
|
|
"""
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2021-09-10 09:35:32 +00:00
|
|
|
alias Mobilizon.{Actors, Admin, Events}
|
2019-09-07 17:54:11 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2020-02-07 15:28:49 +00:00
|
|
|
alias Mobilizon.Config
|
2019-12-20 12:04:34 +00:00
|
|
|
alias Mobilizon.Events.{Event, EventParticipantStats}
|
2019-03-05 16:23:05 +00:00
|
|
|
alias Mobilizon.Users.User
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2020-01-26 20:11:16 +00:00
|
|
|
alias Mobilizon.GraphQL.API
|
2020-01-26 19:34:25 +00:00
|
|
|
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Activity
|
2021-07-23 09:34:28 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Permission
|
2021-10-10 14:25:50 +00:00
|
|
|
alias Mobilizon.Service.TimezoneDetector
|
2021-01-25 11:00:06 +00:00
|
|
|
import Mobilizon.Users.Guards, only: [is_moderator: 1]
|
2020-09-29 07:53:48 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2021-08-13 09:22:04 +00:00
|
|
|
import Mobilizon.GraphQL.Resolvers.Event.Utils
|
2021-10-10 14:25:50 +00:00
|
|
|
require Logger
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2018-12-21 10:45:55 +00:00
|
|
|
# We limit the max number of events that can be retrieved
|
|
|
|
@event_max_limit 100
|
2021-10-29 08:38:03 +00:00
|
|
|
@number_of_related_events 4
|
2018-12-21 10:45:55 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec organizer_for_event(Event.t(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Actor.t() | nil} | {:error, String.t()}
|
2021-01-25 11:00:06 +00:00
|
|
|
def organizer_for_event(
|
|
|
|
%Event{attributed_to_id: attributed_to_id, organizer_actor_id: organizer_actor_id},
|
|
|
|
_args,
|
2021-09-10 09:35:32 +00:00
|
|
|
%{
|
|
|
|
context: %{current_user: %User{role: user_role}, current_actor: %Actor{id: actor_id}}
|
|
|
|
} = _resolution
|
2021-01-25 11:00:06 +00:00
|
|
|
)
|
|
|
|
when not is_nil(attributed_to_id) do
|
|
|
|
with %Actor{id: group_id} <- Actors.get_actor(attributed_to_id),
|
|
|
|
{:member, true} <-
|
|
|
|
{:member, Actors.is_member?(actor_id, group_id) or is_moderator(user_role)},
|
|
|
|
%Actor{} = actor <- Actors.get_actor(organizer_actor_id) do
|
|
|
|
{:ok, actor}
|
|
|
|
else
|
|
|
|
_ -> {:ok, nil}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def organizer_for_event(
|
|
|
|
%Event{attributed_to_id: attributed_to_id},
|
|
|
|
_args,
|
|
|
|
_resolution
|
|
|
|
)
|
|
|
|
when not is_nil(attributed_to_id) do
|
|
|
|
case Actors.get_actor(attributed_to_id) do
|
|
|
|
%Actor{} -> {:ok, nil}
|
|
|
|
_ -> {:error, "Unable to get organizer actor"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def organizer_for_event(
|
|
|
|
%Event{organizer_actor_id: organizer_actor_id},
|
|
|
|
_args,
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
case Actors.get_actor(organizer_actor_id) do
|
|
|
|
%Actor{} = actor -> {:ok, actor}
|
|
|
|
_ -> {:error, "Unable to get organizer actor"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec list_events(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Event.t())} | {:error, :events_max_limit_reached}
|
2021-03-16 18:08:00 +00:00
|
|
|
def list_events(
|
|
|
|
_parent,
|
|
|
|
%{page: page, limit: limit, order_by: order_by, direction: direction},
|
|
|
|
_resolution
|
|
|
|
)
|
2018-12-21 10:45:55 +00:00
|
|
|
when limit < @event_max_limit do
|
2021-03-16 18:08:00 +00:00
|
|
|
{:ok, Events.list_events(page, limit, order_by, direction)}
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
|
2018-12-21 11:21:56 +00:00
|
|
|
def list_events(_parent, %{page: _page, limit: _limit}, _resolution) do
|
2018-12-21 10:45:55 +00:00
|
|
|
{:error, :events_max_limit_reached}
|
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec find_private_event(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Event.t()} | {:error, :event_not_found}
|
2019-10-08 16:13:06 +00:00
|
|
|
defp find_private_event(
|
|
|
|
_parent,
|
|
|
|
%{uuid: uuid},
|
2021-09-10 09:35:32 +00:00
|
|
|
%{context: %{current_actor: %Actor{} = profile}} = _resolution
|
2019-10-08 16:13:06 +00:00
|
|
|
) do
|
2021-07-23 09:34:28 +00:00
|
|
|
case Events.get_event_by_uuid_with_preload(uuid) do
|
|
|
|
# Event attributed to group
|
|
|
|
%Event{attributed_to: %Actor{}} = event ->
|
|
|
|
if Permission.can_access_group_object?(profile, event) do
|
|
|
|
{:ok, event}
|
|
|
|
else
|
|
|
|
{:error, :event_not_found}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Own event
|
|
|
|
%Event{organizer_actor: %Actor{id: actor_id}} = event ->
|
|
|
|
if actor_id == profile.id do
|
|
|
|
{:ok, event}
|
|
|
|
else
|
|
|
|
{:error, :event_not_found}
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
2020-10-01 13:07:15 +00:00
|
|
|
{:error, :event_not_found}
|
2019-10-02 15:59:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-01 13:07:15 +00:00
|
|
|
defp find_private_event(_parent, _args, _resolution) do
|
|
|
|
{:error, :event_not_found}
|
2020-01-26 19:34:25 +00:00
|
|
|
end
|
2019-10-08 19:31:47 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec find_event(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Event.t()} | {:error, :event_not_found}
|
2020-02-07 15:28:49 +00:00
|
|
|
def find_event(parent, %{uuid: uuid} = args, %{context: context} = resolution) do
|
2021-11-08 17:46:04 +00:00
|
|
|
case Events.get_public_event_by_uuid_with_preload(uuid) do
|
|
|
|
%Event{} = event ->
|
|
|
|
if Map.has_key?(context, :current_user) || check_event_access?(event) do
|
|
|
|
{:ok, event}
|
|
|
|
else
|
|
|
|
{:error, :event_not_found}
|
|
|
|
end
|
2020-02-07 15:28:49 +00:00
|
|
|
|
2021-11-08 17:46:04 +00:00
|
|
|
_ ->
|
|
|
|
find_private_event(parent, args, resolution)
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 22:30:47 +00:00
|
|
|
@doc """
|
|
|
|
List participants for event (through an event request)
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec list_participants_for_event(Event.t(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Participant.t())} | {:error, String.t()}
|
2019-09-20 16:22:03 +00:00
|
|
|
def list_participants_for_event(
|
2021-08-13 09:22:04 +00:00
|
|
|
%Event{id: event_id} = event,
|
2020-11-19 16:06:28 +00:00
|
|
|
%{page: page, limit: limit, roles: roles},
|
2021-09-10 09:35:32 +00:00
|
|
|
%{context: %{current_actor: %Actor{} = actor}} = _resolution
|
2019-09-20 16:22:03 +00:00
|
|
|
) do
|
2021-09-10 09:35:32 +00:00
|
|
|
# Check that moderator has right
|
|
|
|
if can_event_be_updated_by?(event, actor) do
|
2019-09-26 14:38:58 +00:00
|
|
|
roles =
|
|
|
|
case roles do
|
2021-05-17 07:38:04 +00:00
|
|
|
nil ->
|
|
|
|
[]
|
|
|
|
|
2019-09-26 14:38:58 +00:00
|
|
|
"" ->
|
|
|
|
[]
|
2019-09-20 16:22:03 +00:00
|
|
|
|
2019-09-26 14:38:58 +00:00
|
|
|
roles ->
|
|
|
|
roles
|
|
|
|
|> String.split(",")
|
|
|
|
|> Enum.map(&String.downcase/1)
|
|
|
|
|> Enum.map(&String.to_existing_atom/1)
|
|
|
|
end
|
|
|
|
|
2020-06-18 13:23:05 +00:00
|
|
|
participants = Events.list_participants_for_event(event_id, roles, page, limit)
|
|
|
|
{:ok, participants}
|
2019-09-26 14:38:58 +00:00
|
|
|
else
|
2021-09-10 09:35:32 +00:00
|
|
|
{:error,
|
|
|
|
dgettext("errors", "Provided profile doesn't have moderator permissions on this event")}
|
2019-09-26 14:38:58 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-20 16:22:03 +00:00
|
|
|
|
2019-09-26 14:38:58 +00:00
|
|
|
def list_participants_for_event(_, _args, _resolution) do
|
2020-03-05 18:32:34 +00:00
|
|
|
{:ok, %{total: 0, elements: []}}
|
2018-11-12 22:30:47 +00:00
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec stats_participants(Event.t(), map(), Absinthe.Resolution.t()) :: {:ok, map()}
|
2020-06-15 17:41:11 +00:00
|
|
|
def stats_participants(
|
|
|
|
%Event{participant_stats: %EventParticipantStats{} = stats, id: event_id} = _event,
|
|
|
|
_args,
|
|
|
|
%{context: %{current_user: %User{id: user_id} = _user}} = _resolution
|
|
|
|
) do
|
|
|
|
if Events.is_user_moderator_for_event?(user_id, event_id) do
|
2020-06-18 14:24:00 +00:00
|
|
|
{:ok,
|
|
|
|
Map.put(
|
|
|
|
stats,
|
|
|
|
:going,
|
|
|
|
stats.participant + stats.moderator + stats.administrator + stats.creator
|
|
|
|
)}
|
2020-06-15 17:41:11 +00:00
|
|
|
else
|
2020-06-18 14:24:00 +00:00
|
|
|
{:ok, %{participant: stats.participant}}
|
2020-06-15 17:41:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-18 14:24:00 +00:00
|
|
|
def stats_participants(
|
|
|
|
%Event{participant_stats: %EventParticipantStats{participant: participant}},
|
|
|
|
_args,
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
{:ok, %EventParticipantStats{participant: participant}}
|
|
|
|
end
|
|
|
|
|
2020-06-15 17:41:11 +00:00
|
|
|
def stats_participants(_event, _args, _resolution) do
|
|
|
|
{:ok, %EventParticipantStats{}}
|
|
|
|
end
|
|
|
|
|
2019-04-11 16:25:32 +00:00
|
|
|
@doc """
|
|
|
|
List related events
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec list_related_events(Event.t(), map(), Absinthe.Resolution.t()) :: {:ok, list(Event.t())}
|
2019-04-11 16:25:32 +00:00
|
|
|
def list_related_events(
|
2021-10-29 08:38:03 +00:00
|
|
|
%Event{uuid: uuid} = event,
|
2019-04-11 16:25:32 +00:00
|
|
|
_args,
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
# We get the organizer's next public event
|
|
|
|
events =
|
2021-10-29 08:38:03 +00:00
|
|
|
event
|
|
|
|
|> organizer_next_public_event()
|
|
|
|
# We find similar events with the same tags
|
|
|
|
|> similar_events_common_tags(event)
|
|
|
|
# TODO: We should use tag_relations to find more appropriate events
|
|
|
|
# We've considered all recommended events, so we fetch the latest events
|
|
|
|
|> add_latest_events()
|
2019-04-11 16:25:32 +00:00
|
|
|
# We remove the same event from the results
|
|
|
|
|> Enum.filter(fn event -> event.uuid != uuid end)
|
|
|
|
# We return only @number_of_related_events right now
|
|
|
|
|> Enum.take(@number_of_related_events)
|
|
|
|
|
|
|
|
{:ok, events}
|
|
|
|
end
|
|
|
|
|
2021-10-29 08:38:03 +00:00
|
|
|
@spec organizer_next_public_event(Event.t()) :: list(Event.t())
|
|
|
|
defp organizer_next_public_event(%Event{attributed_to: %Actor{} = group, uuid: uuid}) do
|
|
|
|
[Events.get_upcoming_public_event_for_actor(group, uuid)]
|
|
|
|
|> Enum.filter(&is_map/1)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp organizer_next_public_event(%Event{organizer_actor: %Actor{} = profile, uuid: uuid}) do
|
|
|
|
[Events.get_upcoming_public_event_for_actor(profile, uuid)]
|
|
|
|
|> Enum.filter(&is_map/1)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec similar_events_common_tags(list(Event.t()), Event.t()) :: list(Event.t())
|
|
|
|
defp similar_events_common_tags(events, %Event{tags: tags, uuid: uuid}) do
|
|
|
|
events
|
|
|
|
|> Enum.concat(Events.list_events_by_tags(tags, @number_of_related_events))
|
|
|
|
|> Enum.filter(fn event -> event.uuid != uuid end)
|
|
|
|
# uniq_by : It's possible event_from_same_actor is inside events_from_tags
|
|
|
|
|> uniq_events()
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec add_latest_events(list(Event.t())) :: list(Event.t())
|
|
|
|
defp add_latest_events(events) do
|
|
|
|
if @number_of_related_events - length(events) > 0 do
|
|
|
|
events
|
|
|
|
|> Enum.concat(
|
|
|
|
Events.list_events(1, @number_of_related_events + 1, :begins_on, :asc, true).elements
|
|
|
|
)
|
|
|
|
|> uniq_events()
|
|
|
|
else
|
|
|
|
events
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec uniq_events(list(Event.t())) :: list(Event.t())
|
2019-04-12 15:00:55 +00:00
|
|
|
defp uniq_events(events), do: Enum.uniq_by(events, fn event -> event.uuid end)
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
@doc """
|
|
|
|
Create an event
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec create_event(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Event.t()} | {:error, String.t() | Ecto.Changeset.t()}
|
2019-09-02 15:23:00 +00:00
|
|
|
def create_event(
|
|
|
|
_parent,
|
|
|
|
%{organizer_actor_id: organizer_actor_id} = args,
|
2021-10-10 14:25:50 +00:00
|
|
|
%{context: %{current_user: %User{} = user}} = _resolution
|
2019-09-02 15:23:00 +00:00
|
|
|
) do
|
2021-10-11 15:13:57 +00:00
|
|
|
case User.owns_actor(user, organizer_actor_id) do
|
|
|
|
{:is_owned, %Actor{} = organizer_actor} ->
|
|
|
|
if can_create_event?(args) do
|
2021-10-10 14:25:50 +00:00
|
|
|
if is_organizer_group_member?(args) do
|
|
|
|
args_with_organizer =
|
|
|
|
args |> Map.put(:organizer_actor, organizer_actor) |> extract_timezone(user.id)
|
|
|
|
|
|
|
|
case API.Events.create_event(args_with_organizer) do
|
|
|
|
{:ok, %Activity{data: %{"object" => %{"type" => "Event"}}}, %Event{} = event} ->
|
|
|
|
{:ok, event}
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = error} ->
|
|
|
|
{:error, error}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
Logger.warning("Unknown error while creating event: #{inspect(err)}")
|
|
|
|
|
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"Unknown error while creating event"
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"Organizer profile doesn't have permission to create an event on behalf of this group"
|
|
|
|
)}
|
|
|
|
end
|
2021-10-11 15:13:57 +00:00
|
|
|
else
|
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"Only groups can create events"
|
|
|
|
)}
|
|
|
|
end
|
2021-07-26 15:23:42 +00:00
|
|
|
|
2021-10-11 15:13:57 +00:00
|
|
|
{:is_owned, nil} ->
|
|
|
|
{:error, dgettext("errors", "Organizer profile is not owned by the user")}
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_event(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to create events")}
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
2019-01-25 16:06:57 +00:00
|
|
|
|
2021-10-11 15:13:57 +00:00
|
|
|
@spec can_create_event?(map()) :: boolean()
|
|
|
|
defp can_create_event?(args) do
|
|
|
|
if Config.only_groups_can_create_events?() do
|
|
|
|
Map.get(args, :attributed_to_id) != nil
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-02 15:23:00 +00:00
|
|
|
@doc """
|
|
|
|
Update an event
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec update_event(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Event.t()} | {:error, String.t() | Ecto.Changeset.t()}
|
2019-09-02 15:23:00 +00:00
|
|
|
def update_event(
|
|
|
|
_parent,
|
|
|
|
%{event_id: event_id} = args,
|
2021-09-10 09:35:32 +00:00
|
|
|
%{context: %{current_user: %User{} = user, current_actor: %Actor{} = actor}} = _resolution
|
2019-09-02 15:23:00 +00:00
|
|
|
) do
|
2019-09-04 16:24:31 +00:00
|
|
|
# See https://github.com/absinthe-graphql/absinthe/issues/490
|
2021-09-24 14:46:42 +00:00
|
|
|
args = Map.put(args, :options, args[:options] || %{})
|
|
|
|
|
|
|
|
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id),
|
2021-07-26 15:23:42 +00:00
|
|
|
{:ok, args} <- verify_profile_change(args, event, user, actor),
|
2021-10-10 14:25:50 +00:00
|
|
|
args <- extract_timezone(args, user.id),
|
2021-07-26 15:23:42 +00:00
|
|
|
{:event_can_be_managed, true} <-
|
|
|
|
{:event_can_be_managed, can_event_be_updated_by?(event, actor)},
|
2019-09-21 21:59:07 +00:00
|
|
|
{:ok, %Activity{data: %{"object" => %{"type" => "Event"}}}, %Event{} = event} <-
|
2020-01-26 20:11:16 +00:00
|
|
|
API.Events.update_event(args, event) do
|
2019-09-02 15:23:00 +00:00
|
|
|
{:ok, event}
|
|
|
|
else
|
2021-07-26 15:23:42 +00:00
|
|
|
{:event_can_be_managed, false} ->
|
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"This profile doesn't have permission to update an event on behalf of this group"
|
|
|
|
)}
|
|
|
|
|
2019-09-02 15:23:00 +00:00
|
|
|
{:error, :event_not_found} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Event not found")}
|
2019-09-04 16:24:31 +00:00
|
|
|
|
2020-10-09 16:12:35 +00:00
|
|
|
{:old_actor, _} ->
|
|
|
|
{:error, dgettext("errors", "You can't edit this event.")}
|
|
|
|
|
|
|
|
{:new_actor, _} ->
|
2020-10-09 17:29:12 +00:00
|
|
|
{:error, dgettext("errors", "You can't attribute this event to this profile.")}
|
2019-11-18 17:40:03 +00:00
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
{:error, %Ecto.Changeset{} = error} ->
|
2019-11-18 17:40:03 +00:00
|
|
|
{:error, error}
|
2019-09-02 15:23:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_event(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to update an event")}
|
2019-09-02 15:23:00 +00:00
|
|
|
end
|
|
|
|
|
2019-01-25 16:06:57 +00:00
|
|
|
@doc """
|
|
|
|
Delete an event
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec delete_event(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Event.t()} | {:error, String.t() | Ecto.Changeset.t()}
|
2019-09-02 15:23:00 +00:00
|
|
|
def delete_event(
|
|
|
|
_parent,
|
2020-11-19 16:06:28 +00:00
|
|
|
%{event_id: event_id},
|
2021-09-10 09:35:32 +00:00
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: %User{role: role},
|
|
|
|
current_actor: %Actor{id: actor_id} = actor
|
|
|
|
}
|
|
|
|
}
|
2019-09-02 15:23:00 +00:00
|
|
|
) do
|
2021-09-10 09:35:32 +00:00
|
|
|
case Events.get_event_with_preload(event_id) do
|
|
|
|
{:ok, %Event{local: is_local} = event} ->
|
|
|
|
cond do
|
|
|
|
{:event_can_be_managed, true} ==
|
|
|
|
{:event_can_be_managed, can_event_be_deleted_by?(event, actor)} ->
|
|
|
|
do_delete_event(event, actor)
|
|
|
|
|
|
|
|
role in [:moderator, :administrator] ->
|
|
|
|
with {:ok, res} <- do_delete_event(event, actor, !is_local),
|
|
|
|
%Actor{} = actor <- Actors.get_actor(actor_id) do
|
|
|
|
Admin.log_action(actor, "delete", event)
|
|
|
|
|
|
|
|
{:ok, res}
|
|
|
|
end
|
|
|
|
|
|
|
|
true ->
|
|
|
|
{:error, dgettext("errors", "You cannot delete this event")}
|
|
|
|
end
|
|
|
|
|
2019-01-25 16:06:57 +00:00
|
|
|
{:error, :event_not_found} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Event not found")}
|
2019-01-25 16:06:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_event(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to delete an event")}
|
2019-01-25 16:06:57 +00:00
|
|
|
end
|
2019-09-09 07:31:08 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec do_delete_event(Event.t(), Actor.t(), boolean()) :: {:ok, map()}
|
2020-07-09 15:24:28 +00:00
|
|
|
defp do_delete_event(%Event{} = event, %Actor{} = actor, federate \\ true)
|
|
|
|
when is_boolean(federate) do
|
|
|
|
with {:ok, _activity, event} <- API.Events.delete_event(event, actor) do
|
2019-09-09 07:31:08 +00:00
|
|
|
{:ok, %{id: event.id}}
|
|
|
|
end
|
|
|
|
end
|
2021-07-26 15:23:42 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec is_organizer_group_member?(map()) :: boolean()
|
2021-07-26 15:23:42 +00:00
|
|
|
defp is_organizer_group_member?(%{
|
|
|
|
attributed_to_id: attributed_to_id,
|
|
|
|
organizer_actor_id: organizer_actor_id
|
|
|
|
})
|
|
|
|
when not is_nil(attributed_to_id) do
|
|
|
|
Actors.is_member?(organizer_actor_id, attributed_to_id) &&
|
|
|
|
Permission.can_create_group_object?(organizer_actor_id, attributed_to_id, %Event{})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp is_organizer_group_member?(_), do: true
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec verify_profile_change(map(), Event.t(), User.t(), Actor.t()) :: {:ok, map()}
|
2021-07-26 15:23:42 +00:00
|
|
|
defp verify_profile_change(
|
|
|
|
args,
|
|
|
|
%Event{attributed_to: %Actor{}},
|
|
|
|
%User{} = _user,
|
|
|
|
%Actor{} = current_profile
|
|
|
|
) do
|
|
|
|
# The organizer_actor has to be the current profile, because otherwise we're left with a possible remote organizer
|
2021-07-30 15:29:30 +00:00
|
|
|
args =
|
|
|
|
args
|
|
|
|
|> Map.put(:organizer_actor, current_profile)
|
|
|
|
|> Map.put(:organizer_actor_id, current_profile.id)
|
|
|
|
|
2021-07-26 15:23:42 +00:00
|
|
|
{:ok, args}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_profile_change(
|
|
|
|
args,
|
|
|
|
%Event{organizer_actor: %Actor{id: organizer_actor_id}},
|
|
|
|
%User{} = user,
|
|
|
|
%Actor{} = _actor
|
|
|
|
) do
|
|
|
|
with {:old_actor, {:is_owned, %Actor{}}} <-
|
|
|
|
{:old_actor, User.owns_actor(user, organizer_actor_id)},
|
|
|
|
new_organizer_actor_id <- args |> Map.get(:organizer_actor_id, organizer_actor_id),
|
|
|
|
{:new_actor, {:is_owned, %Actor{} = organizer_actor}} <-
|
|
|
|
{:new_actor, User.owns_actor(user, new_organizer_actor_id)},
|
2021-07-30 15:29:30 +00:00
|
|
|
args <-
|
|
|
|
args
|
|
|
|
|> Map.put(:organizer_actor, organizer_actor)
|
|
|
|
|> Map.put(:organizer_actor_id, organizer_actor.id) do
|
2021-07-26 15:23:42 +00:00
|
|
|
{:ok, args}
|
|
|
|
end
|
|
|
|
end
|
2021-10-10 14:25:50 +00:00
|
|
|
|
|
|
|
@spec extract_timezone(map(), String.t() | integer()) :: map()
|
|
|
|
defp extract_timezone(args, user_id) do
|
|
|
|
event_options = Map.get(args, :options, %{})
|
|
|
|
timezone = Map.get(event_options, :timezone)
|
|
|
|
physical_address = Map.get(args, :physical_address)
|
|
|
|
|
|
|
|
fallback_tz =
|
|
|
|
case Mobilizon.Users.get_setting(user_id) do
|
|
|
|
nil -> nil
|
|
|
|
setting -> setting |> Map.from_struct() |> get_in([:timezone])
|
|
|
|
end
|
|
|
|
|
|
|
|
timezone = determine_timezone(timezone, physical_address, fallback_tz)
|
|
|
|
|
|
|
|
event_options = Map.put(event_options, :timezone, timezone)
|
|
|
|
|
|
|
|
Map.put(args, :options, event_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec determine_timezone(
|
|
|
|
String.t() | nil,
|
|
|
|
any(),
|
|
|
|
String.t() | nil
|
|
|
|
) :: String.t() | nil
|
|
|
|
defp determine_timezone(timezone, physical_address, fallback_tz) do
|
|
|
|
case physical_address do
|
|
|
|
physical_address when is_map(physical_address) ->
|
|
|
|
TimezoneDetector.detect(
|
|
|
|
timezone,
|
2021-10-12 06:49:12 +00:00
|
|
|
Map.get(physical_address, :geom),
|
2021-10-10 14:25:50 +00:00
|
|
|
fallback_tz
|
|
|
|
)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
timezone || fallback_tz
|
|
|
|
end
|
|
|
|
end
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|