document.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. use crate::{
  2. configuration::*,
  3. request::{HttpRequestBuilder, ResponseMiddleware},
  4. };
  5. use flowy_block::BlockCloudService;
  6. use flowy_collaboration::entities::document_info::{BlockId, BlockInfo, CreateBlockParams, ResetDocumentParams};
  7. use flowy_error::FlowyError;
  8. use http_flowy::response::FlowyResponse;
  9. use lazy_static::lazy_static;
  10. use lib_infra::future::FutureResult;
  11. use std::sync::Arc;
  12. pub struct BlockHttpCloudService {
  13. config: ClientServerConfiguration,
  14. }
  15. impl BlockHttpCloudService {
  16. pub fn new(config: ClientServerConfiguration) -> Self {
  17. Self { config }
  18. }
  19. }
  20. impl BlockCloudService for BlockHttpCloudService {
  21. fn create_block(&self, token: &str, params: CreateBlockParams) -> FutureResult<(), FlowyError> {
  22. let token = token.to_owned();
  23. let url = self.config.doc_url();
  24. FutureResult::new(async move { create_document_request(&token, params, &url).await })
  25. }
  26. fn read_block(&self, token: &str, params: BlockId) -> FutureResult<Option<BlockInfo>, FlowyError> {
  27. let token = token.to_owned();
  28. let url = self.config.doc_url();
  29. FutureResult::new(async move { read_document_request(&token, params, &url).await })
  30. }
  31. fn update_block(&self, token: &str, params: ResetDocumentParams) -> FutureResult<(), FlowyError> {
  32. let token = token.to_owned();
  33. let url = self.config.doc_url();
  34. FutureResult::new(async move { reset_doc_request(&token, params, &url).await })
  35. }
  36. }
  37. pub async fn create_document_request(token: &str, params: CreateBlockParams, url: &str) -> Result<(), FlowyError> {
  38. let _ = request_builder()
  39. .post(&url.to_owned())
  40. .header(HEADER_TOKEN, token)
  41. .protobuf(params)?
  42. .send()
  43. .await?;
  44. Ok(())
  45. }
  46. pub async fn read_document_request(token: &str, params: BlockId, url: &str) -> Result<Option<BlockInfo>, FlowyError> {
  47. let doc = request_builder()
  48. .get(&url.to_owned())
  49. .header(HEADER_TOKEN, token)
  50. .protobuf(params)?
  51. .option_response()
  52. .await?;
  53. Ok(doc)
  54. }
  55. pub async fn reset_doc_request(token: &str, params: ResetDocumentParams, url: &str) -> Result<(), FlowyError> {
  56. let _ = request_builder()
  57. .patch(&url.to_owned())
  58. .header(HEADER_TOKEN, token)
  59. .protobuf(params)?
  60. .send()
  61. .await?;
  62. Ok(())
  63. }
  64. fn request_builder() -> HttpRequestBuilder {
  65. HttpRequestBuilder::new().middleware(MIDDLEWARE.clone())
  66. }
  67. lazy_static! {
  68. pub(crate) static ref MIDDLEWARE: Arc<DocumentResponseMiddleware> = Arc::new(DocumentResponseMiddleware {});
  69. }
  70. pub(crate) struct DocumentResponseMiddleware {}
  71. impl ResponseMiddleware for DocumentResponseMiddleware {
  72. fn receive_response(&self, token: &Option<String>, response: &FlowyResponse) {
  73. if let Some(error) = &response.error {
  74. if error.is_unauthorized() {
  75. tracing::error!("document user is unauthorized");
  76. match token {
  77. None => {}
  78. Some(_token) => {
  79. // let error =
  80. // FlowyError::new(ErrorCode::UserUnauthorized, "");
  81. // observable(token,
  82. // WorkspaceObservable::UserUnauthorized).error(error).
  83. // build()
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }