Spaces:
Runtime error

vibra / src /models /users.py
valent-p's picture
chore: update node_modules dependencies and project configuration files
330c3b5
Raw History Blame Contribute Delete
1.72 kB
# src/models/users.py
import secrets
from datetime import datetime
from sqlalchemy import DateTime, String, Text, func, Boolean, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from .base import Base
def generate_public_id() -> str:
# 8 bytes translates into a 16-character cryptographically secure hex string
return secrets.token_hex(8)
class User(Base):
__tablename__ = "users"
# Internal Primary Key (Int) used for fast database joins/indexing
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
# Public-facing ID (Hex String) to expose to clients
public_id: Mapped[str] = mapped_column(
String(16), unique=True, index=True, default=generate_public_id
)
username: Mapped[str] = mapped_column(String(50), unique=True, index=True)
hashed_password: Mapped[str] = mapped_column(String(255))
# Stores the browser's generated asymmetric (E2EE) Public Key
public_key: Mapped[str] = mapped_column(Text)
# Privacy Settings
share_online_status: Mapped[bool] = mapped_column(Boolean, server_default="1")
share_typing_status: Mapped[bool] = mapped_column(Boolean, server_default="1")
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
class Friendship(Base):
__tablename__ = "friendships"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
user_a_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
user_b_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())