Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
577 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import typing | ||
|
||
import strawberry | ||
from channels.db import database_sync_to_async | ||
from django.conf import settings | ||
from strawberry.types import Info | ||
|
||
from server.app.blog import models as blog_models | ||
from server.app.blog.graph import types as blog_types | ||
|
||
|
||
@database_sync_to_async | ||
def _get_post(id: str) -> blog_models.Post | None: # noqa: A002 | ||
try: | ||
return blog_models.Post.objects.get(id=id) | ||
except blog_models.Post.DoesNotExist: | ||
return None | ||
|
||
|
||
@strawberry.type | ||
class Subscription: | ||
@strawberry.subscription | ||
async def post_notify( | ||
self, | ||
info: Info, | ||
) -> typing.AsyncGenerator[blog_types.PostNotification, None]: | ||
ws = info.context["ws"] | ||
channel_layer = ws.channel_layer | ||
await channel_layer.group_add(settings.POSTS_CHANNEL, ws.channel_name) | ||
|
||
async with ws.listen_to_channel( | ||
"chat.message", | ||
groups=[settings.POSTS_CHANNEL], | ||
) as cm: | ||
async for message in cm: | ||
post = await _get_post(message["post_id"]) | ||
if post: | ||
yield blog_types.PostNotification( | ||
id=post.pk, | ||
title=post.title, | ||
publish_at=post.published_at, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters