53 lines
2.3 KiB
Python
53 lines
2.3 KiB
Python
from typing import AsyncGenerator
|
|
import strawberry
|
|
from channels.db import database_sync_to_async
|
|
from strawberry.types import Info
|
|
from core.graphql.pubsub import pubsub
|
|
from core.graphql.types.account import AccountAddressType
|
|
from core.graphql.utils import _extract_id
|
|
from core.models.account import AccountAddress
|
|
|
|
|
|
@strawberry.type
|
|
class Subscription:
|
|
@strawberry.subscription(description="Subscribe to account address creation events")
|
|
async def account_address_created(self, info: Info) -> AsyncGenerator[AccountAddressType, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("account_address_created") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
try:
|
|
instance = await database_sync_to_async(AccountAddress.objects.get)(pk=entity_id)
|
|
except AccountAddress.DoesNotExist:
|
|
continue
|
|
yield instance
|
|
|
|
@strawberry.subscription(description="Subscribe to account address updates")
|
|
async def account_address_updated(self, info: Info) -> AsyncGenerator[AccountAddressType, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("account_address_updated") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
try:
|
|
instance = await database_sync_to_async(AccountAddress.objects.get)(pk=entity_id)
|
|
except AccountAddress.DoesNotExist:
|
|
continue
|
|
yield instance
|
|
|
|
@strawberry.subscription(description="Subscribe to account address deletion events")
|
|
async def account_address_deleted(self, info: Info) -> AsyncGenerator[strawberry.ID, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("account_address_deleted") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
yield strawberry.ID(entity_id)
|