mirror of
				https://github.com/AppFlowy-IO/AppFlowy.git
				synced 2025-10-30 17:38:40 +00:00 
			
		
		
		
	send delta to server
This commit is contained in:
		
							parent
							
								
									526f1408bc
								
							
						
					
					
						commit
						45e74e48a2
					
				| @ -77,7 +77,7 @@ enum AuthStatus { | |||||||
|     NotAuthorized, |     NotAuthorized, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub const EXPIRED_DURATION_DAYS: i64 = 5; | pub const EXPIRED_DURATION_DAYS: i64 = 30; | ||||||
| 
 | 
 | ||||||
| pub struct AuthorizedUsers(DashMap<LoggedUser, AuthStatus>); | pub struct AuthorizedUsers(DashMap<LoggedUser, AuthStatus>); | ||||||
| impl AuthorizedUsers { | impl AuthorizedUsers { | ||||||
|  | |||||||
| @ -3,6 +3,7 @@ use crate::{ | |||||||
|     errors::{internal_error, DocError}, |     errors::{internal_error, DocError}, | ||||||
|     services::{doc_controller::DocController, open_doc::OpenedDocManager, server::construct_doc_server, ws::WsManager}, |     services::{doc_controller::DocController, open_doc::OpenedDocManager, server::construct_doc_server, ws::WsManager}, | ||||||
| }; | }; | ||||||
|  | use bytes::Bytes; | ||||||
| use diesel::SqliteConnection; | use diesel::SqliteConnection; | ||||||
| use flowy_database::ConnectionPool; | use flowy_database::ConnectionPool; | ||||||
| use parking_lot::RwLock; | use parking_lot::RwLock; | ||||||
| @ -56,7 +57,7 @@ impl FlowyDocument { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub async fn apply_changeset(&self, params: ApplyChangesetParams, pool: Arc<ConnectionPool>) -> Result<Doc, DocError> { |     pub async fn apply_changeset(&self, params: ApplyChangesetParams, pool: Arc<ConnectionPool>) -> Result<Doc, DocError> { | ||||||
|         let _ = self.doc_manager.apply_changeset(¶ms.id, params.data, pool).await?; |         let _ = self.doc_manager.apply_changeset(¶ms.id, Bytes::from(params.data), pool).await?; | ||||||
|         let data = self.doc_manager.read_doc(¶ms.id).await?; |         let data = self.doc_manager.read_doc(¶ms.id).await?; | ||||||
|         let doc = Doc { id: params.id, data }; |         let doc = Doc { id: params.id, data }; | ||||||
|         Ok(doc) |         Ok(doc) | ||||||
|  | |||||||
| @ -135,8 +135,6 @@ impl OpenedDocPersistence for DocController { | |||||||
|     fn save(&self, params: SaveDocParams, pool: Arc<ConnectionPool>) -> Result<(), DocError> { |     fn save(&self, params: SaveDocParams, pool: Arc<ConnectionPool>) -> Result<(), DocError> { | ||||||
|         let changeset = DocTableChangeset::new(params.clone()); |         let changeset = DocTableChangeset::new(params.clone()); | ||||||
|         let _ = self.sql.update_doc_table(changeset, &*(pool.get().map_err(internal_error)?))?; |         let _ = self.sql.update_doc_table(changeset, &*(pool.get().map_err(internal_error)?))?; | ||||||
|         let _ = self.update_doc_on_server(params)?; |  | ||||||
| 
 |  | ||||||
|         Ok(()) |         Ok(()) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -5,6 +5,7 @@ use crate::{ | |||||||
|         ws::WsManager, |         ws::WsManager, | ||||||
|     }, |     }, | ||||||
| }; | }; | ||||||
|  | use bytes::Bytes; | ||||||
| use dashmap::DashMap; | use dashmap::DashMap; | ||||||
| use flowy_database::ConnectionPool; | use flowy_database::ConnectionPool; | ||||||
| use flowy_ot::{core::Delta, errors::OTError}; | use flowy_ot::{core::Delta, errors::OTError}; | ||||||
| @ -32,7 +33,12 @@ impl OpenedDocManager { | |||||||
|         T: Into<DocId> + Debug, |         T: Into<DocId> + Debug, | ||||||
|         D: TryInto<Delta, Error = OTError>, |         D: TryInto<Delta, Error = OTError>, | ||||||
|     { |     { | ||||||
|         let doc = Arc::new(OpenedDoc::new(id.into(), data.try_into()?, self.persistence.clone())); |         let doc = Arc::new(OpenedDoc::new( | ||||||
|  |             id.into(), | ||||||
|  |             data.try_into()?, | ||||||
|  |             self.persistence.clone(), | ||||||
|  |             self.ws_manager.read().sender.clone(), | ||||||
|  |         )); | ||||||
|         self.ws_manager.write().register_handler(doc.id.as_ref(), doc.clone()); |         self.ws_manager.write().register_handler(doc.id.as_ref(), doc.clone()); | ||||||
|         self.doc_map.insert(doc.id.clone(), doc.clone()); |         self.doc_map.insert(doc.id.clone(), doc.clone()); | ||||||
|         Ok(()) |         Ok(()) | ||||||
| @ -47,7 +53,7 @@ impl OpenedDocManager { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     #[tracing::instrument(level = "debug", skip(self, changeset, pool), err)] |     #[tracing::instrument(level = "debug", skip(self, changeset, pool), err)] | ||||||
|     pub(crate) async fn apply_changeset<T>(&self, id: T, changeset: Vec<u8>, pool: Arc<ConnectionPool>) -> Result<(), DocError> |     pub(crate) async fn apply_changeset<T>(&self, id: T, changeset: Bytes, pool: Arc<ConnectionPool>) -> Result<(), DocError> | ||||||
|     where |     where | ||||||
|         T: Into<DocId> + Debug, |         T: Into<DocId> + Debug, | ||||||
|     { |     { | ||||||
|  | |||||||
| @ -4,8 +4,9 @@ use crate::{ | |||||||
|         ws::{WsDocumentData, WsSource}, |         ws::{WsDocumentData, WsSource}, | ||||||
|     }, |     }, | ||||||
|     errors::DocError, |     errors::DocError, | ||||||
|     services::ws::WsHandler, |     services::ws::{WsHandler, WsSender}, | ||||||
| }; | }; | ||||||
|  | use bytes::Bytes; | ||||||
| use flowy_database::ConnectionPool; | use flowy_database::ConnectionPool; | ||||||
| use flowy_ot::{client::Document, core::Delta}; | use flowy_ot::{client::Document, core::Delta}; | ||||||
| use parking_lot::RwLock; | use parking_lot::RwLock; | ||||||
| @ -30,23 +31,28 @@ pub(crate) trait OpenedDocPersistence: Send + Sync { | |||||||
| pub(crate) struct OpenedDoc { | pub(crate) struct OpenedDoc { | ||||||
|     pub(crate) id: DocId, |     pub(crate) id: DocId, | ||||||
|     document: RwLock<Document>, |     document: RwLock<Document>, | ||||||
|  |     ws_sender: Arc<dyn WsSender>, | ||||||
|     persistence: Arc<dyn OpenedDocPersistence>, |     persistence: Arc<dyn OpenedDocPersistence>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl OpenedDoc { | impl OpenedDoc { | ||||||
|     pub(crate) fn new(id: DocId, delta: Delta, persistence: Arc<dyn OpenedDocPersistence>) -> Self { |     pub(crate) fn new(id: DocId, delta: Delta, persistence: Arc<dyn OpenedDocPersistence>, ws_sender: Arc<dyn WsSender>) -> Self { | ||||||
|  |         let document = RwLock::new(Document::from_delta(delta)); | ||||||
|         Self { |         Self { | ||||||
|             id, |             id, | ||||||
|             document: RwLock::new(Document::from_delta(delta)), |             document, | ||||||
|  |             ws_sender, | ||||||
|             persistence, |             persistence, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub(crate) fn data(&self) -> Vec<u8> { self.document.read().to_bytes() } |     pub(crate) fn data(&self) -> Vec<u8> { self.document.read().to_bytes() } | ||||||
| 
 | 
 | ||||||
|     pub(crate) fn apply_delta(&self, data: Vec<u8>, pool: Arc<ConnectionPool>) -> Result<(), DocError> { |     pub(crate) fn apply_delta(&self, data: Bytes, pool: Arc<ConnectionPool>) -> Result<(), DocError> { | ||||||
|         let mut write_guard = self.document.write(); |         let mut write_guard = self.document.write(); | ||||||
|         let _ = write_guard.apply_changeset(data)?; |         let _ = write_guard.apply_changeset(data.clone())?; | ||||||
|  | 
 | ||||||
|  |         self.ws_sender.send_data(data); | ||||||
| 
 | 
 | ||||||
|         // Opti: strategy to save the document
 |         // Opti: strategy to save the document
 | ||||||
|         let mut save = SaveDocParams { |         let mut save = SaveDocParams { | ||||||
|  | |||||||
| @ -12,12 +12,12 @@ lazy_static! { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub struct WsManager { | pub struct WsManager { | ||||||
|     sender: Box<dyn WsSender>, |     pub(crate) sender: Arc<dyn WsSender>, | ||||||
|     doc_handlers: HashMap<String, Arc<dyn WsHandler>>, |     doc_handlers: HashMap<String, Arc<dyn WsHandler>>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl WsManager { | impl WsManager { | ||||||
|     pub fn new(sender: Box<dyn WsSender>) -> Self { |     pub fn new(sender: Arc<dyn WsSender>) -> Self { | ||||||
|         Self { |         Self { | ||||||
|             sender, |             sender, | ||||||
|             doc_handlers: HashMap::new(), |             doc_handlers: HashMap::new(), | ||||||
| @ -45,9 +45,9 @@ impl WsManager { | |||||||
|             }, |             }, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     pub fn send_data(&self, data: WsDocumentData) { |     pub fn send_data(&self, data: WsDocumentData) { | ||||||
|         let bytes: Bytes = data.try_into().unwrap(); |         let bytes: Bytes = data.try_into().unwrap(); | ||||||
| 
 |  | ||||||
|         match self.sender.send_data(bytes) { |         match self.sender.send_data(bytes) { | ||||||
|             Ok(_) => {}, |             Ok(_) => {}, | ||||||
|             Err(e) => { |             Err(e) => { | ||||||
|  | |||||||
| @ -17,6 +17,7 @@ lazy_static = "1.4.0" | |||||||
| url = "2.2" | url = "2.2" | ||||||
| strum = "0.21" | strum = "0.21" | ||||||
| strum_macros = "0.21" | strum_macros = "0.21" | ||||||
|  | bytes = "1.0" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| [dev-dependencies] | [dev-dependencies] | ||||||
|  | |||||||
| @ -3,6 +3,8 @@ use crate::{ | |||||||
|     errors::{ErrorBuilder, OTError, OTErrorCode}, |     errors::{ErrorBuilder, OTError, OTErrorCode}, | ||||||
| }; | }; | ||||||
| use bytecount::num_chars; | use bytecount::num_chars; | ||||||
|  | use bytes::Bytes; | ||||||
|  | use serde::__private::TryFrom; | ||||||
| use std::{ | use std::{ | ||||||
|     cmp::{min, Ordering}, |     cmp::{min, Ordering}, | ||||||
|     fmt, |     fmt, | ||||||
| @ -44,6 +46,15 @@ impl std::convert::TryFrom<Vec<u8>> for Delta { | |||||||
|     fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> { Delta::from_bytes(bytes) } |     fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> { Delta::from_bytes(bytes) } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | impl std::convert::TryFrom<Bytes> for Delta { | ||||||
|  |     type Error = OTError; | ||||||
|  | 
 | ||||||
|  |     fn try_from(value: Bytes) -> Result<Self, Self::Error> { | ||||||
|  |         let bytes = value.to_vec(); | ||||||
|  |         Delta::from_bytes(bytes) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // impl<T: AsRef<Vec<u8>>> std::convert::From<T> for Delta {
 | // impl<T: AsRef<Vec<u8>>> std::convert::From<T> for Delta {
 | ||||||
| //     fn from(bytes: T) -> Self {
 | //     fn from(bytes: T) -> Self {
 | ||||||
| // Delta::from_bytes(bytes.as_ref().to_vec()).unwrap() } }
 | // Delta::from_bytes(bytes.as_ref().to_vec()).unwrap() } }
 | ||||||
|  | |||||||
| @ -22,7 +22,7 @@ impl DocumentDepsResolver { | |||||||
|             user: self.user_session.clone(), |             user: self.user_session.clone(), | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|         let sender = Box::new(WsSenderImpl { |         let sender = Arc::new(WsSenderImpl { | ||||||
|             user: self.user_session.clone(), |             user: self.user_session.clone(), | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 appflowy
						appflowy