inbox
This commit is contained in:
parent
bf57a30da0
commit
34580f267f
|
@ -1,6 +1,8 @@
|
||||||
from graphql.error import GraphQLError
|
from graphql.error import GraphQLError
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove traceback from logs for defined exceptions
|
||||||
|
|
||||||
class BaseHttpException(GraphQLError):
|
class BaseHttpException(GraphQLError):
|
||||||
code = 500
|
code = 500
|
||||||
message = "500 Server error"
|
message = "500 Server error"
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import asyncio
|
|
||||||
import json
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
@ -6,41 +5,7 @@ from datetime import datetime
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from base.redis import redis
|
from base.redis import redis
|
||||||
from base.resolvers import mutation, query, subscription
|
from base.resolvers import mutation, query, subscription
|
||||||
|
from services.inbox import MessageResult, MessagesStorage, ChatFollowing
|
||||||
|
|
||||||
class ChatFollowing:
|
|
||||||
queue = asyncio.Queue()
|
|
||||||
|
|
||||||
def __init__(self, chat_id):
|
|
||||||
self.chat_id = chat_id
|
|
||||||
|
|
||||||
|
|
||||||
class MessagesStorage:
|
|
||||||
lock = asyncio.Lock()
|
|
||||||
chats = []
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
async def register_chat(chat):
|
|
||||||
async with MessagesStorage.lock:
|
|
||||||
MessagesStorage.chats.append(chat)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
async def remove_chat(chat):
|
|
||||||
async with MessagesStorage.lock:
|
|
||||||
MessagesStorage.chats.remove(chat)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
async def put(message_result):
|
|
||||||
async with MessagesStorage.lock:
|
|
||||||
for chat in MessagesStorage.chats:
|
|
||||||
if message_result.message["chatId"] == chat.chat_id:
|
|
||||||
chat.queue.put_nowait(message_result)
|
|
||||||
|
|
||||||
|
|
||||||
class MessageResult:
|
|
||||||
def __init__(self, status, message):
|
|
||||||
self.status = status
|
|
||||||
self.message = message
|
|
||||||
|
|
||||||
|
|
||||||
async def get_unread_counter(user_slug):
|
async def get_unread_counter(user_slug):
|
||||||
|
@ -57,20 +22,17 @@ async def get_unread_counter(user_slug):
|
||||||
return unread
|
return unread
|
||||||
|
|
||||||
|
|
||||||
async def add_user_to_chat(user_slug, chat_id, chat=None):
|
async def add_user_to_chat(user_slug: str, chat_id: int, chat=None):
|
||||||
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
|
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
|
||||||
if not chats:
|
if chats:
|
||||||
chats = set()
|
chats = list(json.loads(chats))
|
||||||
else:
|
else:
|
||||||
chats = set(json.loads(chats))
|
chats = []
|
||||||
chats.add(str(chat_id))
|
if chat_id not in chats:
|
||||||
chats = list(chats)
|
chats.append(chat_id)
|
||||||
await redis.execute("SET", f"chats_by_user/{user_slug}", json.dumps(chats))
|
await redis.execute("SET", f"chats_by_user/{user_slug}", json.dumps(chats))
|
||||||
|
if user_slug not in chat["users"]:
|
||||||
if chat:
|
chat["users"].append(user_slug)
|
||||||
users = set(chat["users"])
|
|
||||||
users.add(user_slug)
|
|
||||||
chat["users"] = list(users)
|
|
||||||
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
|
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
|
||||||
|
|
||||||
|
|
||||||
|
@ -84,14 +46,15 @@ async def invite_to_chat(_, info, invited, chat_id):
|
||||||
|
|
||||||
@mutation.field("createChat")
|
@mutation.field("createChat")
|
||||||
@login_required
|
@login_required
|
||||||
async def create_chat(_, info, description, title=""):
|
async def create_chat(_, info, description="", title=""):
|
||||||
user = info.context["request"].user
|
user = info.context["request"].user
|
||||||
|
|
||||||
chat_id = uuid.uuid4()
|
chat_id = uuid.uuid4()
|
||||||
chat = {
|
chat = {
|
||||||
"title": title,
|
"title": title,
|
||||||
"description": description,
|
"description": description,
|
||||||
"createdAt": str(datetime.now),
|
"createdAt": str(datetime.now().timestamp()),
|
||||||
|
"updatedAt": str(datetime.now().timestamp()),
|
||||||
"createdBy": user.slug,
|
"createdBy": user.slug,
|
||||||
"id": str(chat_id),
|
"id": str(chat_id),
|
||||||
"users": [user.slug],
|
"users": [user.slug],
|
||||||
|
@ -99,20 +62,19 @@ async def create_chat(_, info, description, title=""):
|
||||||
|
|
||||||
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
|
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
|
||||||
await redis.execute("SET", f"chats/{chat_id}/next_message_id", 0)
|
await redis.execute("SET", f"chats/{chat_id}/next_message_id", 0)
|
||||||
|
|
||||||
await add_user_to_chat(user.slug, chat_id)
|
await add_user_to_chat(user.slug, chat_id)
|
||||||
|
|
||||||
return {"chatId": chat_id}
|
return chat
|
||||||
|
|
||||||
|
|
||||||
async def load_messages(chatId, size, page):
|
async def load_messages(chatId: int, size: int, page: int):
|
||||||
message_ids = await redis.lrange(
|
message_ids = await redis.lrange(
|
||||||
f"chats/{chatId}/message_ids", size * (page - 1), size * page - 1
|
f"chats/{chatId}/message_ids", size * (page - 1), size * page - 1
|
||||||
)
|
)
|
||||||
messages = []
|
messages = []
|
||||||
if message_ids:
|
if message_ids:
|
||||||
message_keys = [
|
message_keys = [
|
||||||
f"chats/{chatId}/messages/{id.decode('UTF-8')}" for id in message_ids
|
f"chats/{chatId}/messages/{mid}" for mid in message_ids
|
||||||
]
|
]
|
||||||
messages = await redis.mget(*message_keys)
|
messages = await redis.mget(*message_keys)
|
||||||
messages = [json.loads(msg) for msg in messages]
|
messages = [json.loads(msg) for msg in messages]
|
||||||
|
@ -123,7 +85,6 @@ async def load_messages(chatId, size, page):
|
||||||
@login_required
|
@login_required
|
||||||
async def user_chats(_, info):
|
async def user_chats(_, info):
|
||||||
user = info.context["request"].user
|
user = info.context["request"].user
|
||||||
|
|
||||||
chats = await redis.execute("GET", f"chats_by_user/{user.slug}")
|
chats = await redis.execute("GET", f"chats_by_user/{user.slug}")
|
||||||
if not chats:
|
if not chats:
|
||||||
chats = list()
|
chats = list()
|
||||||
|
|
|
@ -19,11 +19,6 @@ type ChatUpdatedResult {
|
||||||
message: Message
|
message: Message
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateChatResult {
|
|
||||||
chatId: String
|
|
||||||
error: String
|
|
||||||
}
|
|
||||||
|
|
||||||
type EnterChatResult {
|
type EnterChatResult {
|
||||||
chat: Chat
|
chat: Chat
|
||||||
messages: [Message]
|
messages: [Message]
|
||||||
|
@ -145,7 +140,7 @@ enum FollowingEntity {
|
||||||
|
|
||||||
type Mutation {
|
type Mutation {
|
||||||
# inbox
|
# inbox
|
||||||
createChat(description: String): CreateChatResult!
|
createChat(description: String): Chat!
|
||||||
inviteChat(chatId: String!, userslug: String!): Result!
|
inviteChat(chatId: String!, userslug: String!): Result!
|
||||||
createMessage(chatId: String!, body: String!, replyTo: Int): MessageResult!
|
createMessage(chatId: String!, body: String!, replyTo: Int): MessageResult!
|
||||||
updateMessage(chatId: String!, id: Int!, body: String!): MessageResult!
|
updateMessage(chatId: String!, id: Int!, body: String!): MessageResult!
|
||||||
|
@ -488,13 +483,13 @@ type Token {
|
||||||
|
|
||||||
type Message {
|
type Message {
|
||||||
author: String!
|
author: String!
|
||||||
chatRoom: Int!
|
chatId: Int!
|
||||||
body: String!
|
body: String!
|
||||||
createdAt: DateTime!
|
createdAt: DateTime!
|
||||||
id: Int!
|
id: Int!
|
||||||
replyTo: Int
|
replyTo: Int
|
||||||
updatedAt: DateTime!
|
updatedAt: DateTime!
|
||||||
visibleForUsers: [Int]!
|
visibleForUsers: [Int]
|
||||||
}
|
}
|
||||||
|
|
||||||
type Chat {
|
type Chat {
|
||||||
|
@ -505,5 +500,5 @@ type Chat {
|
||||||
title: String
|
title: String
|
||||||
description: String
|
description: String
|
||||||
users: [User]!
|
users: [User]!
|
||||||
messages: [Message]
|
messages: [Message]!
|
||||||
}
|
}
|
||||||
|
|
36
services/inbox.py
Normal file
36
services/inbox.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
|
class ChatFollowing:
|
||||||
|
queue = asyncio.Queue()
|
||||||
|
|
||||||
|
def __init__(self, chat_id):
|
||||||
|
self.chat_id = chat_id
|
||||||
|
|
||||||
|
|
||||||
|
class MessagesStorage:
|
||||||
|
lock = asyncio.Lock()
|
||||||
|
chats = []
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def register_chat(chat):
|
||||||
|
async with MessagesStorage.lock:
|
||||||
|
MessagesStorage.chats.append(chat)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def remove_chat(chat):
|
||||||
|
async with MessagesStorage.lock:
|
||||||
|
MessagesStorage.chats.remove(chat)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def put(message_result):
|
||||||
|
async with MessagesStorage.lock:
|
||||||
|
for chat in MessagesStorage.chats:
|
||||||
|
if message_result.message["chatId"] == chat.chat_id:
|
||||||
|
chat.queue.put_nowait(message_result)
|
||||||
|
|
||||||
|
|
||||||
|
class MessageResult:
|
||||||
|
def __init__(self, status, message):
|
||||||
|
self.status = status
|
||||||
|
self.message = message
|
Loading…
Reference in New Issue
Block a user