try-to-sse
This commit is contained in:
parent
c89d226dee
commit
00054a94ec
|
@ -27,3 +27,4 @@ python-dateutil~=2.8.2
|
||||||
beautifulsoup4~=4.11.1
|
beautifulsoup4~=4.11.1
|
||||||
lxml
|
lxml
|
||||||
sentry-sdk>=0.10.2
|
sentry-sdk>=0.10.2
|
||||||
|
sse_starlette
|
||||||
|
|
|
@ -119,7 +119,6 @@ async def load_recipients(_, info, limit=50, offset=0):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
chat_users += await followed_authors(auth.user_id)
|
chat_users += await followed_authors(auth.user_id)
|
||||||
print("[resolvers.inbox] ")
|
|
||||||
limit = limit - len(chat_users)
|
limit = limit - len(chat_users)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -143,10 +143,14 @@ async def mark_as_read(_, info, chat_id: str, messages: [int]):
|
||||||
@subscription.source("newMessage")
|
@subscription.source("newMessage")
|
||||||
@login_required
|
@login_required
|
||||||
async def message_generator(obj, info):
|
async def message_generator(obj, info):
|
||||||
try:
|
print(f"[resolvers.messages] generator {info}")
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
|
return await messages_generator_by_user(auth.user_id)
|
||||||
|
|
||||||
user_following_chats = await redis.execute("GET", f"chats_by_user/{auth.user_id}")
|
|
||||||
|
async def messages_generator_by_user(user_id):
|
||||||
|
try:
|
||||||
|
user_following_chats = await redis.execute("GET", f"chats_by_user/{user_id}")
|
||||||
if user_following_chats:
|
if user_following_chats:
|
||||||
user_following_chats = list(json.loads(user_following_chats)) # chat ids
|
user_following_chats = list(json.loads(user_following_chats)) # chat ids
|
||||||
else:
|
else:
|
||||||
|
@ -170,3 +174,4 @@ async def message_generator(obj, info):
|
||||||
yield msg
|
yield msg
|
||||||
finally:
|
finally:
|
||||||
await MessagesStorage.remove_chat(following_chat)
|
await MessagesStorage.remove_chat(following_chat)
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
from sse_starlette.sse import EventSourceResponse
|
from sse_starlette.sse import EventSourceResponse
|
||||||
from resolvers.inbox.messages import message_generator
|
from starlette.requests import Request
|
||||||
|
from resolvers.inbox.messages import messages_generator_by_user
|
||||||
|
from base.exceptions import Unauthorized
|
||||||
|
|
||||||
|
|
||||||
async def sse_messages(request):
|
async def sse_messages(request: Request):
|
||||||
print(f'[SSE] {request}')
|
print(f'[SSE] {request.scope}')
|
||||||
# https://github.com/enisdenjo/graphql-sse/blob/master/PROTOCOL.md
|
# https://github.com/enisdenjo/graphql-sse/blob/master/PROTOCOL.md
|
||||||
|
if request['user']:
|
||||||
return EventSourceResponse(message_generator)
|
return EventSourceResponse(messages_generator_by_user(request['user'].user_id))
|
||||||
|
else:
|
||||||
|
raise Unauthorized("Please login")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user