Добавил sentry, отредактировал chatdao
This commit is contained in:
parent
c17ac09993
commit
1918ad6726
7 changed files with 57 additions and 30 deletions
|
@ -26,4 +26,6 @@ SMTP_PASS=
|
||||||
IMAGE_UPLOAD_SERVER=
|
IMAGE_UPLOAD_SERVER=
|
||||||
|
|
||||||
INVITATION_LINK_HOST=
|
INVITATION_LINK_HOST=
|
||||||
INVITATION_LINK_TOKEN_KEY=
|
INVITATION_LINK_TOKEN_KEY=
|
||||||
|
|
||||||
|
SENTRY_DSN=
|
|
@ -37,5 +37,7 @@ class Settings(BaseSettings):
|
||||||
INVITATION_LINK_HOST: str
|
INVITATION_LINK_HOST: str
|
||||||
INVITATION_LINK_TOKEN_KEY: bytes
|
INVITATION_LINK_TOKEN_KEY: bytes
|
||||||
|
|
||||||
|
SENTRY_DSN: str
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|
|
@ -77,3 +77,8 @@ class WrongCodeException(BlackPhoenixException):
|
||||||
class UserNotFoundException(BlackPhoenixException):
|
class UserNotFoundException(BlackPhoenixException):
|
||||||
status_code = status.HTTP_404_NOT_FOUND
|
status_code = status.HTTP_404_NOT_FOUND
|
||||||
detail = 'Юзер не найден'
|
detail = 'Юзер не найден'
|
||||||
|
|
||||||
|
|
||||||
|
class UserAlreadyInChatException(BlackPhoenixException):
|
||||||
|
status_code = status.HTTP_409_CONFLICT
|
||||||
|
detail = "Юзер уже добавлен в чат"
|
||||||
|
|
10
app/main.py
10
app/main.py
|
@ -2,17 +2,25 @@ from fastapi import FastAPI
|
||||||
from sqladmin import Admin
|
from sqladmin import Admin
|
||||||
from starlette.staticfiles import StaticFiles
|
from starlette.staticfiles import StaticFiles
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
import sentry_sdk
|
||||||
|
|
||||||
from app.admin.auth import authentication_backend
|
from app.admin.auth import authentication_backend
|
||||||
from app.admin.views import UsersAdmin, ChatsAdmin, MessagesAdmin, UsersXChatsAdmin
|
from app.admin.views import UsersAdmin, ChatsAdmin, MessagesAdmin, UsersXChatsAdmin
|
||||||
|
from app.config import settings
|
||||||
from app.database import engine
|
from app.database import engine
|
||||||
from app.users.auth import check_verificated_user
|
from app.users.auth import check_verificated_user
|
||||||
from app.users.chat.router import router as chat_router
|
|
||||||
from app.users.router import router as user_router
|
from app.users.router import router as user_router
|
||||||
from app.pages.router import router as pages_router
|
from app.pages.router import router as pages_router
|
||||||
from app.users.chat.websocket import router as websocket_router
|
from app.users.chat.websocket import router as websocket_router
|
||||||
from app.images.router import router as image_router
|
from app.images.router import router as image_router
|
||||||
|
|
||||||
|
|
||||||
|
sentry_sdk.init(
|
||||||
|
dsn=settings.SENTRY_DSN,
|
||||||
|
traces_sample_rate=1.0,
|
||||||
|
profiles_sample_rate=1.0,
|
||||||
|
)
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="Чат BP",
|
title="Чат BP",
|
||||||
root_path="/api"
|
root_path="/api"
|
||||||
|
|
|
@ -2,6 +2,7 @@ from sqlalchemy import insert, select, update, and_
|
||||||
|
|
||||||
from app.dao.base import BaseDAO
|
from app.dao.base import BaseDAO
|
||||||
from app.database import async_session_maker
|
from app.database import async_session_maker
|
||||||
|
from app.exceptions import UserAlreadyInChatException
|
||||||
from app.users.models import Users
|
from app.users.models import Users
|
||||||
from app.users.chat.models import Chats, Messages, UsersXChats
|
from app.users.chat.models import Chats, Messages, UsersXChats
|
||||||
|
|
||||||
|
@ -9,8 +10,8 @@ from app.users.chat.models import Chats, Messages, UsersXChats
|
||||||
class ChatDAO(BaseDAO):
|
class ChatDAO(BaseDAO):
|
||||||
model = Chats
|
model = Chats
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def create(cls, user_id: int, chat_name: str, created_by: int) -> int:
|
async def create(user_id: int, chat_name: str, created_by: int) -> int:
|
||||||
query = insert(Chats).values(chat_for=user_id, chat_name=chat_name, created_by=created_by).returning(Chats.id)
|
query = insert(Chats).values(chat_for=user_id, chat_name=chat_name, created_by=created_by).returning(Chats.id)
|
||||||
async with async_session_maker() as session:
|
async with async_session_maker() as session:
|
||||||
result = await session.execute(query)
|
result = await session.execute(query)
|
||||||
|
@ -18,16 +19,21 @@ class ChatDAO(BaseDAO):
|
||||||
result = result.scalar()
|
result = result.scalar()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def add_user_to_chat(cls, user_id: int, chat_id: int):
|
async def add_user_to_chat(user_id: int, chat_id: int):
|
||||||
query = insert(UsersXChats).values(user_id=user_id, chat_id=chat_id)
|
query = select(UsersXChats.user_id).where(UsersXChats.chat_id == chat_id)
|
||||||
async with async_session_maker() as session:
|
async with async_session_maker() as session:
|
||||||
|
result = await session.execute(query)
|
||||||
|
result = [res['user_id'] for res in result.mappings().all()]
|
||||||
|
if user_id in result:
|
||||||
|
raise UserAlreadyInChatException
|
||||||
|
query = insert(UsersXChats).values(user_id=user_id, chat_id=chat_id)
|
||||||
await session.execute(query)
|
await session.execute(query)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def send_message(cls, user_id, chat_id: int, message: str, image_url: str | None = None):
|
async def send_message(user_id, chat_id: int, message: str, image_url: str | None = None):
|
||||||
query = (insert(Messages).values(chat_id=chat_id, user_id=user_id, message=message, image_url=image_url)
|
query = (insert(Messages).values(chat_id=chat_id, user_id=user_id, message=message, image_url=image_url)
|
||||||
.returning(Messages.id))
|
.returning(Messages.id))
|
||||||
async with async_session_maker() as session:
|
async with async_session_maker() as session:
|
||||||
|
@ -35,8 +41,8 @@ class ChatDAO(BaseDAO):
|
||||||
await session.commit()
|
await session.commit()
|
||||||
return result.scalar()
|
return result.scalar()
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def get_message_by_id(cls, message_id: int):
|
async def get_message_by_id(message_id: int):
|
||||||
query = (select(Messages.message, Messages.image_url, Messages.chat_id, Messages.user_id,
|
query = (select(Messages.message, Messages.image_url, Messages.chat_id, Messages.user_id,
|
||||||
Messages.created_at, Users.avatar_image, Users.username).select_from(Messages)
|
Messages.created_at, Users.avatar_image, Users.username).select_from(Messages)
|
||||||
.join(Users, Users.id == Messages.user_id)
|
.join(Users, Users.id == Messages.user_id)
|
||||||
|
@ -52,20 +58,16 @@ class ChatDAO(BaseDAO):
|
||||||
if result:
|
if result:
|
||||||
return result[0]
|
return result[0]
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def delete_message(cls, message_id: int):
|
async def delete_message(message_id: int):
|
||||||
query = update(Messages).where(Messages.id == message_id).values(visibility=False)
|
query = update(Messages).where(Messages.id == message_id).values(visibility=False)
|
||||||
async with async_session_maker() as session:
|
async with async_session_maker() as session:
|
||||||
await session.execute(query)
|
await session.execute(query)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def delete_chat(cls, chat_id: int, user_id: int):
|
async def get_some_messages(chat_id: int, message_number_from: int, messages_to_get: int):
|
||||||
pass
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
async def get_some_messages(cls, chat_id: int, message_number_from: int, messages_to_get: int):
|
|
||||||
"""
|
"""
|
||||||
WITH messages_with_users AS (
|
WITH messages_with_users AS (
|
||||||
SELECT *
|
SELECT *
|
||||||
|
@ -101,3 +103,7 @@ class ChatDAO(BaseDAO):
|
||||||
if result:
|
if result:
|
||||||
result = [dict(res) for res in result]
|
result = [dict(res) for res in result]
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def delete_chat(chat_id: int, user_id: int):
|
||||||
|
pass
|
||||||
|
|
|
@ -107,7 +107,7 @@ async def create_invitation_link(chat_id: int, user: Users = Depends(get_current
|
||||||
|
|
||||||
@router.get("/invite_to_chat/{invitation_token}")
|
@router.get("/invite_to_chat/{invitation_token}")
|
||||||
async def invite_to_chat(invitation_token: str, user: Users = Depends(get_current_user)):
|
async def invite_to_chat(invitation_token: str, user: Users = Depends(get_current_user)):
|
||||||
invitation_token = bytes(invitation_token, 'utf-8')
|
invitation_token = invitation_token.encode()
|
||||||
cipher_suite = Fernet(settings.INVITATION_LINK_TOKEN_KEY)
|
cipher_suite = Fernet(settings.INVITATION_LINK_TOKEN_KEY)
|
||||||
chat_id = int(cipher_suite.decrypt(invitation_token))
|
chat_id = int(cipher_suite.decrypt(invitation_token))
|
||||||
chat = await ChatDAO.find_one_or_none(id=chat_id)
|
chat = await ChatDAO.find_one_or_none(id=chat_id)
|
||||||
|
@ -115,3 +115,7 @@ async def invite_to_chat(invitation_token: str, user: Users = Depends(get_curren
|
||||||
raise UserCanNotReadThisChatException
|
raise UserCanNotReadThisChatException
|
||||||
return await ChatDAO.add_user_to_chat(chat_id=chat_id, user_id=user.id)
|
return await ChatDAO.add_user_to_chat(chat_id=chat_id, user_id=user.id)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/delete_user_from_chat/{chat_id}")
|
||||||
|
async def delete_user_from_chat(chat_id: int, user_id: int, user: Users = Depends(get_current_user)):
|
||||||
|
pass
|
||||||
|
|
|
@ -12,8 +12,8 @@ from app.users.models import Users, UsersVerificationCodes
|
||||||
class UserDAO(BaseDAO):
|
class UserDAO(BaseDAO):
|
||||||
model = Users
|
model = Users
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def change_data(cls, user_id: int, **data_to_change):
|
async def change_data(user_id: int, **data_to_change):
|
||||||
query = update(Users).where(Users.id == user_id).values(**data_to_change)
|
query = update(Users).where(Users.id == user_id).values(**data_to_change)
|
||||||
async with async_session_maker() as session:
|
async with async_session_maker() as session:
|
||||||
await session.execute(query)
|
await session.execute(query)
|
||||||
|
@ -22,15 +22,15 @@ class UserDAO(BaseDAO):
|
||||||
result = await session.execute(query)
|
result = await session.execute(query)
|
||||||
return result.scalar()
|
return result.scalar()
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def get_user_role(cls, user_id: int):
|
async def get_user_role(user_id: int):
|
||||||
query = select(Users.role).where(Users.id == user_id)
|
query = select(Users.role).where(Users.id == user_id)
|
||||||
async with async_session_maker() as session:
|
async with async_session_maker() as session:
|
||||||
result = await session.execute(query)
|
result = await session.execute(query)
|
||||||
return result.scalar()
|
return result.scalar()
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def get_user_allowed_chats(cls, user_id: int):
|
async def get_user_allowed_chats(user_id: int):
|
||||||
"""
|
"""
|
||||||
WITH chats_with_descriptions AS (
|
WITH chats_with_descriptions AS (
|
||||||
SELECT *
|
SELECT *
|
||||||
|
@ -81,8 +81,8 @@ class UserDAO(BaseDAO):
|
||||||
result = result.mappings().all()
|
result = result.mappings().all()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def get_user_avatar(cls, user_id: int):
|
async def get_user_avatar(user_id: int):
|
||||||
query = select(Users.avatar_image).where(Users.id == user_id)
|
query = select(Users.avatar_image).where(Users.id == user_id)
|
||||||
async with async_session_maker() as session:
|
async with async_session_maker() as session:
|
||||||
result = await session.execute(query)
|
result = await session.execute(query)
|
||||||
|
@ -102,8 +102,8 @@ class UserCodesDAO(BaseDAO):
|
||||||
await session.commit()
|
await session.commit()
|
||||||
return result.scalar()
|
return result.scalar()
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
async def get_user_codes(cls, **filter_by) -> list[dict | None]:
|
async def get_user_codes(**filter_by) -> list[dict | None]:
|
||||||
"""
|
"""
|
||||||
SELECT
|
SELECT
|
||||||
usersverificationcodes.id,
|
usersverificationcodes.id,
|
||||||
|
|
Loading…
Add table
Reference in a new issue