Add all the user request handling
parent: tbd commit: e3bda14
1 | use ; |
2 | |
3 | use Error; |
4 | use ; |
5 | use Version; |
6 | use Serialize; |
7 | use |
8 | , | TcpStream
9 | |
10 | , |
11 | Mutex, |
12 | , |
13 | , | JoinHandle
14 | ; |
15 | use ; |
16 | |
17 | use crate:: |
18 | , | AuthenticationTokenGranter
19 | , |
20 | , |
21 | , | Listeners
22 | |
23 | |
24 | AuthenticationMessage, AuthenticationRequest, AuthenticationResponse, |
25 | TokenExtensionResponse, |
26 | , |
27 | |
28 | RepositoryMessage, RepositoryMessageKind, RepositoryRequest, RepositoryResponse, |
29 | , |
30 | |
31 | UserMessage, UserMessageKind, UserMessageRequest, UserMessageResponse, |
32 | UserRepositoriesResponse, |
33 | , |
34 | MessageKind, |
35 | , |
36 | |
37 | , |
38 | , | Repository
39 | , | User
40 | , |
41 | validate_version, version, |
42 | ; |
43 | |
44 | |
45 | pub task: , |
46 | |
47 | |
48 | |
49 | pub instance: InstanceMeta, |
50 | pub sender: , |
51 | pub task: , |
52 | |
53 | |
54 | /// Represents a connection which hasn't finished the handshake. |
55 | |
56 | pub socket: , |
57 | |
58 | |
59 | |
60 | |
61 | pub connections: , |
62 | pub instance_connections: , |
63 | |
64 | |
65 | pub async |
66 | mut socket: , |
67 | listeners: , |
68 | connections: , |
69 | backend: , |
70 | user_backend: , |
71 | auth_granter: , |
72 | discovery_backend: , |
73 | addr: SocketAddr, |
74 | |
75 | let mut handshaked = false; |
76 | let this_instance = Instance |
77 | url: String from, |
78 | ; |
79 | |
80 | while let Some = socket.next .await |
81 | let message = match message |
82 | Ok => message, |
83 | Err => |
84 | error!; |
85 | continue; |
86 | |
87 | ; |
88 | |
89 | let payload = match message |
90 | => text.into_bytes, | Text
91 | => bytes, | Binary
92 | => continue, | Ping
93 | => continue, | Pong
94 | => | Close
95 | info!; |
96 | |
97 | return; |
98 | |
99 | _ => unreachable!, |
100 | ; |
101 | |
102 | let message = match |
103 | Ok => message, |
104 | Err => |
105 | error!; |
106 | continue; |
107 | |
108 | ; |
109 | |
110 | // info!("Read payload: {}", std::str::from_utf8(&payload).unwrap()); |
111 | |
112 | if let Handshake = message |
113 | match handshake |
114 | => | Initiate
115 | // Send HandshakeMessage::Response |
116 | let message = HandshakeResponse |
117 | identity: this_instance.clone, |
118 | version: version, |
119 | ; |
120 | |
121 | let version_check = validate_version; |
122 | |
123 | let _result = if !version_check |
124 | error! |
125 | "Version compatibility failure! Our Version: {}, Their Version: {}", |
126 | from_str |
127 | .unwrap, |
128 | request.version |
129 | ; |
130 | |
131 | send |
132 | &mut socket, |
133 | Handshake |
134 | success: false, |
135 | , |
136 | |
137 | .await |
138 | else |
139 | send |
140 | &mut socket, |
141 | , | Handshake
142 | |
143 | .await |
144 | ; |
145 | |
146 | continue; |
147 | |
148 | => | Response
149 | // Check version |
150 | let message = if validate_version |
151 | error! |
152 | "Version compatibility failure! Our Version: {}, Their Version: {}", |
153 | version, |
154 | response.version |
155 | ; |
156 | |
157 | HandshakeFinalize |
158 | else |
159 | info!; |
160 | |
161 | HandshakeFinalize |
162 | ; |
163 | |
164 | let _result = send |
165 | &mut socket, |
166 | , | Handshake
167 | |
168 | .await; |
169 | |
170 | continue; |
171 | |
172 | => | Finalize
173 | if !response.success |
174 | error!; |
175 | return; |
176 | |
177 | |
178 | handshaked = true; |
179 | |
180 | // Send HandshakeMessage::Finalize |
181 | let message = HandshakeFinalize ; |
182 | |
183 | let _result = send |
184 | &mut socket, |
185 | , | Handshake
186 | |
187 | .await; |
188 | |
189 | continue; |
190 | |
191 | |
192 | |
193 | |
194 | if !handshaked |
195 | continue; |
196 | |
197 | |
198 | if let Repository = &message |
199 | if repository.target.instance != this_instance |
200 | info!; |
201 | // We need to send this command to a different instance |
202 | |
203 | let mut listener = send_and_get_listener .await; |
204 | |
205 | // Wait for response |
206 | while let Ok = listener.recv .await |
207 | if let Repository |
208 | command: Response, |
209 | .. |
210 | = message |
211 | |
212 | let _result = send .await; |
213 | |
214 | |
215 | continue; |
216 | else |
217 | // This message is targeting this instance |
218 | match &repository.command |
219 | => match request.clone | Request
220 | => | CreateRepository
221 | let mut backend = backend.lock .await; |
222 | let request = request.validate .await.unwrap; |
223 | let response = backend.create_repository .await; |
224 | |
225 | let response = match response |
226 | Ok => response, |
227 | Err => |
228 | error!; |
229 | continue; |
230 | |
231 | ; |
232 | drop; |
233 | |
234 | let _result = send |
235 | &mut socket, |
236 | Repository |
237 | target: repository.target.clone, |
238 | command: Response |
239 | , | CreateRepository
240 | , |
241 | , |
242 | |
243 | .await; |
244 | |
245 | continue; |
246 | |
247 | => | RepositoryFileInspect
248 | let mut backend = backend.lock .await; |
249 | let request = request.validate .await.unwrap; |
250 | let response = backend.repository_file_inspect; |
251 | |
252 | let response = match response |
253 | Ok => response, |
254 | Err => |
255 | error!; |
256 | continue; |
257 | |
258 | ; |
259 | drop; |
260 | |
261 | let _result = send |
262 | &mut socket, |
263 | Repository |
264 | target: repository.target.clone, |
265 | command: Response |
266 | , | RepositoryFileInspection
267 | , |
268 | , |
269 | |
270 | .await; |
271 | |
272 | continue; |
273 | |
274 | => | RepositoryInfo
275 | let mut backend = backend.lock .await; |
276 | let request = request.validate .await.unwrap; |
277 | let response = backend.repository_info .await; |
278 | |
279 | let response = match response |
280 | Ok => response, |
281 | Err => |
282 | error!; |
283 | continue; |
284 | |
285 | ; |
286 | drop; |
287 | |
288 | let _result = send |
289 | &mut socket, |
290 | Repository |
291 | target: repository.target.clone, |
292 | command: Response |
293 | , | RepositoryInfo
294 | , |
295 | , |
296 | |
297 | .await; |
298 | |
299 | continue; |
300 | |
301 | => | IssuesCount
302 | let request = &request.validate .await.unwrap; |
303 | |
304 | let mut backend = backend.lock .await; |
305 | let response = backend.issues_count; |
306 | |
307 | let response = match response |
308 | Ok => response, |
309 | Err => |
310 | error!; |
311 | continue; |
312 | |
313 | ; |
314 | drop; |
315 | |
316 | let _result = send |
317 | &mut socket, |
318 | Repository |
319 | target: repository.target.clone, |
320 | command: Response |
321 | , | IssuesCount
322 | , |
323 | , |
324 | |
325 | .await; |
326 | |
327 | continue; |
328 | |
329 | => | IssueLabels
330 | let request = &request.validate .await.unwrap; |
331 | |
332 | let mut backend = backend.lock .await; |
333 | let response = backend.issue_labels; |
334 | |
335 | let response = match response |
336 | Ok => response, |
337 | Err => |
338 | error!; |
339 | continue; |
340 | |
341 | ; |
342 | drop; |
343 | |
344 | let _result = send |
345 | &mut socket, |
346 | Repository |
347 | target: repository.target.clone, |
348 | command: Response |
349 | , | IssueLabels
350 | , |
351 | , |
352 | |
353 | .await; |
354 | |
355 | continue; |
356 | |
357 | => | Issues
358 | let request = request.validate .await.unwrap; |
359 | |
360 | let mut backend = backend.lock .await; |
361 | let response = backend.issues; |
362 | |
363 | let response = match response |
364 | Ok => response, |
365 | Err => |
366 | error!; |
367 | continue; |
368 | |
369 | ; |
370 | drop; |
371 | |
372 | let _result = send |
373 | &mut socket, |
374 | Repository |
375 | target: repository.target.clone, |
376 | command: Response |
377 | , | Issues
378 | , |
379 | , |
380 | |
381 | .await; |
382 | |
383 | continue; |
384 | |
385 | , |
386 | => | Response
387 | unreachable! |
388 | |
389 | |
390 | |
391 | |
392 | |
393 | if let Authentication = &message |
394 | match authentication |
395 | => match request | Request
396 | => | AuthenticationToken
397 | let mut granter = auth_granter.lock .await; |
398 | |
399 | let response = granter.token_request .await.unwrap; |
400 | drop; |
401 | |
402 | let _result = send |
403 | &mut socket, |
404 | Authentication |
405 | , | AuthenticationToken
406 | , |
407 | |
408 | .await; |
409 | |
410 | continue; |
411 | |
412 | => | TokenExtension
413 | let mut granter = auth_granter.lock .await; |
414 | |
415 | let response = granter |
416 | .extension_request |
417 | .await |
418 | .unwrap_or; |
419 | drop; |
420 | |
421 | let _result = send |
422 | &mut socket, |
423 | Authentication |
424 | , | TokenExtension
425 | , |
426 | |
427 | .await; |
428 | |
429 | continue; |
430 | |
431 | => | RegisterAccount
432 | let request = request.inner .await.clone; |
433 | |
434 | let mut user_backend = user_backend.lock .await; |
435 | |
436 | let response = user_backend.register .await.unwrap; |
437 | drop; |
438 | |
439 | let _result = send |
440 | &mut socket, |
441 | Authentication |
442 | , | RegisterAccount
443 | , |
444 | |
445 | .await; |
446 | |
447 | continue; |
448 | |
449 | , |
450 | => unreachable!, | Response
451 | |
452 | |
453 | |
454 | if let Discovery = &message |
455 | let mut backend = discovery_backend.lock .await; |
456 | backend.try_handle .await.unwrap; |
457 | |
458 | continue; |
459 | |
460 | |
461 | if let User = &message |
462 | match &message.message |
463 | => match request | Request
464 | => | DisplayName
465 | let mut user_backend = user_backend.lock .await; |
466 | |
467 | let response = user_backend.display_name .await; |
468 | |
469 | let response = match response |
470 | Ok => response, |
471 | Err => |
472 | error!; |
473 | continue; |
474 | |
475 | ; |
476 | drop; |
477 | |
478 | let _result = send |
479 | &mut socket, |
480 | User |
481 | instance: message.instance.clone, |
482 | message: Response |
483 | , | DisplayName
484 | , |
485 | , |
486 | |
487 | .await; |
488 | |
489 | continue; |
490 | |
491 | => | DisplayImage
492 | let mut user_backend = user_backend.lock .await; |
493 | |
494 | let response = user_backend.display_image .await; |
495 | |
496 | let response = match response |
497 | Ok => response, |
498 | Err => |
499 | error!; |
500 | continue; |
501 | |
502 | ; |
503 | drop; |
504 | |
505 | let _result = send |
506 | &mut socket, |
507 | User |
508 | instance: message.instance.clone, |
509 | message: Response |
510 | , | DisplayImage
511 | , |
512 | , |
513 | |
514 | .await; |
515 | |
516 | continue; |
517 | |
518 | => | Bio
519 | let mut user_backend = user_backend.lock .await; |
520 | |
521 | let response = user_backend.bio .await; |
522 | |
523 | let response = match response |
524 | Ok => response, |
525 | Err => |
526 | error!; |
527 | continue; |
528 | |
529 | ; |
530 | drop; |
531 | |
532 | let _result = send |
533 | &mut socket, |
534 | User |
535 | instance: message.instance.clone, |
536 | message: Response |
537 | response, |
538 | , |
539 | , |
540 | |
541 | .await; |
542 | |
543 | continue; |
544 | |
545 | => | Repositories
546 | let mut repository_backend = backend.lock .await; |
547 | |
548 | let repositories = repository_backend |
549 | .repositories_for_user |
550 | .await; |
551 | |
552 | let repositories = match repositories |
553 | Ok => repositories, |
554 | Err => |
555 | error!; |
556 | continue; |
557 | |
558 | ; |
559 | drop; |
560 | |
561 | let response = UserRepositoriesResponse ; |
562 | |
563 | let _result = send |
564 | &mut socket, |
565 | User |
566 | instance: message.instance.clone, |
567 | message: Response |
568 | , | Repositories
569 | , |
570 | , |
571 | |
572 | .await; |
573 | |
574 | continue; |
575 | |
576 | , |
577 | => unreachable!, | Response
578 | |
579 | |
580 | |
581 | |
582 | info!; |
583 | |
584 | |
585 | async |
586 | message: MessageKind, |
587 | listeners: & , |
588 | connections: & , |
589 | |
590 | let : = |
591 | match &message |
592 | => | Handshake
593 | todo! |
594 | |
595 | => , | Repository
596 | => todo!, | Authentication
597 | => todo!, | Discovery
598 | => todo!, | User
599 | ; |
600 | |
601 | let target = match |
602 | => instance.clone, |
603 | => user.instance.clone, |
604 | => repository.instance.clone, |
605 | _ => unreachable!, |
606 | ; |
607 | |
608 | let mut listeners = listeners.lock .await; |
609 | let listener = listeners.add; |
610 | drop; |
611 | |
612 | let connections = connections.lock .await; |
613 | |
614 | if let Some = connections.instance_connections.get |
615 | if let Err = connection.sender.send |
616 | error!; |
617 | |
618 | else |
619 | error!; |
620 | |
621 | panic!; |
622 | |
623 | |
624 | drop; |
625 | |
626 | listener |
627 | |
628 | |
629 | async |
630 | socket: &mut , |
631 | message: T, |
632 | |
633 | socket |
634 | .send |
635 | .await?; |
636 | |
637 | Ok |
638 | |
639 |