feat(bot): 🎨 Make message queues a hash.

This commit is contained in:
Nyeki.py
2024-10-19 22:26:10 -03:00
parent 0787240ce7
commit 98e1e50831
2 changed files with 25 additions and 20 deletions
+20 -15
View File
@@ -16,10 +16,10 @@ import aiohttp
import orjson import orjson
from payphone import bot from payphone import bot
from payphone.db import utils, models from payphone.db import utils as dbutils, models
from payphone.cogs import base from payphone.cogs import base
from payphone.cogs.calls import embeds from payphone.cogs.calls import embeds
from payphone.core import constants, checks, censorship, formatting from payphone.core import constants, checks, censorship, formatting, utils
from payphone.core.redis import redis from payphone.core.redis import redis
@@ -45,8 +45,8 @@ class Calls(base.Cog):
pipe = redis.pipeline() pipe = redis.pipeline()
pipe.smembers(constants.MESSAGES_QUEUE) pipe.hgetall(constants.MESSAGES_QUEUE)
pipe.smembers(constants.MESSAGE_CONTENTS_QUEUE) pipe.hgetall(constants.MESSAGE_CONTENTS_QUEUE)
pipe.delete( pipe.delete(
constants.MESSAGES_QUEUE, constants.MESSAGES_QUEUE,
@@ -57,11 +57,14 @@ class Calls(base.Cog):
messages, message_contents, _ = await pipe.execute() messages, message_contents, _ = await pipe.execute()
messages = sorted( messages = sorted(
[models.Message(**orjson.loads(msg)) for msg in messages], [models.Message(**orjson.loads(msg)) for msg in messages.values()],
key=lambda m: m.created_at, key=lambda m: m.created_at,
) )
message_contents = sorted( message_contents = sorted(
[models.MessageContent(**orjson.loads(msg)) for msg in message_contents], [
models.MessageContent(**orjson.loads(msg))
for msg in message_contents.values()
],
key=lambda m: m.created_at, key=lambda m: m.created_at,
) )
@@ -233,7 +236,7 @@ class Calls(base.Cog):
break break
channel = await utils.get_channel(ctx.channel) channel = await dbutils.get_channel(ctx.channel)
if other_channel_id is not None: if other_channel_id is not None:
await redis.sadd( await redis.sadd(
@@ -249,7 +252,7 @@ class Calls(base.Cog):
call = await models.Call.create(is_nsfw=ctx.channel.is_nsfw()) call = await models.Call.create(is_nsfw=ctx.channel.is_nsfw())
user, profile = await utils.get_user(ctx.author) user, profile = await dbutils.get_user(ctx.author)
await models.CallUserChannelRelationship.create( await models.CallUserChannelRelationship.create(
call=call, call=call,
@@ -426,7 +429,7 @@ class Calls(base.Cog):
call = await models.Call.create(is_nsfw=ctx.channel.is_nsfw()) call = await models.Call.create(is_nsfw=ctx.channel.is_nsfw())
user, profile = await utils.get_user(ctx.author) user, profile = await dbutils.get_user(ctx.author)
await models.CallUserChannelRelationship.create( await models.CallUserChannelRelationship.create(
call=call, call=call,
@@ -492,7 +495,7 @@ class Calls(base.Cog):
) )
if is_on_call: if is_on_call:
channel = await utils.get_channel(ctx.channel) channel = await dbutils.get_channel(ctx.channel)
call = await models.Call.get(id=channel.call_id).prefetch_related( call = await models.Call.get(id=channel.call_id).prefetch_related(
"channels" "channels"
@@ -559,7 +562,7 @@ class Calls(base.Cog):
- `<reason>`: The reason why you're reporting the call. - `<reason>`: The reason why you're reporting the call.
""" """
user, _ = await utils.get_user(ctx.author) user, _ = await dbutils.get_user(ctx.author)
channel_id = ctx.channel.id channel_id = ctx.channel.id
if call_id is None: if call_id is None:
@@ -702,7 +705,7 @@ class Calls(base.Cog):
if is_banned: if is_banned:
return return
channel = await utils.get_channel(message.channel) channel = await dbutils.get_channel(message.channel)
try: try:
call = await models.Call.get(id=channel.call_id).prefetch_related( call = await models.Call.get(id=channel.call_id).prefetch_related(
@@ -721,7 +724,7 @@ class Calls(base.Cog):
# BUG: The call wasn't properly set up. # BUG: The call wasn't properly set up.
return return
user, profile = await utils.get_user(message.author) user, profile = await dbutils.get_user(message.author)
call_rss = await models.CallUserChannelRelationship.filter( call_rss = await models.CallUserChannelRelationship.filter(
call=call, channel=channel call=call, channel=channel
@@ -795,8 +798,9 @@ class Calls(base.Cog):
pipe = redis.pipeline() pipe = redis.pipeline()
pipe.sadd( pipe.hset(
constants.MESSAGES_QUEUE, constants.MESSAGES_QUEUE,
message.id,
orjson.dumps( orjson.dumps(
{ {
"id": message.id, "id": message.id,
@@ -814,8 +818,9 @@ class Calls(base.Cog):
} }
), ),
) )
pipe.sadd( pipe.hset(
constants.MESSAGE_CONTENTS_QUEUE, constants.MESSAGE_CONTENTS_QUEUE,
message.id,
orjson.dumps( orjson.dumps(
{ {
"message_id": message.id, "message_id": message.id,
+5 -5
View File
@@ -5,11 +5,11 @@ T = TypeVar("T")
U = TypeVar("U") U = TypeVar("U")
def deduplicate(iter: Iterable[T], key: Callable[[T], U] = lambda i: i) -> list[T]: def deduplicate(i: Iterable[T], key: Callable[[T], U] = lambda i: i) -> list[T]:
"""Deduplicates an Iterable. """Deduplicates an Iterable.
Args: Args:
iter (Iterable[T]): The iterable with items to deduplicate. i (Iterable[T]): The iterable with items to deduplicate.
key (Callable[[T], U], optional): The key by which to deduplicate. Defaults to `lambda i: i`. key (Callable[[T], U], optional): The key by which to deduplicate. Defaults to `lambda i: i`.
Returns: Returns:
@@ -20,8 +20,8 @@ def deduplicate(iter: Iterable[T], key: Callable[[T], U] = lambda i: i) -> list[
result = [] result = []
for i in iter: for item in i:
if key(i) not in found_keys: if key(item) not in found_keys:
result.append(i) result.append(item)
return result return result