schema-fix
This commit is contained in:
parent
26174a67e4
commit
6485b16144
|
@ -49,6 +49,10 @@ async def invite_to_chat(_, info, invited, chat_id):
|
||||||
chat = await redis.execute("GET", f"chats/{chat_id}")
|
chat = await redis.execute("GET", f"chats/{chat_id}")
|
||||||
if user.slug in chat['users']:
|
if user.slug in chat['users']:
|
||||||
add_user_to_chat(invited, chat_id, chat)
|
add_user_to_chat(invited, chat_id, chat)
|
||||||
|
return {
|
||||||
|
"error": None,
|
||||||
|
"chat": chat
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("createChat")
|
@mutation.field("createChat")
|
||||||
|
@ -71,21 +75,27 @@ async def create_chat(_, info, description="", title=""):
|
||||||
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 chat
|
return {
|
||||||
|
"error": None,
|
||||||
|
"chat": chat
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def load_messages(chatId: int, size: int, page: int):
|
async def load_messages(chatId: int, size: int, page: int):
|
||||||
|
messages = []
|
||||||
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 = []
|
|
||||||
if message_ids:
|
if message_ids:
|
||||||
message_keys = [
|
message_keys = [
|
||||||
f"chats/{chatId}/messages/{mid}" for mid 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]
|
||||||
return messages
|
return {
|
||||||
|
"messages": messages,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@query.field("myChats")
|
@query.field("myChats")
|
||||||
|
@ -94,13 +104,16 @@ 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 = []
|
||||||
else:
|
else:
|
||||||
chats = list(json.loads(chats))
|
chats = json.loads(chats)
|
||||||
for c in chats:
|
for c in chats:
|
||||||
c['messages'] = await load_messages(c['id'], 50, 1)
|
c['messages'] = await load_messages(c['id'], 50, 1)
|
||||||
c['unread'] = await get_unread_counter(c['id'], user.slug)
|
c['unread'] = await get_unread_counter(c['id'], user.slug)
|
||||||
return chats
|
return {
|
||||||
|
"chats": chats,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@query.field("enterChat")
|
@query.field("enterChat")
|
||||||
|
@ -110,11 +123,17 @@ async def enter_chat(_, info, chatId):
|
||||||
|
|
||||||
chat = await redis.execute("GET", f"chats/{chatId}")
|
chat = await redis.execute("GET", f"chats/{chatId}")
|
||||||
if not chat:
|
if not chat:
|
||||||
return {"error": "chat not exist"}
|
return {
|
||||||
|
"error": "chat not exist"
|
||||||
|
}
|
||||||
|
else:
|
||||||
chat = json.loads(chat)
|
chat = json.loads(chat)
|
||||||
await add_user_to_chat(user.slug, chatId, chat)
|
await add_user_to_chat(user.slug, chatId, chat)
|
||||||
chat['messages'] = await load_messages(chatId, 50, 1)
|
chat['messages'] = await load_messages(chatId, 50, 1)
|
||||||
return chat
|
return {
|
||||||
|
"chat": chat,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("createMessage")
|
@mutation.field("createMessage")
|
||||||
|
@ -124,7 +143,8 @@ async def create_message(_, info, chatId, body, replyTo=None):
|
||||||
|
|
||||||
chat = await redis.execute("GET", f"chats/{chatId}")
|
chat = await redis.execute("GET", f"chats/{chatId}")
|
||||||
if not chat:
|
if not chat:
|
||||||
return {"error": "chat not exist"}
|
return {
|
||||||
|
"error": "chat not exist"}
|
||||||
|
|
||||||
message_id = await redis.execute("GET", f"chats/{chatId}/next_message_id")
|
message_id = await redis.execute("GET", f"chats/{chatId}/next_message_id")
|
||||||
message_id = int(message_id)
|
message_id = int(message_id)
|
||||||
|
@ -154,7 +174,10 @@ async def create_message(_, info, chatId, body, replyTo=None):
|
||||||
result = MessageResult("NEW", new_message)
|
result = MessageResult("NEW", new_message)
|
||||||
await MessagesStorage.put(result)
|
await MessagesStorage.put(result)
|
||||||
|
|
||||||
return {"message": new_message}
|
return {
|
||||||
|
"message": new_message,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@query.field("loadChat")
|
@query.field("loadChat")
|
||||||
|
@ -166,7 +189,10 @@ async def get_messages(_, info, chatId, size, page):
|
||||||
|
|
||||||
messages = await load_messages(chatId, size, page)
|
messages = await load_messages(chatId, size, page)
|
||||||
|
|
||||||
return messages
|
return {
|
||||||
|
"messages": messages,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("updateMessage")
|
@mutation.field("updateMessage")
|
||||||
|
@ -194,7 +220,10 @@ async def update_message(_, info, chatId, id, body):
|
||||||
result = MessageResult("UPDATED", message)
|
result = MessageResult("UPDATED", message)
|
||||||
await MessagesStorage.put(result)
|
await MessagesStorage.put(result)
|
||||||
|
|
||||||
return {"message": message}
|
return {
|
||||||
|
"message": message,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("deleteMessage")
|
@mutation.field("deleteMessage")
|
||||||
|
@ -244,7 +273,9 @@ async def mark_as_read(_, info, chatId, ids):
|
||||||
for id in ids:
|
for id in ids:
|
||||||
await redis.execute("LREM", f"chats/{chatId}/unread/{user.slug}", 0, str(id))
|
await redis.execute("LREM", f"chats/{chatId}/unread/{user.slug}", 0, str(id))
|
||||||
|
|
||||||
return {}
|
return {
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@subscription.source("chatUpdated")
|
@subscription.source("chatUpdated")
|
||||||
|
|
|
@ -2,38 +2,12 @@ scalar DateTime
|
||||||
|
|
||||||
################################### Payload ###################################
|
################################### Payload ###################################
|
||||||
|
|
||||||
type MessageResult {
|
|
||||||
error: String
|
|
||||||
message: Message
|
|
||||||
}
|
|
||||||
|
|
||||||
enum MessageStatus {
|
enum MessageStatus {
|
||||||
NEW
|
NEW
|
||||||
UPDATED
|
UPDATED
|
||||||
DELETED
|
DELETED
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatUpdatedResult {
|
|
||||||
error: String
|
|
||||||
status: MessageStatus
|
|
||||||
message: Message
|
|
||||||
}
|
|
||||||
|
|
||||||
type EnterChatResult {
|
|
||||||
chat: Chat
|
|
||||||
messages: [Message]
|
|
||||||
error: String
|
|
||||||
}
|
|
||||||
|
|
||||||
type ChatResult {
|
|
||||||
error: String
|
|
||||||
members: [User]!
|
|
||||||
createdAt: DateTime!
|
|
||||||
createdBy: User
|
|
||||||
messages: [Message]
|
|
||||||
title: String
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserFollowings {
|
type UserFollowings {
|
||||||
unread: Int
|
unread: Int
|
||||||
topics: [String]
|
topics: [String]
|
||||||
|
@ -49,8 +23,22 @@ type AuthResult {
|
||||||
news: UserFollowings
|
news: UserFollowings
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChatMember {
|
||||||
|
slug: String!
|
||||||
|
name: String!
|
||||||
|
pic: String
|
||||||
|
invitedAt: DateTime
|
||||||
|
invitedBy: String # user slug
|
||||||
|
# TODO: add more
|
||||||
|
}
|
||||||
|
|
||||||
type Result {
|
type Result {
|
||||||
error: String
|
error: String
|
||||||
|
chat: Chat
|
||||||
|
chats: [Chat]
|
||||||
|
message: Message
|
||||||
|
messages: [Message]
|
||||||
|
members: [ChatMember]
|
||||||
shout: Shout
|
shout: Shout
|
||||||
shouts: [Shout]
|
shouts: [Shout]
|
||||||
author: User
|
author: User
|
||||||
|
@ -142,8 +130,8 @@ type Mutation {
|
||||||
# inbox
|
# inbox
|
||||||
createChat(description: String): Chat!
|
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): Result!
|
||||||
updateMessage(chatId: String!, id: Int!, body: String!): MessageResult!
|
updateMessage(chatId: String!, id: Int!, body: String!): Result!
|
||||||
deleteMessage(chatId: String!, id: Int!): Result!
|
deleteMessage(chatId: String!, id: Int!): Result!
|
||||||
markAsRead(chatId: String!, ids: [Int]!): Result!
|
markAsRead(chatId: String!, ids: [Int]!): Result!
|
||||||
|
|
||||||
|
@ -201,8 +189,8 @@ type Mutation {
|
||||||
|
|
||||||
type Query {
|
type Query {
|
||||||
# inbox
|
# inbox
|
||||||
myChats: [ChatResult]!
|
myChats: [Chat]!
|
||||||
enterChat(chatId: String!): ChatResult!
|
enterChat(chatId: String!): Chat!
|
||||||
loadChat(chatId: String!, size: Int!, page: Int!): [Message]!
|
loadChat(chatId: String!, size: Int!, page: Int!): [Message]!
|
||||||
|
|
||||||
# auth
|
# auth
|
||||||
|
|
Loading…
Reference in New Issue
Block a user