From 6c3a8e1bc707d3bf3fd4f560d965ee4e600af08f Mon Sep 17 00:00:00 2001 From: Tnix Date: Sat, 16 Nov 2024 18:35:31 +1300 Subject: [PATCH] make inbox and admin user posts index more efficient --- database.py | 7 ++++--- rest_api/admin.py | 5 ++--- rest_api/v0/inbox.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/database.py b/database.py index 20359ec..b8e6cf9 100644 --- a/database.py +++ b/database.py @@ -85,13 +85,14 @@ db.posts.create_index([ ("post_origin", pymongo.ASCENDING), ("isDeleted", pymongo.ASCENDING), - ("t.e", pymongo.DESCENDING), - ("u", pymongo.ASCENDING) + ("t.e", pymongo.DESCENDING) ], name="default") except: pass try: db.posts.create_index([ - ("u", pymongo.ASCENDING) + ("u", pymongo.ASCENDING), + ("post_origin", pymongo.ASCENDING), + ("t.e", pymongo.DESCENDING) ], name="user") except: pass try: diff --git a/rest_api/admin.py b/rest_api/admin.py index 22075b7..9f90cd2 100644 --- a/rest_api/admin.py +++ b/rest_api/admin.py @@ -725,9 +725,8 @@ async def get_user_posts(username, query_args: GetUserPostsQueryArgs): # Get posts if query_args.origin: query = { - "post_origin": query_args.origin, - "$or": [{"isDeleted": False}, {"isDeleted": True}], "u": username, + "post_origin": query_args.origin } else: query = {"u": username} @@ -769,7 +768,7 @@ async def clear_user_posts(username, query_args: ClearUserPostsQueryArgs): # Delete posts if query_args.origin: - query = {"post_origin": query_args.origin, "isDeleted": False, "u": username} + query = {"u": username, "post_origin": query_args.origin, "isDeleted": False} else: query = {"u": username, "isDeleted": False} db.posts.update_many( diff --git a/rest_api/v0/inbox.py b/rest_api/v0/inbox.py index 1c13913..ae2a756 100644 --- a/rest_api/v0/inbox.py +++ b/rest_api/v0/inbox.py @@ -22,7 +22,7 @@ async def get_inbox_posts(query_args: GetInboxQueryArgs): abort(401) # Get and return posts - query = {"post_origin": "inbox", "isDeleted": False, "$or": [{"u": request.user}, {"u": "Server"}]} + query = {"u": {"$or": [request.user, "Server"]}, "post_origin": "inbox", "isDeleted": False} return { "error": False, "autoget": app.supporter.parse_posts_v0(db.posts.find(