pondsocket 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pondsocket/__init__.py +205 -0
- pondsocket/channel.py +580 -0
- pondsocket/contexts/__init__.py +16 -0
- pondsocket/contexts/connection_context.py +135 -0
- pondsocket/contexts/event_context.py +127 -0
- pondsocket/contexts/join_context.py +162 -0
- pondsocket/contexts/leave_context.py +78 -0
- pondsocket/contexts/outgoing_context.py +72 -0
- pondsocket/distributed/__init__.py +5 -0
- pondsocket/distributed/redis_pubsub.py +137 -0
- pondsocket/endpoint.py +348 -0
- pondsocket/errors.py +90 -0
- pondsocket/hooks.py +227 -0
- pondsocket/in_memory_transport.py +112 -0
- pondsocket/lobby.py +183 -0
- pondsocket/middleware.py +100 -0
- pondsocket/parser.py +85 -0
- pondsocket/presence.py +109 -0
- pondsocket/pubsub.py +44 -0
- pondsocket/pubsub_local.py +85 -0
- pondsocket/py.typed +0 -0
- pondsocket/server.py +88 -0
- pondsocket/sse_transport.py +122 -0
- pondsocket/store.py +83 -0
- pondsocket/transport.py +29 -0
- pondsocket/types.py +154 -0
- pondsocket/wire.py +87 -0
- pondsocket-0.0.1.dist-info/METADATA +144 -0
- pondsocket-0.0.1.dist-info/RECORD +30 -0
- pondsocket-0.0.1.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from enum import StrEnum
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from pondsocket_common import Headers, IncomingConnection, PondAssigns, PondMessage
|
|
7
|
+
|
|
8
|
+
from ..errors import bad_request
|
|
9
|
+
from ..types import Route, SystemEntity
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ConnectionDecision(StrEnum):
|
|
13
|
+
PENDING = "PENDING"
|
|
14
|
+
ACCEPTED = "ACCEPTED"
|
|
15
|
+
DECLINED = "DECLINED"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ConnectionContext:
|
|
19
|
+
__slots__ = (
|
|
20
|
+
"_assigns",
|
|
21
|
+
"_decision",
|
|
22
|
+
"_decline_code",
|
|
23
|
+
"_decline_message",
|
|
24
|
+
"_pending_reply",
|
|
25
|
+
"_user_id",
|
|
26
|
+
"request",
|
|
27
|
+
"route",
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
*,
|
|
33
|
+
user_id: str,
|
|
34
|
+
request: IncomingConnection,
|
|
35
|
+
route: Route | None = None,
|
|
36
|
+
) -> None:
|
|
37
|
+
self._user_id = user_id
|
|
38
|
+
self.request = request
|
|
39
|
+
self.route = route or Route()
|
|
40
|
+
self._assigns: PondAssigns = {}
|
|
41
|
+
self._decision: ConnectionDecision = ConnectionDecision.PENDING
|
|
42
|
+
self._decline_code: int = 0
|
|
43
|
+
self._decline_message: str = ""
|
|
44
|
+
self._pending_reply: tuple[str, PondMessage] | None = None
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def user_id(self) -> str:
|
|
48
|
+
return self._user_id
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def headers(self) -> Headers:
|
|
52
|
+
return self.request.headers
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def cookies(self) -> dict[str, str]:
|
|
56
|
+
return self.request.cookies
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def query(self) -> dict[str, str]:
|
|
60
|
+
return self.request.query
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def params(self) -> dict[str, str]:
|
|
64
|
+
return self.request.params
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def address(self) -> str:
|
|
68
|
+
return self.request.address
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def assigns(self) -> PondAssigns:
|
|
72
|
+
return dict(self._assigns)
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def decision(self) -> ConnectionDecision:
|
|
76
|
+
return self._decision
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def is_accepted(self) -> bool:
|
|
80
|
+
return self._decision is ConnectionDecision.ACCEPTED
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def is_declined(self) -> bool:
|
|
84
|
+
return self._decision is ConnectionDecision.DECLINED
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def decline_info(self) -> tuple[int, str]:
|
|
88
|
+
return (self._decline_code, self._decline_message)
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def pending_reply(self) -> tuple[str, PondMessage] | None:
|
|
92
|
+
return self._pending_reply
|
|
93
|
+
|
|
94
|
+
def accept(self, **assigns: Any) -> ConnectionContext:
|
|
95
|
+
if self._decision is not ConnectionDecision.PENDING:
|
|
96
|
+
raise bad_request(
|
|
97
|
+
SystemEntity.GATEWAY.value,
|
|
98
|
+
"ConnectionContext: the response has already been sent",
|
|
99
|
+
)
|
|
100
|
+
self._decision = ConnectionDecision.ACCEPTED
|
|
101
|
+
if assigns:
|
|
102
|
+
self._assigns.update(assigns)
|
|
103
|
+
return self
|
|
104
|
+
|
|
105
|
+
def decline(self, code: int, message: str) -> None:
|
|
106
|
+
if self._decision is not ConnectionDecision.PENDING:
|
|
107
|
+
raise bad_request(
|
|
108
|
+
SystemEntity.GATEWAY.value,
|
|
109
|
+
"ConnectionContext: the response has already been sent",
|
|
110
|
+
)
|
|
111
|
+
self._decision = ConnectionDecision.DECLINED
|
|
112
|
+
self._decline_code = code
|
|
113
|
+
self._decline_message = message
|
|
114
|
+
|
|
115
|
+
def reply(self, event_name: str, payload: PondMessage | None = None) -> ConnectionContext:
|
|
116
|
+
if self._decision is ConnectionDecision.PENDING:
|
|
117
|
+
self.accept()
|
|
118
|
+
if self._decision is ConnectionDecision.DECLINED:
|
|
119
|
+
raise bad_request(
|
|
120
|
+
SystemEntity.GATEWAY.value,
|
|
121
|
+
"ConnectionContext: cannot reply after declining",
|
|
122
|
+
)
|
|
123
|
+
self._pending_reply = (event_name, payload or {})
|
|
124
|
+
return self
|
|
125
|
+
|
|
126
|
+
def set_assign(self, key: str, value: Any) -> ConnectionContext:
|
|
127
|
+
self._assigns[key] = value
|
|
128
|
+
return self
|
|
129
|
+
|
|
130
|
+
def set_assigns(self, **assigns: Any) -> ConnectionContext:
|
|
131
|
+
self._assigns.update(assigns)
|
|
132
|
+
return self
|
|
133
|
+
|
|
134
|
+
def get_assign(self, key: str) -> Any:
|
|
135
|
+
return self._assigns.get(key)
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING, Any
|
|
4
|
+
|
|
5
|
+
from pondsocket_common import PondAssigns, PondMessage, PondPresence
|
|
6
|
+
|
|
7
|
+
from ..types import Event, MessageEvent, Route, User
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from ..channel import Channel
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class EventContext:
|
|
14
|
+
__slots__ = ("_replied", "channel", "event", "route", "user")
|
|
15
|
+
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
*,
|
|
19
|
+
channel: Channel,
|
|
20
|
+
message: MessageEvent,
|
|
21
|
+
route: Route | None = None,
|
|
22
|
+
) -> None:
|
|
23
|
+
self.channel = channel
|
|
24
|
+
self.event: Event = message.event
|
|
25
|
+
self.user: User = message.user
|
|
26
|
+
self.route = route or Route()
|
|
27
|
+
self._replied: bool = False
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def request_id(self) -> str:
|
|
31
|
+
return self.event.request_id
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def event_name(self) -> str:
|
|
35
|
+
return self.event.event
|
|
36
|
+
|
|
37
|
+
def get_payload(self) -> Any:
|
|
38
|
+
return self.event.payload
|
|
39
|
+
|
|
40
|
+
def get_user(self) -> User:
|
|
41
|
+
return self.user
|
|
42
|
+
|
|
43
|
+
def has_replied(self) -> bool:
|
|
44
|
+
return self._replied
|
|
45
|
+
|
|
46
|
+
async def reply(self, event_name: str, payload: PondMessage) -> EventContext:
|
|
47
|
+
if self._replied:
|
|
48
|
+
return self
|
|
49
|
+
self._replied = True
|
|
50
|
+
await self.channel.send_system(
|
|
51
|
+
event_name,
|
|
52
|
+
payload,
|
|
53
|
+
request_id=self.event.request_id,
|
|
54
|
+
user_ids=[self.user.id],
|
|
55
|
+
)
|
|
56
|
+
return self
|
|
57
|
+
|
|
58
|
+
async def broadcast(self, event_name: str, payload: PondMessage) -> EventContext:
|
|
59
|
+
await self.channel.broadcast(event_name, payload)
|
|
60
|
+
return self
|
|
61
|
+
|
|
62
|
+
async def broadcast_to(
|
|
63
|
+
self,
|
|
64
|
+
event_name: str,
|
|
65
|
+
payload: PondMessage,
|
|
66
|
+
*user_ids: str,
|
|
67
|
+
) -> EventContext:
|
|
68
|
+
await self.channel.broadcast_to(event_name, payload, *user_ids)
|
|
69
|
+
return self
|
|
70
|
+
|
|
71
|
+
async def broadcast_from(
|
|
72
|
+
self,
|
|
73
|
+
event_name: str,
|
|
74
|
+
payload: PondMessage,
|
|
75
|
+
) -> EventContext:
|
|
76
|
+
await self.channel.broadcast_from(event_name, payload, self.user.id)
|
|
77
|
+
return self
|
|
78
|
+
|
|
79
|
+
async def track(
|
|
80
|
+
self,
|
|
81
|
+
presence: PondPresence,
|
|
82
|
+
*user_ids: str,
|
|
83
|
+
) -> EventContext:
|
|
84
|
+
targets = list(user_ids) or [self.user.id]
|
|
85
|
+
for uid in targets:
|
|
86
|
+
await self.channel.track_presence(uid, presence)
|
|
87
|
+
return self
|
|
88
|
+
|
|
89
|
+
async def update_presence(
|
|
90
|
+
self,
|
|
91
|
+
presence: PondPresence,
|
|
92
|
+
*user_ids: str,
|
|
93
|
+
) -> EventContext:
|
|
94
|
+
targets = list(user_ids) or [self.user.id]
|
|
95
|
+
for uid in targets:
|
|
96
|
+
await self.channel.update_presence(uid, presence)
|
|
97
|
+
return self
|
|
98
|
+
|
|
99
|
+
async def untrack(self, *user_ids: str) -> EventContext:
|
|
100
|
+
targets = list(user_ids) or [self.user.id]
|
|
101
|
+
for uid in targets:
|
|
102
|
+
await self.channel.untrack_presence(uid)
|
|
103
|
+
return self
|
|
104
|
+
|
|
105
|
+
async def evict(self, reason: str, *user_ids: str) -> EventContext:
|
|
106
|
+
targets = list(user_ids) or [self.user.id]
|
|
107
|
+
for uid in targets:
|
|
108
|
+
await self.channel.evict_user(uid, reason)
|
|
109
|
+
return self
|
|
110
|
+
|
|
111
|
+
async def set_assign(self, key: str, value: Any) -> EventContext:
|
|
112
|
+
await self.channel.update_assign(self.user.id, key, value)
|
|
113
|
+
return self
|
|
114
|
+
|
|
115
|
+
async def assign(self, assigns: PondAssigns) -> EventContext:
|
|
116
|
+
for k, v in assigns.items():
|
|
117
|
+
await self.channel.update_assign(self.user.id, k, v)
|
|
118
|
+
return self
|
|
119
|
+
|
|
120
|
+
async def get_assign(self, key: str) -> Any:
|
|
121
|
+
return await self.channel.get_user_assign(self.user.id, key)
|
|
122
|
+
|
|
123
|
+
async def get_all_presence(self) -> dict[str, PondPresence]:
|
|
124
|
+
return await self.channel.get_presence()
|
|
125
|
+
|
|
126
|
+
async def get_all_assigns(self) -> dict[str, PondAssigns]:
|
|
127
|
+
return await self.channel.get_assigns()
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from typing import TYPE_CHECKING, Any
|
|
5
|
+
|
|
6
|
+
from pondsocket_common import (
|
|
7
|
+
JoinParams,
|
|
8
|
+
PondAssigns,
|
|
9
|
+
PondMessage,
|
|
10
|
+
PondPresence,
|
|
11
|
+
ServerActions,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
from ..errors import bad_request
|
|
15
|
+
from ..types import Event, Route, SystemEvents
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from ..channel import Channel
|
|
19
|
+
from ..transport import Transport
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class JoinContext:
|
|
23
|
+
__slots__ = (
|
|
24
|
+
"_accepted",
|
|
25
|
+
"_lock",
|
|
26
|
+
"_responded",
|
|
27
|
+
"channel",
|
|
28
|
+
"event",
|
|
29
|
+
"route",
|
|
30
|
+
"transport",
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
*,
|
|
36
|
+
channel: Channel,
|
|
37
|
+
event: Event,
|
|
38
|
+
transport: Transport,
|
|
39
|
+
route: Route | None = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
self.channel = channel
|
|
42
|
+
self.event = event
|
|
43
|
+
self.transport = transport
|
|
44
|
+
self.route = route or Route()
|
|
45
|
+
self._responded: bool = False
|
|
46
|
+
self._accepted: bool = False
|
|
47
|
+
self._lock = asyncio.Lock()
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def has_responded(self) -> bool:
|
|
51
|
+
return self._responded
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def is_accepted(self) -> bool:
|
|
55
|
+
return self._accepted
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def join_params(self) -> JoinParams:
|
|
59
|
+
payload = self.event.payload
|
|
60
|
+
if isinstance(payload, dict):
|
|
61
|
+
return payload
|
|
62
|
+
return {}
|
|
63
|
+
|
|
64
|
+
def get_join_params(self) -> JoinParams:
|
|
65
|
+
return self.join_params
|
|
66
|
+
|
|
67
|
+
async def accept(self, assigns: PondAssigns | None = None) -> JoinContext:
|
|
68
|
+
async with self._lock:
|
|
69
|
+
if self._responded:
|
|
70
|
+
raise bad_request(self.channel.name, "Already responded to the join request")
|
|
71
|
+
self._responded = True
|
|
72
|
+
self._accepted = True
|
|
73
|
+
if assigns:
|
|
74
|
+
for k, v in assigns.items():
|
|
75
|
+
self.transport.set_assign(k, v)
|
|
76
|
+
await self.channel.add_user(self.transport)
|
|
77
|
+
await self.channel.send_acknowledge(
|
|
78
|
+
self.event.request_id,
|
|
79
|
+
self.transport.get_id(),
|
|
80
|
+
)
|
|
81
|
+
return self
|
|
82
|
+
|
|
83
|
+
async def decline(self, code: int, message: str) -> None:
|
|
84
|
+
async with self._lock:
|
|
85
|
+
if self._responded:
|
|
86
|
+
raise bad_request(self.channel.name, "Already responded to the join request")
|
|
87
|
+
self._responded = True
|
|
88
|
+
self._accepted = False
|
|
89
|
+
ev = Event(
|
|
90
|
+
action=ServerActions.SYSTEM.value,
|
|
91
|
+
channel_name=self.channel.name,
|
|
92
|
+
request_id=self.event.request_id,
|
|
93
|
+
event=SystemEvents.UNAUTHORIZED.value,
|
|
94
|
+
payload={"code": code, "message": message},
|
|
95
|
+
)
|
|
96
|
+
try:
|
|
97
|
+
await self.transport.send_event(ev)
|
|
98
|
+
except Exception:
|
|
99
|
+
return
|
|
100
|
+
|
|
101
|
+
async def reply(self, event_name: str, payload: PondMessage) -> JoinContext:
|
|
102
|
+
if not self._accepted:
|
|
103
|
+
raise bad_request(self.channel.name, "Cannot reply before accepting join request")
|
|
104
|
+
await self.channel.send_system(
|
|
105
|
+
event_name,
|
|
106
|
+
payload,
|
|
107
|
+
request_id=self.event.request_id,
|
|
108
|
+
user_ids=[self.transport.get_id()],
|
|
109
|
+
)
|
|
110
|
+
return self
|
|
111
|
+
|
|
112
|
+
async def track(self, presence: PondPresence) -> JoinContext:
|
|
113
|
+
if not self._accepted:
|
|
114
|
+
raise bad_request(self.channel.name, "Cannot track presence before accepting join request")
|
|
115
|
+
await self.channel.track_presence(self.transport.get_id(), presence)
|
|
116
|
+
return self
|
|
117
|
+
|
|
118
|
+
async def broadcast(self, event_name: str, payload: PondMessage) -> JoinContext:
|
|
119
|
+
if not self._accepted:
|
|
120
|
+
raise bad_request(self.channel.name, "Cannot broadcast before accepting join request")
|
|
121
|
+
await self.channel.broadcast(event_name, payload)
|
|
122
|
+
return self
|
|
123
|
+
|
|
124
|
+
async def broadcast_to(
|
|
125
|
+
self,
|
|
126
|
+
event_name: str,
|
|
127
|
+
payload: PondMessage,
|
|
128
|
+
*user_ids: str,
|
|
129
|
+
) -> JoinContext:
|
|
130
|
+
if not self._accepted:
|
|
131
|
+
raise bad_request(self.channel.name, "Cannot broadcast before accepting join request")
|
|
132
|
+
await self.channel.broadcast_to(event_name, payload, *user_ids)
|
|
133
|
+
return self
|
|
134
|
+
|
|
135
|
+
async def broadcast_from(self, event_name: str, payload: PondMessage) -> JoinContext:
|
|
136
|
+
if not self._accepted:
|
|
137
|
+
raise bad_request(self.channel.name, "Cannot broadcast before accepting join request")
|
|
138
|
+
await self.channel.broadcast_from(event_name, payload, self.transport.get_id())
|
|
139
|
+
return self
|
|
140
|
+
|
|
141
|
+
async def set_assign(self, key: str, value: Any) -> JoinContext:
|
|
142
|
+
if not self._accepted:
|
|
143
|
+
self.transport.set_assign(key, value)
|
|
144
|
+
else:
|
|
145
|
+
await self.channel.update_assign(self.transport.get_id(), key, value)
|
|
146
|
+
return self
|
|
147
|
+
|
|
148
|
+
async def assign(self, assigns: PondAssigns) -> JoinContext:
|
|
149
|
+
for k, v in assigns.items():
|
|
150
|
+
await self.set_assign(k, v)
|
|
151
|
+
return self
|
|
152
|
+
|
|
153
|
+
async def get_assign(self, key: str) -> Any:
|
|
154
|
+
if not self._accepted:
|
|
155
|
+
return self.transport.get_assign(key)
|
|
156
|
+
return await self.channel.get_user_assign(self.transport.get_id(), key)
|
|
157
|
+
|
|
158
|
+
async def get_all_presence(self) -> dict[str, PondPresence]:
|
|
159
|
+
return await self.channel.get_presence()
|
|
160
|
+
|
|
161
|
+
async def get_all_assigns(self) -> dict[str, PondAssigns]:
|
|
162
|
+
return await self.channel.get_assigns()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from pondsocket_common import PondAssigns, PondMessage, PondPresence
|
|
6
|
+
|
|
7
|
+
from ..errors import bad_request
|
|
8
|
+
from ..types import Route, User
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from ..channel import Channel
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class LeaveContext:
|
|
15
|
+
__slots__ = ("_responded", "channel", "reason", "route", "user")
|
|
16
|
+
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
*,
|
|
20
|
+
channel: Channel,
|
|
21
|
+
user: User,
|
|
22
|
+
reason: str,
|
|
23
|
+
route: Route | None = None,
|
|
24
|
+
) -> None:
|
|
25
|
+
self.channel = channel
|
|
26
|
+
self.user = user
|
|
27
|
+
self.reason = reason
|
|
28
|
+
self.route = route or Route()
|
|
29
|
+
self._responded: bool = False
|
|
30
|
+
|
|
31
|
+
def get_reason(self) -> str:
|
|
32
|
+
return self.reason
|
|
33
|
+
|
|
34
|
+
def get_user(self) -> User:
|
|
35
|
+
return self.user
|
|
36
|
+
|
|
37
|
+
def get_assign(self, key: str) -> object:
|
|
38
|
+
if not self.user.assigns:
|
|
39
|
+
return None
|
|
40
|
+
return self.user.assigns.get(key)
|
|
41
|
+
|
|
42
|
+
async def remaining_user_count(self) -> int:
|
|
43
|
+
return await self.channel.user_count()
|
|
44
|
+
|
|
45
|
+
async def get_all_presence(self) -> dict[str, PondPresence]:
|
|
46
|
+
return await self.channel.get_presence()
|
|
47
|
+
|
|
48
|
+
async def get_all_assigns(self) -> dict[str, PondAssigns]:
|
|
49
|
+
return await self.channel.get_assigns()
|
|
50
|
+
|
|
51
|
+
def _claim_response(self) -> None:
|
|
52
|
+
if self._responded:
|
|
53
|
+
raise bad_request(self.channel.name, "Already broadcast leave notification")
|
|
54
|
+
self._responded = True
|
|
55
|
+
|
|
56
|
+
async def broadcast(self, event_name: str, payload: PondMessage) -> LeaveContext:
|
|
57
|
+
self._claim_response()
|
|
58
|
+
await self.channel.broadcast(event_name, payload)
|
|
59
|
+
return self
|
|
60
|
+
|
|
61
|
+
async def broadcast_to(
|
|
62
|
+
self,
|
|
63
|
+
event_name: str,
|
|
64
|
+
payload: PondMessage,
|
|
65
|
+
*user_ids: str,
|
|
66
|
+
) -> LeaveContext:
|
|
67
|
+
self._claim_response()
|
|
68
|
+
await self.channel.broadcast_to(event_name, payload, *user_ids)
|
|
69
|
+
return self
|
|
70
|
+
|
|
71
|
+
async def broadcast_from(
|
|
72
|
+
self,
|
|
73
|
+
event_name: str,
|
|
74
|
+
payload: PondMessage,
|
|
75
|
+
) -> LeaveContext:
|
|
76
|
+
self._claim_response()
|
|
77
|
+
await self.channel.broadcast_from(event_name, payload, self.user.id)
|
|
78
|
+
return self
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING, Any
|
|
4
|
+
|
|
5
|
+
from pondsocket_common import PondMessage
|
|
6
|
+
|
|
7
|
+
from ..types import Event, Route, User
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from ..channel import Channel
|
|
11
|
+
from ..transport import Transport
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class OutgoingContext:
|
|
15
|
+
__slots__ = (
|
|
16
|
+
"_blocked",
|
|
17
|
+
"_transformed",
|
|
18
|
+
"channel",
|
|
19
|
+
"event",
|
|
20
|
+
"route",
|
|
21
|
+
"transport",
|
|
22
|
+
"user",
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
*,
|
|
28
|
+
channel: Channel,
|
|
29
|
+
event: Event,
|
|
30
|
+
user: User,
|
|
31
|
+
transport: Transport,
|
|
32
|
+
route: Route | None = None,
|
|
33
|
+
) -> None:
|
|
34
|
+
self.channel = channel
|
|
35
|
+
self.event = event
|
|
36
|
+
self.user = user
|
|
37
|
+
self.transport = transport
|
|
38
|
+
self.route = route or Route()
|
|
39
|
+
self._blocked: bool = False
|
|
40
|
+
self._transformed: bool = False
|
|
41
|
+
|
|
42
|
+
def block(self) -> OutgoingContext:
|
|
43
|
+
self._blocked = True
|
|
44
|
+
return self
|
|
45
|
+
|
|
46
|
+
def unblock(self) -> OutgoingContext:
|
|
47
|
+
self._blocked = False
|
|
48
|
+
return self
|
|
49
|
+
|
|
50
|
+
def is_blocked(self) -> bool:
|
|
51
|
+
return self._blocked
|
|
52
|
+
|
|
53
|
+
def transform(self, payload: PondMessage) -> OutgoingContext:
|
|
54
|
+
self.event.payload = payload
|
|
55
|
+
self._transformed = True
|
|
56
|
+
return self
|
|
57
|
+
|
|
58
|
+
def has_transformed(self) -> bool:
|
|
59
|
+
return self._transformed
|
|
60
|
+
|
|
61
|
+
def get_payload(self) -> Any:
|
|
62
|
+
return self.event.payload
|
|
63
|
+
|
|
64
|
+
def get_event(self) -> str:
|
|
65
|
+
return self.event.event
|
|
66
|
+
|
|
67
|
+
def get_user(self) -> User:
|
|
68
|
+
return self.user
|
|
69
|
+
|
|
70
|
+
async def refresh_user(self) -> OutgoingContext:
|
|
71
|
+
self.user = await self.channel.get_user(self.user.id)
|
|
72
|
+
return self
|