Allow to accept / reject participants

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-09-20 18:22:03 +02:00
parent ffa4ec9209
commit abf3a58657
31 changed files with 1208 additions and 299 deletions

View File

@@ -24,6 +24,7 @@ defmodule MobilizonWeb.API.Events do
begins_on: begins_on,
ends_on: ends_on,
category: category,
join_options: join_options,
options: options
} <- prepare_args(args),
event <-
@@ -39,7 +40,8 @@ defmodule MobilizonWeb.API.Events do
ends_on: ends_on,
physical_address: physical_address,
category: category,
options: options
options: options,
join_options: join_options
}
) do
ActivityPub.create(%{
@@ -73,6 +75,7 @@ defmodule MobilizonWeb.API.Events do
begins_on: begins_on,
ends_on: ends_on,
category: category,
join_options: join_options,
options: options
} <-
prepare_args(Map.merge(event, args)),
@@ -89,6 +92,7 @@ defmodule MobilizonWeb.API.Events do
ends_on: ends_on,
physical_address: physical_address,
category: category,
join_options: join_options,
options: options
},
event.uuid,
@@ -112,7 +116,8 @@ defmodule MobilizonWeb.API.Events do
options: options,
tags: tags,
begins_on: begins_on,
category: category
category: category,
join_options: join_options
} = args
) do
with physical_address <- Map.get(args, :physical_address, nil),
@@ -132,6 +137,7 @@ defmodule MobilizonWeb.API.Events do
begins_on: begins_on,
ends_on: Map.get(args, :ends_on, nil),
category: category,
join_options: join_options,
options: options
}
end

View File

@@ -4,9 +4,9 @@ defmodule MobilizonWeb.API.Participations do
"""
alias Mobilizon.Actors.Actor
alias Mobilizon.Events
alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.Service.ActivityPub
require Logger
@spec join(Event.t(), Actor.t()) :: {:ok, Participant.t()}
def join(%Event{id: event_id} = event, %Actor{id: actor_id} = actor) do
@@ -21,4 +21,42 @@ defmodule MobilizonWeb.API.Participations do
{:ok, activity, participant}
end
end
def accept(
%Participant{} = participation,
%Actor{} = moderator
) do
with {:ok, activity, _} <-
ActivityPub.accept(
%{
to: [participation.actor.url],
actor: moderator.url,
object: participation.url
},
"#{MobilizonWeb.Endpoint.url()}/accept/join/#{participation.id}"
),
{:ok, %Participant{role: :participant} = participation} <-
Events.update_participant(participation, %{"role" => :participant}) do
{:ok, activity, participation}
end
end
def reject(
%Participant{} = participation,
%Actor{} = moderator
) do
with {:ok, activity, _} <-
ActivityPub.reject(
%{
to: [participation.actor.url],
actor: moderator.url,
object: participation.url
},
"#{MobilizonWeb.Endpoint.url()}/reject/join/#{participation.id}"
),
{:ok, %Participant{} = participation} <-
Events.delete_participant(participation) do
{:ok, activity, participation}
end
end
end

View File

@@ -42,14 +42,30 @@ defmodule MobilizonWeb.Resolvers.Event do
List participant for event (separate request)
"""
def list_participants_for_event(_parent, %{uuid: uuid, page: page, limit: limit}, _resolution) do
{:ok, Mobilizon.Events.list_participants_for_event(uuid, page, limit)}
{:ok, Mobilizon.Events.list_participants_for_event(uuid, [], page, limit)}
end
@doc """
List participants for event (through an event request)
"""
def list_participants_for_event(%Event{uuid: uuid}, _args, _resolution) do
{:ok, Mobilizon.Events.list_participants_for_event(uuid, 1, 10)}
def list_participants_for_event(
%Event{uuid: uuid},
%{page: page, limit: limit, roles: roles},
_resolution
) do
roles =
case roles do
"" ->
[]
roles ->
roles
|> String.split(",")
|> Enum.map(&String.downcase/1)
|> Enum.map(&String.to_existing_atom/1)
end
{:ok, Mobilizon.Events.list_participants_for_event(uuid, roles, page, limit)}
end
def stats_participants_for_event(%Event{id: id}, _args, _resolution) do
@@ -175,6 +191,87 @@ defmodule MobilizonWeb.Resolvers.Event do
{:error, "You need to be logged-in to leave an event"}
end
def accept_participation(
_parent,
%{id: participation_id, moderator_actor_id: moderator_actor_id},
%{
context: %{
current_user: user
}
}
) do
# Check that moderator provided is rightly authenticated
with {:is_owned, true, moderator_actor} <- User.owns_actor(user, moderator_actor_id),
# Check that participation already exists
{:has_participation, %Participant{role: :not_approved} = participation} <-
{:has_participation, Mobilizon.Events.get_participant(participation_id)},
# Check that moderator has right
{:actor_approve_permission, true} <-
{:actor_approve_permission,
Mobilizon.Events.moderator_for_event?(participation.event.id, moderator_actor_id)},
{:ok, _activity, participation} <-
MobilizonWeb.API.Participations.accept(participation, moderator_actor) do
{:ok, participation}
else
{:is_owned, false} ->
{:error, "Moderator Actor ID is not owned by authenticated user"}
{:has_participation, %Participant{role: role, id: id}} ->
{:error,
"Participant #{id} can't be approved since it's already a participant (with role #{role})"}
{:actor_approve_permission, _} ->
{:error, "Provided moderator actor ID doesn't have permission on this event"}
{:error, :participant_not_found} ->
{:error, "Participant not found"}
end
end
def reject_participation(
_parent,
%{id: participation_id, moderator_actor_id: moderator_actor_id},
%{
context: %{
current_user: user
}
}
) do
# Check that moderator provided is rightly authenticated
with {:is_owned, true, moderator_actor} <- User.owns_actor(user, moderator_actor_id),
# Check that participation really exists
{:has_participation, %Participant{} = participation} <-
{:has_participation, Mobilizon.Events.get_participant(participation_id)},
# Check that moderator has right
{:actor_approve_permission, true} <-
{:actor_approve_permission,
Mobilizon.Events.moderator_for_event?(participation.event.id, moderator_actor_id)},
{:ok, _activity, participation} <-
MobilizonWeb.API.Participations.reject(participation, moderator_actor) do
{
:ok,
%{
id: participation.id,
event: %{
id: participation.event.id
},
actor: %{
id: participation.actor.id
}
}
}
else
{:is_owned, false} ->
{:error, "Moderator Actor ID is not owned by authenticated user"}
{:actor_approve_permission, _} ->
{:error, "Provided moderator actor ID doesn't have permission on this event"}
{:has_participation, nil} ->
{:error, "Participant not found"}
end
end
@doc """
Create an event
"""

View File

@@ -23,7 +23,8 @@ defmodule MobilizonWeb.Schema.EventType do
field(:begins_on, :datetime, description: "Datetime for when the event begins")
field(:ends_on, :datetime, description: "Datetime for when the event ends")
field(:status, :event_status, description: "Status of the event")
field(:visibility, :event_visibility, description: "The event's visibility")
field(:visibility, :event_visibility, description: "The event's visibility")
field(:join_options, :event_join_options, description: "The event's visibility")
field(:picture, :picture,
description: "The event's picture",
@@ -56,10 +57,12 @@ defmodule MobilizonWeb.Schema.EventType do
field(:participant_stats, :participant_stats, resolve: &Event.stats_participants_for_event/3)
field(:participants, list_of(:participant),
resolve: &Event.list_participants_for_event/3,
description: "The event's participants"
)
field(:participants, list_of(:participant), description: "The event's participants") do
arg(:page, :integer, default_value: 1)
arg(:limit, :integer, default_value: 10)
arg(:roles, :string, default_value: "")
resolve(&Event.list_participants_for_event/3)
end
field(:related_events, list_of(:event),
resolve: &Event.list_related_events/3,
@@ -78,13 +81,18 @@ defmodule MobilizonWeb.Schema.EventType do
enum :event_visibility do
value(:public, description: "Publicly listed and federated. Can be shared.")
value(:unlisted, description: "Visible only to people with the link - or invited")
value(:restricted, description: "Visible only after a moderator accepted")
value(:private,
description: "Visible only to people members of the group or followers of the person"
)
end
value(:moderated, description: "Visible only after a moderator accepted")
value(:invite, description: "visible only to people invited")
@desc "The list of join options for an event"
enum :event_join_options do
value(:free, description: "Anyone can join and is automatically accepted")
value(:restricted, description: "Manual acceptation")
value(:invite, description: "Participants must be invited")
end
@desc "The list of possible options for the event's status"
@@ -218,6 +226,7 @@ defmodule MobilizonWeb.Schema.EventType do
arg(:ends_on, :datetime)
arg(:status, :event_status)
arg(:visibility, :event_visibility, default_value: :private)
arg(:join_options, :event_join_options, default_value: :free)
arg(:tags, list_of(:string),
default_value: [],
@@ -250,6 +259,7 @@ defmodule MobilizonWeb.Schema.EventType do
arg(:ends_on, :datetime)
arg(:status, :event_status)
arg(:visibility, :event_visibility)
arg(:join_options, :event_join_options)
arg(:tags, list_of(:string), description: "The list of tags associated to the event")

View File

@@ -10,6 +10,8 @@ defmodule MobilizonWeb.Schema.Events.ParticipantType do
@desc "Represents a participant to an event"
object :participant do
field(:id, :id, description: "The participation ID")
field(
:event,
:event,
@@ -24,11 +26,20 @@ defmodule MobilizonWeb.Schema.Events.ParticipantType do
description: "The actor that participates to the event"
)
field(:role, :integer, description: "The role of this actor at this event")
field(:role, :participant_role_enum, description: "The role of this actor at this event")
end
enum :participant_role_enum do
value(:not_approved)
value(:participant)
value(:moderator)
value(:administrator)
value(:creator)
end
@desc "Represents a deleted participant"
object :deleted_participant do
field(:id, :id)
field(:event, :deleted_object)
field(:actor, :deleted_object)
end
@@ -59,5 +70,21 @@ defmodule MobilizonWeb.Schema.Events.ParticipantType do
resolve(&Resolvers.Event.actor_leave_event/3)
end
@desc "Accept a participation"
field :accept_participation, :participant do
arg(:id, non_null(:id))
arg(:moderator_actor_id, non_null(:id))
resolve(&Resolvers.Event.accept_participation/3)
end
@desc "Reject a participation"
field :reject_participation, :deleted_participant do
arg(:id, non_null(:id))
arg(:moderator_actor_id, non_null(:id))
resolve(&Resolvers.Event.reject_participation/3)
end
end
end