ws_data.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. use crate::{
  2. entities::revision::{RepeatedRevision, RevId, Revision, RevisionRange},
  3. errors::CollaborateError,
  4. };
  5. use bytes::Bytes;
  6. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  7. use std::convert::{TryFrom, TryInto};
  8. #[derive(Debug, Clone, ProtoBuf_Enum, Eq, PartialEq, Hash)]
  9. pub enum ClientRevisionWSDataType {
  10. ClientPushRev = 0,
  11. ClientPing = 1,
  12. }
  13. impl ClientRevisionWSDataType {
  14. pub fn data<T>(&self, bytes: Bytes) -> Result<T, CollaborateError>
  15. where
  16. T: TryFrom<Bytes, Error = CollaborateError>,
  17. {
  18. T::try_from(bytes)
  19. }
  20. }
  21. impl std::default::Default for ClientRevisionWSDataType {
  22. fn default() -> Self {
  23. ClientRevisionWSDataType::ClientPushRev
  24. }
  25. }
  26. #[derive(ProtoBuf, Default, Debug, Clone)]
  27. pub struct ClientRevisionWSData {
  28. #[pb(index = 1)]
  29. pub object_id: String,
  30. #[pb(index = 2)]
  31. pub ty: ClientRevisionWSDataType,
  32. #[pb(index = 3)]
  33. pub revisions: RepeatedRevision,
  34. #[pb(index = 4)]
  35. data_id: String,
  36. }
  37. impl ClientRevisionWSData {
  38. pub fn from_revisions(object_id: &str, revisions: Vec<Revision>) -> Self {
  39. let rev_id = match revisions.first() {
  40. None => 0,
  41. Some(revision) => revision.rev_id,
  42. };
  43. Self {
  44. object_id: object_id.to_owned(),
  45. ty: ClientRevisionWSDataType::ClientPushRev,
  46. revisions: RepeatedRevision::new(revisions),
  47. data_id: rev_id.to_string(),
  48. }
  49. }
  50. pub fn ping(object_id: &str, rev_id: i64) -> Self {
  51. Self {
  52. object_id: object_id.to_owned(),
  53. ty: ClientRevisionWSDataType::ClientPing,
  54. revisions: RepeatedRevision::empty(),
  55. data_id: rev_id.to_string(),
  56. }
  57. }
  58. pub fn id(&self) -> String {
  59. self.data_id.clone()
  60. }
  61. }
  62. #[derive(Debug, Clone, ProtoBuf_Enum, Eq, PartialEq, Hash)]
  63. pub enum ServerRevisionWSDataType {
  64. ServerAck = 0,
  65. ServerPushRev = 1,
  66. ServerPullRev = 2,
  67. UserConnect = 3,
  68. }
  69. impl std::default::Default for ServerRevisionWSDataType {
  70. fn default() -> Self {
  71. ServerRevisionWSDataType::ServerPushRev
  72. }
  73. }
  74. #[derive(ProtoBuf, Default, Debug, Clone)]
  75. pub struct ServerRevisionWSData {
  76. #[pb(index = 1)]
  77. pub object_id: String,
  78. #[pb(index = 2)]
  79. pub ty: ServerRevisionWSDataType,
  80. #[pb(index = 3)]
  81. pub data: Vec<u8>,
  82. }
  83. pub struct ServerRevisionWSDataBuilder();
  84. impl ServerRevisionWSDataBuilder {
  85. pub fn build_push_message(object_id: &str, repeated_revision: RepeatedRevision) -> ServerRevisionWSData {
  86. let bytes: Bytes = repeated_revision.try_into().unwrap();
  87. ServerRevisionWSData {
  88. object_id: object_id.to_string(),
  89. ty: ServerRevisionWSDataType::ServerPushRev,
  90. data: bytes.to_vec(),
  91. }
  92. }
  93. pub fn build_pull_message(object_id: &str, range: RevisionRange) -> ServerRevisionWSData {
  94. let bytes: Bytes = range.try_into().unwrap();
  95. ServerRevisionWSData {
  96. object_id: object_id.to_string(),
  97. ty: ServerRevisionWSDataType::ServerPullRev,
  98. data: bytes.to_vec(),
  99. }
  100. }
  101. pub fn build_ack_message(object_id: &str, rev_id: i64) -> ServerRevisionWSData {
  102. let rev_id: RevId = rev_id.into();
  103. let bytes: Bytes = rev_id.try_into().unwrap();
  104. ServerRevisionWSData {
  105. object_id: object_id.to_string(),
  106. ty: ServerRevisionWSDataType::ServerAck,
  107. data: bytes.to_vec(),
  108. }
  109. }
  110. }
  111. #[derive(ProtoBuf, Default, Debug, Clone)]
  112. pub struct NewDocumentUser {
  113. #[pb(index = 1)]
  114. pub user_id: String,
  115. #[pb(index = 2)]
  116. pub doc_id: String,
  117. // revision_data: the latest rev_id of the document.
  118. #[pb(index = 3)]
  119. pub revision_data: Vec<u8>,
  120. }