время в топе и тэги креативов

This commit is contained in:
Artem Tsyrulnikov
2026-01-28 12:34:00 +03:00
parent 0b25566ffb
commit 484e52c077
26 changed files with 326 additions and 36 deletions

View File

@@ -1,6 +1,8 @@
import logging
from typing import TYPE_CHECKING
from tortoise import timezone
from src import domain, dto
from src.usecase.purchase.create_placements import _build_placement_output
@@ -24,7 +26,10 @@ def _build_post_output(post: domain.Post) -> dto.PostOutput:
def _build_placement_post_output(
placement_post: domain.PlacementPost, subscriptions_count: int, views_count: int | None
placement_post: domain.PlacementPost,
subscriptions_count: int,
views_count: int | None,
time_on_top: int | None = None,
) -> dto.PlacementPostOutput | None:
if not placement_post.post:
log.warning('PlacementPost %s missing post', placement_post.id)
@@ -40,6 +45,7 @@ def _build_placement_post_output(
subscriptions_count=subscriptions_count,
views_count=views_count,
created_at=placement_post.created_at,
time_on_top=time_on_top,
post=post_output,
)
@@ -85,12 +91,21 @@ async def get_placement_user(self: 'Usecase', input: dto.GetPlacementInput) -> d
log.warning('Placement %s has %s placement_posts, returning latest', placement.id, len(placement_posts))
for placement_post in placement_posts:
views_count = None
time_on_top = None
if placement_post.post:
views_count = views_map.get(placement_post.post.id, (None,))[0]
# Calculate time_on_top
post = placement_post.post
next_post = await self.database.get_next_post_after(post.channel_id, post.message_id)
if next_post and next_post.published_at and post.published_at:
time_on_top = int((next_post.published_at - post.published_at).total_seconds())
elif post.published_at:
time_on_top = int((timezone.now() - post.published_at).total_seconds())
placement_post_output = _build_placement_post_output(
placement_post,
subscriptions_count,
views_count,
time_on_top,
)
if placement_post_output is not None:
break