Изменения чата
This commit is contained in:
parent
b6ea1e3d72
commit
39db291f5c
5 changed files with 22 additions and 29 deletions
|
@ -43,13 +43,13 @@ async def create_chat(
|
||||||
user: SUser = Depends(check_verificated_user_with_exc),
|
user: SUser = Depends(check_verificated_user_with_exc),
|
||||||
uow=Depends(UnitOfWork)
|
uow=Depends(UnitOfWork)
|
||||||
):
|
):
|
||||||
|
if user.id == user_to_exclude:
|
||||||
|
raise UserCanNotReadThisChatException
|
||||||
async with uow:
|
async with uow:
|
||||||
if user.id == user_to_exclude:
|
|
||||||
raise UserCanNotReadThisChatException
|
|
||||||
chat_id = await uow.chat.create(user_id=user_to_exclude, chat_name=chat_name, created_by=user.id)
|
chat_id = await uow.chat.create(user_id=user_to_exclude, chat_name=chat_name, created_by=user.id)
|
||||||
user_added_to_chat = await uow.chat.add_user_to_chat(user.id, chat_id)
|
await uow.chat.add_user_to_chat(user.id, chat_id)
|
||||||
await uow.chat.add_user_to_chat(settings.ADMIN_USER_ID, chat_id)
|
await uow.chat.add_user_to_chat(settings.ADMIN_USER_ID, chat_id)
|
||||||
return user_added_to_chat
|
await uow.commit()
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from sqlalchemy import insert, select, update, delete
|
from sqlalchemy import insert, select, update, delete
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
from app.dao.base import BaseDAO
|
from app.dao.base import BaseDAO
|
||||||
from app.database import engine # noqa
|
from app.database import engine # noqa
|
||||||
|
@ -19,26 +20,22 @@ class ChatDAO(BaseDAO):
|
||||||
async def find_one_or_none(self, **filter_by):
|
async def find_one_or_none(self, **filter_by):
|
||||||
query = select(Chats.__table__.columns).filter_by(**filter_by)
|
query = select(Chats.__table__.columns).filter_by(**filter_by)
|
||||||
result = await self.session.execute(query)
|
result = await self.session.execute(query)
|
||||||
result = result.scalar_one_or_none()
|
result = result.mappings().one_or_none()
|
||||||
|
return result
|
||||||
|
|
||||||
async def create(self, user_id: int, chat_name: str, created_by: int) -> int:
|
async def create(self, user_id: int, chat_name: str, created_by: int) -> int:
|
||||||
stmt = insert(Chats).values(chat_for=user_id, chat_name=chat_name, created_by=created_by).returning(Chats.id)
|
stmt = insert(Chats).values(chat_for=user_id, chat_name=chat_name, created_by=created_by).returning(Chats.id)
|
||||||
|
|
||||||
result = await self.session.execute(stmt)
|
result = await self.session.execute(stmt)
|
||||||
await self.session.commit()
|
|
||||||
result = result.scalar()
|
result = result.scalar()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
async def add_user_to_chat(self, user_id: int, chat_id: int) -> bool:
|
async def add_user_to_chat(self, user_id: int, chat_id: int) -> None:
|
||||||
query = select(UserChat.user_id).where(UserChat.chat_id == chat_id)
|
try:
|
||||||
result = await self.session.execute(query)
|
stmt = insert(UserChat).values(user_id=user_id, chat_id=chat_id)
|
||||||
result = result.scalars().all()
|
await self.session.execute(stmt)
|
||||||
if user_id in result:
|
except IntegrityError:
|
||||||
raise UserAlreadyInChatException
|
raise UserAlreadyInChatException
|
||||||
stmt = insert(UserChat).values(user_id=user_id, chat_id=chat_id)
|
|
||||||
await self.session.execute(stmt)
|
|
||||||
await self.session.commit()
|
|
||||||
return True
|
|
||||||
|
|
||||||
async def send_message(self, user_id: int, chat_id: int, message: str, image_url: str | None = None) -> SMessage:
|
async def send_message(self, user_id: int, chat_id: int, message: str, image_url: str | None = None) -> SMessage:
|
||||||
inserted_image = (
|
inserted_image = (
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
"""Database Creation
|
"""Database Creation
|
||||||
|
|
||||||
Revision ID: 00acc3992d64
|
Revision ID: 40559c83c848
|
||||||
Revises:
|
Revises:
|
||||||
Create Date: 2024-06-05 12:56:38.627620
|
Create Date: 2024-06-05 19:00:07.382050
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from typing import Sequence, Union
|
from typing import Sequence, Union
|
||||||
|
@ -12,7 +12,7 @@ import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision: str = '00acc3992d64'
|
revision: str = '40559c83c848'
|
||||||
down_revision: Union[str, None] = None
|
down_revision: Union[str, None] = None
|
||||||
branch_labels: Union[str, Sequence[str], None] = None
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
depends_on: Union[str, Sequence[str], None] = None
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
@ -72,12 +72,11 @@ def upgrade() -> None:
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_table('user_chat',
|
op.create_table('user_chat',
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||||
sa.Column('user_id', sa.Integer(), nullable=True),
|
sa.Column('chat_id', sa.Integer(), nullable=False),
|
||||||
sa.Column('chat_id', sa.Integer(), nullable=True),
|
|
||||||
sa.ForeignKeyConstraint(['chat_id'], ['chat.id'], ),
|
sa.ForeignKeyConstraint(['chat_id'], ['chat.id'], ),
|
||||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('user_id', 'chat_id')
|
||||||
)
|
)
|
||||||
op.create_table('message_answer',
|
op.create_table('message_answer',
|
||||||
sa.Column('self_id', sa.Integer(), nullable=False),
|
sa.Column('self_id', sa.Integer(), nullable=False),
|
||||||
|
|
|
@ -7,6 +7,5 @@ from app.database import Base
|
||||||
class UserChat(Base):
|
class UserChat(Base):
|
||||||
__tablename__ = "user_chat"
|
__tablename__ = "user_chat"
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(primary_key=True)
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), primary_key=True)
|
||||||
user_id = mapped_column(ForeignKey("users.id"))
|
chat_id: Mapped[int] = mapped_column(ForeignKey("chat.id"), primary_key=True)
|
||||||
chat_id = mapped_column(ForeignKey("chat.id"))
|
|
||||||
|
|
|
@ -17,6 +17,8 @@ from app.users.schemas import SUser, SConfirmationData, SInvitationData
|
||||||
|
|
||||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||||
|
|
||||||
|
cipher_suite = Fernet(settings.INVITATION_LINK_TOKEN_KEY)
|
||||||
|
|
||||||
|
|
||||||
def get_password_hash(password: str) -> str:
|
def get_password_hash(password: str) -> str:
|
||||||
return pwd_context.hash(password)
|
return pwd_context.hash(password)
|
||||||
|
@ -35,27 +37,23 @@ def create_access_token(data: dict[str, str | datetime]) -> str:
|
||||||
|
|
||||||
|
|
||||||
def encode_invitation_token(user_data: SInvitationData) -> str:
|
def encode_invitation_token(user_data: SInvitationData) -> str:
|
||||||
cipher_suite = Fernet(settings.INVITATION_LINK_TOKEN_KEY)
|
|
||||||
invitation_token = cipher_suite.encrypt(user_data.model_dump_json().encode())
|
invitation_token = cipher_suite.encrypt(user_data.model_dump_json().encode())
|
||||||
return invitation_token.decode()
|
return invitation_token.decode()
|
||||||
|
|
||||||
|
|
||||||
def decode_invitation_token(invitation_token: str) -> SInvitationData:
|
def decode_invitation_token(invitation_token: str) -> SInvitationData:
|
||||||
user_code = invitation_token.encode()
|
user_code = invitation_token.encode()
|
||||||
cipher_suite = Fernet(settings.INVITATION_LINK_TOKEN_KEY)
|
|
||||||
user_data = cipher_suite.decrypt(user_code)
|
user_data = cipher_suite.decrypt(user_code)
|
||||||
return SInvitationData.model_validate_json(user_data)
|
return SInvitationData.model_validate_json(user_data)
|
||||||
|
|
||||||
|
|
||||||
def encode_confirmation_token(user_data: SConfirmationData) -> str:
|
def encode_confirmation_token(user_data: SConfirmationData) -> str:
|
||||||
cipher_suite = Fernet(settings.INVITATION_LINK_TOKEN_KEY)
|
|
||||||
invitation_token = cipher_suite.encrypt(user_data.model_dump_json().encode())
|
invitation_token = cipher_suite.encrypt(user_data.model_dump_json().encode())
|
||||||
return invitation_token.decode()
|
return invitation_token.decode()
|
||||||
|
|
||||||
|
|
||||||
def decode_confirmation_token(invitation_token: str) -> SConfirmationData:
|
def decode_confirmation_token(invitation_token: str) -> SConfirmationData:
|
||||||
user_code = invitation_token.encode()
|
user_code = invitation_token.encode()
|
||||||
cipher_suite = Fernet(settings.INVITATION_LINK_TOKEN_KEY)
|
|
||||||
user_data = cipher_suite.decrypt(user_code)
|
user_data = cipher_suite.decrypt(user_code)
|
||||||
return SConfirmationData.model_validate_json(user_data)
|
return SConfirmationData.model_validate_json(user_data)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue