# retoor <retoor@molodetz.nl>
from datetime import datetime, timezone
from devplacepy.database import get_table, invalidate_admins_cache
from devplacepy.config import DEFAULT_CORRECTION_PROMPT, DEFAULT_MODIFIER_PROMPT
from devplacepy.utils.text import generate_uid
from devplacepy.utils.badges import award_badge
from devplacepy.utils.passwords import hash_password, hash_password_async
def _create_account(username: str, email: str, password_hash: str) -> tuple[str, str, bool]:
users = get_table("users")
uid = generate_uid()
is_first = users.count() == 0
role = "Admin" if is_first else "Member"
users.insert(
{
"uid": uid,
"username": username,
"email": email.strip().lower(),
"api_key": generate_uid(),
"password_hash": password_hash,
"bio": "",
"location": "",
"git_link": "",
"website": "",
"role": role,
"is_active": True,
"level": 1,
"xp": 0,
"stars": 0,
"ai_correction_enabled": 0,
"ai_correction_prompt": DEFAULT_CORRECTION_PROMPT,
"ai_correction_sync": 0,
"ai_modifier_enabled": 1,
"ai_modifier_prompt": DEFAULT_MODIFIER_PROMPT,
"ai_modifier_sync": 1,
"avatar_seed": None,
"created_at": datetime.now(timezone.utc).isoformat(),
}
)
award_badge(uid, "Member")
if is_first:
invalidate_admins_cache()
return uid, role, is_first
def register_account(username: str, email: str, password: str) -> tuple[str, str, bool]:
return _create_account(username, email, hash_password(password))
async def register_account_async(
username: str, email: str, password: str
) -> tuple[str, str, bool]:
return _create_account(username, email, await hash_password_async(password))