|
3 | 3 | from psycopg_pool import AsyncConnectionPool |
4 | 4 |
|
5 | 5 | from .base import AsyncRepository |
| 6 | +from .models import KnownUser |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class PostgresRepository(AsyncRepository): |
@@ -261,5 +262,122 @@ async def is_globally_banned(self, user_id: int) -> bool: |
261 | 262 | ) |
262 | 263 | return await cur.fetchone() is not None |
263 | 264 |
|
| 265 | + # -- Known users -- |
| 266 | + |
| 267 | + async def upsert_known_user( |
| 268 | + self, |
| 269 | + user_id: int, |
| 270 | + username: str | None, |
| 271 | + first_name: str | None, |
| 272 | + last_name: str | None, |
| 273 | + ) -> None: |
| 274 | + async with self._pool.connection() as conn: |
| 275 | + await conn.execute( |
| 276 | + """ |
| 277 | + INSERT INTO known_users (user_id, username, first_name, last_name, updated_at) |
| 278 | + VALUES (%s, %s, %s, %s, NOW()) |
| 279 | + ON CONFLICT (user_id) |
| 280 | + DO UPDATE SET username = EXCLUDED.username, |
| 281 | + first_name = EXCLUDED.first_name, |
| 282 | + last_name = EXCLUDED.last_name, |
| 283 | + updated_at = NOW() |
| 284 | + """, |
| 285 | + (user_id, username, first_name, last_name), |
| 286 | + ) |
| 287 | + |
| 288 | + async def get_known_user(self, user_id: int) -> KnownUser | None: |
| 289 | + async with self._pool.connection() as conn: |
| 290 | + async with conn.cursor() as cur: |
| 291 | + await cur.execute( |
| 292 | + "SELECT user_id, username, first_name, last_name, updated_at " |
| 293 | + "FROM known_users WHERE user_id = %s", |
| 294 | + (user_id,), |
| 295 | + ) |
| 296 | + row = await cur.fetchone() |
| 297 | + if row is None: |
| 298 | + return None |
| 299 | + return KnownUser( |
| 300 | + user_id=row[0], |
| 301 | + username=row[1], |
| 302 | + first_name=row[2], |
| 303 | + last_name=row[3], |
| 304 | + updated_at=row[4], |
| 305 | + ) |
| 306 | + |
| 307 | + async def get_known_user_by_username(self, username: str) -> KnownUser | None: |
| 308 | + async with self._pool.connection() as conn: |
| 309 | + async with conn.cursor() as cur: |
| 310 | + await cur.execute( |
| 311 | + "SELECT user_id, username, first_name, last_name, updated_at " |
| 312 | + "FROM known_users WHERE LOWER(username) = LOWER(%s)", |
| 313 | + (username,), |
| 314 | + ) |
| 315 | + row = await cur.fetchone() |
| 316 | + if row is None: |
| 317 | + return None |
| 318 | + return KnownUser( |
| 319 | + user_id=row[0], |
| 320 | + username=row[1], |
| 321 | + first_name=row[2], |
| 322 | + last_name=row[3], |
| 323 | + updated_at=row[4], |
| 324 | + ) |
| 325 | + |
| 326 | + # -- Welcomed users -- |
| 327 | + |
| 328 | + async def has_been_welcomed(self, user_id: int, chat_id: int) -> bool: |
| 329 | + async with self._pool.connection() as conn: |
| 330 | + async with conn.cursor() as cur: |
| 331 | + await cur.execute( |
| 332 | + "SELECT 1 FROM welcomed_users WHERE user_id = %s AND chat_id = %s", |
| 333 | + (user_id, chat_id), |
| 334 | + ) |
| 335 | + return await cur.fetchone() is not None |
| 336 | + |
| 337 | + async def mark_welcomed(self, user_id: int, chat_id: int) -> None: |
| 338 | + async with self._pool.connection() as conn: |
| 339 | + await conn.execute( |
| 340 | + """ |
| 341 | + INSERT INTO welcomed_users (user_id, chat_id) |
| 342 | + VALUES (%s, %s) |
| 343 | + ON CONFLICT (user_id, chat_id) DO NOTHING |
| 344 | + """, |
| 345 | + (user_id, chat_id), |
| 346 | + ) |
| 347 | + |
| 348 | + # -- Welcome delay -- |
| 349 | + |
| 350 | + async def get_welcome_delay(self, chat_id: int) -> int | None: |
| 351 | + async with self._pool.connection() as conn: |
| 352 | + async with conn.cursor() as cur: |
| 353 | + await cur.execute( |
| 354 | + "SELECT welcome_delay_minutes FROM group_settings WHERE chat_id = %s", |
| 355 | + (chat_id,), |
| 356 | + ) |
| 357 | + row = await cur.fetchone() |
| 358 | + return row[0] if row else None |
| 359 | + |
| 360 | + async def set_welcome_delay(self, chat_id: int, minutes: int | None) -> None: |
| 361 | + async with self._pool.connection() as conn: |
| 362 | + if minutes is None: |
| 363 | + await conn.execute( |
| 364 | + """ |
| 365 | + UPDATE group_settings SET welcome_delay_minutes = NULL, updated_at = NOW() |
| 366 | + WHERE chat_id = %s |
| 367 | + """, |
| 368 | + (chat_id,), |
| 369 | + ) |
| 370 | + else: |
| 371 | + await conn.execute( |
| 372 | + """ |
| 373 | + INSERT INTO group_settings (chat_id, welcome_delay_minutes, updated_at) |
| 374 | + VALUES (%s, %s, NOW()) |
| 375 | + ON CONFLICT (chat_id) |
| 376 | + DO UPDATE SET welcome_delay_minutes = EXCLUDED.welcome_delay_minutes, |
| 377 | + updated_at = NOW() |
| 378 | + """, |
| 379 | + (chat_id, minutes), |
| 380 | + ) |
| 381 | + |
264 | 382 | async def close(self) -> None: |
265 | 383 | await self._pool.close() |
0 commit comments