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}
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2019-12-20 12:04:34 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.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
|
|
|
|
2019-12-20 12:04:34 +00:00
|
|
|
@spec join(Event.t(), Actor.t(), map()) :: {:ok, Activity.t(), Participant.t()}
|
|
|
|
def join(%Event{id: event_id} = event, %Actor{id: actor_id} = actor, args \\ %{}) do
|
|
|
|
with {:error, :participant_not_found} <-
|
|
|
|
Mobilizon.Events.get_participant(event_id, actor_id, args),
|
|
|
|
{:ok, activity, participant} <-
|
|
|
|
ActivityPub.join(event, actor, Map.get(args, :local, true), %{metadata: args}) do
|
2019-08-14 15:45:11 +00:00
|
|
|
{:ok, activity, participant}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-20 12:04:34 +00:00
|
|
|
@spec leave(Event.t(), Actor.t()) :: {:ok, Activity.t(), Participant.t()}
|
|
|
|
def leave(%Event{} = event, %Actor{} = actor, args \\ %{}) do
|
|
|
|
with {:ok, activity, participant} <-
|
|
|
|
ActivityPub.leave(event, actor, Map.get(args, :local, true), %{metadata: args}) do
|
2019-08-14 15:45:11 +00:00
|
|
|
{:ok, activity, participant}
|
|
|
|
end
|
|
|
|
end
|
2019-09-20 16:22:03 +00:00
|
|
|
|
2019-09-30 11:48:47 +00:00
|
|
|
@doc """
|
|
|
|
Update participation status
|
|
|
|
"""
|
2019-12-20 12:04:34 +00:00
|
|
|
@spec update(Participant.t(), Actor.t(), atom()) :: {:ok, Activity.t(), Participant.t()}
|
|
|
|
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
|
|
|
@spec update(Participant.t(), Actor.t(), atom()) :: {:ok, Activity.t(), Participant.t()}
|
|
|
|
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
|
|
|
@spec update(Participant.t(), Actor.t(), atom()) :: {:ok, Activity.t(), Participant.t()}
|
|
|
|
def update(%Participant{} = participation, %Actor{} = moderator, :rejected),
|
|
|
|
do: reject(participation, moderator)
|
|
|
|
|
|
|
|
@spec accept(Participant.t(), Actor.t()) :: {:ok, Activity.t(), Participant.t()}
|
2019-09-30 11:48:47 +00:00
|
|
|
defp accept(
|
|
|
|
%Participant{} = participation,
|
|
|
|
%Actor{} = moderator
|
|
|
|
) do
|
2019-12-03 10:29:51 +00:00
|
|
|
with {:ok, activity, %Participant{role: :participant} = participation} <-
|
2019-09-20 16:22:03 +00:00
|
|
|
ActivityPub.accept(
|
2019-10-25 15:43:37 +00:00
|
|
|
:join,
|
|
|
|
participation,
|
|
|
|
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-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} <-
|
2019-09-20 16:22:03 +00:00
|
|
|
ActivityPub.reject(
|
2019-12-03 10:29:51 +00:00
|
|
|
:join,
|
|
|
|
participation,
|
|
|
|
%{"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
|