forge
Explore
Log in

Don't you dare go Hollow

Add message history request

Browse files at this commit

ea10cf3d99ebde0dd5f6ac466c4fa55a2b22d91cAhmet Kaan GümüşParent: 4367327
  • feat
    • add message history request
Show full message
✨ feat(common): add message history request

ReadMessageByCommunityIDWithMarkerAndLimit pages a community's messages
from a marker id. Response bound is now derived from MESSAGE_PAGE_LIMIT,
since a page no longer fits in a single-message budget.
crates/common/src/constant.rsmodified+2 -0
@@ -4,3 +4,5 @@ pub const MAXIMUM_MESSAGE_LENGTH: usize = 1024;
44 pub const MINIMUM_MESSAGE_LENGTH: usize = 1;
55 pub const MAXIMUM_COMMUNITY_NAME_LENGTH: usize = 16;
66 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 @@
11 use quinn::{RecvStream, SendStream};
22
33 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,
58 response::Response,
69 };
710
811 pub const AUTHENTICATION_MAX_READ_LENGHT: usize = 64;
912 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;
1115
1216 pub struct Network;
1317
@@ -42,7 +46,7 @@ impl Network {
4246 send_stream.write_all(&bitcode::encode(request)).await?;
4347 send_stream.finish()?;
4448
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?;
4650
4751 Ok(bitcode::decode::<Response>(&response)?)
4852 }
@@ -65,10 +69,10 @@ impl Network {
6569 receive_stream.read_exact(&mut length_bytes).await?;
6670
6771 let length = u32::from_le_bytes(length_bytes) as usize;
68- if length > RESPONCE_MAX_READ_LENGHT {
72+ if length > MESSAGE_MAX_ENCODED_LENGTH {
6973 return Err(crate::error::network::Error::ReadBoundExceed(
7074 length,
71- RESPONCE_MAX_READ_LENGHT,
75+ RESPONSE_MAX_READ_LENGHT,
7276 )
7377 .into());
7478 }
crates/common/src/request.rsmodified+5 -1
@@ -2,7 +2,10 @@ use bitcode::{Decode, Encode};
22
33 use crate::request::{
44 community::{CreateCommunity, DeleteCommunity, ReadCommunity, UpdateCommunity},
5- message::{CreateMessage, DeleteMessage, ReadMessage, UpdateMessage},
5+ message::{
6+ CreateMessage, DeleteMessage, ReadMessage, ReadMessageByCommunityIDWithMarkerAndLimit,
7+ UpdateMessage,
8+ },
69 user::{CreateUser, DeleteUser, ReadUser, UpdateUser},
710 user_community::{CommunityOf, IsUserIn, JoinCommunity, LeaveCommunity, UsersIn},
811 };
@@ -54,6 +57,7 @@ pub enum Request {
5457 ReadMessage(ReadMessage),
5558 UpdateMessage(UpdateMessage),
5659 DeleteMessage(DeleteMessage),
60+ ReadMessageByCommunityIDWithMarkerAndLimit(ReadMessageByCommunityIDWithMarkerAndLimit),
5761 JoinCommunity(JoinCommunity),
5862 LeaveCommunity(LeaveCommunity),
5963 IsUserIn(IsUserIn),
crates/common/src/request/message.rsmodified+32 -0
@@ -96,3 +96,35 @@ impl DeleteMessage {
9696 &self.message_id
9797 }
9898 }
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 {
2222 ReadMessage(Message),
2323 UpdateMessage(Message),
2424 DeleteMessage(Message),
25+ ReadMessageByCommunityIDWithMarkerAndLimit(Vec<Message>),
2526 JoinCommunity,
2627 LeaveCommunity,
2728 IsUserIn(bool),