瀏覽代碼

Merge pull request #10809 from TobiasGoerke/feat/update_timestamp_asynchronously

feat: update get_current_user to refresh last active timestamp asynchronously
Timothy Jaeryang Baek 2 月之前
父節點
當前提交
674d6e08fc
共有 1 個文件被更改,包括 5 次插入2 次删除
  1. 5 2
      backend/open_webui/utils/auth.py

+ 5 - 2
backend/open_webui/utils/auth.py

@@ -16,7 +16,7 @@ from open_webui.models.users import Users
 from open_webui.constants import ERROR_MESSAGES
 from open_webui.constants import ERROR_MESSAGES
 from open_webui.env import WEBUI_SECRET_KEY, TRUSTED_SIGNATURE_KEY, STATIC_DIR, SRC_LOG_LEVELS
 from open_webui.env import WEBUI_SECRET_KEY, TRUSTED_SIGNATURE_KEY, STATIC_DIR, SRC_LOG_LEVELS
 
 
-from fastapi import Depends, HTTPException, Request, Response, status
+from fastapi import BackgroundTasks, Depends, HTTPException, Request, Response, status
 from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
 from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
 from passlib.context import CryptContext
 from passlib.context import CryptContext
 
 
@@ -145,6 +145,7 @@ def get_http_authorization_cred(auth_header: str):
 
 
 def get_current_user(
 def get_current_user(
     request: Request,
     request: Request,
+    background_tasks: BackgroundTasks,
     auth_token: HTTPAuthorizationCredentials = Depends(bearer_security),
     auth_token: HTTPAuthorizationCredentials = Depends(bearer_security),
 ):
 ):
     token = None
     token = None
@@ -197,7 +198,9 @@ def get_current_user(
                 detail=ERROR_MESSAGES.INVALID_TOKEN,
                 detail=ERROR_MESSAGES.INVALID_TOKEN,
             )
             )
         else:
         else:
-            Users.update_user_last_active_by_id(user.id)
+            # Refresh the user's last active timestamp asynchronously
+            # to prevent blocking the request
+            background_tasks.add_task(Users.update_user_last_active_by_id, user.id)
         return user
         return user
     else:
     else:
         raise HTTPException(
         raise HTTPException(