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:
shockrah
2021-01-19 22:26:53 -08:00
parent 3b3fa14496
commit 01320899a3
2 changed files with 92 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ use serde_json::Value;
use hyper::header::HeaderValue;
use serde_json::json;
use db::messages::Message;
pub async fn get_by_time(pool: &Pool, response: &mut Response<Body>, params: Value) {
/*
@@ -30,7 +31,7 @@ pub async fn get_by_time(pool: &Pool, response: &mut Response<Body>, params: Val
Some(val) => val.as_i64(),
None => None
};
use db::messages::Message;
// TODO: flatten me mommy
match (channel, start_time, end_time) {
(Some(channel), Some(start), Some(end)) => {
match Message::get_time_range(pool, channel, start, end).await {
@@ -108,6 +109,57 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Va
}
}
pub async fn from_id(pool: &Pool, response: &mut Response<Body>, params: Value) {
/*
* @start-id: u64
* @limit: optional<u64>
* @channel: u64
* {
* "channel": 1,
* "start": 123,
* "limit": 100
* }
*/
let channel = match params.get("channel") {
Some(chan_v) => chan_v.as_u64(),
None => None
};
let start_id = match params.get("start-id") {
Some(val) => val.as_u64(),
None => None
};
let limit = match params.get("limit") {
Some(val) => val.as_u64(),
None => None
};
// TODO: untested lmao
if let (Some(channel), Some(start_id)) = (channel, start_id) {
match Message::get_from_id(pool, channel, start_id, limit).await {
Ok(db_response) => {
match db_response {
db::Response::Set(messages) => {
response.headers_mut().insert(
"Content-Type",
HeaderValue::from_static("application/json"));
let payload = json!({"messages": messages});
*response.body_mut() = Body::from(payload.to_string());
},
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
};
},
Err(err) => {
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
eprintln!("{}", err);
}
};
}
else {
*response.status_mut() = StatusCode::BAD_REQUEST;
}
}
#[cfg(test)]
mod messaging_tests {
use crate::testing::{get_pool, hyper_resp};