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