This commit is contained in:
parent
b7cb93a746
commit
e0d2ae37eb
|
@ -17,9 +17,7 @@ async def get_unread_counter(chat_id: str, author_id: int) -> int:
|
|||
|
||||
|
||||
# NOTE: not an API handler
|
||||
async def load_messages(
|
||||
chat_id: str, limit: int = 5, offset: int = 0, ids: Optional[List[int]] = None
|
||||
) -> List[Message]:
|
||||
async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids: Optional[List[int]] = None) -> List[Message]:
|
||||
"""load :limit messages for :chat_id with :offset"""
|
||||
if ids is None:
|
||||
ids = []
|
||||
|
@ -28,11 +26,11 @@ async def load_messages(
|
|||
message_ids = [] + ids
|
||||
if limit:
|
||||
mids = (await redis.lrange(f"chats/{chat_id}/message_ids", offset, offset + limit)) or []
|
||||
mids = [mid for mid in mids]
|
||||
message_ids += mids
|
||||
if message_ids:
|
||||
message_keys = [f"chats/{chat_id}/messages/{mid}" for mid in message_ids]
|
||||
messages = (await redis.mget(*message_keys)) or []
|
||||
messages = [json.loads(m) if m else None for m in messages]
|
||||
replies = []
|
||||
for m in messages:
|
||||
if m:
|
||||
|
|
|
@ -81,8 +81,8 @@ async def create_message(_, info, chat: str, body: str, reply_to=None):
|
|||
async def update_message(_, info, chat_id: str, message_id: int, body: str):
|
||||
author_id = info.context["author_id"]
|
||||
|
||||
chat = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if not chat:
|
||||
chat_str = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if not chat_str:
|
||||
return {"error": "chat not exist"}
|
||||
|
||||
message = await redis.execute("GET", f"chats/{chat_id}/messages/{message_id}")
|
||||
|
@ -98,9 +98,7 @@ async def update_message(_, info, chat_id: str, message_id: int, body: str):
|
|||
|
||||
await redis.execute("SET", f"chats/{chat_id}/messages/{message_id}", json.dumps(message))
|
||||
|
||||
# result = FollowingResult("UPDATE", "chat", new_message)
|
||||
# await FollowingManager.push("chat", result)
|
||||
# TODO: subscribe on updates
|
||||
# TODO: use presence service to notify about updated message
|
||||
|
||||
return {"message": message, "error": None}
|
||||
|
||||
|
@ -110,10 +108,10 @@ async def update_message(_, info, chat_id: str, message_id: int, body: str):
|
|||
async def delete_message(_, info, chat_id: str, message_id: int):
|
||||
author_id = info.context["author_id"]
|
||||
|
||||
chat = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if not chat:
|
||||
chat_str = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if not chat_str:
|
||||
return {"error": "chat not exist"}
|
||||
chat = json.loads(chat)
|
||||
chat = json.loads(chat_str)
|
||||
|
||||
message_data = await redis.execute("GET", f"chats/{chat_id}/messages/{str(message_id)}")
|
||||
if not message_data:
|
||||
|
@ -129,9 +127,7 @@ async def delete_message(_, info, chat_id: str, message_id: int):
|
|||
for member_id in members:
|
||||
await redis.execute("LREM", f"chats/{chat_id}/unread/{member_id}", 0, str(message_id))
|
||||
|
||||
# result = FollowingResult("DELETED", "chat", message)
|
||||
# await FollowingManager.push(result)
|
||||
# TODO ?
|
||||
# TODO: use presence service to notify about deleted message
|
||||
|
||||
return {}
|
||||
|
||||
|
@ -141,11 +137,11 @@ async def delete_message(_, info, chat_id: str, message_id: int):
|
|||
async def mark_as_read(_, info, chat_id: str, messages: List[int]):
|
||||
author_id = info.context["author_id"]
|
||||
|
||||
chat = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if not chat:
|
||||
chat_str = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if not chat_str:
|
||||
return {"error": "chat not exist"}
|
||||
|
||||
chat = json.loads(chat)
|
||||
chat = json.loads(chat_str)
|
||||
members = set(chat["members"])
|
||||
if author_id not in members:
|
||||
return {"error": "access denied"}
|
||||
|
|
Loading…
Reference in New Issue
Block a user