Base implementation of new /message/from_id route
! Requires unit testing ! Written with max response length in mind, (still ignores mem limits howevr)
This commit is contained in:
@@ -190,5 +190,44 @@ impl Message {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_from_id(p: &Pool, channel_id: UBigInt, start: UBigInt, limit: Option<UBigInt>) -> Result<Response<Self>, SqlError> {
|
||||
//! @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
|
||||
}
|
||||
} else {
|
||||
MAX // messages at a time
|
||||
};
|
||||
|
||||
let q = "SELECT id, time, content, author_id FROM messages WHERE
|
||||
channel_id = :channel AND id >= :start LIMIT :limit";
|
||||
|
||||
let params = params!{
|
||||
"channel" => channel_id,
|
||||
"start" => start,
|
||||
"limit" => limit
|
||||
};
|
||||
|
||||
let select_result = conn.prep_exec(q, params).await?;
|
||||
|
||||
let (_conn, messages) = select_result.map_and_drop(|row| {
|
||||
type Tuple = (UBigInt, BigInt, String, UBigInt);
|
||||
let (id, time, content, author_id): Tuple = mysql_async::from_row(row);
|
||||
Self {
|
||||
id,
|
||||
time,
|
||||
content,
|
||||
author_id,
|
||||
channel_id
|
||||
}
|
||||
}).await?;
|
||||
|
||||
Ok(Response::Set(messages))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user