feat(userbot,api,frontend): backfill stories and unbreak all-stories list
This commit is contained in:
1 parent
1e143c7573
commit
12cc7d57e3
11 files changed
+326
-102
No files matched your search
@@ -22,6 +22,20 @@ ON CONFLICT (account_id, peer_id, story_id) DO UPDATE SET
|
||||
"""
|
||||
|
||||
|
||||
async def is_downloaded(
|
||||
pool: asyncpg.Pool, account_id: int, peer_id: int, story_id: int
|
||||
) -> bool:
|
||||
return bool(
|
||||
await pool.fetchval(
|
||||
"SELECT downloaded FROM stories "
|
||||
"WHERE account_id = $1 AND peer_id = $2 AND story_id = $3",
|
||||
account_id,
|
||||
peer_id,
|
||||
story_id,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def upsert_story( # noqa: PLR0913
|
||||
pool: asyncpg.Pool,
|
||||
account_id: int,
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
from io import BytesIO
|
||||
|
||||
from pyrogram import Client
|
||||
from pyrogram.types import Story
|
||||
|
||||
from userbot.modules.capture.context import CaptureContext
|
||||
from userbot.modules.stories import repository
|
||||
|
||||
|
||||
def story_peer_id(story: Story) -> int:
|
||||
if story.chat is not None:
|
||||
return story.chat.id or 0
|
||||
if story.from_user is not None:
|
||||
return story.from_user.id or 0
|
||||
return 0
|
||||
|
||||
|
||||
async def save_story(client: Client, capture: CaptureContext, story: Story) -> None:
|
||||
peer_id = story_peer_id(story)
|
||||
storage_key: str | None = None
|
||||
file_size: int | None = None
|
||||
downloaded = False
|
||||
stored = await repository.is_downloaded(
|
||||
capture.pool, capture.account_id, peer_id, story.id
|
||||
)
|
||||
if not (stored or story.deleted or story.media is None):
|
||||
buffer = await client.download_media(story, in_memory=True)
|
||||
if isinstance(buffer, BytesIO):
|
||||
data = buffer.getvalue()
|
||||
storage_key = capture.storage.put(data)
|
||||
file_size = len(data)
|
||||
downloaded = True
|
||||
await repository.upsert_story(
|
||||
capture.pool,
|
||||
capture.account_id,
|
||||
peer_id,
|
||||
story.id,
|
||||
story.date,
|
||||
story.expire_date,
|
||||
story.caption,
|
||||
story.media.name.lower() if story.media else None,
|
||||
storage_key,
|
||||
file_size,
|
||||
story.views,
|
||||
str(story.raw),
|
||||
pinned=bool(story.pinned),
|
||||
deleted=bool(story.deleted),
|
||||
downloaded=downloaded,
|
||||
)
|
||||
Reference in new issue
Block a user