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 | 4 | pub const MINIMUM_MESSAGE_LENGTH: usize = 1; |
| 5 | 5 | pub const MAXIMUM_COMMUNITY_NAME_LENGTH: usize = 16; |
| 6 | 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 | 1 | use quinn::{RecvStream, SendStream}; |
| 2 | 2 | |
| 3 | 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 | 8 | response::Response, |
| 6 | 9 | }; |
| 7 | 10 | |
| 8 | 11 | pub const AUTHENTICATION_MAX_READ_LENGHT: usize = 64; |
| 9 | 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 | 16 | pub struct Network; |
| 13 | 17 | |
| @@ -42,7 +46,7 @@ impl Network { | ||
| 42 | 46 | send_stream.write_all(&bitcode::encode(request)).await?; |
| 43 | 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 | 51 | Ok(bitcode::decode::<Response>(&response)?) |
| 48 | 52 | } |
| @@ -65,10 +69,10 @@ impl Network { | ||
| 65 | 69 | receive_stream.read_exact(&mut length_bytes).await?; |
| 66 | 70 | |
| 67 | 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 | 73 | return Err(crate::error::network::Error::ReadBoundExceed( |
| 70 | 74 | length, |
| 71 | - RESPONCE_MAX_READ_LENGHT, | |
| 75 | + RESPONSE_MAX_READ_LENGHT, | |
| 72 | 76 | ) |
| 73 | 77 | .into()); |
| 74 | 78 | } |
crates/common/src/request.rsmodified+5 -1
| @@ -2,7 +2,10 @@ use bitcode::{Decode, Encode}; | ||
| 2 | 2 | |
| 3 | 3 | use crate::request::{ |
| 4 | 4 | community::{CreateCommunity, DeleteCommunity, ReadCommunity, UpdateCommunity}, |
| 5 | - message::{CreateMessage, DeleteMessage, ReadMessage, UpdateMessage}, | |
| 5 | + message::{ | |
| 6 | + CreateMessage, DeleteMessage, ReadMessage, ReadMessageByCommunityIDWithMarkerAndLimit, | |
| 7 | + UpdateMessage, | |
| 8 | + }, | |
| 6 | 9 | user::{CreateUser, DeleteUser, ReadUser, UpdateUser}, |
| 7 | 10 | user_community::{CommunityOf, IsUserIn, JoinCommunity, LeaveCommunity, UsersIn}, |
| 8 | 11 | }; |
| @@ -54,6 +57,7 @@ pub enum Request { | ||
| 54 | 57 | ReadMessage(ReadMessage), |
| 55 | 58 | UpdateMessage(UpdateMessage), |
| 56 | 59 | DeleteMessage(DeleteMessage), |
| 60 | + ReadMessageByCommunityIDWithMarkerAndLimit(ReadMessageByCommunityIDWithMarkerAndLimit), | |
| 57 | 61 | JoinCommunity(JoinCommunity), |
| 58 | 62 | LeaveCommunity(LeaveCommunity), |
| 59 | 63 | IsUserIn(IsUserIn), |
crates/common/src/request/message.rsmodified+32 -0
| @@ -96,3 +96,35 @@ impl DeleteMessage { | ||
| 96 | 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 | 22 | ReadMessage(Message), |
| 23 | 23 | UpdateMessage(Message), |
| 24 | 24 | DeleteMessage(Message), |
| 25 | + ReadMessageByCommunityIDWithMarkerAndLimit(Vec<Message>), | |
| 25 | 26 | JoinCommunity, |
| 26 | 27 | LeaveCommunity, |
| 27 | 28 | IsUserIn(bool), |