added post order modes

This commit is contained in:
katboi01 2025-01-11 18:32:03 +01:00
parent 155394ab75
commit 6b0de05a80
2 changed files with 15 additions and 6 deletions

View File

@ -75,9 +75,11 @@ class Database:
result = self.db.run_query(query)
return result[0][0]
def get_posts(self, num_posts, artist, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0, include_ratings = ["SFW", "NSFW"]):
def get_posts(self, num_posts, artist, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0, include_ratings = ["SFW", "NSFW"], order = "DESC"):
num_posts = max(1, min(num_posts, 100))
order_by = "RANDOM()" if order == "RAND" else "x_posts.id ASC" if order == "ASC" else "x_posts.id DESC"
where_query = self.build_where_query(artist, actions_taken, last_id, include_ratings)
query = f'''
@ -87,13 +89,13 @@ class Database:
LEFT JOIN accounts
ON x_posts.account_id = accounts.id
{where_query}
ORDER BY x_posts.id DESC
ORDER BY {order_by}
LIMIT {num_posts} OFFSET {offset}'''
result = self.db.run_query(query)
results = self.db.run_query(query)
posts = []
if result is not None:
for result in self.db.run_query(query):
if results is not None:
for result in results:
posts.append(get_post_dictionary(result[0], -1, result[1], result[2], result[3], result[4], result[5], result[6]))
return posts

View File

@ -42,10 +42,17 @@ class Archive_GetPosts(Resource):
if ratings == []:
ratings = ["SFW", "NSFW"]
if "random" in request.args:
order = "RAND"
elif "ascending" in request.args:
order = "ASC"
else:
order = "DESC"
real_page = page-1
offset = real_page * count
result = db.get_posts(count, artist, actions_taken=actions, last_id= -1, offset= offset, include_ratings= ratings)
result = db.get_posts(count, artist, actions_taken=actions, last_id= -1, offset= offset, include_ratings= ratings, order=order)
if result is None:
response = app.response_class(status=400)