171 lines
5.7 KiB
Rust
171 lines
5.7 KiB
Rust
/*
|
|
With this module we take care of getting all kinds of channel information out to the users
|
|
Base prefix is "/channels"
|
|
*/
|
|
use diesel::{self, prelude::*};
|
|
use rocket_contrib::json::Json;
|
|
use std::vec::Vec;
|
|
use crate::models::{InsertableChannel, Channel};
|
|
use crate::auth::AuthResult;
|
|
use crate::{DBConn, schema, payload};
|
|
use crate::err::{self, DbError};
|
|
|
|
const VOICE_CHANNEL: i32 = 1;
|
|
const TEXT_CHANNEL: i32 = 2;
|
|
|
|
#[post("/create/<qname>/<perms>/<lim>/<ctype>")]
|
|
pub fn insert_new_channel(qname: String, perms: i32, lim: i32, ctype: i32, conn: DBConn) ->
|
|
DbError<Json<payload::ChannelIndexed>, err::DbResponse> {
|
|
use schema::channels::dsl::*;
|
|
|
|
let chan = InsertableChannel {
|
|
name: qname,
|
|
permissions: perms,
|
|
limit: lim,
|
|
type_: ctype,
|
|
};
|
|
let result = diesel::insert_into(channels)
|
|
.values(&chan)
|
|
.execute(&conn.0);
|
|
|
|
match result {
|
|
Ok(_d) => Ok(Json(payload::ChannelIndexed{name:chan.name, ctype:chan.type_})),
|
|
Err(_e) => Err(err::DbResponse{err_msg:"Could not create channel"})
|
|
}
|
|
}
|
|
|
|
pub fn get_channel_by_type(conn: &MysqlConnection, ctype: i32/*short for channel type*/) -> Result<Vec<Channel>, err::DbResponse> {
|
|
use schema::channels::dsl::*;
|
|
if let Ok(result) = channels.filter(type_.eq(ctype)).load(conn) {
|
|
Ok(result)
|
|
}
|
|
else {
|
|
Err(err::DbResponse{err_msg: "/channeels/list/<type> Could not query database"})
|
|
}
|
|
|
|
}
|
|
|
|
#[get("/list/voice")]
|
|
pub fn get_voice_channels(conn: DBConn) -> AuthResult<Json<Vec<Channel>>, err::DbResponse> {
|
|
let db_result = get_channel_by_type(&conn.0, VOICE_CHANNEL);
|
|
// We have to rewrap the data given to us which is annoying but hey
|
|
// at least its safe.jpg
|
|
if let Ok(data) = db_result {
|
|
Ok(Json(data))
|
|
}
|
|
else {
|
|
Err(err::DbResponse{err_msg:"Could not query voice channels"})
|
|
}
|
|
}
|
|
|
|
|
|
#[get("/list/text")]
|
|
pub fn get_text_chanels(conn: DBConn) -> DbError<Json<Vec<Channel>>, err::DbResponse> {
|
|
let db_result = get_channel_by_type(&conn.0, TEXT_CHANNEL);
|
|
if let Ok(data) = db_result {
|
|
Ok(Json(data))
|
|
}
|
|
else {
|
|
Err(err::DbResponse{err_msg:"Could not query text channels"})
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod channel_tests {
|
|
use super::*;
|
|
use rocket;
|
|
use rocket::local::Client;
|
|
use rocket::http::Status;
|
|
use serde_json::from_str;
|
|
|
|
macro_rules! rocket_inst {
|
|
($func:expr) => {
|
|
rocket::ignite()
|
|
.mount("/channels", routes![$func])
|
|
.attach(DBConn::fairing())
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn generate_channels_test() {
|
|
use diesel::mysql::MysqlConnection;
|
|
use schema::channels::dsl::*;
|
|
let conn = MysqlConnection::establish("mysql://freechat_dev:password@localhost:3306/freechat")
|
|
.unwrap();
|
|
|
|
let app = rocket_inst!(insert_new_channel);
|
|
let client = Client::new(app).expect("bad rocket instance");
|
|
|
|
let text_names = vec!["tone", "ttwo", "tthre"];
|
|
for i in text_names.iter() {
|
|
let uri = format!("/channels/create/{}/{}/{}/{}", i, 0, 0, TEXT_CHANNEL);
|
|
let mut response = client.post(uri).dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
let body = response.body_string().unwrap();
|
|
let data: crate::payload::ChannelIndexed = from_str(&body).unwrap();
|
|
assert_eq!(data.name, i.to_string());
|
|
}
|
|
|
|
let voice_names = vec!["voiceone", "voicetwo", "voicethre"];
|
|
for i in voice_names.iter() {
|
|
let uri = format!("/channels/create/{}/{}/{}/{}", i, 0, 0, VOICE_CHANNEL);
|
|
let mut response = client.post(uri).dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
let body = response.body_string().unwrap();
|
|
let data: crate::payload::ChannelIndexed = from_str(&body).unwrap();
|
|
assert_eq!(data.name, i.to_string());
|
|
}
|
|
let _ = diesel::delete(channels).filter(id.is_not_null())
|
|
.execute(&conn)
|
|
.expect("bro what the hell delete * from channels where id is not null;");
|
|
}
|
|
|
|
fn generate_dummy_channels() {
|
|
let app = rocket_inst!(insert_new_channel);
|
|
let client = Client::new(app).expect("bad rocket instance");
|
|
|
|
let text_names = vec!["tone", "ttwo", "tthre"];
|
|
for i in text_names.iter() {
|
|
let uri = format!("/channels/create/{}/{}/{}/{}", i, 0, 0, TEXT_CHANNEL);
|
|
let mut response = client.post(uri).dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
let body = response.body_string().unwrap();
|
|
let data: crate::payload::ChannelIndexed = from_str(&body).unwrap();
|
|
assert_eq!(data.name, i.to_string());
|
|
}
|
|
|
|
let voice_names = vec!["voiceone", "voicetwo", "voicethre"];
|
|
for i in voice_names.iter() {
|
|
let uri = format!("/channels/create/{}/{}/{}/{}", i, 0, 0, VOICE_CHANNEL);
|
|
let mut response = client.post(uri).dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
let body = response.body_string().unwrap();
|
|
let data: crate::payload::ChannelIndexed = from_str(&body).unwrap();
|
|
assert_eq!(data.name, i.to_string());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_text_channels_get() {
|
|
generate_dummy_channels();
|
|
let app = rocket_inst!(get_voice_channels);
|
|
|
|
let client = Client::new(app).expect("bad rocket instance");
|
|
let mut response = client.get("/channels/text").dispatch();
|
|
let _body: String = response.body_string().unwrap();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
assert_ne!(response.status(), Status::NotFound);
|
|
|
|
println!("succ me")
|
|
}
|
|
}
|