Skip to content

Commit

Permalink
fix: update mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniwave committed Aug 13, 2024
1 parent 7a4fd6f commit 36caafc
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 230 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/services/database/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Call {
let database = super::get_database();
database
.collection::<Call>("calls")
.insert_one(self.clone(), None)
.insert_one(self.clone())
.await?;
Ok(())
}
Expand All @@ -39,7 +39,6 @@ impl Call {
"ended_at": chrono::Utc::now().timestamp_millis(),
},
},
None,
)
.await?;
Ok(())
Expand Down
12 changes: 5 additions & 7 deletions src/services/database/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,9 @@ impl Channel {
let database = super::get_database();
let channel = database
.collection::<Channel>("channels")
.find_one(
doc! {
"id": id,
},
None,
)
.find_one(doc! {
"id": id,
})
.await?;
match channel {
Some(channel) => Ok(channel),
Expand Down Expand Up @@ -94,7 +91,8 @@ impl Channel {
.build();
let messages: Vec<_> = database
.collection::<Message>("messages")
.find(query, options)
.find(query)
.with_options(options)
.await?
.collect()
.await;
Expand Down
9 changes: 3 additions & 6 deletions src/services/database/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,9 @@ pub async fn get_event(event_id: String) -> Result<Event> {
let database = super::get_database();
let event = database
.collection::<Event>("events")
.find_one(
doc! {
"id": event_id,
},
None,
)
.find_one(doc! {
"id": event_id,
})
.await?;
match event {
Some(event) => Ok(event),
Expand Down
30 changes: 12 additions & 18 deletions src/services/database/infractions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub async fn create_ban(
};
database
.collection::<Infraction>("infraction")
.insert_one(ban, None)
.insert_one(ban)
.await?;
Ok(())
}
Expand All @@ -54,17 +54,14 @@ pub async fn is_banned(user_id: String, space_id: String) -> Result<bool> {
let database = super::get_database();
let bans = database
.collection::<Infraction>("infractions")
.find(
doc! {
"member_id": user_id,
"space_id": space_id,
"expires_at": {
"$gt": chrono::Utc::now().timestamp_millis()
},
"infraction_type": "BAN"
.find(doc! {
"member_id": user_id,
"space_id": space_id,
"expires_at": {
"$gt": chrono::Utc::now().timestamp_millis()
},
None,
)
"infraction_type": "BAN"
})
.await?;
let is_banned = bans.count().await > 0;
Ok(is_banned)
Expand All @@ -74,13 +71,10 @@ pub async fn revoke_ban(ban_id: String) -> Result<()> {
let database = super::get_database();
database
.collection::<Infraction>("infractions")
.delete_one(
doc! {
"id": ban_id,
"infraction_type": "BAN"
},
None,
)
.delete_one(doc! {
"id": ban_id,
"infraction_type": "BAN"
})
.await?;
Ok(())
}
22 changes: 8 additions & 14 deletions src/services/database/invites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Invite {
let database = super::get_database();
database
.collection::<Invite>("invites")
.insert_one(invite.clone(), None)
.insert_one(invite.clone())
.await?;
Ok(invite)
}
Expand All @@ -57,12 +57,9 @@ impl Invite {
let database = super::get_database();
let invite = database
.collection::<Invite>("invites")
.find_one(
doc! {
"id": code,
},
None,
)
.find_one(doc! {
"id": code,
})
.await?;
match invite {
Some(invite) => Ok(invite),
Expand All @@ -73,12 +70,9 @@ impl Invite {
let database = super::get_database();
let result = database
.collection::<Invite>("invites")
.delete_one(
doc! {
"id": &self.id,
},
None,
)
.delete_one(doc! {
"id": &self.id,
})
.await?
.deleted_count
> 0;
Expand All @@ -104,7 +98,7 @@ pub async fn get_invites(channel_id: String, space_id: Option<String>) -> Result
}
let invites: std::result::Result<Vec<Invite>, _> = database
.collection::<Invite>("invites")
.find(query, None)
.find(query)
.await?
.collect::<Vec<_>>()
.await
Expand Down
22 changes: 8 additions & 14 deletions src/services/database/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,10 @@ impl Member {
let database = super::get_database();
let member = database
.collection::<Member>("members")
.find_one(
doc! {
"id": id,
"space_id": space_id,
},
None,
)
.find_one(doc! {
"id": id,
"space_id": space_id,
})
.await?
.ok_or(crate::errors::Error::NotFound)?;
Ok(member)
Expand All @@ -139,13 +136,10 @@ impl Member {
let database = super::get_database();
database
.collection::<Member>("members")
.delete_one(
doc! {
"id": self.id.clone(),
"space_id": space_id
},
None,
)
.delete_one(doc! {
"id": self.id.clone(),
"space_id": space_id
})
.await?;
Ok(())
}
Expand Down
5 changes: 2 additions & 3 deletions src/services/database/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Message {
let database = super::get_database();
database
.collection::<Message>("messages")
.insert_one(message.clone(), None)
.insert_one(message.clone())
.await?;
Ok(message)
}
Expand All @@ -45,7 +45,6 @@ impl Message {
"edited": true,
"editedAt": chrono::Utc::now().timestamp_millis(),
} },
None,
)
.await?;
match message {
Expand All @@ -58,7 +57,7 @@ impl Message {
let database = super::get_database();
let message = database
.collection::<Message>("messages")
.find_one_and_delete(doc! { "id": &self.id }, None)
.find_one_and_delete(doc! { "id": &self.id })
.await?;
match message {
Some(message) => Ok(message),
Expand Down
21 changes: 7 additions & 14 deletions src/services/database/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,26 @@ impl Role {
space_id: space.id.clone(),
scope_id: "global".to_string(), // FIXME: scope_id
};
roles.insert_one(role.clone(), None).await?;
roles.insert_one(role.clone()).await?;
Ok(role)
}

pub async fn delete(&self) -> Result<()> {
let roles = super::get_database().collection::<Role>("roles");
roles
.delete_one(
doc! {
"id": &self.id,
},
None,
)
.delete_one(doc! {
"id": &self.id,
})
.await?;
Ok(())
}

pub async fn get(id: &String) -> Result<Role> {
let roles = super::get_database().collection::<Role>("roles");
let role = roles
.find_one(
doc! {
"id": &id,
},
None,
)
.find_one(doc! {
"id": &id,
})
.await?;
match role {
Some(role) => Ok(role),
Expand All @@ -90,7 +84,6 @@ impl Role {
"color": bson::to_bson(&color)?,
},
},
None,
)
.await?;
match role {
Expand Down
37 changes: 14 additions & 23 deletions src/services/database/scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ impl Scope {
pub async fn create(name: String) -> Result<()> {
let scopes = super::get_database().collection::<Scope>("scopes");
scopes
.insert_one(
Scope {
id: Ulid::new().to_string(),
name,
disabled: false,
nexuses: Vec::new(),
channels: Vec::new(),
users: Vec::new(),
},
None,
)
.insert_one(Scope {
id: Ulid::new().to_string(),
name,
disabled: false,
nexuses: Vec::new(),
channels: Vec::new(),
users: Vec::new(),
})
.await?;
Ok(())
}
Expand All @@ -43,12 +40,9 @@ impl Scope {
) -> Result<bool> {
let scopes = super::get_database().collection::<Scope>("scopes");
let scope = scopes
.find_one(
doc! {
"id": &self.id
},
None,
)
.find_one(doc! {
"id": &self.id
})
.await?;
match scope {
Some(mut s) => {
Expand Down Expand Up @@ -80,12 +74,9 @@ impl Scope {
} else {
let scopes = super::get_database().collection::<Scope>("scopes");
scopes
.delete_one(
doc! {
"id": &self.id
},
None,
)
.delete_one(doc! {
"id": &self.id
})
.await?;
Ok(true)
}
Expand Down
Loading

0 comments on commit 36caafc

Please sign in to comment.