context.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use crate::errors::FlowyError;
  2. use backend_service::configuration::ClientServerConfiguration;
  3. use crate::{
  4. controller::DocumentController,
  5. core::{DocumentWSReceivers, DocumentWebSocket},
  6. server::construct_doc_server,
  7. };
  8. use flowy_database::ConnectionPool;
  9. use std::sync::Arc;
  10. pub trait DocumentUser: Send + Sync {
  11. fn user_dir(&self) -> Result<String, FlowyError>;
  12. fn user_id(&self) -> Result<String, FlowyError>;
  13. fn token(&self) -> Result<String, FlowyError>;
  14. fn db_pool(&self) -> Result<Arc<ConnectionPool>, FlowyError>;
  15. }
  16. pub struct DocumentContext {
  17. pub controller: Arc<DocumentController>,
  18. pub user: Arc<dyn DocumentUser>,
  19. }
  20. impl DocumentContext {
  21. pub fn new(
  22. user: Arc<dyn DocumentUser>,
  23. ws_receivers: Arc<DocumentWSReceivers>,
  24. ws_sender: Arc<dyn DocumentWebSocket>,
  25. server_config: &ClientServerConfiguration,
  26. ) -> DocumentContext {
  27. let server = construct_doc_server(server_config);
  28. let doc_ctrl = Arc::new(DocumentController::new(server, user.clone(), ws_receivers, ws_sender));
  29. Self {
  30. controller: doc_ctrl,
  31. user,
  32. }
  33. }
  34. pub fn init(&self) -> Result<(), FlowyError> {
  35. let _ = self.controller.init()?;
  36. Ok(())
  37. }
  38. }