From feef6104c61d14376dabef889c6c5b4f5c57accf Mon Sep 17 00:00:00 2001 From: shockrah Date: Sat, 23 Jan 2021 15:14:15 -0800 Subject: [PATCH] * Changed a local var MAX to a const module level alias MAX_MESSAGES + Adding limiter to get_time_range --- server-api/db/src/messages.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/server-api/db/src/messages.rs b/server-api/db/src/messages.rs index 8790ed8..db72679 100644 --- a/server-api/db/src/messages.rs +++ b/server-api/db/src/messages.rs @@ -21,6 +21,7 @@ pub struct Message { pub channel_id: UBigInt } +const MAX_MESSAGES: u64 = 1000; #[async_trait] impl FromDB for Message { type Row = Option<(UBigInt, BigInt, VarChar, UBigInt, UBigInt)>; @@ -155,7 +156,7 @@ impl Message { } - pub async fn get_time_range(p: &Pool, channel_id: UBigInt, start: BigInt, end: BigInt) -> Result, SqlError> { + pub async fn get_time_range(p: &Pool, channel_id: UBigInt, start: BigInt, end: BigInt, limit: Option) -> Result, SqlError> { //! @returns on success : Set(Vec) //! @returns on userfail: RestrictedInput(message) //! @returns on error : Err(SqlError) @@ -165,13 +166,25 @@ impl Message { } else { let conn = p.get_conn().await?; - let q = "SELECT id, time, content, author_id FROM messages WHERE channel_id = :channel AND time >= :start AND time < :end"; + let limit = if let Some(limit) = limit { + match limit { + 1 ..= MAX_MESSAGES => limit, + _ => MAX_MESSAGES + } + } else { + MAX_MESSAGES + }; + + let q = "SELECT id, time, content, author_id FROM messages + WHERE channel_id = :channel AND time >= :start AND time < :end + LIMIT :limit"; let select_result = conn.prep_exec( q, params!{ "start" => start, "end" => end, - "channel" => channel_id + "channel" => channel_id, + "limit" => limit }).await?; let(_conn, messages) = select_result.map_and_drop(|row| { @@ -194,14 +207,13 @@ impl Message { //! @returns on success : Set(Vec) //! @returns on failure : Err(SqlError) let conn = p.get_conn().await?; - const MAX: u64 = 1000; let limit = if let Some(limit) = limit{ match limit { - 1 ..= MAX => limit, - _ => MAX + 1 ..= MAX_MESSAGES => limit, + _ => MAX_MESSAGES } } else { - MAX // messages at a time + MAX_MESSAGES // messages at a time }; let q = "SELECT id, time, content, author_id FROM messages WHERE