2020-01-26 21:11:16 +01:00
|
|
|
defmodule Mobilizon.GraphQL.API.Participations do
|
2019-08-14 17:45:11 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Common API to join events and groups.
|
2019-08-14 17:45:11 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Events
|
2019-08-14 17:45:11 +02:00
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Actions, Activity}
|
2020-10-07 17:05:15 +02:00
|
|
|
alias Mobilizon.Service.Notifications.Scheduler
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.Email.Participation
|
2019-08-14 17:45:11 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec join(Event.t(), Actor.t(), map()) ::
|
|
|
|
{:ok, Activity.t(), Participant.t()} | {:error, :already_participant}
|
2019-12-20 13:04:34 +01:00
|
|
|
def join(%Event{id: event_id} = event, %Actor{id: actor_id} = actor, args \\ %{}) do
|
2021-09-24 16:46:42 +02:00
|
|
|
case Mobilizon.Events.get_participant(event_id, actor_id, args) do
|
|
|
|
{:ok, %Participant{}} ->
|
|
|
|
{:error, :already_participant}
|
2019-08-14 17:45:11 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
{:error, :participant_not_found} ->
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Join.join(event, actor, Map.get(args, :local, true), %{metadata: args})
|
2019-08-14 17:45:11 +02:00
|
|
|
end
|
|
|
|
end
|
2019-09-20 18:22:03 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec leave(Event.t(), Actor.t(), map()) ::
|
|
|
|
{:ok, Activity.t(), Participant.t()}
|
|
|
|
| {:error, :is_only_organizer | :participant_not_found | Ecto.Changeset.t()}
|
|
|
|
def leave(%Event{} = event, %Actor{} = actor, args \\ %{}),
|
2021-09-28 19:40:37 +02:00
|
|
|
do: Actions.Leave.leave(event, actor, Map.get(args, :local, true), %{metadata: args})
|
2021-09-24 16:46:42 +02:00
|
|
|
|
2019-09-30 13:48:47 +02:00
|
|
|
@doc """
|
|
|
|
Update participation status
|
|
|
|
"""
|
2021-10-04 18:59:41 +02:00
|
|
|
@spec update(Participant.t(), Actor.t(), atom()) ::
|
|
|
|
{:ok, Activity.t(), Participant.t()} | {:error, Ecto.Changeset.t()}
|
2019-12-20 13:04:34 +01:00
|
|
|
def update(%Participant{} = participation, %Actor{} = moderator, :participant),
|
|
|
|
do: accept(participation, moderator)
|
2019-09-30 13:48:47 +02:00
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
def update(%Participant{} = participation, %Actor{} = _moderator, :not_approved) do
|
|
|
|
with {:ok, %Participant{} = participant} <-
|
|
|
|
Events.update_participant(participation, %{role: :not_approved}) do
|
2020-10-07 17:05:15 +02:00
|
|
|
Scheduler.pending_participation_notification(participation.event)
|
2019-12-20 13:04:34 +01:00
|
|
|
{:ok, nil, participant}
|
|
|
|
end
|
2020-01-26 21:11:16 +01:00
|
|
|
end
|
2019-09-30 13:48:47 +02:00
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
def update(%Participant{} = participation, %Actor{} = moderator, :rejected),
|
|
|
|
do: reject(participation, moderator)
|
|
|
|
|
2021-10-04 18:59:41 +02:00
|
|
|
@spec accept(Participant.t(), Actor.t()) ::
|
|
|
|
{:ok, Activity.t(), Participant.t()} | {:error, Ecto.Changeset.t()}
|
2019-09-30 13:48:47 +02:00
|
|
|
defp accept(
|
|
|
|
%Participant{} = participation,
|
|
|
|
%Actor{} = moderator
|
|
|
|
) do
|
2021-09-28 19:40:37 +02:00
|
|
|
case Actions.Accept.accept(
|
|
|
|
:join,
|
|
|
|
participation,
|
|
|
|
true,
|
|
|
|
%{"actor" => moderator.url}
|
|
|
|
) do
|
|
|
|
{:ok, activity, %Participant{role: :participant} = participation} ->
|
|
|
|
Participation.send_emails_to_local_user(participation)
|
|
|
|
{:ok, activity, participation}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2019-09-20 18:22:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
@spec reject(Participant.t(), Actor.t()) :: {:ok, Activity.t(), Participant.t()}
|
2019-09-30 13:48:47 +02:00
|
|
|
defp reject(
|
|
|
|
%Participant{} = participation,
|
|
|
|
%Actor{} = moderator
|
|
|
|
) do
|
2019-12-03 11:29:51 +01:00
|
|
|
with {:ok, activity, %Participant{role: :rejected} = participation} <-
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Reject.reject(
|
2019-12-03 11:29:51 +01:00
|
|
|
:join,
|
|
|
|
participation,
|
2021-08-13 11:22:04 +02:00
|
|
|
true,
|
2019-12-03 11:29:51 +01:00
|
|
|
%{"actor" => moderator.url}
|
2019-09-20 18:22:03 +02:00
|
|
|
),
|
2019-09-30 13:48:47 +02:00
|
|
|
:ok <- Participation.send_emails_to_local_user(participation) do
|
2019-09-20 18:22:03 +02:00
|
|
|
{:ok, activity, participation}
|
|
|
|
end
|
|
|
|
end
|
2019-08-14 17:45:11 +02:00
|
|
|
end
|