mirror of https://gitlab.com/litecord/litecord.git
guilds: add message contexts
- guilds: fix fetch_readable_channels()
This commit is contained in:
parent
9514551405
commit
2322c2603c
|
|
@ -391,7 +391,7 @@ async def fetch_readable_channels(guild_id: int, user_id: int) -> List[int]:
|
||||||
for channel_id in channel_ids:
|
for channel_id in channel_ids:
|
||||||
perms = await get_permissions(user_id, channel_id)
|
perms = await get_permissions(user_id, channel_id)
|
||||||
|
|
||||||
if perms.read_messages:
|
if perms.bits.read_messages:
|
||||||
res.append(channel_id)
|
res.append(channel_id)
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
@ -416,30 +416,39 @@ async def search_messages(guild_id):
|
||||||
can_read = await fetch_readable_channels(guild_id, user_id)
|
can_read = await fetch_readable_channels(guild_id, user_id)
|
||||||
|
|
||||||
rows = await app.db.fetch(f"""
|
rows = await app.db.fetch(f"""
|
||||||
SELECT messages.id,
|
SELECT orig.id AS current_id,
|
||||||
COUNT(*) OVER() as total_results
|
COUNT(*) OVER() as total_results,
|
||||||
FROM messages
|
array((SELECT messages.id AS before_id
|
||||||
|
FROM messages WHERE messages.id < orig.id
|
||||||
|
ORDER BY messages.id DESC LIMIT 2)) AS before,
|
||||||
|
array((SELECT messages.id AS after_id
|
||||||
|
FROM messages WHERE messages.id > orig.id
|
||||||
|
ORDER BY messages.id ASC LIMIT 2)) AS after
|
||||||
|
|
||||||
|
FROM messages AS orig
|
||||||
WHERE guild_id = $1
|
WHERE guild_id = $1
|
||||||
AND messages.content LIKE '%'||$2||'%'
|
AND orig.content LIKE '%'||$2||'%'
|
||||||
AND ARRAY[messages.channel_id] <@ $4::bigint[]
|
AND ARRAY[orig.channel_id] <@ $4::bigint[]
|
||||||
ORDER BY messages.id DESC
|
ORDER BY orig.id DESC
|
||||||
LIMIT 50
|
LIMIT 50
|
||||||
OFFSET $3
|
OFFSET $3
|
||||||
""", guild_id, j['content'], j['offset'], can_read)
|
""", guild_id, j['content'], j['offset'], can_read)
|
||||||
|
|
||||||
results = 0 if not rows else rows[0]['total_results']
|
results = 0 if not rows else rows[0]['total_results']
|
||||||
main_messages = [r['id'] for r in rows]
|
|
||||||
|
|
||||||
# fetch contexts for each message
|
|
||||||
# (2 messages before, 2 messages after).
|
|
||||||
|
|
||||||
# TODO: actual contexts
|
|
||||||
res = []
|
res = []
|
||||||
|
|
||||||
for message_id in main_messages:
|
for row in rows:
|
||||||
msg = await app.storage.get_message(message_id)
|
before, after = [], []
|
||||||
|
|
||||||
|
for before_id in reversed(row['before']):
|
||||||
|
before.append(await app.storage.get_message(before_id))
|
||||||
|
|
||||||
|
for after_id in row['after']:
|
||||||
|
after.append(await app.storage.get_message(after_id))
|
||||||
|
|
||||||
|
msg = await app.storage.get_message(row['current_id'])
|
||||||
msg['hit'] = True
|
msg['hit'] = True
|
||||||
res.append([msg])
|
res.append(before + [msg] + after)
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'total_results': results,
|
'total_results': results,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue