filter tree returned from git
fixes #7
Diff
chartered-db/src/crates.rs | 24 ++++++++++++++++++++++++
chartered-git/src/main.rs | 19 +++++++++++++++++--
2 files changed, 37 insertions(+), 6 deletions(-)
@@ -28,6 +28,30 @@
.await?
}
pub async fn all_visible_with_versions(
conn: ConnectionPool,
given_user_id: i32,
) -> Result<HashMap<Crate, Vec<CrateVersion>>> {
tokio::task::spawn_blocking(move || {
let conn = conn.get()?;
let crate_versions = crates::table
.inner_join(crate::schema::user_crate_permissions::table)
.filter(
crate::schema::user_crate_permissions::permissions
.bitwise_and(crate::users::UserCratePermissionValue::VISIBLE.bits())
.ne(0),
)
.filter(crate::schema::user_crate_permissions::dsl::user_id.eq(given_user_id))
.inner_join(crate_versions::table)
.select((crates::all_columns, crate_versions::all_columns))
.load(&conn)?;
Ok(crate_versions.into_iter().into_grouping_map().collect())
})
.await?
}
pub async fn find_by_name(conn: ConnectionPool, crate_name: String) -> Result<Option<Self>> {
use crate::schema::crates::dsl::{crates, name};
@@ -87,6 +87,13 @@
None => anyhow::bail!("user not set after auth"),
}
}
fn user_ssh_key(&self) -> Result<&Arc<chartered_db::users::UserSshKey>, anyhow::Error> {
match self.user_ssh_key {
Some(ref ssh_key) => Ok(ssh_key),
None => anyhow::bail!("user not set after auth"),
}
}
}
type AsyncHandlerFut<T> =
@@ -242,9 +249,7 @@
let config = format!(
r#"{{"dl":"http://127.0.0.1:8888/a/{key}/api/v1/crates","api":"http://127.0.0.1:8888/a/{key}"}}"#,
key = self
.user_ssh_key
.as_ref()
.unwrap()
.user_ssh_key()?
.clone()
.get_or_insert_api_key(self.db.clone())
.await?
@@ -259,8 +264,9 @@
});
pack_file_entries.push(config_file);
let tree = fetch_tree(self.db.clone()).await;
let tree = fetch_tree(self.db.clone(), self.user()?.id).await;
build_tree(&mut root_tree, &mut pack_file_entries, &tree)?;
let root_tree = PackFileEntry::Tree(root_tree);
@@ -343,13 +349,14 @@
async fn fetch_tree(
db: chartered_db::ConnectionPool,
user_id: i32,
) -> TwoCharTree<TwoCharTree<BTreeMap<String, String>>> {
use chartered_db::crates::Crate;
let mut tree: TwoCharTree<TwoCharTree<BTreeMap<String, String>>> = BTreeMap::new();
for (crate_def, versions) in Crate::all_with_versions(db).await.unwrap() {
for (crate_def, versions) in Crate::all_visible_with_versions(db, user_id).await.unwrap() {
let mut name_chars = crate_def.name.as_bytes().iter();
let first_dir = [*name_chars.next().unwrap(), *name_chars.next().unwrap()];
let second_dir = [*name_chars.next().unwrap(), *name_chars.next().unwrap()];