35
35
from .middleware .auth import AuthBackend
36
36
from .routes .applications import Applications
37
37
from .routes .auth import Auth
38
+ from .routes .members import Members
38
39
from .routes .users import Users
39
40
40
41
@@ -43,15 +44,15 @@ def __init__(self, *, session: aiohttp.ClientSession, database: core.Database) -
43
44
self .session = session
44
45
self .database = database
45
46
46
- views : list [core .View ] = [Users (self ), Auth (self ), Applications (self )]
47
+ views : list [core .View ] = [Users (self ), Auth (self ), Applications (self ), Members ( self ) ]
47
48
middleware : list [Middleware ] = [
48
49
Middleware (CORSMiddleware , allow_origins = ['*' ], allow_methods = ['*' ], allow_headers = ['*' ]),
49
50
Middleware (AuthenticationMiddleware , backend = AuthBackend (self )),
50
51
]
51
52
52
53
self .sockets : dict [int , WebSocket ] = {}
53
54
self .subscription_sockets : dict [str , set [int ]] = {
54
- 'discord_py_mod_log' : set ()
55
+ core . WebsocketSubscriptions . DPY_MOD_LOG : set ()
55
56
}
56
57
57
58
super ().__init__ (
@@ -82,7 +83,7 @@ async def websocket_connector(self, websocket: WebSocket) -> None:
82
83
83
84
# Send the initial accepted response. Includes user_id and subscriptions... op: 0
84
85
data : dict [str , Any ] = {
85
- 'op' : core .WebsocketOPCodes .ACCEPTED ,
86
+ 'op' : core .WebsocketOPCodes .HELLO ,
86
87
'user_id' : uid ,
87
88
'subscriptions' : subscriptions
88
89
}
@@ -93,14 +94,73 @@ async def websocket_connector(self, websocket: WebSocket) -> None:
93
94
while True :
94
95
95
96
try :
96
- message : dict [str | int , Any ] = await websocket .receive_json ()
97
+ message : dict [str , Any ] = await websocket .receive_json ()
97
98
except WebSocketDisconnect :
98
99
break
99
100
100
- # TODO: Process messages...
101
- print (message )
101
+ op : str | None = message .get ('op' )
102
+
103
+ if op == core .WebsocketOPCodes .SUBSCRIBE :
104
+ response = self .websocket_subscribe (uid = uid , message = message )
105
+ await websocket .send_json (data = response )
106
+
107
+ elif op == core .WebsocketOPCodes .UNSUBSCRIBE :
108
+ response = self .websocket_unsubscribe (uid = uid , message = message )
109
+ await websocket .send_json (data = response )
110
+
111
+ else :
112
+ response = {
113
+ 'op' : core .WebsocketOPCodes .NOTIFICATION ,
114
+ 'type' : core .WebsocketNotificationTypes .UNKNOWN_OP ,
115
+ 'received' : op
116
+ }
117
+ await websocket .send_json (data = response )
102
118
103
119
# Remove the websocket and it's subscriptions...
104
120
del self .sockets [uid ]
105
121
for sub in subscriptions :
106
122
self .subscription_sockets [sub ].remove (uid )
123
+
124
+ def websocket_subscribe (self , * , uid : int , message : dict [str , Any ]) -> dict [str , Any ]:
125
+ subs : list [str ] = message .get ('subscriptions' , [])
126
+
127
+ # Filter out bad subscriptions...
128
+ valid : list [str ] = list (self .subscription_sockets .keys ())
129
+ subscriptions : list [str ] = [sub for sub in subs if sub in valid ]
130
+
131
+ for sub in subscriptions :
132
+ self .subscription_sockets [sub ].add (uid )
133
+
134
+ subscribed : list [str ] = [sub for sub in self .subscription_sockets if uid in self .subscription_sockets [sub ]]
135
+
136
+ data : dict [str , Any ] = {
137
+ 'op' : core .WebsocketOPCodes .NOTIFICATION ,
138
+ 'type' : core .WebsocketNotificationTypes .SUBSCRIPTION_ADDED ,
139
+ 'user_id' : uid ,
140
+ 'added' : subscriptions ,
141
+ 'subscriptions' : subscribed
142
+ }
143
+
144
+ return data
145
+
146
+ def websocket_unsubscribe (self , * , uid : int , message : dict [str , Any ]) -> dict [str , Any ]:
147
+ subs : list [str ] = message .get ('subscriptions' , [])
148
+
149
+ # Filter out bad subscriptions...
150
+ valid : list [str ] = list (self .subscription_sockets .keys ())
151
+ subscriptions : list [str ] = [sub for sub in subs if sub in valid ]
152
+
153
+ for sub in subscriptions :
154
+ self .subscription_sockets [sub ].remove (uid )
155
+
156
+ subscribed : list [str ] = [sub for sub in self .subscription_sockets if uid in self .subscription_sockets [sub ]]
157
+
158
+ data : dict [str , Any ] = {
159
+ 'op' : core .WebsocketOPCodes .NOTIFICATION ,
160
+ 'type' : core .WebsocketNotificationTypes .SUBSCRIPTION_REMOVED ,
161
+ 'user_id' : uid ,
162
+ 'removed' : subscriptions ,
163
+ 'subscriptions' : subscribed
164
+ }
165
+
166
+ return data
0 commit comments