Add API to join and leave an event

This commit is contained in:
Chocobozzz
2019-02-01 15:38:35 +01:00
committed by Thomas Citharel
parent 421fbda9fa
commit 250f0b3bd1
12 changed files with 430 additions and 74 deletions

View File

@@ -138,15 +138,10 @@ defmodule Mobilizon.Actors.User do
{:ok, user}
end
def owns_actor(%User{default_actor_id: default_actor_id}, %Actor{id: actor_id})
when default_actor_id == actor_id do
{:is_owned, true}
end
def owns_actor(%User{actors: actors}, actor_id) do
case Enum.any?(actors, fn a -> a.id == actor_id end) do
true -> {:is_owned, true}
_ -> {:is_owned, false}
case Enum.find(actors, fn a -> a.id == actor_id end) do
nil -> {:is_owned, false}
actor -> {:is_owned, true, actor}
end
end
end

View File

@@ -600,6 +600,16 @@ defmodule Mobilizon.Events do
Repo.get_by!(Participant, event_id: event_id, actor_id: actor_id)
end
@doc """
Get a single participant
"""
def get_participant(event_id, actor_id) do
case Repo.get_by(Participant, event_id: event_id, actor_id: actor_id) do
nil -> {:error, :participant_not_found}
participant -> {:ok, participant}
end
end
@doc """
Creates a participant.
@@ -665,6 +675,18 @@ defmodule Mobilizon.Events do
Participant.changeset(participant, %{})
end
@doc """
Get the default participant role depending on the event visbility
"""
def get_default_participant_role(%Event{} = event) do
case event.visibility do
# Participant
:public -> 1
# Not approved
_ -> 0
end
end
@doc """
List event participation requests for an actor
"""

View File

@@ -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}}

View File

@@ -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

View File

@@ -143,6 +143,7 @@ defmodule MobilizonWeb.Schema do
import_fields(:event_mutations)
import_fields(:category_mutations)
import_fields(:comment_mutations)
import_fields(:participant_mutations)
# @desc "Upload a picture"
# field :upload_picture, :picture do

View File

@@ -12,7 +12,7 @@ defmodule MobilizonWeb.Schema.ActorInterface do
@desc "An ActivityPub actor"
interface :actor do
field(:id, :id, description: "Internal ID for this actor")
field(:id, :integer, description: "Internal ID for this actor")
field(:url, :string, description: "The ActivityPub actor's URL")
field(:type, :actor_type, description: "The type of Actor (Person, Group,…)")
field(:name, :string, description: "The actor's displayed name")

View File

@@ -13,7 +13,7 @@ defmodule MobilizonWeb.Schema.Actors.GroupType do
object :group do
interfaces([:actor])
field(:id, :id, description: "Internal ID for this group")
field(:id, :integer, description: "Internal ID for this group")
field(:url, :string, description: "The ActivityPub actor's URL")
field(:type, :actor_type, description: "The type of Actor (Person, Group,…)")
field(:name, :string, description: "The actor's displayed name")

View File

@@ -13,7 +13,7 @@ defmodule MobilizonWeb.Schema.Actors.PersonType do
"""
object :person do
interfaces([:actor])
field(:id, :id, description: "Internal ID for this person")
field(:id, :integer, description: "Internal ID for this person")
field(:user, :user, description: "The user this actor is associated to")
field(:member_of, list_of(:member), description: "The list of groups this person is member of")

View File

@@ -12,7 +12,7 @@ defmodule MobilizonWeb.Schema.EventType do
@desc "An event"
object :event do
field(:id, :id, description: "Internal ID for this event")
field(:id, :integer, description: "Internal ID for this event")
field(:uuid, :uuid, description: "The Event UUID")
field(:url, :string, description: "The ActivityPub Event URL")
field(:local, :boolean, description: "Whether the event is local or not")

View File

@@ -5,18 +5,34 @@ defmodule MobilizonWeb.Schema.Events.ParticipantType do
use Absinthe.Schema.Notation
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
alias MobilizonWeb.Resolvers
alias Mobilizon.Events
alias Mobilizon.Actors
@desc "Represents a participant to an event"
object :participant do
field(:event, :event,
field(
:event,
:event,
resolve: dataloader(Events),
description: "The event which the actor participates in"
)
field(:actor, :actor, description: "The actor that participates to the event")
field(
:actor,
:actor,
resolve: dataloader(Actors),
description: "The actor that participates to the event"
)
field(:role, :integer, description: "The role of this actor at this event")
end
@desc "Represents a deleted participant"
object :deleted_participant do
field(:event, :deleted_object)
field(:actor, :deleted_object)
end
object :participant_queries do
@desc "Get all participants for an event uuid"
field :participants, list_of(:participant) do
@@ -26,4 +42,22 @@ defmodule MobilizonWeb.Schema.Events.ParticipantType do
resolve(&Resolvers.Event.list_participants_for_event/3)
end
end
object :participant_mutations do
@desc "Join an event"
field :join_event, :participant do
arg(:event_id, non_null(:integer))
arg(:actor_id, non_null(:integer))
resolve(&Resolvers.Event.actor_join_event/3)
end
@desc "Leave an event"
field :leave_event, :deleted_participant do
arg(:event_id, non_null(:integer))
arg(:actor_id, non_null(:integer))
resolve(&Resolvers.Event.actor_leave_event/3)
end
end
end