-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(routes): Add server members routes
- Loading branch information
1 parent
736c561
commit 9b74e22
Showing
2 changed files
with
100 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,98 @@ | ||
use crate::guards::r#ref::Ref; | ||
use crate::structures::*; | ||
use crate::utils::error::*; | ||
use crate::utils::permissions::Permissions; | ||
use rocket::serde::{json::Json, Deserialize}; | ||
use validator::Validate; | ||
|
||
#[derive(Deserialize, Validate)] | ||
struct CreateChannelSchema<'a> { | ||
r#type: ChannelTypes, | ||
#[validate(length(min = 1, max = 32))] | ||
name: &'a str, | ||
} | ||
|
||
#[get("/<server_id>/<channel_id>")] | ||
async fn fetch_one(user: User, server_id: u64, channel_id: u64) -> Result<Json<Channel>> { | ||
if !user.is_in_server(server_id).await { | ||
return Err(Error::UnknownServer); | ||
} | ||
|
||
let channel = Channel::find_one(|q| q.eq("id", channel_id).eq("server_id", server_id)).await; | ||
|
||
match channel { | ||
Some(channel) => Ok(Json(channel)), | ||
None => Err(Error::UnknownChannel), | ||
} | ||
} | ||
|
||
#[get("/<server_id>")] | ||
async fn fetch_many(user: User, server_id: u64) -> Result<Json<Vec<Channel>>> { | ||
if !user.is_in_server(server_id).await { | ||
return Err(Error::UnknownServer); | ||
} | ||
|
||
let channels = Channel::find(|q| q.eq("server_id", server_id)).await; | ||
|
||
Ok(Json(channels)) | ||
} | ||
|
||
#[post("/<server_id>", data = "<data>")] | ||
async fn create( | ||
user: User, | ||
server_id: u64, | ||
data: Json<CreateChannelSchema<'_>>, | ||
) -> Result<Json<Channel>> { | ||
if !user.is_in_server(server_id).await { | ||
return Err(Error::UnknownServer); | ||
} | ||
|
||
let p = Permissions::fetch(&user, server_id.into(), None).await?; | ||
|
||
if !p.contains(Permissions::MANAGE_CHANNELS) { | ||
return Err(Error::MissingPermissions); | ||
} | ||
|
||
match data.r#type { | ||
ChannelTypes::Text => Ok(Json(Channel::new_text(data.name.into(), server_id))), | ||
ChannelTypes::Category => Ok(Json(Channel::new_category(data.name.into(), server_id))), | ||
ChannelTypes::Voice => Ok(Json(Channel::new_voice(data.name.into(), server_id))), | ||
_ => Err(Error::MissingAccess), | ||
} | ||
} | ||
|
||
#[delete("/<server_id>/<channel_id>")] | ||
async fn delete(user: User, server_id: u64, channel_id: Ref) -> Result<()> { | ||
if !user.is_in_server(server_id).await { | ||
return Err(Error::UnknownServer); | ||
} | ||
|
||
let p = Permissions::fetch(&user, server_id.into(), channel_id.0.into()).await?; | ||
|
||
if !p.contains(Permissions::MANAGE_CHANNELS) { | ||
return Err(Error::MissingPermissions); | ||
} | ||
|
||
channel_id.channel(None).await?.delete(channel_id.0).await; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[patch("/<server_id>/<channel_id>")] | ||
async fn update(user: User, server_id: u64, channel_id: Ref) -> Result<Json<Channel>> { | ||
if !user.is_in_server(server_id).await { | ||
return Err(Error::UnknownServer); | ||
} | ||
|
||
let p = Permissions::fetch(&user, server_id.into(), channel_id.0.into()).await?; | ||
|
||
if !p.contains(Permissions::MANAGE_CHANNELS) { | ||
return Err(Error::MissingPermissions); | ||
} | ||
|
||
todo!("Update channels route") | ||
} | ||
|
||
pub fn routes() -> Vec<rocket::Route> { | ||
routes![] | ||
routes![fetch_one, fetch_many, create, update, delete] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters