Adding new routes for /members/get_online & /members/me

Passing the previous tests as well
This commit is contained in:
shockrah 2021-02-20 20:15:43 -08:00
parent 34736bef3f
commit 7263df8928
4 changed files with 45 additions and 14 deletions

View File

@ -9,7 +9,7 @@ use serde::Serialize;
use crate::{Response, no_conn, sql_err};
use crate::{UBigInt, BigInt, Integer, VarChar};
use crate::common::{FromDB};
use crate::common::FromDB;
#[derive(Debug, Serialize)]
pub struct Member {
@ -38,6 +38,8 @@ impl FromDB<Member, Integer> for Member {
type Row = Option<(VarChar, VarChar, BigInt, Integer, UBigInt)>;
async fn get(p: &Pool, id: UBigInt) -> Response<Self> {
//! @returns Row on success
//! @returns Other on failure
if let Ok(conn) = p.get_conn().await {
let q = "SELECT secret, name, joindate, status, permissions FROM members WHERE id = :id";
let db_res : Result<(Conn, Self::Row), SqlError>
@ -45,7 +47,7 @@ impl FromDB<Member, Integer> for Member {
if let Ok((_, row)) = db_res {
return match row {
Some(row) => Response::Row(Self {
id: id,
id,
secret: row.0,
name: row.1,
joindate: row.2,
@ -114,20 +116,20 @@ impl FromDB<Member, Integer> for Member {
// i.e. we should be able to get user 100 -> 150 instead of just the
// first 1000 people
let q =
"SELECT id, name, status, permissions FROM memebrs
WHERE status = :stat LIMIT 1000"; // high limit for now
"SELECT id, name, status, permissions FROM members
WHERE status = :stat LIMIT 100"; // high limit for now
if let Ok(query) = conn.prep_exec(q, params!{"stat" => status}).await {
let mapping_r = query.map_and_drop(|row| {
let (id, name, status, permissions): (UBigInt, VarChar, Integer, UBigInt) =
mysql_async::from_row(row);
Member {
id: id,
id,
secret: "".into(), // no show for obv reasons
name: name,
name,
joindate: 0, // doesn't matter
status: status,
permissions: permissions
status,
permissions
}
}).await;
match mapping_r {
@ -136,7 +138,7 @@ impl FromDB<Member, Integer> for Member {
}
}
else {
Response::Other(sql_err!("db::Members::filter"))
Response::Other(sql_err!("Initial query faile: db::Members::filter"))
}
}
_ => Response::Other(sql_err!("err"))
@ -183,7 +185,7 @@ impl Member {
if let Some(id) = opt_id {
return Ok(Response::Row(Self {
id: id,
id,
secret: secret.into(),
name: name.into(),
joindate: now,

View File

@ -72,6 +72,7 @@ async fn route_dispatcher(
(POST, routes::SET_PERMS_BY_ADMIN) => admin::set_permissions(pool, resp, params).await,
/* MEMBERS */
(GET, routes::GET_ONLINE_MEMBERS) => members::get_online_members(pool, resp).await,
(GET, routes::GET_MYSELF) => members::get_self(pool, resp, params).await,
/* OWNER */
(POST, routes::SET_NEW_ADMIN) => admin::new_admin(pool, resp, params).await,
/* META ROUTE */

View File

@ -1,21 +1,48 @@
use std::collections::HashMap;
use hyper::{Response, Body, StatusCode};
use mysql_async::Pool;
use serde_json::json;
use db::member::STATUS_ONLINE;
use db::common::FromDB;
use db::member::Member;
use db::Response::*;
use crate::http::set_json_body;
use crate::qs_param;
pub async fn get_online_members(p: &Pool, response: &mut Response<Body>) {
// TODO: at some point we should provide a way of not querying literally every user in
// existance
// TODO: loggin at some point or something idklol
return match db::channels::Channel::filter(p, STATUS_ONLINE).await {
db::Response::Set(users) => {
return match Member::filter(p, STATUS_ONLINE).await {
Set(users) => {
set_json_body(response, json!(users));
},
db::Response::Other(_msg) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR,
db::Response::Other(e) => {
eprintln!("{}", e);
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
},
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
}
}
pub async fn get_self(p: &Pool, response: &mut Response<Body>, params: HashMap<String, String>) {
let uid = qs_param!(params, "id", u64).unwrap();
match Member::get(p, uid).await {
Row(user) => {
set_json_body(response, json!({
"id" : user.id,
"name" : user.name,
"joindate" : user.joindate,
"permissions" : user.permissions
}));
},
Other(e) => eprintln!("{}", e),
_ => {
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
}
};
}

View File

@ -14,7 +14,8 @@ pub const MESSAGE_SEND: Rstr = "/message/send"; // requires @content per
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 GET_ONLINE_MEMBERS: Rstr = "/members/get_online";
pub const GET_ONLINE_MEMBERS: Rstr = "/members/get_online"; // requires none
pub const GET_MYSELF: Rstr = "/members/me"; // @requires none
// ADMIN ROUTES