Read messages by community with marker and limit
Browse files at this commit18f0ac48e1efee53499b701ff25799cd46cab013Parent: ea10cf3- feat
- read messages by community with marker and limit
4 changed files +136 -8
- crates/server/src/message.rs +86 -0
- crates/server/src/message/database.rs +27 -0
- crates/server/src/network.rs +3 -3
- crates/server/src/request.rs +20 -5
crates/server/src/message.rsmodified+86 -0
| @@ -146,6 +146,21 @@ impl Message { | |||
| 146 | database::read_by_community_id(community_id, database_connection).await | 146 | database::read_by_community_id(community_id, database_connection).await |
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | + pub async fn read_by_community_id_with_marker_and_limit( | ||
| 150 | + community_id: &CommunityID, | ||
| 151 | + marker: &MessageID, | ||
| 152 | + limit: usize, | ||
| 153 | + database_connection: &DB, | ||
| 154 | + ) -> Result<Vec<Message>, Error> { | ||
| 155 | + database::read_by_community_id_with_marker_and_limit( | ||
| 156 | + community_id, | ||
| 157 | + marker, | ||
| 158 | + limit, | ||
| 159 | + database_connection, | ||
| 160 | + ) | ||
| 161 | + .await | ||
| 162 | + } | ||
| 163 | + | ||
| 149 | pub async fn read_by_user_id( | 164 | pub async fn read_by_user_id( |
| 150 | user_id: &UserID, | 165 | user_id: &UserID, |
| 151 | database_connection: &DB, | 166 | database_connection: &DB, |
| @@ -283,6 +298,77 @@ mod tests { | |||
| 283 | assert_eq!(expected_result, searched_messages); | 298 | assert_eq!(expected_result, searched_messages); |
| 284 | } | 299 | } |
| 285 | 300 | ||
| 301 | + #[tokio::test] | ||
| 302 | + async fn read_by_community_id_with_marker_and_limit() { | ||
| 303 | + let (database_connection, _temp_directory) = test::get_database().await; | ||
| 304 | + | ||
| 305 | + let username = "Tahinli"; | ||
| 306 | + let name = username; | ||
| 307 | + | ||
| 308 | + let user_1 = User::create(username, &database_connection).await.unwrap(); | ||
| 309 | + let user_2 = User::create("Not Tahinli", &database_connection) | ||
| 310 | + .await | ||
| 311 | + .unwrap(); | ||
| 312 | + let community = Community::create(name, &database_connection).await.unwrap(); | ||
| 313 | + | ||
| 314 | + let message_ = name; | ||
| 315 | + let created_message_1 = | ||
| 316 | + Message::create(&user_1, &community, message_, &database_connection) | ||
| 317 | + .await | ||
| 318 | + .unwrap(); | ||
| 319 | + let created_message_2 = | ||
| 320 | + Message::create(&user_2, &community, message_, &database_connection) | ||
| 321 | + .await | ||
| 322 | + .unwrap(); | ||
| 323 | + | ||
| 324 | + let created_message_3 = | ||
| 325 | + Message::create(&user_2, &community, message_, &database_connection) | ||
| 326 | + .await | ||
| 327 | + .unwrap(); | ||
| 328 | + | ||
| 329 | + let created_message_2_id = created_message_2.get_id().to_owned(); | ||
| 330 | + let created_message_3_id = created_message_3.get_id().to_owned(); | ||
| 331 | + | ||
| 332 | + let mut expected_result = vec![created_message_1, created_message_2, created_message_3]; | ||
| 333 | + | ||
| 334 | + let searched_messages = Message::read_by_community_id_with_marker_and_limit( | ||
| 335 | + community.get_id(), | ||
| 336 | + &created_message_3_id, | ||
| 337 | + 3, | ||
| 338 | + &database_connection, | ||
| 339 | + ) | ||
| 340 | + .await | ||
| 341 | + .unwrap(); | ||
| 342 | + | ||
| 343 | + assert_eq!(expected_result, searched_messages); | ||
| 344 | + | ||
| 345 | + expected_result.remove(0); | ||
| 346 | + | ||
| 347 | + let searched_messages = Message::read_by_community_id_with_marker_and_limit( | ||
| 348 | + community.get_id(), | ||
| 349 | + &created_message_3_id, | ||
| 350 | + 2, | ||
| 351 | + &database_connection, | ||
| 352 | + ) | ||
| 353 | + .await | ||
| 354 | + .unwrap(); | ||
| 355 | + | ||
| 356 | + assert_eq!(expected_result, searched_messages); | ||
| 357 | + | ||
| 358 | + expected_result.pop(); | ||
| 359 | + | ||
| 360 | + let searched_messages = Message::read_by_community_id_with_marker_and_limit( | ||
| 361 | + community.get_id(), | ||
| 362 | + &created_message_2_id, | ||
| 363 | + 1, | ||
| 364 | + &database_connection, | ||
| 365 | + ) | ||
| 366 | + .await | ||
| 367 | + .unwrap(); | ||
| 368 | + | ||
| 369 | + assert_eq!(expected_result, searched_messages); | ||
| 370 | + } | ||
| 371 | + | ||
| 286 | #[tokio::test] | 372 | #[tokio::test] |
| 287 | async fn read_by_user_id() { | 373 | async fn read_by_user_id() { |
| 288 | let (database_connection, _temp_directory) = test::get_database().await; | 374 | let (database_connection, _temp_directory) = test::get_database().await; |
crates/server/src/message/database.rsmodified+27 -0
| @@ -41,6 +41,33 @@ pub(super) async fn read_by_community_id( | |||
| 41 | Ok(result) | 41 | Ok(result) |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | +pub(super) async fn read_by_community_id_with_marker_and_limit( | ||
| 45 | + community_id: &CommunityID, | ||
| 46 | + marker: &MessageID, | ||
| 47 | + limit: usize, | ||
| 48 | + database_connection: &DB, | ||
| 49 | +) -> Result<Vec<Message>, Error> { | ||
| 50 | + let query = " | ||
| 51 | + SELECT * FROM type::table($table) | ||
| 52 | + WHERE community_id = $community_id AND id <= $before | ||
| 53 | + ORDER BY id DESC LIMIT $limit; | ||
| 54 | + "; | ||
| 55 | + | ||
| 56 | + let mut result = database_connection | ||
| 57 | + .query(query) | ||
| 58 | + .bind(("table", MESSAGE_TABLE)) | ||
| 59 | + .bind(("community_id", community_id.as_record_id())) | ||
| 60 | + .bind(("limit", limit)) | ||
| 61 | + .bind(("before", marker.as_record_id())) | ||
| 62 | + .await? | ||
| 63 | + .check()?; | ||
| 64 | + | ||
| 65 | + let mut result = result.take::<Vec<_>>(0)?; | ||
| 66 | + result.reverse(); | ||
| 67 | + | ||
| 68 | + Ok(result) | ||
| 69 | +} | ||
| 70 | + | ||
| 44 | pub(super) async fn read_by_user_id( | 71 | pub(super) async fn read_by_user_id( |
| 45 | user_id: &UserID, | 72 | user_id: &UserID, |
| 46 | database_connection: &DB, | 73 | database_connection: &DB, |
crates/server/src/network.rsmodified+3 -3
| @@ -106,7 +106,7 @@ async fn establish_connection( | |||
| 106 | 106 | ||
| 107 | let user_id = user.get_id().to_owned(); | 107 | let user_id = user.get_id().to_owned(); |
| 108 | tokio::spawn(listen( | 108 | tokio::spawn(listen( |
| 109 | - Some(user_id), | 109 | + user_id, |
| 110 | connection.clone(), | 110 | connection.clone(), |
| 111 | message_sender, | 111 | message_sender, |
| 112 | database_connection.clone(), | 112 | database_connection.clone(), |
| @@ -124,12 +124,12 @@ async fn establish_connection( | |||
| 124 | } | 124 | } |
| 125 | 125 | ||
| 126 | async fn listen( | 126 | async fn listen( |
| 127 | - user_id: Option<UserID>, | 127 | + user_id: UserID, |
| 128 | connection: Connection, | 128 | connection: Connection, |
| 129 | message_sender: mpsc::Sender<common::message::Message>, | 129 | message_sender: mpsc::Sender<common::message::Message>, |
| 130 | database_connection: DB, | 130 | database_connection: DB, |
| 131 | ) { | 131 | ) { |
| 132 | - let read_and_answer = async |user_id: Option<UserID>, | 132 | + let read_and_answer = async |user_id: UserID, |
| 133 | send_stream: SendStream, | 133 | send_stream: SendStream, |
| 134 | receive_stream: RecvStream, | 134 | receive_stream: RecvStream, |
| 135 | message_sender: mpsc::Sender<common::message::Message>, | 135 | message_sender: mpsc::Sender<common::message::Message>, |
crates/server/src/request.rsmodified+20 -5
| @@ -11,7 +11,7 @@ use crate::{ | |||
| 11 | }; | 11 | }; |
| 12 | 12 | ||
| 13 | pub async fn handle_request( | 13 | pub async fn handle_request( |
| 14 | - user_id: Option<UserID>, | 14 | + user_id: UserID, |
| 15 | request: Request, | 15 | request: Request, |
| 16 | database_connection: &DB, | 16 | database_connection: &DB, |
| 17 | ) -> Response { | 17 | ) -> Response { |
| @@ -66,10 +66,6 @@ pub async fn handle_request( | |||
| 66 | Community::create(create_community.get_community_name(), database_connection) | 66 | Community::create(create_community.get_community_name(), database_connection) |
| 67 | .await?; | 67 | .await?; |
| 68 | 68 | ||
| 69 | - let Some(user_id) = user_id else { | ||
| 70 | - unreachable!("Unexpected, user must be logged in"); | ||
| 71 | - }; | ||
| 72 | - | ||
| 73 | JoinCommunity::apply(&user_id, community.get_id(), database_connection).await?; | 69 | JoinCommunity::apply(&user_id, community.get_id(), database_connection).await?; |
| 74 | Ok(Response::CreateCommunity(community.into())) | 70 | Ok(Response::CreateCommunity(community.into())) |
| 75 | } | 71 | } |
| @@ -147,6 +143,25 @@ pub async fn handle_request( | |||
| 147 | 143 | ||
| 148 | Ok(Response::DeleteMessage(message.into())) | 144 | Ok(Response::DeleteMessage(message.into())) |
| 149 | } | 145 | } |
| 146 | + Request::ReadMessageByCommunityIDWithMarkerAndLimit( | ||
| 147 | + read_message_by_community_id_with_marker_and_limit, | ||
| 148 | + ) => { | ||
| 149 | + let messages = Message::read_by_community_id_with_marker_and_limit( | ||
| 150 | + &read_message_by_community_id_with_marker_and_limit | ||
| 151 | + .get_community_id() | ||
| 152 | + .into(), | ||
| 153 | + &read_message_by_community_id_with_marker_and_limit | ||
| 154 | + .get_marker() | ||
| 155 | + .into(), | ||
| 156 | + read_message_by_community_id_with_marker_and_limit.get_limit(), | ||
| 157 | + database_connection, | ||
| 158 | + ) | ||
| 159 | + .await?; | ||
| 160 | + | ||
| 161 | + Ok(Response::ReadMessageByCommunityIDWithMarkerAndLimit( | ||
| 162 | + messages.into_iter().map(Into::into).collect(), | ||
| 163 | + )) | ||
| 164 | + } | ||
| 150 | Request::JoinCommunity(join_community) => { | 165 | Request::JoinCommunity(join_community) => { |
| 151 | JoinCommunity::apply( | 166 | JoinCommunity::apply( |
| 152 | &join_community.get_user_id().into(), | 167 | &join_community.get_user_id().into(), |