use redis for storing messages
This commit is contained in:
parent
25b4f32025
commit
c7639f38ae
|
@ -25,6 +25,9 @@ class Redis:
|
||||||
async def execute(self, command, *args, **kwargs):
|
async def execute(self, command, *args, **kwargs):
|
||||||
return await self._instance.execute_command(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():
|
async def test():
|
||||||
redis = Redis()
|
redis = Redis()
|
||||||
|
|
|
@ -5,7 +5,10 @@ from resolvers.base import mutation, query, subscription
|
||||||
|
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
|
|
||||||
import asyncio
|
import asyncio, uuid, json
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from redis import redis
|
||||||
|
|
||||||
class MessageSubscriptions:
|
class MessageSubscriptions:
|
||||||
lock = asyncio.Lock()
|
lock = asyncio.Lock()
|
||||||
|
@ -32,21 +35,57 @@ class MessageResult:
|
||||||
self.status = status
|
self.status = status
|
||||||
self.message = message
|
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")
|
@mutation.field("createMessage")
|
||||||
@login_required
|
@login_required
|
||||||
async def create_message(_, info, body, replyTo = None):
|
async def create_message(_, info, chatId, body, replyTo = None):
|
||||||
auth = info.context["request"].auth
|
user = info.context["request"].user
|
||||||
user_id = auth.user_id
|
|
||||||
|
|
||||||
new_message = Message.create(
|
chat = await redis.execute("GET", f"chats/{chatId}")
|
||||||
author = user_id,
|
if not chat:
|
||||||
body = body,
|
return { "error" : "chat not exist" }
|
||||||
replyTo = replyTo
|
|
||||||
)
|
|
||||||
|
|
||||||
result = MessageResult("NEW", new_message)
|
new_message = {
|
||||||
await MessageSubscriptions.put(result)
|
"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}
|
return {"message" : new_message}
|
||||||
|
|
||||||
|
|
|
@ -64,19 +64,20 @@ enum MessageStatus {
|
||||||
DELETED
|
DELETED
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateChatResult {
|
|
||||||
chatId: Int
|
|
||||||
error: String
|
|
||||||
}
|
|
||||||
|
|
||||||
type MessageWithStatus {
|
type MessageWithStatus {
|
||||||
status: MessageStatus!
|
status: MessageStatus!
|
||||||
message: Message!
|
message: Message!
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatRoomResult {
|
type CreateChatResult {
|
||||||
messages: [Message]!
|
chatId: String
|
||||||
room: ChatRoom!
|
error: String
|
||||||
|
}
|
||||||
|
|
||||||
|
type EnterChatResult {
|
||||||
|
chat: Chat
|
||||||
|
messages: [Message]
|
||||||
|
error: String
|
||||||
}
|
}
|
||||||
|
|
||||||
input TopicInput {
|
input TopicInput {
|
||||||
|
@ -97,11 +98,10 @@ type TopicResult {
|
||||||
|
|
||||||
type Mutation {
|
type Mutation {
|
||||||
# message
|
# message
|
||||||
createChat: CreateChatResult!
|
createChat(description: String): CreateChatResult!
|
||||||
getRoom(chatRoom: Int!): ChatRoomResult! # TODO: private rooms protection
|
createMessage(chatId: String!, body: String!, replyTo: Int): MessageResult!
|
||||||
createMessage(body: String!, replyTo: Int): MessageResult!
|
updateMessage(chatId: String!, id: Int!, body: String!): MessageResult!
|
||||||
updateMessage(id: Int!, body: String!): MessageResult!
|
deleteMessage(chatId: String!, messageId: Int!): Result!
|
||||||
deleteMessage(messageId: Int!): Result!
|
|
||||||
|
|
||||||
# auth
|
# auth
|
||||||
confirmEmail(token: String!): AuthResult!
|
confirmEmail(token: String!): AuthResult!
|
||||||
|
@ -178,13 +178,16 @@ type Query {
|
||||||
# communities
|
# communities
|
||||||
getCommunity(slug: String): Community!
|
getCommunity(slug: String): Community!
|
||||||
getCommunities: [Community]!
|
getCommunities: [Community]!
|
||||||
|
|
||||||
|
#messages
|
||||||
|
enterChat(chatId: String!): EnterChatResult!
|
||||||
}
|
}
|
||||||
|
|
||||||
############################################ Subscription
|
############################################ Subscription
|
||||||
|
|
||||||
type Subscription {
|
type Subscription {
|
||||||
messageChanged: MessageWithStatus!
|
messageChanged: MessageWithStatus!
|
||||||
chatUpdated: ChatRoomResult!
|
chatUpdated: MessageWithStatus!
|
||||||
onlineUpdated: [User!]!
|
onlineUpdated: [User!]!
|
||||||
shoutUpdated: Shout!
|
shoutUpdated: Shout!
|
||||||
userUpdated: User!
|
userUpdated: User!
|
||||||
|
@ -267,11 +270,11 @@ type Message {
|
||||||
visibleForUsers: [Int]!
|
visibleForUsers: [Int]!
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatRoom {
|
type Chat {
|
||||||
id: Int!
|
id: Int!
|
||||||
createdAt: DateTime!
|
createdAt: DateTime!
|
||||||
updatedAt: DateTime!
|
updatedAt: DateTime!
|
||||||
notes: String
|
description: String
|
||||||
}
|
}
|
||||||
|
|
||||||
type Comment {
|
type Comment {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user