2019-03-05 17:23:05 +01:00
|
|
|
defmodule Mobilizon.Users do
|
|
|
|
@moduledoc """
|
|
|
|
The Users context.
|
|
|
|
"""
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
import Ecto.Query
|
|
|
|
import EctoEnum
|
2019-03-05 17:23:05 +01:00
|
|
|
|
2019-09-08 01:49:56 +02:00
|
|
|
import Mobilizon.Storage.Ecto
|
2019-03-05 17:23:05 +01:00
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
alias Ecto.Multi
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-07 19:54:11 +02:00
|
|
|
alias Mobilizon.Events
|
2020-06-11 19:13:21 +02:00
|
|
|
alias Mobilizon.Events.FeedToken
|
2019-09-08 01:49:56 +02:00
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Users.{Setting, User}
|
2019-03-05 17:23:05 +01:00
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
defenum(UserRole, :user_role, [:administrator, :moderator, :user])
|
|
|
|
|
2020-06-08 12:28:19 +02:00
|
|
|
defenum(NotificationPendingNotificationDelay, none: 0, direct: 1, one_hour: 5, one_day: 10)
|
|
|
|
|
2019-03-08 18:52:27 +01:00
|
|
|
@doc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Registers an user.
|
2019-03-08 18:52:27 +01:00
|
|
|
"""
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec register(map) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
2019-10-01 13:08:09 +02:00
|
|
|
def register(args) do
|
2019-03-08 18:52:27 +01:00
|
|
|
with {:ok, %User{} = user} <-
|
2019-08-12 16:04:16 +02:00
|
|
|
%User{}
|
|
|
|
|> User.registration_changeset(args)
|
2019-09-07 19:54:11 +02:00
|
|
|
|> Repo.insert() do
|
2020-02-13 15:48:12 +01:00
|
|
|
Events.create_feed_token(%{user_id: user.id})
|
2019-09-07 19:54:11 +02:00
|
|
|
|
2019-03-08 18:52:27 +01:00
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
@spec create_external(String.t(), String.t()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def create_external(email, provider) do
|
|
|
|
with {:ok, %User{} = user} <-
|
|
|
|
%User{}
|
|
|
|
|> User.auth_provider_changeset(%{email: email, provider: provider})
|
|
|
|
|> Repo.insert() do
|
|
|
|
Events.create_feed_token(%{user_id: user.id})
|
|
|
|
|
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-05 17:23:05 +01:00
|
|
|
@doc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Gets a single user.
|
|
|
|
Raises `Ecto.NoResultsError` if the user does not exist.
|
|
|
|
"""
|
|
|
|
@spec get_user!(integer | String.t()) :: User.t()
|
|
|
|
def get_user!(id), do: Repo.get!(User, id)
|
2019-03-05 17:23:05 +01:00
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
@spec get_user(integer | String.t() | nil) :: User.t() | nil
|
|
|
|
def get_user(nil), do: nil
|
2020-02-18 08:57:00 +01:00
|
|
|
def get_user(id), do: Repo.get(User, id)
|
|
|
|
|
|
|
|
def get_user_with_settings!(id) do
|
|
|
|
User
|
|
|
|
|> Repo.get(id)
|
|
|
|
|> Repo.preload([:settings])
|
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@doc """
|
|
|
|
Gets an user by its email.
|
2019-03-05 17:23:05 +01:00
|
|
|
"""
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec get_user_by_email(String.t(), boolean | nil) ::
|
|
|
|
{:ok, User.t()} | {:error, :user_not_found}
|
2019-03-05 17:23:05 +01:00
|
|
|
def get_user_by_email(email, activated \\ nil) do
|
2019-09-07 19:54:11 +02:00
|
|
|
query = user_by_email_query(email, activated)
|
2019-03-05 17:23:05 +01:00
|
|
|
|
|
|
|
case Repo.one(query) do
|
2019-09-11 03:16:37 +02:00
|
|
|
nil ->
|
|
|
|
{:error, :user_not_found}
|
|
|
|
|
|
|
|
user ->
|
|
|
|
{:ok, user}
|
2019-03-05 17:23:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
@doc """
|
|
|
|
Gets an user by its email.
|
|
|
|
"""
|
|
|
|
@spec get_user_by_email!(String.t(), boolean | nil) :: User.t()
|
|
|
|
def get_user_by_email!(email, activated \\ nil) do
|
|
|
|
email
|
|
|
|
|> user_by_email_query(activated)
|
|
|
|
|> Repo.one!()
|
|
|
|
end
|
|
|
|
|
2019-03-05 17:23:05 +01:00
|
|
|
@doc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Get an user by its activation token.
|
2019-03-05 17:23:05 +01:00
|
|
|
"""
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec get_user_by_activation_token(String.t()) :: Actor.t() | nil
|
2019-03-05 17:23:05 +01:00
|
|
|
def get_user_by_activation_token(token) do
|
2019-09-07 19:54:11 +02:00
|
|
|
token
|
|
|
|
|> user_by_activation_token_query()
|
|
|
|
|> Repo.one()
|
2019-03-05 17:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Get an user by its reset password token.
|
2019-03-05 17:23:05 +01:00
|
|
|
"""
|
|
|
|
@spec get_user_by_reset_password_token(String.t()) :: Actor.t()
|
|
|
|
def get_user_by_reset_password_token(token) do
|
2019-09-07 19:54:11 +02:00
|
|
|
token
|
|
|
|
|> user_by_reset_password_token_query()
|
|
|
|
|> Repo.one()
|
2019-03-05 17:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Updates an user.
|
2019-03-05 17:23:05 +01:00
|
|
|
"""
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec update_user(User.t(), map) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
2019-03-05 17:23:05 +01:00
|
|
|
def update_user(%User{} = user, attrs) do
|
|
|
|
with {:ok, %User{} = user} <-
|
|
|
|
user
|
|
|
|
|> User.changeset(attrs)
|
|
|
|
|> Repo.update() do
|
|
|
|
{:ok, Repo.preload(user, [:default_actor])}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
@delete_user_default_options [reserve_email: true]
|
|
|
|
|
2019-03-05 17:23:05 +01:00
|
|
|
@doc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Deletes an user.
|
2020-11-26 14:48:10 +01:00
|
|
|
|
|
|
|
Options:
|
|
|
|
* `reserve_email` whether to keep a record of the email so that the user can't register again
|
2019-03-05 17:23:05 +01:00
|
|
|
"""
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec delete_user(User.t()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
2020-06-11 19:13:21 +02:00
|
|
|
def delete_user(%User{id: user_id} = user, options \\ @delete_user_default_options) do
|
|
|
|
delete_user_options = Keyword.merge(@delete_user_default_options, options)
|
|
|
|
|
|
|
|
multi =
|
|
|
|
Multi.new()
|
|
|
|
|> Multi.delete_all(:settings, from(s in Setting, where: s.user_id == ^user_id))
|
|
|
|
|> Multi.delete_all(:feed_tokens, from(f in FeedToken, where: f.user_id == ^user_id))
|
|
|
|
|
|
|
|
multi =
|
|
|
|
if Keyword.get(delete_user_options, :reserve_email, true) do
|
|
|
|
Multi.update(multi, :user, User.delete_changeset(user))
|
|
|
|
else
|
|
|
|
Multi.delete(multi, :user, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
case Repo.transaction(multi) do
|
|
|
|
{:ok, %{user: %User{} = user}} ->
|
|
|
|
{:ok, user}
|
|
|
|
|
|
|
|
{:error, remove, error, _} when remove in [:settings, :feed_tokens] ->
|
|
|
|
{:error, error}
|
|
|
|
end
|
|
|
|
end
|
2019-03-05 17:23:05 +01:00
|
|
|
|
|
|
|
@doc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Get an user with its actors
|
|
|
|
Raises `Ecto.NoResultsError` if the user does not exist.
|
2019-03-05 17:23:05 +01:00
|
|
|
"""
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec get_user_with_actors!(integer | String.t()) :: User.t()
|
2019-03-05 17:23:05 +01:00
|
|
|
def get_user_with_actors!(id) do
|
2019-09-07 19:54:11 +02:00
|
|
|
id
|
|
|
|
|> get_user!()
|
|
|
|
|> Repo.preload([:actors, :default_actor])
|
2019-03-05 17:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Get user with its actors.
|
2019-03-05 17:23:05 +01:00
|
|
|
"""
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec get_user_with_actors(integer()) :: {:ok, User.t()} | {:error, String.t()}
|
2019-03-05 17:23:05 +01:00
|
|
|
def get_user_with_actors(id) do
|
|
|
|
case Repo.get(User, id) do
|
|
|
|
nil ->
|
|
|
|
{:error, "User with ID #{id} not found"}
|
|
|
|
|
|
|
|
user ->
|
|
|
|
user =
|
|
|
|
user
|
|
|
|
|> Repo.preload([:actors, :default_actor])
|
|
|
|
|> Map.put(:actors, get_actors_for_user(user))
|
|
|
|
|
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Gets the associated actor for an user, either the default set one or the first
|
|
|
|
found.
|
2019-03-05 17:23:05 +01:00
|
|
|
"""
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec get_actor_for_user(User.t()) :: Actor.t() | nil
|
|
|
|
def get_actor_for_user(%User{} = user) do
|
|
|
|
actor =
|
|
|
|
user
|
|
|
|
|> actor_for_user_query()
|
|
|
|
|> Repo.one()
|
|
|
|
|
|
|
|
case actor do
|
2019-03-05 17:23:05 +01:00
|
|
|
nil ->
|
2019-09-07 19:54:11 +02:00
|
|
|
case get_actors_for_user(user) do
|
2019-09-11 03:16:37 +02:00
|
|
|
[] ->
|
|
|
|
nil
|
|
|
|
|
|
|
|
actors ->
|
|
|
|
hd(actors)
|
2019-03-05 17:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
actor ->
|
|
|
|
actor
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@doc """
|
|
|
|
Gets actors for an user.
|
|
|
|
"""
|
|
|
|
@spec get_actors_for_user(User.t()) :: [Actor.t()]
|
|
|
|
def get_actors_for_user(%User{} = user) do
|
|
|
|
user
|
|
|
|
|> actors_for_user_query()
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates user's default actor.
|
|
|
|
Raises `Ecto.NoResultsError` if the user does not exist.
|
|
|
|
"""
|
|
|
|
@spec update_user_default_actor(integer | String.t(), integer | String.t()) :: User.t()
|
|
|
|
def update_user_default_actor(user_id, actor_id) do
|
|
|
|
with _ <-
|
|
|
|
user_id
|
|
|
|
|> update_user_default_actor_query(actor_id)
|
|
|
|
|> Repo.update_all([]) do
|
|
|
|
user_id
|
|
|
|
|> get_user!()
|
|
|
|
|> Repo.preload([:default_actor])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of users.
|
|
|
|
"""
|
2020-06-11 19:13:21 +02:00
|
|
|
@spec list_users(String.t(), integer | nil, integer | nil, atom | nil, atom | nil) :: Page.t()
|
|
|
|
def list_users(email \\ "", page \\ nil, limit \\ nil, sort \\ nil, direction \\ nil)
|
|
|
|
|
|
|
|
def list_users("", page, limit, sort, direction) do
|
2019-09-07 19:54:11 +02:00
|
|
|
User
|
|
|
|
|> sort(sort, direction)
|
2020-06-11 19:13:21 +02:00
|
|
|
|> preload([u], [:actors, :feed_tokens, :settings, :default_actor])
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_users(email, page, limit, sort, direction) do
|
|
|
|
User
|
|
|
|
|> where([u], ilike(u.email, ^"%#{email}%"))
|
|
|
|
|> sort(sort, direction)
|
|
|
|
|> preload([u], [:actors, :feed_tokens, :settings, :default_actor])
|
|
|
|
|> Page.build_page(page, limit)
|
2019-09-07 19:54:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of administrators.
|
|
|
|
"""
|
|
|
|
@spec list_admins :: [User.t()]
|
|
|
|
def list_admins do
|
|
|
|
User
|
|
|
|
|> where([u], u.role == ^:administrator)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of moderators.
|
|
|
|
"""
|
|
|
|
@spec list_moderators :: [User.t()]
|
|
|
|
def list_moderators do
|
|
|
|
User
|
|
|
|
|> where([u], u.role in ^[:administrator, :moderator])
|
|
|
|
|> Repo.all()
|
2019-03-05 17:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Counts users.
|
2019-03-05 17:23:05 +01:00
|
|
|
"""
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec count_users :: integer
|
2020-10-13 14:39:05 +02:00
|
|
|
def count_users, do: Repo.one(from(u in User, select: count(u.id), where: u.disabled == false))
|
2019-09-07 19:54:11 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@doc """
|
|
|
|
Gets a settings for an user.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Setting does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_setting!(123)
|
|
|
|
%Setting{}
|
|
|
|
|
|
|
|
iex> get_setting!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def get_setting!(user_id), do: Repo.get!(Setting, user_id)
|
|
|
|
|
|
|
|
@spec get_setting(User.t()) :: Setting.t()
|
|
|
|
def get_setting(%User{id: user_id}), do: get_setting(user_id)
|
|
|
|
|
|
|
|
@spec get_setting(String.t() | integer()) :: Setting.t()
|
|
|
|
def get_setting(user_id), do: Repo.get(Setting, user_id)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a setting.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> create_setting(%{field: value})
|
|
|
|
{:ok, %Setting{}}
|
|
|
|
|
|
|
|
iex> create_setting(%{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_setting(attrs \\ %{}) do
|
|
|
|
%Setting{}
|
|
|
|
|> Setting.changeset(attrs)
|
|
|
|
|> Repo.insert(
|
|
|
|
on_conflict: {:replace_all_except, [:user_id, :inserted_at]},
|
|
|
|
conflict_target: :user_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a setting.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> update_setting(setting, %{field: new_value})
|
|
|
|
{:ok, %Setting{}}
|
|
|
|
|
|
|
|
iex> update_setting(setting, %{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_setting(%Setting{} = setting, attrs) do
|
|
|
|
setting
|
|
|
|
|> Setting.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a setting.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> delete_setting(setting)
|
|
|
|
{:ok, %Setting{}}
|
|
|
|
|
|
|
|
iex> delete_setting(setting)
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def delete_setting(%Setting{} = setting) do
|
|
|
|
Repo.delete(setting)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking setting changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> change_setting(setting)
|
|
|
|
%Ecto.Changeset{source: %Setting{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def change_setting(%Setting{} = setting) do
|
|
|
|
Setting.changeset(setting, %{})
|
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec user_by_email_query(String.t(), boolean | nil) :: Ecto.Query.t()
|
|
|
|
defp user_by_email_query(email, nil) do
|
2020-02-13 15:48:12 +01:00
|
|
|
from(u in User,
|
|
|
|
where: u.email == ^email or u.unconfirmed_email == ^email,
|
|
|
|
preload: :default_actor
|
|
|
|
)
|
2019-03-05 17:23:05 +01:00
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
defp user_by_email_query(email, true) do
|
|
|
|
from(
|
|
|
|
u in User,
|
2020-10-13 15:21:00 +02:00
|
|
|
where: u.email == ^email and not is_nil(u.confirmed_at) and not u.disabled,
|
2019-09-07 19:54:11 +02:00
|
|
|
preload: :default_actor
|
2019-03-05 17:23:05 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
defp user_by_email_query(email, false) do
|
|
|
|
from(
|
|
|
|
u in User,
|
2020-02-13 15:48:12 +01:00
|
|
|
where: (u.email == ^email or u.unconfirmed_email == ^email) and is_nil(u.confirmed_at),
|
2019-09-07 19:54:11 +02:00
|
|
|
preload: :default_actor
|
|
|
|
)
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec user_by_activation_token_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp user_by_activation_token_query(token) do
|
|
|
|
from(
|
|
|
|
u in User,
|
|
|
|
where: u.confirmation_token == ^token,
|
|
|
|
preload: [:default_actor]
|
|
|
|
)
|
|
|
|
end
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec user_by_reset_password_token_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp user_by_reset_password_token_query(token) do
|
|
|
|
from(
|
|
|
|
u in User,
|
|
|
|
where: u.reset_password_token == ^token,
|
|
|
|
preload: [:default_actor]
|
|
|
|
)
|
|
|
|
end
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec actor_for_user_query(User.t()) :: Ecto.Query.t()
|
|
|
|
defp actor_for_user_query(%User{id: user_id}) do
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: u in User,
|
|
|
|
on: u.default_actor_id == a.id,
|
|
|
|
where: u.id == ^user_id
|
|
|
|
)
|
|
|
|
end
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec actors_for_user_query(User.t()) :: Ecto.Query.t()
|
|
|
|
defp actors_for_user_query(%User{id: user_id}) do
|
|
|
|
from(a in Actor, where: a.user_id == ^user_id)
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec update_user_default_actor_query(integer | String.t(), integer | String.t()) ::
|
|
|
|
Ecto.Query.t()
|
|
|
|
defp update_user_default_actor_query(user_id, actor_id) do
|
|
|
|
from(
|
|
|
|
u in User,
|
|
|
|
where: u.id == ^user_id,
|
|
|
|
update: [set: [default_actor_id: ^actor_id]]
|
2019-03-05 17:23:05 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|