Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Jun 8, 2022
1 parent fb2283d commit db55cb5
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/routes/auth/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::SMTP_ENABLED;
use crate::guards::captcha::Captcha;
use crate::structures::{Base, User};
use crate::structures::*;
use crate::utils::error::*;
use argon2::Config;
use rocket::serde::{json::Json, Deserialize};
Expand Down
2 changes: 1 addition & 1 deletion src/routes/auth/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub async fn delete(user: User, target: Ref, token: &str) -> Result<()> {
return Err(Error::InvalidToken);
}

session.delete(session.id).await;
session.delete().await;

Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions src/routes/bots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ async fn delete(user: User, bot_id: Ref) -> Result<()> {
return Err(Error::MissingAccess);
}

bot.delete().await;

Ok(())
}

Expand Down
19 changes: 18 additions & 1 deletion src/routes/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,27 @@ async fn delete_group(user: User, group_id: Ref) -> Result<()> {
return Err(Error::MissingPermissions);
}

group.delete(group.id).await;
group.delete().await;

Ok(())
}

#[post("/<group_id>/invite")]
async fn create_invite(user: User, group_id: Ref) -> Result<Json<Invite>> {
let group = group_id.channel(user.id.into()).await?;

let p = Permissions::fetch(&user, None, Some(group.id)).await?;

if !p.contains(Permissions::INVITE_OTHERS) {
return Err(Error::MissingPermissions);
}

let invite = Invite::new(user.id, group.id, None);
invite.save().await;

Ok(Json(invite))
}

pub fn routes() -> Vec<rocket::Route> {
routes![
fetch_one,
Expand All @@ -110,5 +126,6 @@ pub fn routes() -> Vec<rocket::Route> {
delete_group,
add_user_to_group,
remove_user_from_group,
create_invite
]
}
2 changes: 1 addition & 1 deletion src/routes/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async fn delete(user: User, message_id: Ref) -> Result<()> {
return Err(Error::MissingPermissions);
}

msg.delete(msg.id).await;
msg.delete().await;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn delete(user: User, server_id: u64, channel_id: Ref) -> Result<()> {
return Err(Error::MissingPermissions);
}

channel_id.channel(None).await?.delete(channel_id.0).await;
channel_id.channel(None).await?.delete().await;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/invites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn delete(user: User, server_id: u64, invite_id: Ref) -> Result<()> {
invite_id
.invite(server_id.into())
.await?
.delete(invite_id.0)
.delete()
.await;

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn kick(user: User, server_id: u64, member_id: Ref) -> Result<()> {
}
}

member_id.member(server_id).await?.delete(member_id.0).await;
member_id.member(server_id).await?.delete().await;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async fn delete(user: User, server_id: u64, role_id: Ref) -> Result<()> {
return Err(Error::MissingPermissions);
}

role_id.role(server_id).await?.delete(role_id.0).await;
role_id.role(server_id).await?.delete().await;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/routes/servers/servers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ async fn delete(user: User, server_id: Ref) -> Result<()> {
let server = server_id.server().await?;

if server.owner_id == user.id {
server.delete(server.id).await;
server.delete().await;
} else {
if let Some(member) = server.fetch_member(user.id).await {
member.delete(member.id).await;
member.delete().await;
} else {
return Err(Error::UnknownMember);
}
Expand Down
10 changes: 8 additions & 2 deletions src/structures/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ use rocket::serde::DeserializeOwned;

#[async_trait]
pub trait Base: CRUDTable + DeserializeOwned {
fn id(&self) -> u64;

fn created_at(&self) -> u64 {
todo!("Extract date from id")
}

async fn find<F>(query: F) -> Vec<Self>
where
F: Send + Fn(Wrapper) -> Wrapper,
Expand Down Expand Up @@ -38,7 +44,7 @@ pub trait Base: CRUDTable + DeserializeOwned {
.expect("Could'nt update thus target");
}

async fn delete(&self, id: u64) -> bool {
db.remove_by_column::<Self, &u64>("id", &id).await.is_ok()
async fn delete(&self) -> bool {
db.remove_by_column::<Self, u64>("id", self.id()).await.is_ok()
}
}
4 changes: 3 additions & 1 deletion src/structures/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub struct Bot {
}

#[async_trait]
impl Base for Bot {}
impl Base for Bot {
fn id(&self) -> u64 { self.id }
}

impl Bot {
pub fn new(username: String, owner_id: u64) -> Self {
Expand Down
4 changes: 3 additions & 1 deletion src/structures/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ pub struct Channel {
pub permissions: Option<Permissions>,
}

impl Base for Channel {}
impl Base for Channel {
fn id(&self) -> u64 { self.id }
}

impl Channel {
pub fn new_dm(user: u64, target: u64) -> Self {
Expand Down
4 changes: 3 additions & 1 deletion src/structures/invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub struct Invite {
pub server_id: Option<u64>,
}

impl Base for Invite {}
impl Base for Invite {
fn id(&self) -> u64 { self.id }
}

impl Invite {
pub fn new(inviter_id: u64, channel_id: u64, server_id: Option<u64>) -> Self {
Expand Down
4 changes: 3 additions & 1 deletion src/structures/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ pub struct Member {
pub roles: Vec<u64>,
}

impl Base for Member {}
impl Base for Member {
fn id(&self) -> u64 { self.id }
}

impl Member {
pub fn new(user_id: u64, server_id: u64) -> Self {
Expand Down
9 changes: 3 additions & 6 deletions src/structures/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ pub struct Message {
*/
}

impl Base for Message {}
impl Base for Message {
fn id(&self) -> u64 { self.id }
}

impl Message {
pub fn new(channel_id: u64, author_id: u64) -> Self {
Expand All @@ -31,11 +33,6 @@ impl Message {
}
}

pub fn created_at() -> u64 {
// TODO: extract time from id
0
}

pub fn is_empty(&self) -> bool {
self.content.is_none() /* && self.attachments.len() == 0 */
}
Expand Down
4 changes: 3 additions & 1 deletion src/structures/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub struct Role {
pub server_id: u64,
}

impl Base for Role {}
impl Base for Role {
fn id(&self) -> u64 { self.id }
}

impl Role {
pub fn new(name: String, server_id: u64) -> Self {
Expand Down
4 changes: 3 additions & 1 deletion src/structures/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub struct Server {
pub permissions: Permissions,
}

impl Base for Server {}
impl Base for Server {
fn id(&self) -> u64 { self.id }
}

impl Server {
pub fn new(name: String, owner_id: u64) -> Self {
Expand Down
4 changes: 3 additions & 1 deletion src/structures/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub struct Session {
pub user_id: u64,
}

impl Base for Session {}
impl Base for Session {
fn id(&self) -> u64 { self.id }
}

impl Session {
pub fn new(user_id: u64) -> Self {
Expand Down
4 changes: 3 additions & 1 deletion src/structures/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub struct User {
pub verified: bool,
}

impl Base for User {}
impl Base for User {
fn id(&self) -> u64 { self.id }
}

impl User {
pub fn new(username: String, email: String, password: String) -> Self {
Expand Down

0 comments on commit db55cb5

Please sign in to comment.