| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use topcoat::{ |
| 4 | Result, |
| 5 | context::{Cx, app_context}, |
| 6 | router::{ |
| 7 | error::RouterErrorExt, |
| 8 | content::websocket::WebSocketUpgrade, |
| 9 | path_param, |
| 10 | response::Response, |
| 11 | route, |
| 12 | }, |
| 13 | }; |
| 14 | |
| 15 | use super::NoteId; |
| 16 | use crate::hub::{Hub, serve_socket}; |
| 17 | use crate::store::Store; |
| 18 | |
| 19 | |
| 20 | #[route(GET)] |
| 21 | async fn websocket(cx: &Cx, upgrade: WebSocketUpgrade) -> Result<Response> { |
| 22 | let store = app_context::<Arc<Store>>(cx).clone(); |
| 23 | let hub = app_context::<Arc<Hub>>(cx).clone(); |
| 24 | let id = path_param::<NoteId>(cx).to_owned(); |
| 25 | store.get_note(&id).await.map_err(topcoat::Error::from_anyhow)?.ok_or_not_found()?; |
| 26 | let room = hub.room(&store, &id).await.map_err(topcoat::Error::from_anyhow)?; |
| 27 | |
| 28 | let max = 128 << 20; |
| 29 | upgrade.max_message_size(max).max_frame_size(max).on_upgrade(move |socket| async move { |
| 30 | serve_socket(hub, store, room, socket).await; |
| 31 | }) |
| 32 | } |