use redis for storing messages

This commit is contained in:
knst-kotov 2022-01-23 14:02:57 +03:00
parent 25b4f32025
commit c7639f38ae
3 changed files with 105 additions and 60 deletions

View File

@ -25,6 +25,9 @@ class Redis:
async def execute(self, command, *args, **kwargs):
return await self._instance.execute_command(command, *args, **kwargs)
async def lrange(self, name, start, end):
return await self._instance.lrange(name, start, end)
async def test():
redis = Redis()

View File

@ -5,7 +5,10 @@ from resolvers.base import mutation, query, subscription
from auth.authenticate import login_required
import asyncio
import asyncio, uuid, json
from datetime import datetime
from redis import redis
class MessageSubscriptions:
lock = asyncio.Lock()
@ -32,21 +35,57 @@ class MessageResult:
self.status = status
self.message = message
@mutation.field("createChat")
@login_required
async def create_chat(_, info, description):
user = info.context["request"].user
chat_id = uuid.uuid4()
chat = {
"description" : description,
"createdAt" : str(datetime.now),
"createdBy" : user.slug,
"id" : str(chat_id)
}
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
return { "chatId" : chat_id }
@query.field("enterChat")
@login_required
async def enter_chat(_, info, chatId):
chat = await redis.execute("GET", f"chats/{chatId}")
if not chat:
return { "error" : "chat not exist" }
chat = json.loads(chat)
messages = await redis.lrange(f"chats/{chatId}/messages", 0, 10)
messages = [json.loads(msg) for msg in messages]
return {
"chat" : chat,
"messages" : messages
}
@mutation.field("createMessage")
@login_required
async def create_message(_, info, body, replyTo = None):
auth = info.context["request"].auth
user_id = auth.user_id
async def create_message(_, info, chatId, body, replyTo = None):
user = info.context["request"].user
new_message = Message.create(
author = user_id,
body = body,
replyTo = replyTo
)
chat = await redis.execute("GET", f"chats/{chatId}")
if not chat:
return { "error" : "chat not exist" }
result = MessageResult("NEW", new_message)
await MessageSubscriptions.put(result)
new_message = {
"chatId" : chatId,
"author" : user.slug,
"body" : body,
"replyTo" : replyTo
}
message_id = await redis.execute("LPUSH", f"chats/{chatId}/messages", json.dumps(new_message))
new_message["id"] = message_id
return {"message" : new_message}

View File

@ -64,19 +64,20 @@ enum MessageStatus {
DELETED
}
type CreateChatResult {
chatId: Int
error: String
}
type MessageWithStatus {
status: MessageStatus!
message: Message!
}
type ChatRoomResult {
messages: [Message]!
room: ChatRoom!
type CreateChatResult {
chatId: String
error: String
}
type EnterChatResult {
chat: Chat
messages: [Message]
error: String
}
input TopicInput {
@ -97,11 +98,10 @@ type TopicResult {
type Mutation {
# message
createChat: CreateChatResult!
getRoom(chatRoom: Int!): ChatRoomResult! # TODO: private rooms protection
createMessage(body: String!, replyTo: Int): MessageResult!
updateMessage(id: Int!, body: String!): MessageResult!
deleteMessage(messageId: Int!): Result!
createChat(description: String): CreateChatResult!
createMessage(chatId: String!, body: String!, replyTo: Int): MessageResult!
updateMessage(chatId: String!, id: Int!, body: String!): MessageResult!
deleteMessage(chatId: String!, messageId: Int!): Result!
# auth
confirmEmail(token: String!): AuthResult!
@ -178,13 +178,16 @@ type Query {
# communities
getCommunity(slug: String): Community!
getCommunities: [Community]!
#messages
enterChat(chatId: String!): EnterChatResult!
}
############################################ Subscription
type Subscription {
messageChanged: MessageWithStatus!
chatUpdated: ChatRoomResult!
chatUpdated: MessageWithStatus!
onlineUpdated: [User!]!
shoutUpdated: Shout!
userUpdated: User!
@ -267,11 +270,11 @@ type Message {
visibleForUsers: [Int]!
}
type ChatRoom {
type Chat {
id: Int!
createdAt: DateTime!
updatedAt: DateTime!
notes: String
description: String
}
type Comment {