Improve member adding and excluding flow

Allow to exclude a member

Send emails to the member when it's excluded

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2020-08-14 11:32:23 +02:00
parent ad13a57afc
commit 156eba0551
94 changed files with 2650 additions and 1862 deletions

View File

@@ -283,6 +283,7 @@ defmodule Mobilizon.Actors.Actor do
|> validate_required(@remote_actor_creation_required_attrs)
|> common_changeset(attrs)
|> unique_username_validator()
|> validate_required(:domain)
|> validate_length(:summary, max: 5000)
|> validate_length(:preferred_username, max: 100)

View File

@@ -558,7 +558,7 @@ defmodule Mobilizon.Actors do
@doc """
Gets a single member.
"""
@spec get_member(integer | String.t()) :: Member.t() | nil
@spec get_member(integer | String.t()) :: {:ok, Member.t()} | nil
def get_member(id) do
Member
|> Repo.get(id)
@@ -642,7 +642,14 @@ defmodule Mobilizon.Actors do
with {:ok, %Member{} = member} <-
%Member{}
|> Member.changeset(attrs)
|> Repo.insert() do
|> Repo.insert(
on_conflict: {:replace_all_except, [:id, :url, :actor_id, :parent_id]},
conflict_target: [:actor_id, :parent_id],
# See https://hexdocs.pm/ecto/Ecto.Repo.html#c:insert/2-upserts,
# when doing an upsert with on_conflict, PG doesn't return whether it's an insert or upsert
# so we need to refresh the fields
returning: true
) do
{:ok, Repo.preload(member, [:actor, :parent, :invited_by])}
end
end
@@ -739,6 +746,20 @@ defmodule Mobilizon.Actors do
|> Repo.all()
end
@doc """
Returns whether the member is the last administrator for a group
"""
@spec is_only_administrator?(integer | String.t(), integer | String.t()) :: boolean()
def is_only_administrator?(member_id, group_id) do
Member
|> where(
[m],
m.parent_id == ^group_id and m.id != ^member_id and m.role in ^@administrator_roles
)
|> Repo.aggregate(:count)
|> (&(&1 == 0)).()
end
@doc """
Gets a single bot.
Raises `Ecto.NoResultsError` if the bot does not exist.
@@ -1240,7 +1261,7 @@ defmodule Mobilizon.Actors do
from(
m in Member,
where: m.actor_id == ^actor_id,
preload: [:parent]
preload: [:parent, :invited_by]
)
end