Skip to content

Commit

Permalink
added import karma
Browse files Browse the repository at this point in the history
  • Loading branch information
ychebyshev committed Mar 16, 2024
1 parent f024359 commit c47707f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/handlers/change_karma.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import asyncio
import json

from aiogram import Bot, F, Router, types
from aiogram.enums import ChatMemberStatus
from aiogram.filters import Command, CommandObject
from aiogram.types import ContentType, LinkPreviewOptions
from aiogram.utils.text_decorations import html_decoration as hd

Expand All @@ -14,6 +17,9 @@
from app.utils.exceptions import CantChangeKarma, DontOffendRestricted, SubZeroKarma
from app.utils.log import Logger

from ..infrastructure.database import models
from ..models import dto
from ..services.karma import import_karma
from . import keyboards as kb

logger = Logger(__name__)
Expand Down Expand Up @@ -140,3 +146,34 @@ async def cancel_karma(
)
await callback_query.answer("Вы отменили изменение кармы", show_alert=True)
await callback_query.message.delete()


@router.message(Command("karma_import", prefix="!"))
async def karma_import_handler(
message: types.Message,
command: CommandObject,
bot: Bot,
event_chat: types.Chat,
event_from_user: types.User,
chat: models.Chat,
user_repo: UserRepo,
):
logger.info("try to start import karma")
args = [dto.Import(d["id"], d["karma"]) for d in json.loads(command.args or "{}")]
chat_owner: types.ChatMemberOwner = next(
filter(
lambda c: c.status == ChatMemberStatus.CREATOR,
await bot.get_chat_administrators(event_chat.id),
)
)
if chat_owner.user.id != event_from_user.id:
await message.reply("Только для владельца чата")
return
logger.warning(
"started import karma for chat %s, by user %s, with data %s",
event_chat.id,
event_from_user.id,
args,
)
await import_karma(args, chat, user_repo)
logger.warning("completed import karma for chat %s", event_chat.id)
1 change: 1 addition & 0 deletions app/models/dto/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .import_karma import Import # noqa: F401
from .user import TargetUser # noqa: F401
7 changes: 7 additions & 0 deletions app/models/dto/import_karma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from dataclasses import dataclass


@dataclass
class Import:
id: int
karma: float
16 changes: 16 additions & 0 deletions app/services/karma.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import typing

from aiogram.utils.markdown import hbold
from tortoise.transactions import in_transaction

from app.infrastructure.database import models
from app.infrastructure.database.models import Chat, User, UserKarma
from app.infrastructure.database.repo.chat import ChatRepo
from app.infrastructure.database.repo.user import UserRepo
from app.models import dto
from app.utils.exceptions import IDParseError, NotEnoughArguments, NotHaveNeighbours


Expand All @@ -18,6 +21,19 @@ async def get_chat(command_arguments: list[str], chat_repo: ChatRepo) -> Chat:
raise IDParseError


async def import_karma(import_data: list[dto.Import], chat: models.Chat, user_repo: UserRepo):
with in_transaction() as using_db:
for data in import_data:
target_user = await user_repo.get_by_id(data.id)
uk, _ = await UserKarma.get_or_create(
user=target_user,
chat=chat,
using_db=using_db,
)
uk.karma = data.karma
await uk.save(using_db=using_db)


async def get_top(
chat: Chat, user: User, user_repo: UserRepo, chat_repo: ChatRepo, limit: int = 15
):
Expand Down

0 comments on commit c47707f

Please sign in to comment.