Pagination for the admin profile page

This commit is contained in:
Thomas Sileo 2022-08-26 08:10:46 +02:00
parent 505abd7da8
commit bec40cc050
2 changed files with 39 additions and 17 deletions

View File

@ -712,12 +712,6 @@ 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:
@ -774,10 +768,10 @@ async def admin_object(
async def admin_profile(
request: Request,
actor_id: str,
cursor: str | None = None,
db_session: AsyncSession = Depends(get_db_session),
) -> templates.TemplateResponse:
# TODO: pagination + show featured/pinned
# TODO: show featured/pinned
actor = (
await db_session.execute(
select(models.Actor).where(models.Actor.ap_id == actor_id)
@ -788,17 +782,27 @@ async def admin_profile(
actors_metadata = await get_actors_metadata(db_session, [actor])
where = [
models.InboxObject.is_deleted.is_(False),
models.InboxObject.actor_id == actor.id,
models.InboxObject.ap_type.in_(
["Note", "Article", "Video", "Page", "Announce"]
),
]
if cursor:
decoded_cursor = pagination.decode_cursor(cursor)
where.append(models.InboxObject.ap_published_at < decoded_cursor)
page_size = 20
remaining_count = await db_session.scalar(
select(func.count(models.InboxObject.id)).where(*where)
)
inbox_objects = (
(
await db_session.scalars(
select(models.InboxObject)
.where(
models.InboxObject.is_deleted.is_(False),
models.InboxObject.actor_id == actor.id,
models.InboxObject.ap_type.in_(
["Note", "Article", "Video", "Page", "Announce"]
),
)
.where(*where)
.options(
joinedload(models.InboxObject.relates_to_inbox_object).options(
joinedload(models.InboxObject.actor)
@ -811,13 +815,19 @@ async def admin_profile(
joinedload(models.InboxObject.actor),
)
.order_by(models.InboxObject.ap_published_at.desc())
.limit(20)
.limit(page_size)
)
)
.unique()
.all()
)
next_cursor = (
pagination.encode_cursor(inbox_objects[-1].created_at)
if inbox_objects and remaining_count > page_size
else None
)
return await templates.render_template(
db_session,
request,
@ -826,6 +836,7 @@ async def admin_profile(
"actors_metadata": actors_metadata,
"actor": actor,
"inbox_objects": inbox_objects,
"next_cursor": next_cursor,
},
)

View File

@ -9,10 +9,21 @@
{{ utils.display_actor(actor, actors_metadata, with_details=True) }}
{% for inbox_object in inbox_objects %}
{% if inbox_object.ap_type == "Announce" %}
{{ utils.actor_action(inbox_object, "shared") }}
{{ utils.actor_action(inbox_object, "shared", with_icon=True) }}
{{ utils.display_object(inbox_object.relates_to_anybox_object) }}
{% else %}
{{ utils.display_object(inbox_object) }}
{% endif %}
{% endfor %}
{% if next_cursor %}
<div class="box">
<p>
<a href="{{ request.url._path }}?actor_id={{ request.query_params.actor_id }}&cursor={{ next_cursor }}">
See more
</a>
</p>
</div>
{% endif %}
{% endblock %}