Skip to content

Commit

Permalink
fix: remove disabled chat
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Oct 20, 2024
1 parent ebc17e8 commit a41c2b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 9 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,15 @@ async def send_notification(
text: str,
parse_mode: str | DefaultValue[None] = DEFAULT_NONE,
) -> None:
await self.bot.send_message(chat_id=chat_id, text=text, parse_mode=parse_mode)
try:
await self.bot.send_message(
chat_id=chat_id, text=text, parse_mode=parse_mode
)
except telegram.error.Forbidden as e:
if e.message == "Forbidden: user is deactivated":
await self.pg.disable_chat(chat_id)
await self.read_from_db()
raise

async def get_chats(self, user_id: int) -> set[int] | None:
async with self.__lock.reader:
Expand Down
13 changes: 9 additions & 4 deletions pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from collections import defaultdict

import asyncpg
from asyncpg import Record

from lib import config

Expand All @@ -25,10 +24,10 @@ async def create_pg_client() -> PG:


class PG:
__pool: asyncpg.Pool[Record]
__pool: asyncpg.Pool

def __init__(self, pool: asyncpg.Pool[Record]):
self.__pool = pool
def __init__(self, pool: asyncpg.Pool):
self.__pool: asyncpg.Pool = pool

async def init(self) -> None:
await self.__pool.execute(
Expand Down Expand Up @@ -74,3 +73,9 @@ async def get_watched_users(self) -> dict[int, set[int]]:
d[user_id].add(chat_id)
return d
return {}

async def disable_chat(self, chat_id: int) -> None:
await self.__pool.execute(
"update telegram_notify_chat set disabled = 1 and disabled = 0 where chat_id = $1",
chat_id,
)

0 comments on commit a41c2b5

Please sign in to comment.