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