Files
beavergram/backend/src/userbot/modules/links/repository.py
T

56 lines
1.5 KiB
Python

import asyncpg
from userbot.modules.links.extract import LinkRow
_INSERT = """
INSERT INTO links
(account_id, chat_id, message_id, position, url, kind, web_url, web_title,
web_description, web_site_name, raw)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11::jsonb)
ON CONFLICT (account_id, chat_id, message_id, position) DO UPDATE SET
url = EXCLUDED.url,
kind = EXCLUDED.kind,
web_url = EXCLUDED.web_url,
web_title = EXCLUDED.web_title,
web_description = EXCLUDED.web_description,
web_site_name = EXCLUDED.web_site_name,
raw = EXCLUDED.raw
"""
async def insert_links( # noqa: PLR0913
pool: asyncpg.Pool,
account_id: int,
chat_id: int,
message_id: int,
rows: list[LinkRow],
raw: str,
) -> None:
async with pool.acquire() as conn, conn.transaction():
await conn.execute(
"DELETE FROM links "
"WHERE account_id = $1 AND chat_id = $2 AND message_id = $3",
account_id,
chat_id,
message_id,
)
await conn.executemany(
_INSERT,
[
(
account_id,
chat_id,
message_id,
position,
row.url,
row.kind,
row.web_url,
row.web_title,
row.web_description,
row.web_site_name,
raw,
)
for position, row in enumerate(rows)
],
)