AppFlowy/frontend/rust-lib/flowy-document2/src/manager.rs
Lucas.Xu d842f228e8
feat: Integrate supabase (#2551)
* feat: integrate supabase auth service

* chore: ignore the sercet

* feat: separate and inject the auth service

* chore: integrate auth service into sign in/up page

* feat: integrate github and google sign in

* chore: update user trait

* feat: box any params in UserCloudService trait

* feat: add flowy-server crate

* refactor: user trait

* docs: doc ThirdPartyAuthPB

* feat: server provider

* feat: pass the uuid to rust side

* feat: pass supabase config to rust side

* feat: integrate env file

* feat: implement login as guest

* feat: impl postgrest

* test: use env

* chore: upper case key

* feat: optimize the file storage

* fix: don't call set auth when user login in local

* docs: add docs

* feat: create/update/get user using postgrest

* feat: optimize the login as guest

* feat: create user workspace

* feat: create user default workspace

* feat: redesign the setting path location page

* feat: use uuid as view id

* feat: send auth info to rust backend

* fix: sign up

* fix: skip to wrong page after login

* fix: integrate test error

* fix: indent command error

* feat: add discord login in type

* fix: flutter analyze

* ci: fix rust tests

* ci: fix tauri build

* ci: fix tauri build

---------

Co-authored-by: nathan <nathan@appflowy.io>
2023-05-21 18:53:59 +08:00

98 lines
3.0 KiB
Rust

use std::{collections::HashMap, sync::Arc};
use appflowy_integrate::collab_builder::AppFlowyCollabBuilder;
use appflowy_integrate::RocksCollabDB;
use parking_lot::RwLock;
use flowy_error::{FlowyError, FlowyResult};
use crate::document_data::DocumentDataWrapper;
use crate::{
document::Document,
entities::DocEventPB,
notification::{send_notification, DocumentNotification},
};
pub trait DocumentUser: Send + Sync {
fn user_id(&self) -> Result<i64, FlowyError>;
fn token(&self) -> Result<Option<String>, FlowyError>; // unused now.
fn collab_db(&self) -> Result<Arc<RocksCollabDB>, FlowyError>;
}
pub struct DocumentManager {
user: Arc<dyn DocumentUser>,
collab_builder: Arc<AppFlowyCollabBuilder>,
documents: Arc<RwLock<HashMap<String, Arc<Document>>>>,
}
impl DocumentManager {
pub fn new(user: Arc<dyn DocumentUser>, collab_builder: Arc<AppFlowyCollabBuilder>) -> Self {
Self {
user,
collab_builder,
documents: Default::default(),
}
}
pub fn create_document(
&self,
doc_id: String,
data: DocumentDataWrapper,
) -> FlowyResult<Arc<Document>> {
tracing::debug!("create a document: {:?}", &doc_id);
let uid = self.user.user_id()?;
let db = self.user.collab_db()?;
let collab = self.collab_builder.build(uid, &doc_id, db);
let document = Arc::new(Document::create_with_data(collab, data.0)?);
Ok(document)
}
pub fn open_document(&self, doc_id: String) -> FlowyResult<Arc<Document>> {
tracing::debug!("open a document: {:?}", &doc_id);
if let Some(doc) = self.documents.read().get(&doc_id) {
return Ok(doc.clone());
}
tracing::debug!("open_document: {:?}", &doc_id);
let uid = self.user.user_id()?;
let db = self.user.collab_db()?;
let collab = self.collab_builder.build(uid, &doc_id, db);
// read the existing document from the disk.
let document = Arc::new(Document::new(collab)?);
// save the document to the memory and read it from the memory if we open the same document again.
// and we don't want to subscribe to the document changes if we open the same document again.
self
.documents
.write()
.insert(doc_id.clone(), document.clone());
// subscribe to the document changes.
document.lock().open(move |events, is_remote| {
tracing::debug!(
"document changed: {:?}, from remote: {}",
&events,
is_remote
);
// send notification to the client.
send_notification(&doc_id, DocumentNotification::DidReceiveUpdate)
.payload::<DocEventPB>((events, is_remote).into())
.send();
})?;
Ok(document)
}
pub fn get_document(&self, doc_id: String) -> FlowyResult<Arc<Document>> {
let uid = self.user.user_id()?;
let db = self.user.collab_db()?;
let collab = self.collab_builder.build(uid, &doc_id, db);
// read the existing document from the disk.
let document = Arc::new(Document::new(collab)?);
Ok(document)
}
pub fn close_document(&self, doc_id: String) -> FlowyResult<()> {
self.documents.write().remove(&doc_id);
Ok(())
}
}