diff --git a/litecord/blueprints/channels.py b/litecord/blueprints/channels.py index b27d5fc..5986e53 100644 --- a/litecord/blueprints/channels.py +++ b/litecord/blueprints/channels.py @@ -502,11 +502,11 @@ async def _search_channel(channel_id): # main message ids rows = await app.db.fetch(f""" - SELECT message_id, + SELECT messages.id, COUNT(*) OVER() as total_results FROM messages WHERE channel_id = $1 AND content LIKE '%'||$3||'%' - ORDER BY + ORDER BY messages.id DESC LIMIT 50 OFFSET $2 """, channel_id, j['offset'], j['content']) @@ -521,7 +521,9 @@ async def _search_channel(channel_id): res = [] for message_id in main_messages: - res.append([await app.storage.get_message(message_id)]) + msg = await app.storage.get_message(message_id) + msg['hit'] = True + res.append([msg]) return jsonify({ 'total_results': results, diff --git a/litecord/blueprints/guilds.py b/litecord/blueprints/guilds.py index 4aba740..a7e8a50 100644 --- a/litecord/blueprints/guilds.py +++ b/litecord/blueprints/guilds.py @@ -286,23 +286,23 @@ async def search_messages(guild_id): user_id = await token_check() await guild_check(user_id, guild_id) - j = validate(request.args, SEARCH_CHANNEL) + j = validate(dict(request.args), SEARCH_CHANNEL) # main message ids # TODO: filter only channels where user can # read messages to prevent leaking rows = await app.db.fetch(f""" - SELECT message_id, + SELECT messages.id, COUNT(*) OVER() as total_results FROM messages WHERE guild_id = $1 - ORDER BY + ORDER BY messages.id DESC LIMIT 50 OFFSET $2 """, guild_id, j['offset']) results = 0 if not rows else rows[0]['total_results'] - main_messages = [r['message_id'] for r in rows] + main_messages = [r['id'] for r in rows] # fetch contexts for each message # (2 messages before, 2 messages after). @@ -311,7 +311,9 @@ async def search_messages(guild_id): res = [] for message_id in main_messages: - res.append([await app.storage.get_message(message_id)]) + msg = await app.storage.get_message(message_id) + msg['hit'] = True + res.append([msg]) return jsonify({ 'total_results': results,