ws.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. use crate::errors::CollaborateError;
  2. use bytes::Bytes;
  3. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  4. use lib_ot::revision::{RevId, Revision, RevisionRange};
  5. use std::convert::{TryFrom, TryInto};
  6. #[derive(Debug, Clone, ProtoBuf_Enum, Eq, PartialEq, Hash)]
  7. pub enum DocumentWSDataType {
  8. // The frontend receives the Acked means the backend has accepted the revision
  9. Acked = 0,
  10. // The frontend receives the PushRev event means the backend is pushing the new revision to frontend
  11. PushRev = 1,
  12. // The fronted receives the PullRev event means the backend try to pull the revision from frontend
  13. PullRev = 2,
  14. UserConnect = 3,
  15. }
  16. impl DocumentWSDataType {
  17. pub fn data<T>(&self, bytes: Bytes) -> Result<T, CollaborateError>
  18. where
  19. T: TryFrom<Bytes, Error = CollaborateError>,
  20. {
  21. T::try_from(bytes)
  22. }
  23. }
  24. impl std::default::Default for DocumentWSDataType {
  25. fn default() -> Self { DocumentWSDataType::Acked }
  26. }
  27. #[derive(ProtoBuf, Default, Debug, Clone)]
  28. pub struct DocumentWSData {
  29. #[pb(index = 1)]
  30. pub doc_id: String,
  31. #[pb(index = 2)]
  32. pub ty: DocumentWSDataType,
  33. #[pb(index = 3)]
  34. pub data: Vec<u8>,
  35. #[pb(index = 4, one_of)]
  36. pub id: Option<i64>,
  37. }
  38. impl std::convert::From<Revision> for DocumentWSData {
  39. fn from(revision: Revision) -> Self {
  40. let doc_id = revision.doc_id.clone();
  41. let rev_id = revision.rev_id;
  42. let bytes: Bytes = revision.try_into().unwrap();
  43. Self {
  44. doc_id,
  45. ty: DocumentWSDataType::PushRev,
  46. data: bytes.to_vec(),
  47. id: Some(rev_id),
  48. }
  49. }
  50. }
  51. pub struct WsDocumentDataBuilder();
  52. impl WsDocumentDataBuilder {
  53. // DocumentWSDataType::PushRev -> Revision
  54. pub fn build_push_rev_message(doc_id: &str, revision: Revision) -> DocumentWSData {
  55. let rev_id = revision.rev_id;
  56. let bytes: Bytes = revision.try_into().unwrap();
  57. DocumentWSData {
  58. doc_id: doc_id.to_string(),
  59. ty: DocumentWSDataType::PushRev,
  60. data: bytes.to_vec(),
  61. id: Some(rev_id),
  62. }
  63. }
  64. // DocumentWSDataType::PullRev -> RevisionRange
  65. pub fn build_push_pull_message(doc_id: &str, range: RevisionRange) -> DocumentWSData {
  66. let bytes: Bytes = range.try_into().unwrap();
  67. DocumentWSData {
  68. doc_id: doc_id.to_string(),
  69. ty: DocumentWSDataType::PullRev,
  70. data: bytes.to_vec(),
  71. id: None,
  72. }
  73. }
  74. // DocumentWSDataType::Acked -> RevId
  75. pub fn build_acked_message(doc_id: &str, rev_id: i64) -> DocumentWSData {
  76. let cloned_rev_id = rev_id;
  77. let rev_id: RevId = rev_id.into();
  78. let bytes: Bytes = rev_id.try_into().unwrap();
  79. DocumentWSData {
  80. doc_id: doc_id.to_string(),
  81. ty: DocumentWSDataType::Acked,
  82. data: bytes.to_vec(),
  83. id: Some(cloned_rev_id),
  84. }
  85. }
  86. // DocumentWSDataType::UserConnect -> DocumentConnected
  87. pub fn build_document_conn_message(doc_id: &str, document_conn: DocumentConnected) -> DocumentWSData {
  88. let rev_id = document_conn.rev_id;
  89. let bytes: Bytes = document_conn.try_into().unwrap();
  90. DocumentWSData {
  91. doc_id: doc_id.to_string(),
  92. ty: DocumentWSDataType::UserConnect,
  93. data: bytes.to_vec(),
  94. id: Some(rev_id),
  95. }
  96. }
  97. }
  98. #[derive(ProtoBuf, Default, Debug, Clone)]
  99. pub struct DocumentConnected {
  100. #[pb(index = 1)]
  101. pub user_id: String,
  102. #[pb(index = 2)]
  103. pub doc_id: String,
  104. #[pb(index = 3)]
  105. pub rev_id: i64,
  106. }