🏡 index : ~doyle/chartered.git

author Jordan Doyle <jordan@doyle.la> 2021-09-10 3:20:52.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2021-09-10 3:21:13.0 +01:00:00
commit
f4a3e07b9e82bd3a07eecbb8bd880f2011decac7 [patch]
tree
bc40c92d276348a9dc04d3e4ee715576690524f4
parent
cd40f03b298fcf84c6d42d4ab5233077d6822996
download
f4a3e07b9e82bd3a07eecbb8bd880f2011decac7.tar.gz

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(-)

diff --git a/chartered-db/src/crates.rs b/chartered-db/src/crates.rs
index 9f8b368..de2e7df 100644
--- a/chartered-db/src/crates.rs
+++ a/chartered-db/src/crates.rs
@@ -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};

diff --git a/chartered-git/src/main.rs b/chartered-git/src/main.rs
index ddd8126..d044c54 100644
--- a/chartered-git/src/main.rs
+++ a/chartered-git/src/main.rs
@@ -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);

            // todo: this needs caching and filtering
            let tree = fetch_tree(self.db.clone()).await;
            // todo: the whole tree needs caching and then we can filter in code rather than at
            //  the database
            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();

    // todo: handle files with 1/2/3 characters
    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()];