Add API to join and leave an event
This commit is contained in:
committed by
Thomas Citharel
parent
421fbda9fa
commit
250f0b3bd1
@@ -4,7 +4,7 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
"""
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Activity
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Events.{Event, Participant}
|
||||
alias Mobilizon.Actors.User
|
||||
|
||||
# We limit the max number of events that can be retrieved
|
||||
@@ -43,6 +43,78 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
{:ok, Mobilizon.Events.list_participants_for_event(uuid, 1, 10)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Join an event for an actor
|
||||
"""
|
||||
def actor_join_event(
|
||||
_parent,
|
||||
%{actor_id: actor_id, event_id: event_id},
|
||||
%{
|
||||
context: %{
|
||||
current_user: user
|
||||
}
|
||||
}
|
||||
) do
|
||||
with {:is_owned, true, actor} <- User.owns_actor(user, actor_id),
|
||||
{:ok, %Event{} = event} <- Mobilizon.Events.get_event(event_id),
|
||||
{:error, :participant_not_found} <- Mobilizon.Events.get_participant(event_id, actor_id),
|
||||
role <- Mobilizon.Events.get_default_participant_role(event),
|
||||
{:ok, participant} <-
|
||||
Mobilizon.Events.create_participant(%{
|
||||
role: role,
|
||||
event_id: event.id,
|
||||
actor_id: actor.id
|
||||
}),
|
||||
participant <-
|
||||
Map.put(participant, :event, event)
|
||||
|> Map.put(:actor, actor) do
|
||||
{:ok, participant}
|
||||
else
|
||||
{:is_owned, false} ->
|
||||
{:error, "Actor id is not owned by authenticated user"}
|
||||
|
||||
{:error, :event_not_found} ->
|
||||
{:error, "Event id not found"}
|
||||
|
||||
{:ok, %Participant{}} ->
|
||||
{:error, "You are already a participant of this event"}
|
||||
end
|
||||
end
|
||||
|
||||
def actor_join_event(_parent, _args, _resolution) do
|
||||
{:error, "You need to be logged-in to join an event"}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Leave an event for an actor
|
||||
"""
|
||||
def actor_leave_event(
|
||||
_parent,
|
||||
%{actor_id: actor_id, event_id: event_id},
|
||||
%{
|
||||
context: %{
|
||||
current_user: user
|
||||
}
|
||||
}
|
||||
) do
|
||||
with {:is_owned, true, _} <- User.owns_actor(user, actor_id),
|
||||
{:ok, %Participant{} = participant} <-
|
||||
Mobilizon.Events.get_participant(event_id, actor_id),
|
||||
{:ok, _} <- Mobilizon.Events.delete_participant(participant) do
|
||||
{:ok, %{event: %{id: event_id}, actor: %{id: actor_id}}}
|
||||
else
|
||||
{:is_owned, false} ->
|
||||
{:error, "Actor id is not owned by authenticated user"}
|
||||
|
||||
{:error, :participant_not_found} ->
|
||||
{:error, "Participant not found"}
|
||||
end
|
||||
end
|
||||
|
||||
def actor_leave_event(_parent, _args, _resolution) do
|
||||
{:error, "You need to be logged-in to leave an event"}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Search events by title
|
||||
"""
|
||||
@@ -103,7 +175,7 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
context: %{current_user: user}
|
||||
}) do
|
||||
with {:ok, %Event{} = event} <- Mobilizon.Events.get_event(event_id),
|
||||
{:is_owned, true} <- User.owns_actor(user, actor_id),
|
||||
{:is_owned, true, _} <- User.owns_actor(user, actor_id),
|
||||
{:event_can_be_managed, true} <- Event.can_event_be_managed_by(event, actor_id),
|
||||
event <- Mobilizon.Events.delete_event!(event) do
|
||||
{:ok, %{id: event.id}}
|
||||
|
||||
@@ -82,7 +82,7 @@ defmodule MobilizonWeb.Resolvers.Group do
|
||||
}
|
||||
) do
|
||||
with {:ok, %Actor{} = group} <- Actors.find_group_by_actor_id(group_id),
|
||||
{:is_owned, true} <- User.owns_actor(user, actor_id),
|
||||
{:is_owned, true, _} <- User.owns_actor(user, actor_id),
|
||||
{:ok, %Member{} = member} <- Member.get_member(actor_id, group.id),
|
||||
{:is_admin, true} <- Member.is_administrator(member),
|
||||
group <- Actors.delete_group!(group) do
|
||||
|
||||
Reference in New Issue
Block a user