! First pass base implementation for /messages/recent

This api method is made for clients to easily say "give me the n[1,100] latest
messages in some channel
This commit contains code that is largely untested but contains some base code
such that I can correct issues in the next _tested_ commit
This commit is contained in:
shockrah
2021-04-17 15:55:10 -07:00
parent 591e993329
commit bb2201c00e
4 changed files with 56 additions and 1 deletions

View File

@@ -77,6 +77,7 @@ async fn route_dispatcher(
(POST, routes::MESSAGE_SEND) => messages::send_message(pool, resp, body, headers, params).await,
(GET, routes::MESSAGE_TIME_RANGE) => messages::get_by_time(pool, resp, params).await,
(GET, routes::MESSAGE_FROM_ID) =>messages::from_id(pool, resp, params).await,
(GET, routes::MESSAGE_LAST_N) => messages::recent_messages(pool, resp, params).await,
/* ADMIN */
(POST, routes::SET_PERMS_BY_ADMIN) => admin::set_permissions(pool, resp, params).await,
/* MEMBERS */

View File

@@ -172,3 +172,32 @@ pub async fn from_id(pool: &Pool, response: &mut Response<Body>, params: HashMap
*response.status_mut() = StatusCode::BAD_REQUEST;
}
}
pub async fn recent_messages(pool: &Pool, response: &mut Response<Body>, params: HashMap<String, String>) {
let limit = qs_param!(params, "limit", i32);
let channel_id = qs_param!(params, "channel_id", u64);
if channel_id.is_some() && limit.is_some() {
// NOTE: using match here to capture+log the error from the database
match Message::last_n(pool, limit.unwrap(), channel_id.unwrap()).await {
Ok(dbresp) => {
match dbresp {
// 200
db::Response::Set(messages) =>
set_json_body(response, json!({"messages": messages})),
// 400
db::Response::RestrictedInput(msg) => {
*response.status_mut() = StatusCode::BAD_REQUEST;
set_json_body(response, json!({"error": msg}));
},
// unreachable branch
_ => {}
}
},
Err(e) => {
eprintln!("{}", e);
}
}
} else {
}
}

View File

@@ -12,7 +12,8 @@ pub const CHANNELS_DELETE: Rstr = "/channels/delete"; // requires @name perms:
pub const MESSAGE_SEND: Rstr = "/message/send"; // requires @content perms::MESSAGE_SEND
pub const MESSAGE_TIME_RANGE: Rstr = "/message/get_range"; // requires @channel(id) @start-time @end-time
pub const MESSAGE_FROM_ID: Rstr = "/message/from_id"; // requires @channel(id) requires @start(id) @<optional>limit(1..1000)
pub const MESSAGE_FROM_ID: Rstr = "/message/from_id"; // requires @channel_id requires @start(id) @<optional>limit(1..1000)
pub const MESSAGE_LAST_N: Rstr = "/message/recent"; // requires @channel_id requires @limit(1..100)
pub const GET_ONLINE_MEMBERS: Rstr = "/members/get_online"; // requires none
pub const GET_MEMBER: Rstr = "/members/single"; // requires @member_id