diff --git a/main.py b/main.py index 37d04a6..14cfa06 100644 --- a/main.py +++ b/main.py @@ -3185,7 +3185,26 @@ async def profile_invitation( user: Dict = Security(get_current_user) ): data = await invitations_collection.find_one({'_id': ObjectId(invitation_id)}) - posts = await delivery_collection.find({'competition_id': data.get('competition').get('_id')}).to_list() + cursor = delivery_collection.aggregate([ + {"$match": { + "$or": [ + {"sender._id": user.get('_id')}, + {"user._id": user.get('_id')}, + {"receiver._id": user.get('_id')} + ], + 'prod': PROD, + 'competition_id': data.get('competition').get('_id'), + }}, + {"$sort": {"_id": -1}}, + {"$group": { + "_id": "$post_id", + "doc": {"$first": "$$ROOT"} + }}, + {"$replaceRoot": {"newRoot": "$doc"}}, + {"$sort": {"_id": -1}}, + ]) + + posts = [doc async for doc in cursor] return templates.TemplateResponse( request=request, name="profile/invitation.html", context={ "user": user, "doc": data, "data": posts} )