-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from Lamroy95/resolve-linked-reports
Resolve linked reports
- Loading branch information
Showing
9 changed files
with
337 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from aiogram import types | ||
from aiogram.filters import BaseFilter | ||
|
||
from app.infrastructure.database.models import Chat | ||
from app.infrastructure.database.repo.report import ReportRepo | ||
|
||
|
||
class HasResolvedReport(BaseFilter): | ||
"""Check if reported message already resolved report""" | ||
|
||
async def __call__( | ||
self, message: types.Message, chat: Chat, report_repo: ReportRepo | ||
) -> bool: | ||
return await report_repo.has_resolved_report( | ||
chat_id=chat.chat_id, message_id=message.reply_to_message.message_id | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from typing import Iterable | ||
|
||
import aiogram | ||
from tortoise import BaseDBAsyncClient | ||
|
||
from app.infrastructure.database.models import Chat, Report, ReportStatus, User | ||
|
||
|
||
class ReportRepo: | ||
def __init__(self, session: BaseDBAsyncClient | None = None): | ||
self.session = session | ||
|
||
async def create( | ||
self, | ||
reporter: User, | ||
reported_user: User, | ||
chat: Chat, | ||
reported_message: aiogram.types.Message, | ||
command_message: aiogram.types.Message, | ||
status: ReportStatus, | ||
) -> Report: | ||
report = await Report.create( | ||
reporter=reporter, | ||
reported_user=reported_user, | ||
chat=chat, | ||
command_message_id=command_message.message_id, | ||
reported_message_id=reported_message.message_id, | ||
reported_message_content=reported_message.html_text, | ||
status=status, | ||
using_db=self.session, | ||
) | ||
return report | ||
|
||
async def save(self, report: Report, fields: Iterable[str] | None = None): | ||
await report.save(update_fields=fields, using_db=self.session) | ||
|
||
async def get_report_by_id(self, report_id: int) -> Report: | ||
return await Report.get(id=report_id, using_db=self.session) | ||
|
||
async def get_linked_pending_reports(self, report_id: int) -> Iterable[Report]: | ||
report = await Report.get(id=report_id, using_db=self.session).prefetch_related( | ||
"chat" | ||
) | ||
return await ( | ||
Report.filter( | ||
chat=report.chat, | ||
reported_message_id=report.reported_message_id, | ||
status=ReportStatus.PENDING, | ||
) | ||
.prefetch_related("chat", "reporter") | ||
.order_by("created_time") | ||
.using_db(self.session) | ||
.all() | ||
) | ||
|
||
async def has_resolved_report(self, chat_id: int, message_id: int) -> bool: | ||
"""Return True, if provided message has reports with status Approved or Declined""" | ||
return await ( | ||
Report.filter( | ||
chat__chat_id=chat_id, | ||
reported_message_id=message_id, | ||
status__in=[ReportStatus.APPROVED.value, ReportStatus.DECLINED.value], | ||
) | ||
.using_db(self.session) | ||
.exists() | ||
) |
Oops, something went wrong.