Servers: Add route for fetching members.

This commit is contained in:
Paul
2021-06-06 10:17:33 +01:00
parent f9fbe54b17
commit dbd70abaca
5 changed files with 57 additions and 28 deletions

View File

@@ -42,9 +42,6 @@ pub struct Server {
pub owner: String,
pub name: String,
// pub default_permissions: u32,
// pub invites: Vec<Invite>,
// pub bans: Vec<Ban>,
pub channels: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
@@ -90,6 +87,8 @@ impl Server {
let messages = get_collection("messages");
// Check if there are any attachments we need to delete.
// ! FIXME: make this generic and merge with channel delete
// ! e.g. delete_channel(filter: doc!)
let message_ids = messages
.find(
doc! {
@@ -206,6 +205,32 @@ impl Server {
Ok(())
}
pub async fn fetch_members(id: &str) -> Result<Vec<String>> {
Ok(get_collection("server_members")
.find(
doc! {
"_id.server": id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "server_members",
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| {
x.get_document("_id")
.ok()
.map(|i| i.get_str("user").ok().map(|x| x.to_string()))
})
.flatten()
.collect::<Vec<String>>())
}
pub async fn join_member(&self, id: &str) -> Result<()> {
get_collection("server_members")
.insert_one(

View File

@@ -184,6 +184,7 @@ impl User {
}
/// Utility function for fetching multiple users from the perspective of one.
/// Assumes user has a mutual connection with others.
pub async fn fetch_multiple_users(&self, user_ids: Vec<String>) -> Result<Vec<User>> {
let mut users = vec![];
let mut cursor = get_collection("users")