Finish guards, add channel info route.

This commit is contained in:
Paul Makles
2020-01-26 15:32:31 +00:00
parent 955e482dae
commit 5262095bed
3 changed files with 35 additions and 2 deletions

View File

@@ -30,7 +30,7 @@ impl<'r> FromParam<'r> for Channel {
if let Some(channel) = result {
Ok(Channel (
Ulid::from_string(channel.get_str("_id").unwrap()).unwrap(),
ChannelType::try_from(channel.get_i32("username").unwrap() as usize).unwrap(),
ChannelType::try_from(channel.get_i32("type").unwrap() as usize).unwrap(),
channel
))
} else {

View File

@@ -1,4 +1,4 @@
use crate::guards::auth::User;
use crate::guards::{ auth::User, channel::Channel };
use crate::database;
use rocket_contrib::json::{ Json, JsonValue };
@@ -15,3 +15,35 @@ pub enum ChannelType {
GROUP_DM = 1,
GUILD_CHANNEL = 2,
}
fn has_permission(user: &User, target: &Channel) -> bool {
let id = user.0.to_string();
match target.1 {
ChannelType::DM |
ChannelType::GROUP_DM => {
for user in target.2.get_array("recipients").expect("DB[recipients]") {
if user.as_str().expect("Expected string id.") == id {
return true;
}
}
false
},
ChannelType::GUILD_CHANNEL =>
false
}
}
/// fetch channel information
#[get("/<target>")]
pub fn channel(user: User, target: Channel) -> Option<JsonValue> {
if !has_permission(&user, &target) {
return None
}
Some(
json!({
"aa": "b"
}
))
}

View File

@@ -8,4 +8,5 @@ pub fn mount(rocket: Rocket) -> Rocket {
rocket
.mount("/api/account", routes![ account::create, account::verify_email, account::resend_email, account::login ])
.mount("/api/users", routes![ user::me, user::user, user::lookup, user::dms, user::dm, user::get_friends, user::get_friend, user::add_friend, user::remove_friend ])
.mount("/api/channels", routes![ channel::channel ])
}