Skip to content

Commit

Permalink
perf: Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Jul 7, 2022
1 parent d189978 commit df93b5a
Show file tree
Hide file tree
Showing 44 changed files with 114 additions and 109 deletions.
5 changes: 2 additions & 3 deletions src/routes/auth/accounts/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ pub async fn register(
.taken_by(Some(user.id))
.used(true)
.update(pool())
.await
.unwrap();
.await?;
}

Ok(Json(user.insert(pool()).await.unwrap()))
Ok(Json(user.insert(pool()).await?))
}
2 changes: 1 addition & 1 deletion src/routes/auth/sessions/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn create(

let session = Session::new(user.id);

Ok(Json(session.insert(pool()).await.unwrap()))
Ok(Json(session.insert(pool()).await?))
}
_ => Err(Error::UnknownAccount),
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/auth/sessions/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn delete(
return Err(Error::InvalidToken);
}

session.delete(pool()).await.unwrap();
session.remove().await?;

Ok(())
}
9 changes: 4 additions & 5 deletions src/routes/auth/sessions/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ pub async fn fetch_one(
Ok(Json(id.session(user.id).await?))
}

pub async fn fetch_many(Extension(user): Extension<User>) -> Json<Vec<Session>> {
Json(
pub async fn fetch_many(Extension(user): Extension<User>) -> Result<Json<Vec<Session>>> {
Ok(Json(
Session::select()
.filter("user_id = $1")
.bind(user.id)
.fetch_all(pool())
.await
.unwrap(),
)
.await?,
))
}
2 changes: 1 addition & 1 deletion src/routes/bots/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub async fn delete(Extension(user): Extension<User>, Path(bot_id): Path<i64>) -
return Err(Error::MissingAccess);
}

bot.delete(pool()).await.unwrap();
bot.remove().await?;

Ok(())
}
6 changes: 2 additions & 4 deletions src/routes/bots/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use crate::structures::*;
use crate::utils::*;

pub async fn fetch_one(Path(bot_id): Path<i64>) -> Result<Json<Bot>> {
let bot = bot_id.bot().await?;
Ok(Json(bot))
Ok(Json(bot_id.bot().await?))
}

pub async fn fetch_many(Extension(user): Extension<User>) -> Json<Vec<Bot>> {
let bots = user.fetch_bots().await;
Json(bots)
Json(user.fetch_bots().await)
}
3 changes: 1 addition & 2 deletions src/routes/channels/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ pub async fn create(
) -> Result<Json<Channel>> {
let group = Channel::new_group(user.id, data.name)
.insert(pool())
.await
.unwrap();
.await?;

for id in group.recipients.as_ref().unwrap() {
publish(*id, Payload::ChannelCreate(group.clone())).await;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/channels/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub async fn delete(Extension(user): Extension<User>, Path(channel_id): Path<i64
return Err(Error::MissingPermissions);
}

channel.delete(pool()).await.unwrap();
channel.remove().await?;

publish(channel_id, Payload::ChannelDelete(channel_id.into())).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/channels/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub async fn edit(
group.name = name.into();
}

let group = group.update_all_fields(pool()).await.unwrap();
let group = group.update_all_fields(pool()).await?;

publish(group.id, Payload::ChannelUpdate(group.clone())).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/channels/kick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn kick(
}
}

let group = group.update_all_fields(pool()).await.unwrap();
let group = group.update_all_fields(pool()).await?;

publish(group_id, Payload::ChannelUpdate(group)).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/invites/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ pub async fn create(

let invite = Invite::new(user.id, channel.id, channel.server_id);

Ok(Json(invite.insert(pool()).await.unwrap()))
Ok(Json(invite.insert(pool()).await?))
}
2 changes: 1 addition & 1 deletion src/routes/invites/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::structures::*;
use crate::utils::*;

pub async fn fetch_one(Path(code): Path<String>) -> Result<Json<Invite>> {
match Invite::get_one(code, pool()).await {
match Invite::find_one(code).await {
Ok(invite) => Ok(Json(invite)),
_ => Err(Error::UnknownInvite),
}
Expand Down
15 changes: 6 additions & 9 deletions src/routes/invites/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::structures::*;
use crate::utils::*;

pub async fn join(Extension(user): Extension<User>, Path(code): Path<String>) -> Result<()> {
let invite = Invite::get_one(code, pool()).await;
let invite = Invite::find_one(code).await;

match invite {
Ok(invite) if invite.server_id.is_some() => {
Expand All @@ -29,29 +29,26 @@ pub async fn join(Extension(user): Extension<User>, Path(code): Path<String>) ->
return Err(Error::MaximumChannels);
}

let member = Member::new(user.id, server_id)
.insert(pool())
.await
.unwrap();
let member = Member::new(user.id, server_id).insert(pool()).await?;

invite
.update_partial()
.uses(invite.uses + 1)
.update(pool())
.await
.unwrap();
.await?;

publish(
user.id,
Payload::ServerCreate(server_id.server(None).await?),
)
.await;

publish(server_id, Payload::ServerMemberJoin(member)).await;

Ok(())
}
Ok(invite) => {
let mut group = Channel::get_one(invite.channel_id, pool()).await.unwrap();
let mut group = Channel::find_one(invite.channel_id).await?;

if let Some(recipients) = group.recipients.as_mut() {
if recipients.len() as u64 > *MAX_GROUP_MEMBERS {
Expand All @@ -66,7 +63,7 @@ pub async fn join(Extension(user): Extension<User>, Path(code): Path<String>) ->
unreachable!()
}

let group = group.update_all_fields(pool()).await.unwrap();
let group = group.update_all_fields(pool()).await?;

publish(group.id, Payload::ChannelUpdate(group.clone())).await;
publish(user.id, Payload::ChannelCreate(group)).await;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/messages/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn create(
return Err(Error::EmptyMessage);
}

let msg = msg.insert(pool()).await.unwrap();
let msg = msg.insert(pool()).await?;

publish(channel_id, Payload::MessageCreate(msg.clone())).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/messages/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub async fn delete(
permissions.has(Permissions::MANAGE_MESSAGES)?;
}

msg.delete(pool()).await.unwrap();
msg.remove().await?;

publish(channel_id, Payload::MessageDelete(id.into())).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/messages/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub async fn edit(

msg.content = data.content.into();

let msg = msg.update_all_fields(pool()).await.unwrap();
let msg = msg.update_all_fields(pool()).await?;

publish(channel_id, Payload::MessageUpdate(msg.clone())).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/channels/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn create(
_ => return Err(Error::MissingAccess),
};

let channel = channel.insert(pool()).await.unwrap();
let channel = channel.insert(pool()).await?;

publish(server_id, Payload::ChannelCreate(channel.clone())).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/channels/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn delete(
.await?
.has(Permissions::MANAGE_CHANNELS)?;

channel.delete(pool()).await.unwrap();
channel.remove().await?;

publish(
server_id,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/channels/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub async fn edit(
channel.name = name.into();
}

let channel = channel.update_all_fields(pool()).await.unwrap();
let channel = channel.update_all_fields(pool()).await?;

publish(server_id, Payload::ChannelUpdate(channel.clone())).await;

Expand Down
9 changes: 4 additions & 5 deletions src/routes/servers/channels/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ pub async fn fetch_one(Path((server_id, channel_id)): Path<(i64, i64)>) -> Resul
Ok(Json(channel))
}

pub async fn fetch_many(Path(server_id): Path<i64>) -> Json<Vec<Channel>> {
Json(
pub async fn fetch_many(Path(server_id): Path<i64>) -> Result<Json<Vec<Channel>>> {
Ok(Json(
Channel::select()
.filter("server_id = $1")
.bind(server_id)
.fetch_all(pool())
.await
.unwrap(),
)
.await?,
))
}
12 changes: 6 additions & 6 deletions src/routes/servers/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ pub async fn create(

chat.parent_id = Some(category.id);

let mut tx = pool().begin().await.unwrap();
let mut tx = pool().begin().await?;

let server = server.insert(&mut tx).await.unwrap();
category.insert(&mut tx).await.unwrap();
chat.insert(&mut tx).await.unwrap();
member.insert(&mut tx).await.unwrap();
let server = server.insert(&mut tx).await?;
category.insert(&mut tx).await?;
chat.insert(&mut tx).await?;
member.insert(&mut tx).await?;

tx.commit().await.unwrap();
tx.commit().await?;

publish(user.id, Payload::ServerCreate(server.clone())).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub async fn delete(Extension(user): Extension<User>, Path(server_id): Path<i64>
return Err(Error::MissingAccess);
}

server.delete(pool()).await.unwrap();
server.remove().await?;

publish(server_id, Payload::ServerDelete(server_id.into())).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub async fn edit(
server.name = name;
}

let server = server.update_all_fields(pool()).await.unwrap();
let server = server.update_all_fields(pool()).await?;

publish(server.id, Payload::ServerUpdate(server.clone())).await;

Expand Down
3 changes: 1 addition & 2 deletions src/routes/servers/invites/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub async fn delete(
.invite(server_id.into())
.await?
.delete(pool())
.await
.unwrap();
.await?;

Ok(())
}
9 changes: 4 additions & 5 deletions src/routes/servers/invites/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ pub async fn fetch_one(Path((server_id, invite_id)): Path<(i64, i64)>) -> Result
Ok(Json(invite_id.invite(server_id.into()).await?))
}

pub async fn fetch_many(Path(server_id): Path<i64>) -> Json<Vec<Invite>> {
Json(
pub async fn fetch_many(Path(server_id): Path<i64>) -> Result<Json<Vec<Invite>>> {
Ok(Json(
Invite::select()
.filter("server_id = $1")
.bind(server_id)
.fetch_all(pool())
.await
.unwrap(),
)
.await?,
))
}
5 changes: 2 additions & 3 deletions src/routes/servers/members/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ pub async fn edit(
.filter("server_id = $1")
.bind(server_id)
.fetch_all(pool())
.await
.unwrap()
.await?
.into_iter();

member.roles = vec![];
Expand All @@ -51,7 +50,7 @@ pub async fn edit(
}
}

let member = member.update_all_fields(pool()).await.unwrap();
let member = member.update_all_fields(pool()).await?;

publish(server_id, Payload::ServerMemberUpdate(member.clone())).await;

Expand Down
7 changes: 3 additions & 4 deletions src/routes/servers/members/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ pub async fn fetch_one(Path((server_id, member_id)): Path<(i64, i64)>) -> Result
pub async fn fetch_many(
Path(server_id): Path<i64>,
Query(query): Query<FetchMembersOptions>,
) -> Json<Vec<Member>> {
) -> Result<Json<Vec<Member>>> {
let limit = query.limit.unwrap_or(100) as usize;
let members = Member::select()
.filter("server_id = $1")
.bind(server_id)
.limit(limit)
.fetch_all(pool())
.await
.unwrap();
Json(members)
.await?;
Ok(Json(members))
}
7 changes: 1 addition & 6 deletions src/routes/servers/members/kick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ pub async fn kick(
.has(Permissions::KICK_MEMBERS)?;
}

member_id
.member(server_id)
.await?
.delete(pool())
.await
.unwrap();
member_id.member(server_id).await?.delete(pool()).await?;

publish(
server_id,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/roles/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub async fn create(
role.hoist = data.hoist;
role.color = data.color;

let role = role.insert(pool()).await.unwrap();
let role = role.insert(pool()).await?;

publish(server_id, Payload::RoleCreate(role.clone())).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/roles/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub async fn delete(
.await?
.has(Permissions::MANAGE_ROLES)?;

role_id.role(server_id).await?.delete(pool()).await.unwrap();
role_id.role(server_id).await?.remove().await?;

publish(server_id, Payload::RoleDelete((role_id, server_id).into())).await;

Expand Down
2 changes: 1 addition & 1 deletion src/routes/servers/roles/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn edit(
role.color = color;
}

let role = role.update_all_fields(pool()).await.unwrap();
let role = role.update_all_fields(pool()).await?;

publish(server_id, Payload::RoleUpdate(role.clone())).await;

Expand Down
Loading

0 comments on commit df93b5a

Please sign in to comment.