ws.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. use crate::{entities::doc::NewDocUser, 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 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, CollaborateError>
  19. where
  20. T: TryFrom<Bytes, Error = CollaborateError>,
  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 WsDocumentData {
  30. #[pb(index = 1)]
  31. pub doc_id: String,
  32. #[pb(index = 2)]
  33. pub ty: WsDataType,
  34. #[pb(index = 3)]
  35. pub data: Vec<u8>,
  36. }
  37. impl std::convert::From<Revision> for WsDocumentData {
  38. fn from(revision: Revision) -> Self {
  39. let doc_id = revision.doc_id.clone();
  40. let bytes: Bytes = revision.try_into().unwrap();
  41. Self {
  42. doc_id,
  43. ty: WsDataType::PushRev,
  44. data: bytes.to_vec(),
  45. }
  46. }
  47. }
  48. impl std::convert::From<NewDocUser> for WsDocumentData {
  49. fn from(user: NewDocUser) -> Self {
  50. let doc_id = user.doc_id.clone();
  51. let bytes: Bytes = user.try_into().unwrap();
  52. Self {
  53. doc_id,
  54. ty: WsDataType::NewDocUser,
  55. data: bytes.to_vec(),
  56. }
  57. }
  58. }
  59. pub struct WsDocumentDataBuilder();
  60. impl WsDocumentDataBuilder {
  61. // WsDataType::PushRev -> Revision
  62. pub fn build_push_rev_message(doc_id: &str, revision: Revision) -> WsDocumentData {
  63. let bytes: Bytes = revision.try_into().unwrap();
  64. WsDocumentData {
  65. doc_id: doc_id.to_string(),
  66. ty: WsDataType::PushRev,
  67. data: bytes.to_vec(),
  68. }
  69. }
  70. // WsDataType::PullRev -> RevisionRange
  71. pub fn build_push_pull_message(doc_id: &str, range: RevisionRange) -> WsDocumentData {
  72. let bytes: Bytes = range.try_into().unwrap();
  73. WsDocumentData {
  74. doc_id: doc_id.to_string(),
  75. ty: WsDataType::PullRev,
  76. data: bytes.to_vec(),
  77. }
  78. }
  79. // WsDataType::Acked -> RevId
  80. pub fn build_acked_message(doc_id: &str, rev_id: i64) -> WsDocumentData {
  81. let rev_id: RevId = rev_id.into();
  82. let bytes: Bytes = rev_id.try_into().unwrap();
  83. WsDocumentData {
  84. doc_id: doc_id.to_string(),
  85. ty: WsDataType::Acked,
  86. data: bytes.to_vec(),
  87. }
  88. }
  89. }