ws.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. use crate::{entities::doc::NewDocUser, errors::DocumentError};
  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 WsDataType {
  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. Conflict = 3,
  15. NewDocUser = 4,
  16. }
  17. impl WsDataType {
  18. pub fn data<T>(&self, bytes: Bytes) -> Result<T, DocumentError>
  19. where
  20. T: TryFrom<Bytes, Error = DocumentError>,
  21. {
  22. T::try_from(bytes)
  23. }
  24. }
  25. impl std::default::Default for WsDataType {
  26. fn default() -> Self { WsDataType::Acked }
  27. }
  28. // #[derive(ProtoBuf, Default, Debug, Clone)]
  29. // pub struct WsDocumentUser {
  30. // #[pb(index = 1)]
  31. // pub user_id: String,
  32. //
  33. // #[pb(index = 2)]
  34. // pub name: String,
  35. // }
  36. #[derive(ProtoBuf, Default, Debug, Clone)]
  37. pub struct WsDocumentData {
  38. #[pb(index = 1)]
  39. pub doc_id: String,
  40. #[pb(index = 2)]
  41. pub ty: WsDataType,
  42. #[pb(index = 3)]
  43. pub data: Vec<u8>,
  44. /* #[pb(index = 4)]
  45. * pub user: WsDocumentUser, */
  46. }
  47. impl std::convert::From<Revision> for WsDocumentData {
  48. fn from(revision: Revision) -> Self {
  49. let doc_id = revision.doc_id.clone();
  50. let bytes: Bytes = revision.try_into().unwrap();
  51. Self {
  52. doc_id,
  53. ty: WsDataType::PushRev,
  54. data: bytes.to_vec(),
  55. }
  56. }
  57. }
  58. impl std::convert::From<NewDocUser> for WsDocumentData {
  59. fn from(user: NewDocUser) -> Self {
  60. let doc_id = user.doc_id.clone();
  61. let bytes: Bytes = user.try_into().unwrap();
  62. Self {
  63. doc_id,
  64. ty: WsDataType::NewDocUser,
  65. data: bytes.to_vec(),
  66. }
  67. }
  68. }
  69. pub struct WsDocumentDataBuilder();
  70. impl WsDocumentDataBuilder {
  71. // WsDataType::PushRev -> Revision
  72. pub fn build_push_rev_message(doc_id: &str, revision: Revision) -> WsDocumentData {
  73. let bytes: Bytes = revision.try_into().unwrap();
  74. WsDocumentData {
  75. doc_id: doc_id.to_string(),
  76. ty: WsDataType::PushRev,
  77. data: bytes.to_vec(),
  78. }
  79. }
  80. // WsDataType::PullRev -> RevisionRange
  81. pub fn build_push_pull_message(doc_id: &str, range: RevisionRange) -> WsDocumentData {
  82. let bytes: Bytes = range.try_into().unwrap();
  83. WsDocumentData {
  84. doc_id: doc_id.to_string(),
  85. ty: WsDataType::PullRev,
  86. data: bytes.to_vec(),
  87. }
  88. }
  89. // WsDataType::Acked -> RevId
  90. pub fn build_acked_message(doc_id: &str, rev_id: i64) -> WsDocumentData {
  91. let rev_id: RevId = rev_id.into();
  92. let bytes: Bytes = rev_id.try_into().unwrap();
  93. WsDocumentData {
  94. doc_id: doc_id.to_string(),
  95. ty: WsDataType::Acked,
  96. data: bytes.to_vec(),
  97. }
  98. }
  99. }