* Changed a local var MAX to a const module level alias MAX_MESSAGES

+ Adding limiter to get_time_range
This commit is contained in:
shockrah 2021-01-23 15:14:15 -08:00
parent d55a8420ac
commit feef6104c6

View File

@ -21,6 +21,7 @@ pub struct Message {
pub channel_id: UBigInt
}
const MAX_MESSAGES: u64 = 1000;
#[async_trait]
impl FromDB<Message, (BigInt, UBigInt)> 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<Response<Self>, SqlError> {
pub async fn get_time_range(p: &Pool, channel_id: UBigInt, start: BigInt, end: BigInt, limit: Option<u64>) -> Result<Response<Self>, SqlError> {
//! @returns on success : Set(Vec<Messages>)
//! @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<Messages>)
//! @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