use redis for storing messages
This commit is contained in:
parent
25b4f32025
commit
c7639f38ae
|
@ -6,44 +6,47 @@ from settings import REDIS_URL
|
||||||
|
|
||||||
|
|
||||||
class Redis:
|
class Redis:
|
||||||
def __init__(self, uri=REDIS_URL):
|
def __init__(self, uri=REDIS_URL):
|
||||||
self._uri: str = uri
|
self._uri: str = uri
|
||||||
self._instance = None
|
self._instance = None
|
||||||
|
|
||||||
async def connect(self):
|
async def connect(self):
|
||||||
if self._instance is not None:
|
if self._instance is not None:
|
||||||
return
|
return
|
||||||
self._instance = aioredis.from_url(self._uri, encoding="utf-8")
|
self._instance = aioredis.from_url(self._uri, encoding="utf-8")
|
||||||
|
|
||||||
async def disconnect(self):
|
async def disconnect(self):
|
||||||
if self._instance is None:
|
if self._instance is None:
|
||||||
return
|
return
|
||||||
self._instance.close()
|
self._instance.close()
|
||||||
await self._instance.wait_closed()
|
await self._instance.wait_closed()
|
||||||
self._instance = None
|
self._instance = None
|
||||||
|
|
||||||
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()
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
await redis.connect()
|
await redis.connect()
|
||||||
await redis.execute("SET", "1-KEY1", 1)
|
await redis.execute("SET", "1-KEY1", 1)
|
||||||
await redis.execute("SET", "1-KEY2", 1)
|
await redis.execute("SET", "1-KEY2", 1)
|
||||||
await redis.execute("SET", "1-KEY3", 1)
|
await redis.execute("SET", "1-KEY3", 1)
|
||||||
await redis.execute("SET", "1-KEY4", 1)
|
await redis.execute("SET", "1-KEY4", 1)
|
||||||
await redis.execute("EXPIREAT", "1-KEY4", int(datetime.utcnow().timestamp()))
|
await redis.execute("EXPIREAT", "1-KEY4", int(datetime.utcnow().timestamp()))
|
||||||
v = await redis.execute("KEYS", "1-*")
|
v = await redis.execute("KEYS", "1-*")
|
||||||
print(v)
|
print(v)
|
||||||
await redis.execute("DEL", *v)
|
await redis.execute("DEL", *v)
|
||||||
v = await redis.execute("KEYS", "1-*")
|
v = await redis.execute("KEYS", "1-*")
|
||||||
print(v)
|
print(v)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
asyncio.run(test())
|
asyncio.run(test())
|
||||||
|
|
|
@ -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,22 +35,58 @@ 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
|
|
||||||
|
chat = await redis.execute("GET", f"chats/{chatId}")
|
||||||
new_message = Message.create(
|
if not chat:
|
||||||
author = user_id,
|
return { "error" : "chat not exist" }
|
||||||
body = body,
|
|
||||||
replyTo = replyTo
|
new_message = {
|
||||||
)
|
"chatId" : chatId,
|
||||||
|
"author" : user.slug,
|
||||||
result = MessageResult("NEW", new_message)
|
"body" : body,
|
||||||
await MessageSubscriptions.put(result)
|
"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}
|
||||||
|
|
||||||
@query.field("getMessages")
|
@query.field("getMessages")
|
||||||
|
|
|
@ -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