mirror of
				https://git.sr.ht/~tsileo/microblog.pub
				synced 2025-06-05 21:59:23 +02:00 
			
		
		
		
	Pagination for the admin profile page
This commit is contained in:
		
							
								
								
									
										39
									
								
								app/admin.py
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								app/admin.py
									
									
									
									
									
								
							| @@ -712,12 +712,6 @@ async def get_notifications( | |||||||
|         notif.is_new = False |         notif.is_new = False | ||||||
|     await db_session.commit() |     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 |     more_unread_count = 0 | ||||||
|     next_cursor = None |     next_cursor = None | ||||||
|     if notifications and remaining_count > page_size: |     if notifications and remaining_count > page_size: | ||||||
| @@ -774,10 +768,10 @@ async def admin_object( | |||||||
| async def admin_profile( | async def admin_profile( | ||||||
|     request: Request, |     request: Request, | ||||||
|     actor_id: str, |     actor_id: str, | ||||||
|  |     cursor: str | None = None, | ||||||
|     db_session: AsyncSession = Depends(get_db_session), |     db_session: AsyncSession = Depends(get_db_session), | ||||||
| ) -> templates.TemplateResponse: | ) -> templates.TemplateResponse: | ||||||
|     # TODO: pagination + show featured/pinned |     # TODO: show featured/pinned | ||||||
|  |  | ||||||
|     actor = ( |     actor = ( | ||||||
|         await db_session.execute( |         await db_session.execute( | ||||||
|             select(models.Actor).where(models.Actor.ap_id == actor_id) |             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]) |     actors_metadata = await get_actors_metadata(db_session, [actor]) | ||||||
|  |  | ||||||
|     inbox_objects = ( |     where = [ | ||||||
|         ( |  | ||||||
|             await db_session.scalars( |  | ||||||
|                 select(models.InboxObject) |  | ||||||
|                 .where( |  | ||||||
|         models.InboxObject.is_deleted.is_(False), |         models.InboxObject.is_deleted.is_(False), | ||||||
|         models.InboxObject.actor_id == actor.id, |         models.InboxObject.actor_id == actor.id, | ||||||
|         models.InboxObject.ap_type.in_( |         models.InboxObject.ap_type.in_( | ||||||
|             ["Note", "Article", "Video", "Page", "Announce"] |             ["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(*where) | ||||||
|                 .options( |                 .options( | ||||||
|                     joinedload(models.InboxObject.relates_to_inbox_object).options( |                     joinedload(models.InboxObject.relates_to_inbox_object).options( | ||||||
|                         joinedload(models.InboxObject.actor) |                         joinedload(models.InboxObject.actor) | ||||||
| @@ -811,13 +815,19 @@ async def admin_profile( | |||||||
|                     joinedload(models.InboxObject.actor), |                     joinedload(models.InboxObject.actor), | ||||||
|                 ) |                 ) | ||||||
|                 .order_by(models.InboxObject.ap_published_at.desc()) |                 .order_by(models.InboxObject.ap_published_at.desc()) | ||||||
|                 .limit(20) |                 .limit(page_size) | ||||||
|             ) |             ) | ||||||
|         ) |         ) | ||||||
|         .unique() |         .unique() | ||||||
|         .all() |         .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( |     return await templates.render_template( | ||||||
|         db_session, |         db_session, | ||||||
|         request, |         request, | ||||||
| @@ -826,6 +836,7 @@ async def admin_profile( | |||||||
|             "actors_metadata": actors_metadata, |             "actors_metadata": actors_metadata, | ||||||
|             "actor": actor, |             "actor": actor, | ||||||
|             "inbox_objects": inbox_objects, |             "inbox_objects": inbox_objects, | ||||||
|  |             "next_cursor": next_cursor, | ||||||
|         }, |         }, | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,10 +9,21 @@ | |||||||
|     {{ utils.display_actor(actor, actors_metadata, with_details=True) }} |     {{ utils.display_actor(actor, actors_metadata, with_details=True) }} | ||||||
|     {% for inbox_object in inbox_objects %} |     {% for inbox_object in inbox_objects %} | ||||||
|         {% if inbox_object.ap_type == "Announce" %} |         {% 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) }} |             {{ utils.display_object(inbox_object.relates_to_anybox_object) }} | ||||||
|         {% else %} |         {% else %} | ||||||
|                 {{ utils.display_object(inbox_object) }} |                 {{ utils.display_object(inbox_object) }} | ||||||
|         {% endif %} |         {% endif %} | ||||||
|     {% endfor %} |     {% 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 %} | {% endblock %} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user