Przeglądaj źródła

Replace chat_id with user_id in database repositories and related services

Librellium 4 tygodni temu
rodzic
commit
55c88ca5f3

+ 8 - 9
anonflow/database/repositories/ban.py

@@ -1,4 +1,3 @@
-from aiogram.types import ChatIdUnion
 from sqlalchemy import func, select, update
 from sqlalchemy.ext.asyncio import AsyncSession
 
@@ -6,36 +5,36 @@ from anonflow.database.orm import Ban
 
 
 class BanRepository:
-    async def ban(self, session: AsyncSession, actor_chat_id: ChatIdUnion, chat_id: ChatIdUnion):
+    async def ban(self, session: AsyncSession, actor_user_id: int, user_id: int):
         async with session.begin():
             ban = Ban(
-                chat_id=chat_id,
-                banned_by=actor_chat_id
+                user_id=user_id,
+                banned_by=actor_user_id
             )
             session.add(ban)
 
-    async def is_banned(self, session: AsyncSession, chat_id: ChatIdUnion):
+    async def is_banned(self, session: AsyncSession, user_id: int):
         result = await session.execute(
             select(Ban)
             .where(
-                Ban.chat_id == chat_id,
+                Ban.user_id == user_id,
                 Ban.is_active.is_(True)
             )
             .limit(1)
         )
         return bool(result.scalar_one_or_none())
 
-    async def unban(self, session: AsyncSession, actor_chat_id: ChatIdUnion, chat_id: ChatIdUnion):
+    async def unban(self, session: AsyncSession, actor_user_id: int, user_id: int):
         async with session.begin():
             await session.execute(
                 update(Ban)
                 .where(
-                    Ban.chat_id == chat_id,
+                    Ban.user_id == user_id,
                     Ban.is_active.is_(True)
                 )
                 .values(
                     is_active=False,
                     unbanned_at=func.now(),
-                    unbanned_by=actor_chat_id
+                    unbanned_by=actor_user_id
                 )
             )

+ 15 - 15
anonflow/database/repositories/moderator.py

@@ -1,7 +1,6 @@
 from dataclasses import dataclass
 from typing import Optional
 
-from aiogram.types import ChatIdUnion
 from sqlalchemy.ext.asyncio import AsyncSession
 from sqlalchemy.orm import joinedload
 
@@ -22,7 +21,7 @@ class ModeratorRepository(BaseRepository):
     async def add(
         self,
         session: AsyncSession,
-        chat_id: ChatIdUnion,
+        user_id: int,
         *,
         can_approve_posts: bool = True,
         can_manage_bans: bool = False,
@@ -31,24 +30,24 @@ class ModeratorRepository(BaseRepository):
         await super()._add(
             session,
             model_args={
-                "chat_id": chat_id,
+                "user_id": user_id,
                 "can_approve_posts": can_approve_posts,
                 "can_manage_bans": can_manage_bans,
                 "can_manage_moderators": can_manage_moderators
             }
         )
 
-    async def get(self, session: AsyncSession, chat_id: ChatIdUnion) -> Optional[Moderator]:
+    async def get(self, session: AsyncSession, user_id: int) -> Optional[Moderator]:
         return await super()._get(
             session,
-            filters={"chat_id": chat_id},
+            filters={"user_id": user_id},
             options=[
                 joinedload(Moderator.user)
             ]
         )
 
-    async def get_permissions(self, session: AsyncSession, chat_id: ChatIdUnion):
-        result = await self.get(session, chat_id)
+    async def get_permissions(self, session: AsyncSession, user_id: int):
+        result = await self.get(session, user_id)
         if result:
             return ModeratorPermissions(
                 result.can_approve_posts.value,
@@ -56,28 +55,29 @@ class ModeratorRepository(BaseRepository):
                 result.can_manage_moderators.value
             )
 
-    async def has(self, session: AsyncSession, chat_id: ChatIdUnion):
+    async def has(self, session: AsyncSession, user_id: int):
         return await super()._has(
             session,
-            filters={"chat_id": chat_id}
+            filters={"user_id": user_id}
         )
 
-    async def remove(self, session: AsyncSession, chat_id: ChatIdUnion):
+    async def remove(self, session: AsyncSession, user_id: int):
         await super()._remove(
             session,
-            filters={"chat_id": chat_id}
+            filters={"user_id": user_id}
         )
 
-    async def update(self, session: AsyncSession, chat_id: ChatIdUnion, **fields):
+    async def update(self, session: AsyncSession, user_id: int, **fields):
         await super()._update(
             session,
-            filters={"chat_id": chat_id}, fields=fields
+            filters={"user_id": user_id},
+            fields=fields
         )
 
     async def update_permissions(
         self,
         session: AsyncSession,
-        chat_id: ChatIdUnion,
+        user_id: int,
         *,
         can_approve_posts: Optional[bool] = None,
         can_manage_bans: Optional[bool] = None,
@@ -94,6 +94,6 @@ class ModeratorRepository(BaseRepository):
 
         await self.update(
             session,
-            chat_id,
+            user_id,
             **to_update
         )

+ 11 - 11
anonflow/database/repositories/user.py

@@ -1,4 +1,3 @@
-from aiogram.types import ChatIdUnion
 from sqlalchemy.ext.asyncio import AsyncSession
 from sqlalchemy.orm import joinedload, selectinload
 
@@ -10,36 +9,37 @@ from .base import BaseRepository
 class UserRepository(BaseRepository):
     model = User
 
-    async def add(self, session: AsyncSession, chat_id: ChatIdUnion):
+    async def add(self, session: AsyncSession, user_id: int):
         await super()._add(
             session,
-            model_args={"chat_id": chat_id}
+            model_args={"user_id": user_id}
         )
 
-    async def get(self, session: AsyncSession, chat_id: ChatIdUnion):
+    async def get(self, session: AsyncSession, user_id: int):
         return await super()._get(
             session,
-            filters={"chat_id": chat_id},
+            filters={"user_id": user_id},
             options=[
                 selectinload(User.bans),
                 joinedload(User.moderator)
             ]
         )
 
-    async def has(self, session: AsyncSession, chat_id: ChatIdUnion):
+    async def has(self, session: AsyncSession, user_id: int):
         return await super()._has(
             session,
-            filters={"chat_id": chat_id}
+            filters={"user_id": user_id}
         )
 
-    async def remove(self, session: AsyncSession, chat_id: ChatIdUnion):
+    async def remove(self, session: AsyncSession, user_id: int):
         await super()._remove(
             session,
-            filters={"chat_id": chat_id}
+            filters={"user_id": user_id}
         )
 
-    async def update(self, session: AsyncSession, chat_id: ChatIdUnion, **fields):
+    async def update(self, session: AsyncSession, user_id: int, **fields):
         await super()._update(
             session,
-            filters={"chat_id": chat_id}, fields=fields
+            filters={"user_id": user_id},
+            fields=fields
         )

+ 45 - 46
anonflow/services/accounts/moderator.py

@@ -1,7 +1,6 @@
 import logging
 from typing import Optional
 
-from aiogram.types import ChatIdUnion
 from sqlalchemy.exc import IntegrityError
 from sqlalchemy.ext.asyncio import AsyncSession
 
@@ -23,95 +22,95 @@ class ModeratorService:
         self._ban_repository = ban_repository
         self._moderator_repository = moderator_repository
 
-    async def add(self, actor_chat_id: ChatIdUnion, chat_id: ChatIdUnion):
+    async def add(self, actor_user_id: int, user_id: int):
         try:
             async with self._database.get_session() as session:
-                if await self._can_manage_moderators(session, actor_chat_id):
-                    await self._moderator_repository.add(session, chat_id)
+                if await self._can_manage_moderators(session, actor_user_id):
+                    await self._moderator_repository.add(session, user_id)
                 else:
                     raise ForbiddenError()
         except IntegrityError:
-            self._logger.warning("Failed to add moderator chat_id=%s", chat_id)
+            self._logger.warning("Failed to add moderator user_id=%s", user_id)
 
-    async def ban(self, actor_chat_id: ChatIdUnion, chat_id: ChatIdUnion):
+    async def ban(self, actor_user_id: int, user_id: int):
         async with self._database.get_session() as session:
-            if await self._can_manage_bans(session, actor_chat_id):
-                await self._ban_repository.ban(session, actor_chat_id, chat_id)
+            if await self._can_manage_bans(session, actor_user_id):
+                await self._ban_repository.ban(session, actor_user_id, user_id)
             else:
                 raise ForbiddenError()
 
-    async def _get_permission(self, session: AsyncSession, actor_chat_id: ChatIdUnion, name: str) -> bool:
-        moderator = await self._moderator_repository.get(session, actor_chat_id)
+    async def _get_permission(self, session: AsyncSession, actor_user_id: int, name: str) -> bool:
+        moderator = await self._moderator_repository.get(session, actor_user_id)
         return getattr(getattr(moderator, name, None), "value", False)
 
-    async def _can_approve_posts(self, session: AsyncSession, actor_chat_id: ChatIdUnion):
-        return await self._get_permission(session, actor_chat_id, "can_approve_posts")
+    async def _can_approve_posts(self, session: AsyncSession, actor_user_id: int):
+        return await self._get_permission(session, actor_user_id, "can_approve_posts")
 
-    async def can_approve_posts(self, actor_chat_id: ChatIdUnion):
+    async def can_approve_posts(self, actor_user_id: int):
         async with self._database.get_session() as session:
-            return await self._can_approve_posts(session, actor_chat_id)
+            return await self._can_approve_posts(session, actor_user_id)
 
-    async def _can_manage_bans(self, session: AsyncSession, actor_chat_id: ChatIdUnion):
-        return await self._get_permission(session, actor_chat_id, "can_manage_bans")
+    async def _can_manage_bans(self, session: AsyncSession, actor_user_id: int):
+        return await self._get_permission(session, actor_user_id, "can_manage_bans")
 
-    async def can_manage_bans(self, actor_chat_id: ChatIdUnion):
+    async def can_manage_bans(self, actor_user_id: int):
         async with self._database.get_session() as session:
-            return await self._can_manage_bans(session, actor_chat_id)
+            return await self._can_manage_bans(session, actor_user_id)
 
-    async def _can_manage_moderators(self, session: AsyncSession, actor_chat_id: ChatIdUnion):
-        return await self._get_permission(session, actor_chat_id, "can_manage_moderators")
+    async def _can_manage_moderators(self, session: AsyncSession, actor_user_id: int):
+        return await self._get_permission(session, actor_user_id, "can_manage_moderators")
 
-    async def can_manage_moderators(self, actor_chat_id: ChatIdUnion):
+    async def can_manage_moderators(self, actor_user_id: int):
         async with self._database.get_session() as session:
-            return await self._can_manage_moderators(session, actor_chat_id)
+            return await self._can_manage_moderators(session, actor_user_id)
 
-    async def get(self, chat_id: ChatIdUnion):
+    async def get(self, user_id: int):
         async with self._database.get_session() as session:
-            return await self._moderator_repository.get(session, chat_id)
+            return await self._moderator_repository.get(session, user_id)
 
-    async def get_permissions(self, chat_id: ChatIdUnion):
+    async def get_permissions(self, user_id: int):
         async with self._database.get_session() as session:
-            return await self._moderator_repository.get_permissions(session, chat_id)
+            return await self._moderator_repository.get_permissions(session, user_id)
 
-    async def has(self, chat_id: ChatIdUnion):
+    async def has(self, user_id: int):
         async with self._database.get_session() as session:
-            return await self._moderator_repository.has(session, chat_id)
+            return await self._moderator_repository.has(session, user_id)
 
-    async def is_banned(self, chat_id: ChatIdUnion):
+    async def is_banned(self, user_id: int):
         async with self._database.get_session() as session:
-            return await self._ban_repository.is_banned(session, chat_id)
+            return await self._ban_repository.is_banned(session, user_id)
 
-    async def remove(self, actor_chat_id: ChatIdUnion, chat_id: ChatIdUnion):
+    async def remove(self, actor_user_id: int, user_id: int):
         try:
             async with self._database.get_session() as session:
-                if await self._can_manage_moderators(session, actor_chat_id):
-                    await self._moderator_repository.remove(session, chat_id)
+                if await self._can_manage_moderators(session, actor_user_id):
+                    await self._moderator_repository.remove(session, user_id)
                 else:
                     raise ForbiddenError()
         except IntegrityError:
-            self._logger.warning("Failed to remove moderator chat_id=%s", chat_id)
+            self._logger.warning("Failed to remove moderator user_id=%s", user_id)
 
-    async def unban(self, actor_chat_id: ChatIdUnion, chat_id: ChatIdUnion):
+    async def unban(self, actor_user_id: int, user_id: int):
         async with self._database.get_session() as session:
-            if await self._can_manage_bans(session, actor_chat_id):
-                await self._ban_repository.unban(session, actor_chat_id, chat_id)
+            if await self._can_manage_bans(session, actor_user_id):
+                await self._ban_repository.unban(session, actor_user_id, user_id)
             else:
                 raise ForbiddenError()
 
-    async def update(self, actor_chat_id: ChatIdUnion, chat_id: ChatIdUnion, **fields):
+    async def update(self, actor_user_id: int, user_id: int, **fields):
         try:
             async with self._database.get_session() as session:
-                if await self._can_manage_moderators(session, actor_chat_id):
-                    await self._moderator_repository.update(session, chat_id, **fields)
+                if await self._can_manage_moderators(session, actor_user_id):
+                    await self._moderator_repository.update(session, user_id, **fields)
                 else:
                     raise ForbiddenError()
         except IntegrityError:
-            self._logger.warning("Failed to update moderator chat_id=%s", chat_id)
+            self._logger.warning("Failed to update moderator user_id=%s", user_id)
 
     async def update_permissions(
         self,
-        actor_chat_id: ChatIdUnion,
-        chat_id: ChatIdUnion,
+        actor_user_id: int,
+        user_id: int,
         *,
         can_approve_posts: Optional[bool] = None,
         can_manage_bans: Optional[bool] = None,
@@ -119,10 +118,10 @@ class ModeratorService:
     ):
         try:
             async with self._database.get_session() as session:
-                if await self._can_manage_moderators(session, actor_chat_id):
+                if await self._can_manage_moderators(session, actor_user_id):
                     await self._moderator_repository.update_permissions(
                         session,
-                        chat_id,
+                        user_id,
                         can_approve_posts=can_approve_posts,
                         can_manage_bans=can_manage_bans,
                         can_manage_moderators=can_manage_moderators
@@ -130,4 +129,4 @@ class ModeratorService:
                 else:
                     raise ForbiddenError()
         except IntegrityError:
-            self._logger.warning("Failed to update moderator chat_id=%s", chat_id)
+            self._logger.warning("Failed to update moderator user_id=%s", user_id)

+ 13 - 14
anonflow/services/accounts/user.py

@@ -1,6 +1,5 @@
 import logging
 
-from aiogram.types import ChatIdUnion
 from sqlalchemy.exc import IntegrityError
 
 from anonflow.database import Database, UserRepository
@@ -13,31 +12,31 @@ class UserService:
         self._database = database
         self._user_repository = user_repository
 
-    async def add(self, chat_id: ChatIdUnion):
+    async def add(self, user_id: int):
         try:
             async with self._database.get_session() as session:
-                await self._user_repository.add(session, chat_id)
+                await self._user_repository.add(session, user_id)
         except IntegrityError:
-            self._logger.warning("Failed to add user chat_id=%s", chat_id)
+            self._logger.warning("Failed to add user user_id=%s", user_id)
 
-    async def get(self, chat_id: ChatIdUnion):
+    async def get(self, user_id: int):
         async with self._database.get_session() as session:
-            return await self._user_repository.get(session, chat_id)
+            return await self._user_repository.get(session, user_id)
 
-    async def has(self, chat_id: ChatIdUnion):
+    async def has(self, user_id: int):
         async with self._database.get_session() as session:
-            return await self._user_repository.has(session, chat_id)
+            return await self._user_repository.has(session, user_id)
 
-    async def remove(self, chat_id: ChatIdUnion):
+    async def remove(self, user_id: int):
         try:
             async with self._database.get_session() as session:
-                await self._user_repository.remove(session, chat_id)
+                await self._user_repository.remove(session, user_id)
         except IntegrityError:
-            self._logger.warning("Failed to remove user chat_id=%s", chat_id)
+            self._logger.warning("Failed to remove user user_id=%s", user_id)
 
-    async def update(self, chat_id: ChatIdUnion, **fields):
+    async def update(self, user_id: int, **fields):
         try:
             async with self._database.get_session() as session:
-                await self._user_repository.update(session, chat_id, **fields)
+                await self._user_repository.update(session, user_id, **fields)
         except IntegrityError:
-            self._logger.warning("Failed to update user chat_id=%s", chat_id)
+            self._logger.warning("Failed to update user user_id=%s", user_id)