2019-08-14 17:45:11 +02:00
|
|
|
defmodule MobilizonWeb.API.Participations do
|
|
|
|
@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
|
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
2019-09-30 13:48:47 +02:00
|
|
|
alias MobilizonWeb.Email.Participation
|
2019-08-14 17:45:11 +02:00
|
|
|
|
|
|
|
@spec join(Event.t(), Actor.t()) :: {:ok, Participant.t()}
|
|
|
|
def join(%Event{id: event_id} = event, %Actor{id: actor_id} = actor) do
|
|
|
|
with {:error, :participant_not_found} <- Mobilizon.Events.get_participant(event_id, actor_id),
|
|
|
|
{:ok, activity, participant} <- ActivityPub.join(event, actor, true) do
|
|
|
|
{:ok, activity, participant}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def leave(%Event{} = event, %Actor{} = actor) do
|
|
|
|
with {:ok, activity, participant} <- ActivityPub.leave(event, actor, true) do
|
|
|
|
{:ok, activity, participant}
|
|
|
|
end
|
|
|
|
end
|
2019-09-20 18:22:03 +02:00
|
|
|
|
2019-09-30 13:48:47 +02:00
|
|
|
@doc """
|
|
|
|
Update participation status
|
|
|
|
"""
|
|
|
|
def update(%Participant{} = participation, %Actor{} = moderator, :participant),
|
|
|
|
do: accept(participation, moderator)
|
|
|
|
|
|
|
|
def update(%Participant{} = participation, %Actor{} = moderator, :rejected),
|
|
|
|
do: reject(participation, moderator)
|
|
|
|
|
|
|
|
defp accept(
|
|
|
|
%Participant{} = participation,
|
|
|
|
%Actor{} = moderator
|
|
|
|
) do
|
2019-12-03 11:29:51 +01:00
|
|
|
with {:ok, activity, %Participant{role: :participant} = participation} <-
|
2019-09-20 18:22:03 +02:00
|
|
|
ActivityPub.accept(
|
2019-10-25 17:43:37 +02:00
|
|
|
:join,
|
|
|
|
participation,
|
|
|
|
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-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} <-
|
2019-09-20 18:22:03 +02:00
|
|
|
ActivityPub.reject(
|
2019-12-03 11:29:51 +01:00
|
|
|
:join,
|
|
|
|
participation,
|
|
|
|
%{"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
|