1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00

Создание чата2

This commit is contained in:
MoonTestUse1
2025-01-05 05:50:09 +06:00
parent ba0b0ec72b
commit 043871d7c6
11 changed files with 107 additions and 39 deletions

11
backend/app/api/api.py Normal file
View File

@@ -0,0 +1,11 @@
from fastapi import APIRouter
from app.api.endpoints import auth, admin, employees, requests, statistics, chat
api_router = APIRouter()
api_router.include_router(auth.router, prefix="/auth", tags=["auth"])
api_router.include_router(employees.router, prefix="/employees", tags=["employees"])
api_router.include_router(requests.router, prefix="/requests", tags=["requests"])
api_router.include_router(admin.router, prefix="/admin", tags=["admin"])
api_router.include_router(statistics.router, prefix="/statistics", tags=["statistics"])
api_router.include_router(chat.router, prefix="/chat", tags=["chat"])

View File

@@ -0,0 +1 @@
# Пустой файл для обозначения пакета

View File

@@ -17,7 +17,7 @@ router = APIRouter()
UPLOAD_DIR = "uploads/chat_files"
os.makedirs(UPLOAD_DIR, exist_ok=True)
@router.websocket("/ws/chat")
@router.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket, db: Session = Depends(get_db)):
await handle_chat_connection(websocket, db)
@@ -64,6 +64,24 @@ def get_messages(
messages = db.query(Message).filter(Message.chat_id == chat.id).all()
return messages
@router.get("/messages/{chat_id}/", response_model=List[Message])
def get_chat_messages(
chat_id: int,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
# Проверяем доступ к чату
chat = db.query(Chat).filter(Chat.id == chat_id).first()
if not chat:
raise HTTPException(status_code=404, detail="Chat not found")
if not current_user.is_admin and chat.employee_id != current_user.id:
raise HTTPException(status_code=403, detail="Not authorized")
# Получаем сообщения
messages = db.query(Message).filter(Message.chat_id == chat_id).all()
return messages
@router.get("/unread-count/")
def get_unread_count(
current_user: User = Depends(get_current_user),
@@ -116,26 +134,4 @@ def get_admin_chats(
).count()
chat.unread_count = unread_count
return chats
@router.get("/messages/{chat_id}/", response_model=List[Message])
def get_chat_messages(
chat_id: int,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
# Проверяем доступ к чату
chat = db.query(Chat).filter(Chat.id == chat_id).first()
if not chat:
raise HTTPException(status_code=404, detail="Chat not found")
if not current_user.is_admin and chat.employee_id != current_user.id:
raise HTTPException(status_code=403, detail="Not authorized")
# Получаем сообщения чата
messages = db.query(Message)\
.filter(Message.chat_id == chat_id)\
.order_by(Message.created_at.asc())\
.all()
return messages
return chats

View File

@@ -1,15 +1,38 @@
from datetime import datetime, timedelta
from typing import Optional
from fastapi import Depends, HTTPException, status, WebSocket
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from passlib.context import CryptContext
from sqlalchemy.orm import Session
from typing import Optional
from app.core.config import settings
from app.database import get_db
from app.models.user import User
# Настройки JWT
SECRET_KEY = "your-secret-key" # В продакшене использовать безопасный ключ
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
def verify_password(plain_password: str, hashed_password: str) -> bool:
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password: str) -> str:
return pwd_context.hash(password)
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)) -> User:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
@@ -17,7 +40,7 @@ def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, settings.secret_key, algorithms=[settings.algorithm])
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
email: str = payload.get("sub")
if email is None:
raise credentials_exception
@@ -38,7 +61,7 @@ async def get_current_user_ws(websocket: WebSocket, db: Session = Depends(get_db
return None
# Проверяем токен
payload = jwt.decode(token, settings.secret_key, algorithms=[settings.algorithm])
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
email: str = payload.get("sub")
if email is None:
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)

View File

@@ -1,30 +1,48 @@
"""Main application module"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.api import api_router
from app.core.config import settings
from app.core.scheduler import setup_scheduler
from app.api.endpoints import admin, employees, requests, auth, statistics, chat
from app.websockets.chat import handle_chat_connection
from app.core.scheduler import setup_scheduler
app = FastAPI(
title=settings.project_name,
openapi_url=f"{settings.api_v1_str}/openapi.json"
# Включаем автоматическое перенаправление со слэшем
redirect_slashes=True,
# Добавляем описание API
title="Support System API",
description="API для системы поддержки",
version="1.0.0"
)
# Настройка CORS
# CORS configuration
origins = [
"http://localhost",
"http://localhost:8080",
"http://localhost:5173",
"http://127.0.0.1:5173",
"http://127.0.0.1:8080",
"http://185.139.70.62", # Добавляем ваш production домен
]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"]
)
# Подключаем роутеры
app.include_router(api_router, prefix=settings.api_v1_str)
# Include routers
app.include_router(auth.router, prefix="/api/auth", tags=["auth"])
app.include_router(employees.router, prefix="/api/employees", tags=["employees"])
app.include_router(requests.router, prefix="/api/requests", tags=["requests"])
app.include_router(admin.router, prefix="/api/admin", tags=["admin"])
app.include_router(statistics.router, prefix="/api/statistics", tags=["statistics"])
app.include_router(chat.router, prefix="/api/chat", tags=["chat"])
# WebSocket для чата
app.add_api_websocket_route("/ws/chat", handle_chat_connection)
app.add_api_websocket_route("/api/ws/chat", handle_chat_connection)
@app.on_event("startup")
async def startup_event():

View File

@@ -66,6 +66,18 @@ async def handle_chat_connection(
db.add(message)
db.commit()
# Если есть файлы, создаем записи для них
if 'files' in data and data['files']:
for file_data in data['files']:
chat_file = ChatFile(
message_id=message.id,
file_name=file_data['filename'],
file_path=file_data['saved_path'],
file_size=file_data['size']
)
db.add(chat_file)
db.commit()
# Определяем получателя
recipient_id = chat.admin_id if current_user.id == chat.employee_id else chat.employee_id
@@ -76,7 +88,14 @@ async def handle_chat_connection(
'sender_id': current_user.id,
'content': message.content,
'created_at': message.created_at.isoformat(),
'is_read': False
'is_read': False,
'files': [
{
'id': f.id,
'file_name': f.file_name,
'file_path': f.file_path
} for f in message.files
] if message.files else []
}
if manager.is_connected(recipient_id):