2023-11-18 16:47:12 -08:00
|
|
|
import time
|
2024-08-28 00:10:27 +02:00
|
|
|
from typing import Optional
|
2024-06-18 15:03:31 +02:00
|
|
|
|
2024-12-10 00:54:13 -08:00
|
|
|
from open_webui.internal.db import Base, JSONField, get_db
|
2025-01-20 23:20:47 -08:00
|
|
|
|
|
|
|
|
|
2025-08-17 04:06:16 +04:00
|
|
|
from open_webui.env import DATABASE_USER_ACTIVE_STATUS_UPDATE_INTERVAL
|
2024-12-10 00:54:13 -08:00
|
|
|
from open_webui.models.chats import Chats
|
2025-11-18 03:44:26 -05:00
|
|
|
from open_webui.models.groups import Groups, GroupMember
|
2025-11-30 10:33:50 -05:00
|
|
|
from open_webui.models.channels import ChannelMember
|
|
|
|
|
|
|
|
|
|
|
2025-08-17 04:06:16 +04:00
|
|
|
from open_webui.utils.misc import throttle
|
2025-01-20 23:20:47 -08:00
|
|
|
|
|
|
|
|
|
2024-08-28 00:10:27 +02:00
|
|
|
from pydantic import BaseModel, ConfigDict
|
2025-11-28 06:29:41 -05:00
|
|
|
from sqlalchemy import (
|
|
|
|
|
BigInteger,
|
|
|
|
|
JSON,
|
|
|
|
|
Column,
|
|
|
|
|
String,
|
|
|
|
|
Boolean,
|
|
|
|
|
Text,
|
|
|
|
|
Date,
|
|
|
|
|
exists,
|
|
|
|
|
select,
|
2025-12-02 05:29:34 -05:00
|
|
|
cast,
|
2025-11-28 06:29:41 -05:00
|
|
|
)
|
2025-11-25 01:37:33 -05:00
|
|
|
from sqlalchemy import or_, case
|
2025-12-02 05:29:34 -05:00
|
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
2025-04-30 16:49:41 +04:00
|
|
|
|
2025-08-21 02:39:25 +04:00
|
|
|
import datetime
|
2023-12-28 23:02:49 -08:00
|
|
|
|
2023-11-18 16:47:12 -08:00
|
|
|
####################
|
|
|
|
|
# User DB Schema
|
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
2025-11-28 06:29:41 -05:00
|
|
|
class UserSettings(BaseModel):
|
|
|
|
|
ui: Optional[dict] = {}
|
|
|
|
|
model_config = ConfigDict(extra="allow")
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2024-06-18 15:03:31 +02:00
|
|
|
class User(Base):
|
|
|
|
|
__tablename__ = "user"
|
2024-04-27 19:38:51 -04:00
|
|
|
|
2025-11-28 01:17:43 -05:00
|
|
|
id = Column(String, primary_key=True, unique=True)
|
2024-06-18 15:03:31 +02:00
|
|
|
email = Column(String)
|
2025-08-21 02:39:25 +04:00
|
|
|
username = Column(String(50), nullable=True)
|
2024-06-18 15:03:31 +02:00
|
|
|
role = Column(String)
|
2025-11-28 06:29:41 -05:00
|
|
|
|
|
|
|
|
name = Column(String)
|
|
|
|
|
|
2024-06-24 13:21:51 +02:00
|
|
|
profile_image_url = Column(Text)
|
2025-11-28 06:29:41 -05:00
|
|
|
profile_banner_image_url = Column(Text, nullable=True)
|
2024-04-27 19:38:51 -04:00
|
|
|
|
2025-08-21 02:39:25 +04:00
|
|
|
bio = Column(Text, nullable=True)
|
|
|
|
|
gender = Column(Text, nullable=True)
|
|
|
|
|
date_of_birth = Column(Date, nullable=True)
|
2025-11-28 06:29:41 -05:00
|
|
|
timezone = Column(String, nullable=True)
|
2023-12-25 21:44:28 -08:00
|
|
|
|
2025-11-28 07:27:55 -05:00
|
|
|
presence_state = Column(String, nullable=True)
|
2025-11-28 06:29:41 -05:00
|
|
|
status_emoji = Column(String, nullable=True)
|
|
|
|
|
status_message = Column(Text, nullable=True)
|
|
|
|
|
status_expires_at = Column(BigInteger, nullable=True)
|
2024-05-26 08:37:09 +01:00
|
|
|
|
2025-11-28 06:29:41 -05:00
|
|
|
info = Column(JSON, nullable=True)
|
|
|
|
|
settings = Column(JSON, nullable=True)
|
2023-12-25 21:44:28 -08:00
|
|
|
|
2025-11-28 06:29:41 -05:00
|
|
|
oauth = Column(JSON, nullable=True)
|
2025-08-21 02:39:25 +04:00
|
|
|
|
2025-11-28 06:29:41 -05:00
|
|
|
last_active_at = Column(BigInteger)
|
2025-08-21 02:39:25 +04:00
|
|
|
updated_at = Column(BigInteger)
|
|
|
|
|
created_at = Column(BigInteger)
|
|
|
|
|
|
2023-12-25 21:44:28 -08:00
|
|
|
|
2023-11-18 16:47:12 -08:00
|
|
|
class UserModel(BaseModel):
|
|
|
|
|
id: str
|
2025-08-21 02:39:25 +04:00
|
|
|
|
2023-11-18 16:47:12 -08:00
|
|
|
email: str
|
2025-08-21 02:39:25 +04:00
|
|
|
username: Optional[str] = None
|
2023-11-18 21:41:43 -08:00
|
|
|
role: str = "pending"
|
2025-11-28 06:29:41 -05:00
|
|
|
|
|
|
|
|
name: str
|
|
|
|
|
|
2024-04-03 22:36:27 -07:00
|
|
|
profile_image_url: str
|
2025-11-28 06:29:41 -05:00
|
|
|
profile_banner_image_url: Optional[str] = None
|
2024-04-27 19:38:51 -04:00
|
|
|
|
2025-08-21 02:39:25 +04:00
|
|
|
bio: Optional[str] = None
|
|
|
|
|
gender: Optional[str] = None
|
|
|
|
|
date_of_birth: Optional[datetime.date] = None
|
2025-11-28 06:29:41 -05:00
|
|
|
timezone: Optional[str] = None
|
|
|
|
|
|
2025-11-28 07:27:55 -05:00
|
|
|
presence_state: Optional[str] = None
|
2025-11-28 06:29:41 -05:00
|
|
|
status_emoji: Optional[str] = None
|
|
|
|
|
status_message: Optional[str] = None
|
|
|
|
|
status_expires_at: Optional[int] = None
|
2024-04-27 19:38:51 -04:00
|
|
|
|
2024-06-16 15:32:26 -07:00
|
|
|
info: Optional[dict] = None
|
2025-08-21 02:39:25 +04:00
|
|
|
settings: Optional[UserSettings] = None
|
2023-11-18 16:47:12 -08:00
|
|
|
|
2025-11-28 06:29:41 -05:00
|
|
|
oauth: Optional[dict] = None
|
2024-05-26 08:37:09 +01:00
|
|
|
|
2025-08-21 02:39:25 +04:00
|
|
|
last_active_at: int # timestamp in epoch
|
|
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
|
|
2024-06-21 14:58:57 +02:00
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
2023-11-18 16:47:12 -08:00
|
|
|
|
2025-11-30 10:40:24 -05:00
|
|
|
class UserStatusModel(UserModel):
|
|
|
|
|
is_active: bool = False
|
|
|
|
|
|
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
|
|
|
|
|
2025-11-28 06:29:41 -05:00
|
|
|
class ApiKey(Base):
|
|
|
|
|
__tablename__ = "api_key"
|
|
|
|
|
|
|
|
|
|
id = Column(Text, primary_key=True, unique=True)
|
|
|
|
|
user_id = Column(Text, nullable=False)
|
|
|
|
|
key = Column(Text, unique=True, nullable=False)
|
|
|
|
|
data = Column(JSON, nullable=True)
|
|
|
|
|
expires_at = Column(BigInteger, nullable=True)
|
|
|
|
|
last_used_at = Column(BigInteger, nullable=True)
|
|
|
|
|
created_at = Column(BigInteger, nullable=False)
|
|
|
|
|
updated_at = Column(BigInteger, nullable=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ApiKeyModel(BaseModel):
|
|
|
|
|
id: str
|
|
|
|
|
user_id: str
|
|
|
|
|
key: str
|
|
|
|
|
data: Optional[dict] = None
|
|
|
|
|
expires_at: Optional[int] = None
|
|
|
|
|
last_used_at: Optional[int] = None
|
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
|
|
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
|
|
|
|
|
2023-11-18 16:47:12 -08:00
|
|
|
####################
|
|
|
|
|
# Forms
|
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
2025-08-21 02:39:25 +04:00
|
|
|
class UpdateProfileForm(BaseModel):
|
|
|
|
|
profile_image_url: str
|
|
|
|
|
name: str
|
|
|
|
|
bio: Optional[str] = None
|
|
|
|
|
gender: Optional[str] = None
|
|
|
|
|
date_of_birth: Optional[datetime.date] = None
|
|
|
|
|
|
|
|
|
|
|
2025-11-18 03:44:26 -05:00
|
|
|
class UserGroupIdsModel(UserModel):
|
|
|
|
|
group_ids: list[str] = []
|
|
|
|
|
|
|
|
|
|
|
2025-11-25 04:38:07 -05:00
|
|
|
class UserModelResponse(UserModel):
|
|
|
|
|
model_config = ConfigDict(extra="allow")
|
|
|
|
|
|
|
|
|
|
|
2025-04-30 16:49:41 +04:00
|
|
|
class UserListResponse(BaseModel):
|
2025-11-25 04:38:07 -05:00
|
|
|
users: list[UserModelResponse]
|
|
|
|
|
total: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserGroupIdsListResponse(BaseModel):
|
2025-11-18 03:44:26 -05:00
|
|
|
users: list[UserGroupIdsModel]
|
2025-04-30 16:49:41 +04:00
|
|
|
total: int
|
|
|
|
|
|
|
|
|
|
|
2025-12-01 13:18:59 -05:00
|
|
|
class UserStatus(BaseModel):
|
|
|
|
|
status_emoji: Optional[str] = None
|
|
|
|
|
status_message: Optional[str] = None
|
|
|
|
|
status_expires_at: Optional[int] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserInfoResponse(UserStatus):
|
2025-07-18 11:46:14 +04:00
|
|
|
id: str
|
|
|
|
|
name: str
|
|
|
|
|
email: str
|
|
|
|
|
role: str
|
|
|
|
|
|
|
|
|
|
|
2025-09-16 21:41:47 -05:00
|
|
|
class UserIdNameResponse(BaseModel):
|
|
|
|
|
id: str
|
|
|
|
|
name: str
|
|
|
|
|
|
|
|
|
|
|
2025-12-01 13:34:57 -05:00
|
|
|
class UserIdNameStatusResponse(UserStatus):
|
2025-11-28 04:24:25 -05:00
|
|
|
id: str
|
|
|
|
|
name: str
|
2025-11-30 10:40:24 -05:00
|
|
|
is_active: Optional[bool] = None
|
2025-11-28 04:24:25 -05:00
|
|
|
|
|
|
|
|
|
2025-07-18 11:46:14 +04:00
|
|
|
class UserInfoListResponse(BaseModel):
|
|
|
|
|
users: list[UserInfoResponse]
|
|
|
|
|
total: int
|
|
|
|
|
|
|
|
|
|
|
2025-09-16 21:41:47 -05:00
|
|
|
class UserIdNameListResponse(BaseModel):
|
|
|
|
|
users: list[UserIdNameResponse]
|
|
|
|
|
total: int
|
|
|
|
|
|
|
|
|
|
|
2025-11-26 21:47:20 -05:00
|
|
|
class UserNameResponse(BaseModel):
|
2024-11-18 05:37:04 -08:00
|
|
|
id: str
|
|
|
|
|
name: str
|
|
|
|
|
role: str
|
|
|
|
|
|
|
|
|
|
|
2025-11-26 21:47:20 -05:00
|
|
|
class UserResponse(UserNameResponse):
|
|
|
|
|
email: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserProfileImageResponse(UserNameResponse):
|
|
|
|
|
email: str
|
2024-12-22 21:50:14 -07:00
|
|
|
profile_image_url: str
|
|
|
|
|
|
|
|
|
|
|
2023-11-19 00:13:59 -08:00
|
|
|
class UserRoleUpdateForm(BaseModel):
|
|
|
|
|
id: str
|
|
|
|
|
role: str
|
|
|
|
|
|
|
|
|
|
|
2024-01-05 20:59:56 -08:00
|
|
|
class UserUpdateForm(BaseModel):
|
2025-05-31 15:00:27 +04:00
|
|
|
role: str
|
2024-01-05 20:59:56 -08:00
|
|
|
name: str
|
|
|
|
|
email: str
|
|
|
|
|
profile_image_url: str
|
|
|
|
|
password: Optional[str] = None
|
|
|
|
|
|
2024-01-03 14:33:57 -08:00
|
|
|
|
2024-01-05 20:59:56 -08:00
|
|
|
class UsersTable:
|
|
|
|
|
def insert_new_user(
|
2024-04-04 01:10:51 -07:00
|
|
|
self,
|
|
|
|
|
id: str,
|
|
|
|
|
name: str,
|
|
|
|
|
email: str,
|
2024-04-06 23:16:29 -07:00
|
|
|
profile_image_url: str = "/user.png",
|
2024-04-04 01:10:51 -07:00
|
|
|
role: str = "pending",
|
2025-11-28 06:39:36 -05:00
|
|
|
oauth: Optional[dict] = None,
|
2024-01-05 20:59:56 -08:00
|
|
|
) -> Optional[UserModel]:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
user = UserModel(
|
|
|
|
|
**{
|
|
|
|
|
"id": id,
|
|
|
|
|
"email": email,
|
2025-11-28 06:41:41 -05:00
|
|
|
"name": name,
|
2024-07-03 23:32:39 -07:00
|
|
|
"role": role,
|
|
|
|
|
"profile_image_url": profile_image_url,
|
|
|
|
|
"last_active_at": int(time.time()),
|
|
|
|
|
"created_at": int(time.time()),
|
|
|
|
|
"updated_at": int(time.time()),
|
2025-11-28 06:39:36 -05:00
|
|
|
"oauth": oauth,
|
2024-07-03 23:32:39 -07:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
result = User(**user.model_dump())
|
|
|
|
|
db.add(result)
|
|
|
|
|
db.commit()
|
|
|
|
|
db.refresh(result)
|
|
|
|
|
if result:
|
|
|
|
|
return user
|
|
|
|
|
else:
|
|
|
|
|
return None
|
2024-06-21 14:58:57 +02:00
|
|
|
|
|
|
|
|
def get_user_by_id(self, id: str) -> Optional[UserModel]:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-28 00:10:27 +02:00
|
|
|
except Exception:
|
2024-06-24 13:06:15 +02:00
|
|
|
return None
|
2024-06-21 14:58:57 +02:00
|
|
|
|
|
|
|
|
def get_user_by_api_key(self, api_key: str) -> Optional[UserModel]:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
2025-11-28 06:49:10 -05:00
|
|
|
user = (
|
|
|
|
|
db.query(User)
|
|
|
|
|
.join(ApiKey, User.id == ApiKey.user_id)
|
|
|
|
|
.filter(ApiKey.key == api_key)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
|
|
|
|
return UserModel.model_validate(user) if user else None
|
2024-08-14 13:38:19 +01:00
|
|
|
except Exception:
|
2024-06-24 13:06:15 +02:00
|
|
|
return None
|
2024-06-21 14:58:57 +02:00
|
|
|
|
|
|
|
|
def get_user_by_email(self, email: str) -> Optional[UserModel]:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
user = db.query(User).filter_by(email=email).first()
|
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 13:38:19 +01:00
|
|
|
except Exception:
|
2024-06-24 13:06:15 +02:00
|
|
|
return None
|
2023-11-18 16:47:12 -08:00
|
|
|
|
2025-11-28 06:39:36 -05:00
|
|
|
def get_user_by_oauth_sub(self, provider: str, sub: str) -> Optional[UserModel]:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2025-12-02 05:29:34 -05:00
|
|
|
with get_db() as db: # type: Session
|
|
|
|
|
dialect_name = db.bind.dialect.name
|
|
|
|
|
|
|
|
|
|
query = db.query(User)
|
|
|
|
|
if dialect_name == "sqlite":
|
|
|
|
|
query = query.filter(User.oauth.contains({provider: {"sub": sub}}))
|
|
|
|
|
elif dialect_name == "postgresql":
|
|
|
|
|
query = query.filter(
|
|
|
|
|
User.oauth[provider].cast(JSONB)["sub"].astext == sub
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
user = query.first()
|
2025-11-28 06:39:36 -05:00
|
|
|
return UserModel.model_validate(user) if user else None
|
2025-12-02 05:29:34 -05:00
|
|
|
except Exception as e:
|
|
|
|
|
# You may want to log the exception here
|
2024-06-24 13:06:15 +02:00
|
|
|
return None
|
2024-06-21 14:58:57 +02:00
|
|
|
|
2024-12-25 00:53:25 -07:00
|
|
|
def get_users(
|
2025-04-28 11:11:41 +07:00
|
|
|
self,
|
2025-04-30 16:49:41 +04:00
|
|
|
filter: Optional[dict] = None,
|
2025-04-28 11:11:41 +07:00
|
|
|
skip: Optional[int] = None,
|
|
|
|
|
limit: Optional[int] = None,
|
2025-09-16 22:53:54 -05:00
|
|
|
) -> dict:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
2025-11-18 03:44:26 -05:00
|
|
|
# Join GroupMember so we can order by group_id when requested
|
2025-11-24 15:39:13 -05:00
|
|
|
query = db.query(User)
|
2024-12-25 00:53:25 -07:00
|
|
|
|
2025-04-30 16:49:41 +04:00
|
|
|
if filter:
|
|
|
|
|
query_key = filter.get("query")
|
|
|
|
|
if query_key:
|
|
|
|
|
query = query.filter(
|
2025-04-28 11:11:41 +07:00
|
|
|
or_(
|
2025-04-30 16:49:41 +04:00
|
|
|
User.name.ilike(f"%{query_key}%"),
|
|
|
|
|
User.email.ilike(f"%{query_key}%"),
|
2025-04-28 11:11:41 +07:00
|
|
|
)
|
|
|
|
|
)
|
2025-04-30 16:49:41 +04:00
|
|
|
|
2025-11-30 10:33:50 -05:00
|
|
|
channel_id = filter.get("channel_id")
|
|
|
|
|
if channel_id:
|
|
|
|
|
query = query.filter(
|
|
|
|
|
exists(
|
|
|
|
|
select(ChannelMember.id).where(
|
|
|
|
|
ChannelMember.user_id == User.id,
|
|
|
|
|
ChannelMember.channel_id == channel_id,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-25 04:38:07 -05:00
|
|
|
user_ids = filter.get("user_ids")
|
2025-11-25 04:45:52 -05:00
|
|
|
group_ids = filter.get("group_ids")
|
|
|
|
|
|
|
|
|
|
if isinstance(user_ids, list) and isinstance(group_ids, list):
|
|
|
|
|
# If both are empty lists, return no users
|
|
|
|
|
if not user_ids and not group_ids:
|
|
|
|
|
return {"users": [], "total": 0}
|
|
|
|
|
|
2025-11-25 04:38:07 -05:00
|
|
|
if user_ids:
|
|
|
|
|
query = query.filter(User.id.in_(user_ids))
|
|
|
|
|
|
|
|
|
|
if group_ids:
|
|
|
|
|
query = query.filter(
|
|
|
|
|
exists(
|
|
|
|
|
select(GroupMember.id).where(
|
|
|
|
|
GroupMember.user_id == User.id,
|
|
|
|
|
GroupMember.group_id.in_(group_ids),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
roles = filter.get("roles")
|
|
|
|
|
if roles:
|
|
|
|
|
include_roles = [role for role in roles if not role.startswith("!")]
|
|
|
|
|
exclude_roles = [role[1:] for role in roles if role.startswith("!")]
|
|
|
|
|
|
|
|
|
|
if include_roles:
|
|
|
|
|
query = query.filter(User.role.in_(include_roles))
|
|
|
|
|
if exclude_roles:
|
|
|
|
|
query = query.filter(~User.role.in_(exclude_roles))
|
|
|
|
|
|
2025-04-30 16:49:41 +04:00
|
|
|
order_by = filter.get("order_by")
|
|
|
|
|
direction = filter.get("direction")
|
|
|
|
|
|
2025-11-18 03:44:26 -05:00
|
|
|
if order_by and order_by.startswith("group_id:"):
|
|
|
|
|
group_id = order_by.split(":", 1)[1]
|
|
|
|
|
|
2025-11-25 01:37:33 -05:00
|
|
|
# Subquery that checks if the user belongs to the group
|
|
|
|
|
membership_exists = exists(
|
|
|
|
|
select(GroupMember.id).where(
|
|
|
|
|
GroupMember.user_id == User.id,
|
|
|
|
|
GroupMember.group_id == group_id,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# CASE: user in group → 1, user not in group → 0
|
|
|
|
|
group_sort = case((membership_exists, 1), else_=0)
|
|
|
|
|
|
2025-11-18 03:44:26 -05:00
|
|
|
if direction == "asc":
|
2025-11-25 01:37:33 -05:00
|
|
|
query = query.order_by(group_sort.asc(), User.name.asc())
|
2025-11-18 03:44:26 -05:00
|
|
|
else:
|
2025-11-25 01:37:33 -05:00
|
|
|
query = query.order_by(group_sort.desc(), User.name.asc())
|
|
|
|
|
|
2025-11-18 03:44:26 -05:00
|
|
|
elif order_by == "name":
|
2025-04-30 16:49:41 +04:00
|
|
|
if direction == "asc":
|
|
|
|
|
query = query.order_by(User.name.asc())
|
|
|
|
|
else:
|
|
|
|
|
query = query.order_by(User.name.desc())
|
2025-11-25 01:37:33 -05:00
|
|
|
|
2025-04-30 16:49:41 +04:00
|
|
|
elif order_by == "email":
|
|
|
|
|
if direction == "asc":
|
|
|
|
|
query = query.order_by(User.email.asc())
|
|
|
|
|
else:
|
|
|
|
|
query = query.order_by(User.email.desc())
|
|
|
|
|
|
|
|
|
|
elif order_by == "created_at":
|
|
|
|
|
if direction == "asc":
|
|
|
|
|
query = query.order_by(User.created_at.asc())
|
|
|
|
|
else:
|
|
|
|
|
query = query.order_by(User.created_at.desc())
|
|
|
|
|
|
|
|
|
|
elif order_by == "last_active_at":
|
|
|
|
|
if direction == "asc":
|
|
|
|
|
query = query.order_by(User.last_active_at.asc())
|
|
|
|
|
else:
|
|
|
|
|
query = query.order_by(User.last_active_at.desc())
|
|
|
|
|
|
|
|
|
|
elif order_by == "updated_at":
|
|
|
|
|
if direction == "asc":
|
|
|
|
|
query = query.order_by(User.updated_at.asc())
|
|
|
|
|
else:
|
|
|
|
|
query = query.order_by(User.updated_at.desc())
|
|
|
|
|
elif order_by == "role":
|
|
|
|
|
if direction == "asc":
|
|
|
|
|
query = query.order_by(User.role.asc())
|
|
|
|
|
else:
|
|
|
|
|
query = query.order_by(User.role.desc())
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
query = query.order_by(User.created_at.desc())
|
2024-12-25 00:53:25 -07:00
|
|
|
|
2025-11-17 05:07:59 -05:00
|
|
|
# Count BEFORE pagination
|
|
|
|
|
total = query.count()
|
|
|
|
|
|
2025-11-24 15:39:13 -05:00
|
|
|
# correct pagination logic
|
|
|
|
|
if skip is not None:
|
2024-12-25 00:53:25 -07:00
|
|
|
query = query.offset(skip)
|
2025-11-24 15:39:13 -05:00
|
|
|
if limit is not None:
|
2024-12-25 00:53:25 -07:00
|
|
|
query = query.limit(limit)
|
|
|
|
|
|
|
|
|
|
users = query.all()
|
2025-04-30 16:49:41 +04:00
|
|
|
return {
|
|
|
|
|
"users": [UserModel.model_validate(user) for user in users],
|
2025-11-17 05:07:59 -05:00
|
|
|
"total": total,
|
2025-04-30 16:49:41 +04:00
|
|
|
}
|
2024-12-25 00:53:25 -07:00
|
|
|
|
2025-11-30 10:40:24 -05:00
|
|
|
def get_users_by_user_ids(self, user_ids: list[str]) -> list[UserStatusModel]:
|
2024-12-25 00:53:25 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
users = db.query(User).filter(User.id.in_(user_ids)).all()
|
2024-07-03 23:32:39 -07:00
|
|
|
return [UserModel.model_validate(user) for user in users]
|
2024-06-21 14:58:57 +02:00
|
|
|
|
|
|
|
|
def get_num_users(self) -> Optional[int]:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
return db.query(User).count()
|
2024-06-21 14:58:57 +02:00
|
|
|
|
2025-08-05 22:15:22 +04:00
|
|
|
def has_users(self) -> bool:
|
|
|
|
|
with get_db() as db:
|
|
|
|
|
return db.query(db.query(User).exists()).scalar()
|
|
|
|
|
|
2024-06-21 14:58:57 +02:00
|
|
|
def get_first_user(self) -> UserModel:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
user = db.query(User).order_by(User.created_at).first()
|
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 13:38:19 +01:00
|
|
|
except Exception:
|
2024-06-24 13:06:15 +02:00
|
|
|
return None
|
2024-05-01 19:59:05 -07:00
|
|
|
|
2024-12-20 22:54:43 -08:00
|
|
|
def get_user_webhook_url_by_id(self, id: str) -> Optional[str]:
|
|
|
|
|
try:
|
|
|
|
|
with get_db() as db:
|
|
|
|
|
user = db.query(User).filter_by(id=id).first()
|
2024-12-25 00:53:25 -07:00
|
|
|
|
|
|
|
|
if user.settings is None:
|
|
|
|
|
return None
|
|
|
|
|
else:
|
|
|
|
|
return (
|
|
|
|
|
user.settings.get("ui", {})
|
|
|
|
|
.get("notifications", {})
|
|
|
|
|
.get("webhook_url", None)
|
|
|
|
|
)
|
2024-12-20 22:54:43 -08:00
|
|
|
except Exception:
|
|
|
|
|
return None
|
|
|
|
|
|
2025-11-17 21:31:24 +01:00
|
|
|
def get_num_users_active_today(self) -> Optional[int]:
|
|
|
|
|
with get_db() as db:
|
|
|
|
|
current_timestamp = int(datetime.datetime.now().timestamp())
|
|
|
|
|
today_midnight_timestamp = current_timestamp - (current_timestamp % 86400)
|
2025-11-19 03:23:33 -05:00
|
|
|
query = db.query(User).filter(
|
|
|
|
|
User.last_active_at > today_midnight_timestamp
|
|
|
|
|
)
|
2025-11-17 21:31:24 +01:00
|
|
|
return query.count()
|
|
|
|
|
|
2024-06-24 09:57:08 +02:00
|
|
|
def update_user_role_by_id(self, id: str, role: str) -> Optional[UserModel]:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
db.query(User).filter_by(id=id).update({"role": role})
|
|
|
|
|
db.commit()
|
|
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 13:38:19 +01:00
|
|
|
except Exception:
|
2024-06-24 13:06:15 +02:00
|
|
|
return None
|
2025-12-01 13:18:59 -05:00
|
|
|
|
|
|
|
|
def update_user_status_by_id(
|
|
|
|
|
self, id: str, form_data: UserStatus
|
|
|
|
|
) -> Optional[UserModel]:
|
|
|
|
|
try:
|
|
|
|
|
with get_db() as db:
|
|
|
|
|
db.query(User).filter_by(id=id).update(
|
|
|
|
|
{**form_data.model_dump(exclude_none=True)}
|
|
|
|
|
)
|
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
|
return UserModel.model_validate(user)
|
|
|
|
|
except Exception:
|
|
|
|
|
return None
|
2023-11-19 00:13:59 -08:00
|
|
|
|
2024-01-26 20:27:45 -08:00
|
|
|
def update_user_profile_image_url_by_id(
|
2024-06-21 14:58:57 +02:00
|
|
|
self, id: str, profile_image_url: str
|
2024-01-26 20:27:45 -08:00
|
|
|
) -> Optional[UserModel]:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
db.query(User).filter_by(id=id).update(
|
|
|
|
|
{"profile_image_url": profile_image_url}
|
|
|
|
|
)
|
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 13:38:19 +01:00
|
|
|
except Exception:
|
2024-06-24 13:06:15 +02:00
|
|
|
return None
|
2024-01-26 20:27:45 -08:00
|
|
|
|
2025-08-17 04:06:16 +04:00
|
|
|
@throttle(DATABASE_USER_ACTIVE_STATUS_UPDATE_INTERVAL)
|
2025-11-28 07:39:02 -05:00
|
|
|
def update_last_active_by_id(self, id: str) -> Optional[UserModel]:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
db.query(User).filter_by(id=id).update(
|
|
|
|
|
{"last_active_at": int(time.time())}
|
|
|
|
|
)
|
|
|
|
|
db.commit()
|
2024-04-27 19:38:51 -04:00
|
|
|
|
2024-07-03 23:32:39 -07:00
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 13:38:19 +01:00
|
|
|
except Exception:
|
2024-06-24 13:06:15 +02:00
|
|
|
return None
|
2024-04-27 19:38:51 -04:00
|
|
|
|
2025-11-28 06:39:36 -05:00
|
|
|
def update_user_oauth_by_id(
|
|
|
|
|
self, id: str, provider: str, sub: str
|
2024-05-27 18:07:38 +01:00
|
|
|
) -> Optional[UserModel]:
|
2025-11-28 06:39:36 -05:00
|
|
|
"""
|
|
|
|
|
Update or insert an OAuth provider/sub pair into the user's oauth JSON field.
|
|
|
|
|
Example resulting structure:
|
|
|
|
|
{
|
|
|
|
|
"google": { "sub": "123" },
|
|
|
|
|
"github": { "sub": "abc" }
|
|
|
|
|
}
|
|
|
|
|
"""
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
2025-11-28 06:39:36 -05:00
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
|
if not user:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
# Load existing oauth JSON or create empty
|
|
|
|
|
oauth = user.oauth or {}
|
|
|
|
|
|
|
|
|
|
# Update or insert provider entry
|
|
|
|
|
oauth[provider] = {"sub": sub}
|
|
|
|
|
|
|
|
|
|
# Persist updated JSON
|
|
|
|
|
db.query(User).filter_by(id=id).update({"oauth": oauth})
|
2024-07-07 23:01:15 -07:00
|
|
|
db.commit()
|
2024-05-27 18:07:38 +01:00
|
|
|
|
2024-07-03 23:32:39 -07:00
|
|
|
return UserModel.model_validate(user)
|
2025-11-28 06:39:36 -05:00
|
|
|
|
2024-08-14 13:38:19 +01:00
|
|
|
except Exception:
|
2024-06-24 13:06:15 +02:00
|
|
|
return None
|
2024-05-27 18:07:38 +01:00
|
|
|
|
2024-06-24 09:57:08 +02:00
|
|
|
def update_user_by_id(self, id: str, updated: dict) -> Optional[UserModel]:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
db.query(User).filter_by(id=id).update(updated)
|
|
|
|
|
db.commit()
|
2024-01-05 20:59:56 -08:00
|
|
|
|
2024-07-03 23:32:39 -07:00
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
|
return UserModel.model_validate(user)
|
|
|
|
|
# return UserModel(**user.dict())
|
2025-08-21 02:39:25 +04:00
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
2024-06-24 13:06:15 +02:00
|
|
|
return None
|
2024-06-21 14:58:57 +02:00
|
|
|
|
2025-02-11 23:42:31 -08:00
|
|
|
def update_user_settings_by_id(self, id: str, updated: dict) -> Optional[UserModel]:
|
|
|
|
|
try:
|
|
|
|
|
with get_db() as db:
|
|
|
|
|
user_settings = db.query(User).filter_by(id=id).first().settings
|
|
|
|
|
|
|
|
|
|
if user_settings is None:
|
|
|
|
|
user_settings = {}
|
|
|
|
|
|
|
|
|
|
user_settings.update(updated)
|
|
|
|
|
|
|
|
|
|
db.query(User).filter_by(id=id).update({"settings": user_settings})
|
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
|
return UserModel.model_validate(user)
|
|
|
|
|
except Exception:
|
|
|
|
|
return None
|
|
|
|
|
|
2024-06-21 14:58:57 +02:00
|
|
|
def delete_user_by_id(self, id: str) -> bool:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2025-01-20 23:20:47 -08:00
|
|
|
# Remove User from Groups
|
|
|
|
|
Groups.remove_user_from_all_groups(id)
|
|
|
|
|
|
2024-06-24 13:06:15 +02:00
|
|
|
# Delete User Chats
|
|
|
|
|
result = Chats.delete_chats_by_user_id(id)
|
|
|
|
|
if result:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
|
|
|
|
# Delete User
|
|
|
|
|
db.query(User).filter_by(id=id).delete()
|
|
|
|
|
db.commit()
|
2024-06-24 13:06:15 +02:00
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
else:
|
2024-06-21 14:58:57 +02:00
|
|
|
return False
|
2024-08-14 13:38:19 +01:00
|
|
|
except Exception:
|
2024-06-24 13:06:15 +02:00
|
|
|
return False
|
2023-12-28 23:02:49 -08:00
|
|
|
|
2025-11-28 06:49:10 -05:00
|
|
|
def get_user_api_key_by_id(self, id: str) -> Optional[str]:
|
|
|
|
|
try:
|
|
|
|
|
with get_db() as db:
|
|
|
|
|
api_key = db.query(ApiKey).filter_by(user_id=id).first()
|
|
|
|
|
return api_key.key if api_key else None
|
|
|
|
|
except Exception:
|
|
|
|
|
return None
|
|
|
|
|
|
2025-06-10 16:20:57 +04:00
|
|
|
def update_user_api_key_by_id(self, id: str, api_key: str) -> bool:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
2025-11-28 06:49:10 -05:00
|
|
|
db.query(ApiKey).filter_by(user_id=id).delete()
|
2024-07-03 23:32:39 -07:00
|
|
|
db.commit()
|
2025-11-28 06:49:10 -05:00
|
|
|
|
|
|
|
|
now = int(time.time())
|
|
|
|
|
new_api_key = ApiKey(
|
|
|
|
|
id=f"key_{id}",
|
|
|
|
|
user_id=id,
|
|
|
|
|
key=api_key,
|
|
|
|
|
created_at=now,
|
|
|
|
|
updated_at=now,
|
|
|
|
|
)
|
|
|
|
|
db.add(new_api_key)
|
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
2024-08-14 13:38:19 +01:00
|
|
|
except Exception:
|
2024-06-24 13:06:15 +02:00
|
|
|
return False
|
2023-12-28 23:02:49 -08:00
|
|
|
|
2025-11-28 06:49:10 -05:00
|
|
|
def delete_user_api_key_by_id(self, id: str) -> bool:
|
2024-06-24 13:06:15 +02:00
|
|
|
try:
|
2024-07-03 23:32:39 -07:00
|
|
|
with get_db() as db:
|
2025-11-28 06:49:10 -05:00
|
|
|
db.query(ApiKey).filter_by(user_id=id).delete()
|
|
|
|
|
db.commit()
|
|
|
|
|
return True
|
2024-08-28 00:10:27 +02:00
|
|
|
except Exception:
|
2025-11-28 06:49:10 -05:00
|
|
|
return False
|
2024-04-02 09:27:35 -07:00
|
|
|
|
2025-01-20 23:09:55 -08:00
|
|
|
def get_valid_user_ids(self, user_ids: list[str]) -> list[str]:
|
|
|
|
|
with get_db() as db:
|
|
|
|
|
users = db.query(User).filter(User.id.in_(user_ids)).all()
|
|
|
|
|
return [user.id for user in users]
|
|
|
|
|
|
2025-05-05 19:38:36 +04:00
|
|
|
def get_super_admin_user(self) -> Optional[UserModel]:
|
|
|
|
|
with get_db() as db:
|
|
|
|
|
user = db.query(User).filter_by(role="admin").first()
|
|
|
|
|
if user:
|
|
|
|
|
return UserModel.model_validate(user)
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
2025-11-28 07:27:55 -05:00
|
|
|
def get_active_user_count(self) -> int:
|
|
|
|
|
with get_db() as db:
|
|
|
|
|
# Consider user active if last_active_at within the last 3 minutes
|
|
|
|
|
three_minutes_ago = int(time.time()) - 180
|
|
|
|
|
count = (
|
|
|
|
|
db.query(User).filter(User.last_active_at >= three_minutes_ago).count()
|
|
|
|
|
)
|
|
|
|
|
return count
|
|
|
|
|
|
|
|
|
|
def is_user_active(self, user_id: str) -> bool:
|
|
|
|
|
with get_db() as db:
|
|
|
|
|
user = db.query(User).filter_by(id=user_id).first()
|
|
|
|
|
if user and user.last_active_at:
|
|
|
|
|
# Consider user active if last_active_at within the last 3 minutes
|
|
|
|
|
three_minutes_ago = int(time.time()) - 180
|
|
|
|
|
return user.last_active_at >= three_minutes_ago
|
|
|
|
|
return False
|
|
|
|
|
|
2023-11-18 16:47:12 -08:00
|
|
|
|
2024-06-18 15:03:31 +02:00
|
|
|
Users = UsersTable()
|