-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
service.py
88 lines (72 loc) · 3.28 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from __future__ import annotations
import asyncio
from contextlib import suppress
from typing import TYPE_CHECKING
from loguru import logger
from satori.client.account import Account
from satori.client import App
from satori.model import Event, LoginStatus
from avilla.core.account import AccountInfo
from avilla.standard.core.account import (
AccountAvailable,
AccountRegistered,
AccountUnavailable,
AccountUnregistered,
)
from ..core import Selector
from ..core.ryanvk.staff import Staff
from .account import SatoriAccount
from .capability import SatoriCapability
from .const import platform
if TYPE_CHECKING:
from .protocol import SatoriProtocol
class SatoriService(App):
id = "satori.service"
protocol: SatoriProtocol
_accounts: dict[str, SatoriAccount]
def __init__(self, protocol: SatoriProtocol):
self.protocol = protocol
self._accounts = {}
super().__init__()
self.register(self.handle_event)
self.lifecycle(self.handle_lifecycle)
def get_staff_components(self):
return {"protocol": self.protocol, "avilla": self.protocol.avilla}
def get_staff_artifacts(self):
return [self.protocol.artifacts, self.protocol.avilla.global_artifacts]
@property
def staff(self):
return Staff(self.get_staff_artifacts(), self.get_staff_components())
async def handle_event(self, account: Account, event: Event):
async def event_parse_task(connection: Account, raw: Event):
with suppress(NotImplementedError):
await SatoriCapability(self.staff.ext({"connection": connection})).handle_event(raw)
return
logger.warning(f"received unsupported event {raw.type}: {raw}")
asyncio.create_task(event_parse_task(account, event))
async def handle_lifecycle(self, account: Account, state: LoginStatus):
if state == LoginStatus.ONLINE:
route = Selector().land(account.platform).account(account.self_id)
_account = SatoriAccount(route, self.protocol)
self.protocol.avilla.accounts[route] = AccountInfo(
route, _account, self.protocol, platform(account.platform)
)
self.protocol.avilla.broadcast.postEvent(AccountRegistered(self.protocol.avilla, _account))
self._accounts[account.identity] = _account
_account.client = account
elif state == LoginStatus.CONNECT:
_account = self._accounts[account.identity]
self.protocol.avilla.broadcast.postEvent(AccountAvailable(self.protocol.avilla, _account))
_account.client = account
_account.status.enabled = True
elif state == LoginStatus.DISCONNECT:
_account = self._accounts[account.identity]
_account.status.enabled = False
self.protocol.avilla.broadcast.postEvent(AccountUnavailable(self.protocol.avilla, _account))
elif state == LoginStatus.OFFLINE:
_account = self._accounts[account.identity]
_account.status.enabled = False
self.protocol.avilla.broadcast.postEvent(AccountUnregistered(self.protocol.avilla, _account))
with suppress(KeyError):
del self._accounts[account.identity]
del self.protocol.avilla.accounts[_account.route]