new get_user_list endpoint to well, get the list of online users

This commit is contained in:
shockrah 2020-03-17 22:27:10 -07:00
parent eb76c070bd
commit 3b3059e6d2
5 changed files with 67 additions and 6 deletions

View File

@ -26,6 +26,7 @@ mod users;
mod channels; mod channels;
use invites::*; use invites::*;
use channels::*; use channels::*;
use users::*;
#[database("freechat")] #[database("freechat")]
pub struct DBConn(diesel::MysqlConnection); pub struct DBConn(diesel::MysqlConnection);
@ -39,6 +40,9 @@ pub fn rocket() -> rocket::Rocket {
.mount("/channels", routes![ .mount("/channels", routes![
get_voice_channels get_voice_channels
]) ])
.mount("/users", routes![
remove_user, get_user_list
])
.attach(Template::fairing()) .attach(Template::fairing())
.attach(DBConn::fairing()) .attach(DBConn::fairing())
} }

View File

@ -18,9 +18,9 @@ pub struct User {
} }
pub const USER_OFFLINE: i32 = 1; pub const USER_OFFLINE: i32 = 1;
pub const USER_ONLINE: i32 = 2; pub const _USER_ONLINE: i32 = 2;
pub const USER_AWAY: i32 = 3; pub const _USER_AWAY: i32 = 3;
pub const USER_DO_NOT_DISTRUB: i32 = 4; pub const _USER_DO_NOT_DISTRUB: i32 = 4;
// These are to be use specifically for discerning between channel types // These are to be use specifically for discerning between channel types
pub const VOICE_CHANNEL: i32 = 1; pub const VOICE_CHANNEL: i32 = 1;

View File

@ -19,6 +19,7 @@ pub struct NewUserResponse {
pub struct OnlineUser { pub struct OnlineUser {
pub id: u64, pub id: u64,
pub username: String, pub username: String,
status: i32,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]

View File

@ -22,6 +22,6 @@ pub fn new_key() -> String { // Returns a random string which we later hash w
buf buf
} }
else { else {
"".into() "".into() // like with new_user_id if this really fires we're screwed anyway
} }
} }

View File

@ -1,9 +1,12 @@
use chrono::Utc; use chrono::Utc;
use rocket_contrib::json::{Json, JsonValue};
use crate::rand_utils::{new_user_id, new_key}; use crate::rand_utils::{new_user_id, new_key};
use crate::models::User; use crate::models::{User, USER_OFFLINE};
use crate::payload::NewUserResponse; use crate::payload::NewUserResponse;
use crate::{DBConn, schema};
use diesel::{self, prelude::*};
use diesel::result::Error;
// Returns a struct of payload::NewUserInfo
pub fn create_new_user() -> User { pub fn create_new_user() -> User {
let uid = new_user_id(); let uid = new_user_id();
let uname = format!("User:#{}", uid); let uname = format!("User:#{}", uid);
@ -13,6 +16,7 @@ pub fn create_new_user() -> User {
username: uname, username: uname,
key: rstring, key: rstring,
date: Utc::now().timestamp() as u64, date: Utc::now().timestamp() as u64,
status: USER_OFFLINE,
} }
} }
@ -34,12 +38,64 @@ pub fn new_user_response(user: &Option<User>, msg: Option<&'static str>) -> NewU
} }
} }
} }
#[delete("/remove/<hash>")]
pub fn remove_user(hash: u64, conn: DBConn) -> JsonValue {
// Attempt to remove the user first
use schema::users::dsl::*;
use diesel::result::Error;
let res: Result<usize, Error> = diesel::delete(users.filter(userid.eq(hash))).execute(&conn.0);
match res {
Ok(_res) => json!({"staus":"ok"}),
Err(_e) => json!({"status":"err"}),
}
}
#[get("/list")]
pub fn get_user_list(conn: DBConn) -> Json<Vec<(u64, String, i32)>> {
use schema::users::dsl::*;
let db_response: Result<Vec<(u64, String, i32)>, Error> = users.select((userid, username, status))
.filter(status.ne(USER_OFFLINE))
.load::<(u64, String, i32)>(&conn.0);
match db_response {
Ok(data) => Json(data),
Err(_e) => Json(vec!())
}
}
#[cfg(test)] #[cfg(test)]
mod user_tests { mod user_tests {
use super::create_new_user; use super::create_new_user;
use super::*;
use rocket;
use rocket::local::Client;
use rocket::http::Status;
#[test] #[test]
fn user_struct() { fn user_struct() {
println!("{:?}", create_new_user()); println!("{:?}", create_new_user());
} }
#[test]
fn remove_user_pass() {
let user = create_new_user();
println!("Client to remove: {:?}", user);
let app = rocket::ignite()
.mount("/users", routes![remove_user])
.attach(DBConn::fairing());
let client = Client::new(app).expect("Bad rocket instance");
let mut response = client.delete(format!("/users/remove/{}", user.userid)).dispatch();
let body: String = response.body_string().unwrap();
assert_eq!(response.status(), Status::Ok);
println!("{}", body);
}
//TODO: fail case for removing users
} }