mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-12-22 05:04:27 +00:00
Pagination for the notifications page
This commit is contained in:
parent
d43bf54609
commit
30cfd6260b
2 changed files with 50 additions and 1 deletions
40
app/admin.py
40
app/admin.py
|
@ -211,6 +211,7 @@ async def admin_bookmarks(
|
|||
request: Request,
|
||||
db_session: AsyncSession = Depends(get_db_session),
|
||||
) -> templates.TemplateResponse:
|
||||
# TODO: support pagination
|
||||
stream = (
|
||||
(
|
||||
await db_session.scalars(
|
||||
|
@ -667,12 +668,25 @@ async def admin_outbox(
|
|||
|
||||
@router.get("/notifications")
|
||||
async def get_notifications(
|
||||
request: Request, db_session: AsyncSession = Depends(get_db_session)
|
||||
request: Request,
|
||||
db_session: AsyncSession = Depends(get_db_session),
|
||||
cursor: str | None = None,
|
||||
) -> templates.TemplateResponse:
|
||||
where = []
|
||||
if cursor:
|
||||
decoded_cursor = pagination.decode_cursor(cursor)
|
||||
where.append(models.Notification.created_at < decoded_cursor)
|
||||
|
||||
page_size = 20
|
||||
remaining_count = await db_session.scalar(
|
||||
select(func.count(models.Notification.id)).where(*where)
|
||||
)
|
||||
|
||||
notifications = (
|
||||
(
|
||||
await db_session.scalars(
|
||||
select(models.Notification)
|
||||
.where(*where)
|
||||
.options(
|
||||
joinedload(models.Notification.actor),
|
||||
joinedload(models.Notification.inbox_object),
|
||||
|
@ -684,6 +698,7 @@ async def get_notifications(
|
|||
joinedload(models.Notification.webmention),
|
||||
)
|
||||
.order_by(models.Notification.created_at.desc())
|
||||
.limit(page_size)
|
||||
)
|
||||
)
|
||||
.unique()
|
||||
|
@ -697,6 +712,27 @@ async def get_notifications(
|
|||
notif.is_new = False
|
||||
await db_session.commit()
|
||||
|
||||
next_cursor = (
|
||||
pagination.encode_cursor(notifications[-1].created_at)
|
||||
if notifications and remaining_count > page_size
|
||||
else None
|
||||
)
|
||||
|
||||
more_unread_count = 0
|
||||
next_cursor = None
|
||||
if notifications and remaining_count > page_size:
|
||||
decoded_next_cursor = notifications[-1].created_at
|
||||
next_cursor = pagination.encode_cursor(decoded_next_cursor)
|
||||
|
||||
# If on the "see more" page there's more unread notification, we want
|
||||
# to display it next to the link
|
||||
more_unread_count = await db_session.scalar(
|
||||
select(func.count(models.Notification.id)).where(
|
||||
models.Notification.is_new.is_(True),
|
||||
models.Notification.created_at < decoded_next_cursor,
|
||||
)
|
||||
)
|
||||
|
||||
return await templates.render_template(
|
||||
db_session,
|
||||
request,
|
||||
|
@ -704,6 +740,8 @@ async def get_notifications(
|
|||
{
|
||||
"notifications": notifications,
|
||||
"actors_metadata": actors_metadata,
|
||||
"next_cursor": next_cursor,
|
||||
"more_unread_count": more_unread_count,
|
||||
},
|
||||
)
|
||||
|
||||
|
|
|
@ -88,4 +88,15 @@
|
|||
</div>
|
||||
{%- endfor %}
|
||||
</div>
|
||||
|
||||
{% if next_cursor %}
|
||||
<div class="box">
|
||||
<p>
|
||||
<a href="{{ request.url._path }}?cursor={{ next_cursor }}">
|
||||
See more{% if more_unread_count %}({{ more_unread_count }} unread left){% endif %}
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in a new issue