Add admin interface to manage instances subscriptions
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -32,7 +32,6 @@ defmodule Mobilizon.Events.Comment do
|
||||
# When deleting an event we only nihilify everything
|
||||
@required_attrs [:url]
|
||||
@creation_required_attrs @required_attrs ++ [:text, :actor_id]
|
||||
@deletion_required_attrs @required_attrs ++ [:deleted_at]
|
||||
@optional_attrs [
|
||||
:text,
|
||||
:actor_id,
|
||||
@@ -81,11 +80,13 @@ defmodule Mobilizon.Events.Comment do
|
||||
|> validate_required(@creation_required_attrs)
|
||||
end
|
||||
|
||||
@spec delete_changeset(t, map) :: Ecto.Changeset.t()
|
||||
def delete_changeset(%__MODULE__{} = comment, attrs) do
|
||||
@spec delete_changeset(t) :: Ecto.Changeset.t()
|
||||
def delete_changeset(%__MODULE__{} = comment) do
|
||||
comment
|
||||
|> common_changeset(attrs)
|
||||
|> validate_required(@deletion_required_attrs)
|
||||
|> change()
|
||||
|> put_change(:text, nil)
|
||||
|> put_change(:actor_id, nil)
|
||||
|> put_change(:deleted_at, DateTime.utc_now() |> DateTime.truncate(:second))
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
||||
@@ -13,6 +13,8 @@ defmodule Mobilizon.Events.Event do
|
||||
|
||||
alias Mobilizon.Addresses
|
||||
|
||||
alias Mobilizon.Events
|
||||
|
||||
alias Mobilizon.Events.{
|
||||
Comment,
|
||||
EventOptions,
|
||||
@@ -73,6 +75,7 @@ defmodule Mobilizon.Events.Event do
|
||||
:category,
|
||||
:status,
|
||||
:draft,
|
||||
:local,
|
||||
:visibility,
|
||||
:join_options,
|
||||
:publish_at,
|
||||
@@ -190,13 +193,16 @@ defmodule Mobilizon.Events.Event do
|
||||
def can_be_managed_by(_event, _actor), do: {:event_can_be_managed, false}
|
||||
|
||||
@spec put_tags(Changeset.t(), map) :: Changeset.t()
|
||||
defp put_tags(%Changeset{} = changeset, %{tags: tags}),
|
||||
do: put_assoc(changeset, :tags, Enum.map(tags, &process_tag/1))
|
||||
defp put_tags(%Changeset{} = changeset, %{tags: tags}) do
|
||||
put_assoc(changeset, :tags, Enum.map(tags, &process_tag/1))
|
||||
end
|
||||
|
||||
defp put_tags(%Changeset{} = changeset, _), do: changeset
|
||||
|
||||
# We need a changeset instead of a raw struct because of slug which is generated in changeset
|
||||
defp process_tag(%{id: _id} = tag), do: tag
|
||||
defp process_tag(%{id: id} = _tag) do
|
||||
Events.get_tag(id)
|
||||
end
|
||||
|
||||
defp process_tag(tag) do
|
||||
Tag.changeset(%Tag{}, tag)
|
||||
|
||||
@@ -39,6 +39,21 @@ defmodule Mobilizon.Events.EventParticipantStats do
|
||||
@doc false
|
||||
@spec changeset(t, map) :: Ecto.Changeset.t()
|
||||
def changeset(%__MODULE__{} = event_options, attrs) do
|
||||
cast(event_options, attrs, @attrs)
|
||||
event_options
|
||||
|> cast(attrs, @attrs)
|
||||
|> validate_stats()
|
||||
end
|
||||
|
||||
defp validate_stats(%Ecto.Changeset{} = changeset) do
|
||||
changeset
|
||||
|> validate_number(:not_approved, greater_than_or_equal_to: 0)
|
||||
|> validate_number(:rejected, greater_than_or_equal_to: 0)
|
||||
|> validate_number(:participant, greater_than_or_equal_to: 0)
|
||||
|> validate_number(:moderator, greater_than_or_equal_to: 0)
|
||||
|> validate_number(:administrator, greater_than_or_equal_to: 0)
|
||||
|> validate_number(:creator, greater_than_or_equal_to: 0)
|
||||
|
||||
# TODO: Replace me with something like the following
|
||||
# Enum.reduce(@attrs, fn key, changeset -> validate_number(changeset, key, greater_than_or_equal_to: 0) end)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -97,6 +97,7 @@ defmodule Mobilizon.Events do
|
||||
|
||||
@comment_preloads [
|
||||
:actor,
|
||||
:event,
|
||||
:attributed_to,
|
||||
:in_reply_to_comment,
|
||||
:origin_comment,
|
||||
@@ -722,6 +723,13 @@ defmodule Mobilizon.Events do
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
@spec list_actors_participants_for_event(String.t()) :: [Actor.t()]
|
||||
def list_actors_participants_for_event(id) do
|
||||
id
|
||||
|> list_participant_actors_for_event_query
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the list of participations for an actor.
|
||||
|
||||
@@ -864,30 +872,15 @@ defmodule Mobilizon.Events do
|
||||
|> Multi.run(:update_event_participation_stats, fn _repo,
|
||||
%{
|
||||
participant:
|
||||
%Participant{
|
||||
role: role,
|
||||
event_id: event_id
|
||||
} = _participant
|
||||
%Participant{role: new_role} =
|
||||
participant
|
||||
} ->
|
||||
with {:update_event_participation_stats, true} <-
|
||||
{:update_event_participation_stats, update_event_participation_stats},
|
||||
{:ok, %Event{} = event} <- get_event(event_id),
|
||||
%EventParticipantStats{} = participant_stats <-
|
||||
Map.get(event, :participant_stats),
|
||||
%EventParticipantStats{} = participant_stats <-
|
||||
Map.update(participant_stats, role, 0, &(&1 + 1)),
|
||||
{:ok, %Event{} = event} <-
|
||||
event
|
||||
|> Event.update_changeset(%{
|
||||
participant_stats: Map.from_struct(participant_stats)
|
||||
})
|
||||
|> Repo.update() do
|
||||
{:ok, event}
|
||||
else
|
||||
{:update_event_participation_stats, false} -> {:ok, nil}
|
||||
{:error, :event_not_found} -> {:error, :event_not_found}
|
||||
err -> {:error, err}
|
||||
end
|
||||
update_participant_stats(
|
||||
participant,
|
||||
nil,
|
||||
new_role,
|
||||
update_event_participation_stats
|
||||
)
|
||||
end)
|
||||
|> Repo.transaction() do
|
||||
{:ok, Repo.preload(participant, [:event, :actor])}
|
||||
@@ -899,10 +892,21 @@ defmodule Mobilizon.Events do
|
||||
"""
|
||||
@spec update_participant(Participant.t(), map) ::
|
||||
{:ok, Participant.t()} | {:error, Changeset.t()}
|
||||
def update_participant(%Participant{} = participant, attrs) do
|
||||
participant
|
||||
|> Participant.changeset(attrs)
|
||||
|> Repo.update()
|
||||
def update_participant(%Participant{role: old_role} = participant, attrs) do
|
||||
with {:ok, %{participant: %Participant{} = participant}} <-
|
||||
Multi.new()
|
||||
|> Multi.update(:participant, Participant.changeset(participant, attrs))
|
||||
|> Multi.run(:update_event_participation_stats, fn _repo,
|
||||
%{
|
||||
participant:
|
||||
%Participant{role: new_role} =
|
||||
participant
|
||||
} ->
|
||||
update_participant_stats(participant, old_role, new_role)
|
||||
end)
|
||||
|> Repo.transaction() do
|
||||
{:ok, Repo.preload(participant, [:event, :actor])}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -910,7 +914,71 @@ defmodule Mobilizon.Events do
|
||||
"""
|
||||
@spec delete_participant(Participant.t()) ::
|
||||
{:ok, Participant.t()} | {:error, Changeset.t()}
|
||||
def delete_participant(%Participant{} = participant), do: Repo.delete(participant)
|
||||
def delete_participant(%Participant{role: old_role} = participant) do
|
||||
with {:ok, %{participant: %Participant{} = participant}} <-
|
||||
Multi.new()
|
||||
|> Multi.delete(:participant, participant)
|
||||
|> Multi.run(:update_event_participation_stats, fn _repo,
|
||||
%{
|
||||
participant:
|
||||
%Participant{} = participant
|
||||
} ->
|
||||
update_participant_stats(participant, old_role, nil)
|
||||
end)
|
||||
|> Repo.transaction() do
|
||||
{:ok, participant}
|
||||
end
|
||||
end
|
||||
|
||||
defp update_participant_stats(
|
||||
%Participant{
|
||||
event_id: event_id
|
||||
} = _participant,
|
||||
old_role,
|
||||
new_role,
|
||||
update_event_participation_stats \\ true
|
||||
) do
|
||||
with {:update_event_participation_stats, true} <-
|
||||
{:update_event_participation_stats, update_event_participation_stats},
|
||||
{:ok, %Event{} = event} <- get_event(event_id),
|
||||
%EventParticipantStats{} = participant_stats <-
|
||||
Map.get(event, :participant_stats),
|
||||
%EventParticipantStats{} = participant_stats <-
|
||||
do_update_participant_stats(participant_stats, old_role, new_role),
|
||||
{:ok, %Event{} = event} <-
|
||||
event
|
||||
|> Event.update_changeset(%{
|
||||
participant_stats: Map.from_struct(participant_stats)
|
||||
})
|
||||
|> Repo.update() do
|
||||
{:ok, event}
|
||||
else
|
||||
{:update_event_participation_stats, false} ->
|
||||
{:ok, nil}
|
||||
|
||||
{:error, :event_not_found} ->
|
||||
{:error, :event_not_found}
|
||||
|
||||
err ->
|
||||
{:error, err}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_update_participant_stats(participant_stats, old_role, new_role) do
|
||||
participant_stats
|
||||
|> decrease_participant_stats(old_role)
|
||||
|> increase_participant_stats(new_role)
|
||||
end
|
||||
|
||||
defp increase_participant_stats(participant_stats, nil), do: participant_stats
|
||||
|
||||
defp increase_participant_stats(participant_stats, role),
|
||||
do: Map.update(participant_stats, role, 0, &(&1 + 1))
|
||||
|
||||
defp decrease_participant_stats(participant_stats, nil), do: participant_stats
|
||||
|
||||
defp decrease_participant_stats(participant_stats, role),
|
||||
do: Map.update(participant_stats, role, 0, &(&1 - 1))
|
||||
|
||||
@doc """
|
||||
Gets a single session.
|
||||
@@ -1170,11 +1238,7 @@ defmodule Mobilizon.Events do
|
||||
@spec delete_comment(Comment.t()) :: {:ok, Comment.t()} | {:error, Changeset.t()}
|
||||
def delete_comment(%Comment{} = comment) do
|
||||
comment
|
||||
|> Comment.delete_changeset(%{
|
||||
text: nil,
|
||||
actor_id: nil,
|
||||
deleted_at: DateTime.utc_now()
|
||||
})
|
||||
|> Comment.delete_changeset()
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@@ -1561,14 +1625,22 @@ defmodule Mobilizon.Events do
|
||||
defp list_participants_for_event_query(event_id) do
|
||||
from(
|
||||
p in Participant,
|
||||
join: e in Event,
|
||||
on: p.event_id == e.id,
|
||||
where: e.id == ^event_id,
|
||||
where: p.event_id == ^event_id,
|
||||
preload: [:actor]
|
||||
)
|
||||
end
|
||||
|
||||
@spec list_participants_for_event_query(String.t()) :: Ecto.Query.t()
|
||||
@spec list_participant_actors_for_event_query(String.t()) :: Ecto.Query.t()
|
||||
defp list_participant_actors_for_event_query(event_id) do
|
||||
from(
|
||||
a in Actor,
|
||||
join: p in Participant,
|
||||
on: p.actor_id == a.id,
|
||||
where: p.event_id == ^event_id
|
||||
)
|
||||
end
|
||||
|
||||
@spec list_local_emails_user_participants_for_event_query(String.t()) :: Ecto.Query.t()
|
||||
def list_local_emails_user_participants_for_event_query(event_id) do
|
||||
Participant
|
||||
|> join(:inner, [p], a in Actor, on: p.actor_id == a.id and is_nil(a.domain))
|
||||
|
||||
@@ -57,6 +57,7 @@ defmodule Mobilizon.Events.Participant do
|
||||
|> cast(attrs, @attrs)
|
||||
|> ensure_url()
|
||||
|> validate_required(@required_attrs)
|
||||
|> unique_constraint(:actor_id, name: :participants_event_id_actor_id_index)
|
||||
end
|
||||
|
||||
# If there's a blank URL that's because we're doing the first insert
|
||||
|
||||
Reference in New Issue
Block a user