Add message history request
Browse files at this commitea10cf3d99ebde0dd5f6ac466c4fa55a2b22d91cParent: 4367327- feat
- add message history request
5 changed files +49 -6
- crates/common/src/constant.rs +2 -0
- crates/common/src/network.rs +9 -5
- crates/common/src/request.rs +5 -1
- crates/common/src/request/message.rs +32 -0
- crates/common/src/response.rs +1 -0
crates/common/src/constant.rsmodified+2 -0
| @@ -4,3 +4,5 @@ pub const MAXIMUM_MESSAGE_LENGTH: usize = 1024; | |||
| 4 | pub const MINIMUM_MESSAGE_LENGTH: usize = 1; | 4 | pub const MINIMUM_MESSAGE_LENGTH: usize = 1; |
| 5 | pub const MAXIMUM_COMMUNITY_NAME_LENGTH: usize = 16; | 5 | pub const MAXIMUM_COMMUNITY_NAME_LENGTH: usize = 16; |
| 6 | pub const MINIMUM_COMMUNITY_NAME_LENGTH: usize = 3; | 6 | pub const MINIMUM_COMMUNITY_NAME_LENGTH: usize = 3; |
| 7 | +pub const MESSAGE_PAGE_LIMIT: usize = 50; | ||
| 8 | +pub const NEWEST_MESSAGE_MARKER: &str = "ZZZZZZZZZZZZZZZZZZZZZZZZZZ"; | ||
crates/common/src/network.rsmodified+9 -5
| @@ -1,13 +1,17 @@ | |||
| 1 | use quinn::{RecvStream, SendStream}; | 1 | use quinn::{RecvStream, SendStream}; |
| 2 | 2 | ||
| 3 | use crate::{ | 3 | use crate::{ |
| 4 | - constant::MAXIMUM_MESSAGE_LENGTH, error::Error, message::Message, request::Request, | 4 | + constant::{MAXIMUM_MESSAGE_LENGTH, MESSAGE_PAGE_LIMIT}, |
| 5 | + error::Error, | ||
| 6 | + message::Message, | ||
| 7 | + request::Request, | ||
| 5 | response::Response, | 8 | response::Response, |
| 6 | }; | 9 | }; |
| 7 | 10 | ||
| 8 | pub const AUTHENTICATION_MAX_READ_LENGHT: usize = 64; | 11 | pub const AUTHENTICATION_MAX_READ_LENGHT: usize = 64; |
| 9 | pub const REQUEST_MAX_READ_LENGHT: usize = MAXIMUM_MESSAGE_LENGTH * 2; | 12 | pub const REQUEST_MAX_READ_LENGHT: usize = MAXIMUM_MESSAGE_LENGTH * 2; |
| 10 | -pub const RESPONCE_MAX_READ_LENGHT: usize = MAXIMUM_MESSAGE_LENGTH * 2; | 13 | +pub const MESSAGE_MAX_ENCODED_LENGTH: usize = MAXIMUM_MESSAGE_LENGTH * 2; |
| 14 | +pub const RESPONSE_MAX_READ_LENGHT: usize = MESSAGE_MAX_ENCODED_LENGTH * MESSAGE_PAGE_LIMIT; | ||
| 11 | 15 | ||
| 12 | pub struct Network; | 16 | pub struct Network; |
| 13 | 17 | ||
| @@ -42,7 +46,7 @@ impl Network { | |||
| 42 | send_stream.write_all(&bitcode::encode(request)).await?; | 46 | send_stream.write_all(&bitcode::encode(request)).await?; |
| 43 | send_stream.finish()?; | 47 | send_stream.finish()?; |
| 44 | 48 | ||
| 45 | - let response = receive_stream.read_to_end(RESPONCE_MAX_READ_LENGHT).await?; | 49 | + let response = receive_stream.read_to_end(RESPONSE_MAX_READ_LENGHT).await?; |
| 46 | 50 | ||
| 47 | Ok(bitcode::decode::<Response>(&response)?) | 51 | Ok(bitcode::decode::<Response>(&response)?) |
| 48 | } | 52 | } |
| @@ -65,10 +69,10 @@ impl Network { | |||
| 65 | receive_stream.read_exact(&mut length_bytes).await?; | 69 | receive_stream.read_exact(&mut length_bytes).await?; |
| 66 | 70 | ||
| 67 | let length = u32::from_le_bytes(length_bytes) as usize; | 71 | let length = u32::from_le_bytes(length_bytes) as usize; |
| 68 | - if length > RESPONCE_MAX_READ_LENGHT { | 72 | + if length > MESSAGE_MAX_ENCODED_LENGTH { |
| 69 | return Err(crate::error::network::Error::ReadBoundExceed( | 73 | return Err(crate::error::network::Error::ReadBoundExceed( |
| 70 | length, | 74 | length, |
| 71 | - RESPONCE_MAX_READ_LENGHT, | 75 | + RESPONSE_MAX_READ_LENGHT, |
| 72 | ) | 76 | ) |
| 73 | .into()); | 77 | .into()); |
| 74 | } | 78 | } |
crates/common/src/request.rsmodified+5 -1
| @@ -2,7 +2,10 @@ use bitcode::{Decode, Encode}; | |||
| 2 | 2 | ||
| 3 | use crate::request::{ | 3 | use crate::request::{ |
| 4 | community::{CreateCommunity, DeleteCommunity, ReadCommunity, UpdateCommunity}, | 4 | community::{CreateCommunity, DeleteCommunity, ReadCommunity, UpdateCommunity}, |
| 5 | - message::{CreateMessage, DeleteMessage, ReadMessage, UpdateMessage}, | 5 | + message::{ |
| 6 | + CreateMessage, DeleteMessage, ReadMessage, ReadMessageByCommunityIDWithMarkerAndLimit, | ||
| 7 | + UpdateMessage, | ||
| 8 | + }, | ||
| 6 | user::{CreateUser, DeleteUser, ReadUser, UpdateUser}, | 9 | user::{CreateUser, DeleteUser, ReadUser, UpdateUser}, |
| 7 | user_community::{CommunityOf, IsUserIn, JoinCommunity, LeaveCommunity, UsersIn}, | 10 | user_community::{CommunityOf, IsUserIn, JoinCommunity, LeaveCommunity, UsersIn}, |
| 8 | }; | 11 | }; |
| @@ -54,6 +57,7 @@ pub enum Request { | |||
| 54 | ReadMessage(ReadMessage), | 57 | ReadMessage(ReadMessage), |
| 55 | UpdateMessage(UpdateMessage), | 58 | UpdateMessage(UpdateMessage), |
| 56 | DeleteMessage(DeleteMessage), | 59 | DeleteMessage(DeleteMessage), |
| 60 | + ReadMessageByCommunityIDWithMarkerAndLimit(ReadMessageByCommunityIDWithMarkerAndLimit), | ||
| 57 | JoinCommunity(JoinCommunity), | 61 | JoinCommunity(JoinCommunity), |
| 58 | LeaveCommunity(LeaveCommunity), | 62 | LeaveCommunity(LeaveCommunity), |
| 59 | IsUserIn(IsUserIn), | 63 | IsUserIn(IsUserIn), |
crates/common/src/request/message.rsmodified+32 -0
| @@ -96,3 +96,35 @@ impl DeleteMessage { | |||
| 96 | &self.message_id | 96 | &self.message_id |
| 97 | } | 97 | } |
| 98 | } | 98 | } |
| 99 | + | ||
| 100 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 101 | +pub struct ReadMessageByCommunityIDWithMarkerAndLimit { | ||
| 102 | + community_id: String, | ||
| 103 | + marker: String, | ||
| 104 | + limit: usize, | ||
| 105 | +} | ||
| 106 | + | ||
| 107 | +impl ReadMessageByCommunityIDWithMarkerAndLimit { | ||
| 108 | + pub fn new(community_id: &str, marker: &str, limit: usize) -> Self { | ||
| 109 | + let community_id = community_id.to_string(); | ||
| 110 | + let marker = marker.to_string(); | ||
| 111 | + | ||
| 112 | + Self { | ||
| 113 | + community_id, | ||
| 114 | + marker, | ||
| 115 | + limit, | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + pub fn get_community_id(&self) -> &str { | ||
| 120 | + &self.community_id | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + pub fn get_marker(&self) -> &str { | ||
| 124 | + self.marker.as_str() | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + pub fn get_limit(&self) -> usize { | ||
| 128 | + self.limit | ||
| 129 | + } | ||
| 130 | +} | ||
crates/common/src/response.rsmodified+1 -0
| @@ -22,6 +22,7 @@ pub enum Response { | |||
| 22 | ReadMessage(Message), | 22 | ReadMessage(Message), |
| 23 | UpdateMessage(Message), | 23 | UpdateMessage(Message), |
| 24 | DeleteMessage(Message), | 24 | DeleteMessage(Message), |
| 25 | + ReadMessageByCommunityIDWithMarkerAndLimit(Vec<Message>), | ||
| 25 | JoinCommunity, | 26 | JoinCommunity, |
| 26 | LeaveCommunity, | 27 | LeaveCommunity, |
| 27 | IsUserIn(bool), | 28 | IsUserIn(bool), |