context.rs 1.4 KB

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