Commit b90581df authored by James Laver's avatar James Laver

Add Accounts.is_username_available?

parent 6b38f657
......@@ -8,9 +8,10 @@ defmodule MoodleNet.Accounts do
User Accounts context
"""
require Ecto.Query
alias MoodleNet.Repo
alias Ecto.Multi
alias Ecto.Query, as: EQuery
alias MoodleNet.Accounts.{
User,
PasswordAuth,
......@@ -19,7 +20,7 @@ defmodule MoodleNet.Accounts do
WhitelistEmail
}
alias MoodleNet.{Mailer, Email}
alias MoodleNet.{Mailer, Email, Token, Gravatar}
alias ActivityPub.SQL.{Alter, Query}
......@@ -187,7 +188,7 @@ defmodule MoodleNet.Accounts do
end
defp renew_reset_password_token(user) do
changeset = MoodleNet.Accounts.ResetPasswordToken.build_changeset(user)
changeset = ResetPasswordToken.build_changeset(user)
opts = [returning: true, on_conflict: :replace_all, conflict_target: :user_id]
Repo.insert(changeset, opts)
end
......@@ -218,7 +219,7 @@ defmodule MoodleNet.Accounts do
end
defp get_reset_password_token(full_token) do
with {:ok, {user_id, _}} <- MoodleNet.Token.split_id_and_token(full_token),
with {:ok, {user_id, _}} <- Token.split_id_and_token(full_token),
ret = %{token: rp_token} <- Repo.get_by(ResetPasswordToken, user_id: user_id),
false <- expired_token?(ret),
^full_token <- rp_token do
......@@ -228,6 +229,15 @@ defmodule MoodleNet.Accounts do
end
end
def is_username_available?(username) do
ret =
"activity_pub_actor_aspects"
|> EQuery.where([a], a.preferred_username == ^username)
|> Repo.aggregate(:count, :local_id)
ret == 0
end
@two_days 60 * 60 * 24 * 2
defp expired_token?(%{inserted_at: date}) do
NaiveDateTime.diff(NaiveDateTime.utc_now(), date) > @two_days
......@@ -246,7 +256,7 @@ defmodule MoodleNet.Accounts do
end
defp get_email_confirmation_token(full_token) do
with {:ok, {user_id, _}} <- MoodleNet.Token.split_id_and_token(full_token),
with {:ok, {user_id, _}} <- Token.split_id_and_token(full_token),
ret = %{token: ec_token} <- Repo.get_by(EmailConfirmationToken, user_id: user_id),
^full_token <- ec_token do
{:ok, ret}
......@@ -273,7 +283,7 @@ defmodule MoodleNet.Accounts do
defp set_default_icon(attrs) do
if email = attrs["email"] || attrs[:email] do
Map.put(attrs, :icon, %{type: "Image", url: MoodleNet.Gravatar.url(email)})
Map.put(attrs, :icon, %{type: "Image", url: Gravatar.url(email)})
else
attrs
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment