Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Fix user deletion (#907)
Browse files Browse the repository at this point in the history
* Fix user deletion

* run prep+fmt

* Update validators
  • Loading branch information
Geometrically authored Apr 23, 2024
1 parent 83ccf49 commit 49cf0c8
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 7 deletions.

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

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

6 changes: 2 additions & 4 deletions src/auth/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ pub async fn filter_visible_projects(
pool,
hide_unlisted,
)
.await
.unwrap();
.await?;
projects.retain(|x| filtered_project_ids.contains(&x.inner.id));
Ok(projects.into_iter().map(|x| x.into()).collect())
}
Expand Down Expand Up @@ -193,8 +192,7 @@ pub async fn filter_visible_versions(
pool,
redis,
)
.await
.unwrap();
.await?;
versions.retain(|x| filtered_version_ids.contains(&x.inner.id));
Ok(versions.into_iter().map(|x| x.into()).collect())
}
Expand Down
38 changes: 37 additions & 1 deletion src/database/models/user_item.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::ids::{ProjectId, UserId};
use super::CollectionId;
use super::{CollectionId, ThreadId};
use crate::database::models;
use crate::database::models::{DatabaseError, OrganizationId};
use crate::database::redis::RedisPool;
use crate::models::ids::base62_impl::{parse_base62, to_base62};
Expand Down Expand Up @@ -454,6 +455,41 @@ impl User {
.execute(&mut **transaction)
.await?;

let user_collections = sqlx::query!(
"
SELECT id
FROM collections
WHERE user_id = $1
",
id as UserId,
)
.fetch_many(&mut **transaction)
.try_filter_map(|e| async { Ok(e.right().map(|x| CollectionId(x.id))) })
.try_collect::<Vec<_>>()
.await?;

for collection_id in user_collections {
models::Collection::remove(collection_id, transaction, &redis).await?;

Check warning on line 472 in src/database/models/user_item.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/database/models/user_item.rs:472:72 | 472 | models::Collection::remove(collection_id, transaction, &redis).await?; | ^^^^^^ help: change this to: `redis` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
}

let report_threads = sqlx::query!(
"
SELECT t.id
FROM threads t
INNER JOIN reports r ON t.report_id = r.id AND (r.user_id = $1 OR r.reporter = $1)
WHERE report_id IS NOT NULL
",
id as UserId,
)
.fetch_many(&mut **transaction)
.try_filter_map(|e| async { Ok(e.right().map(|x| ThreadId(x.id))) })
.try_collect::<Vec<_>>()
.await?;

for thread_id in report_threads {
models::Thread::remove_full(thread_id, transaction).await?;
}

sqlx::query!(
"
DELETE FROM reports
Expand Down
10 changes: 9 additions & 1 deletion src/routes/v3/version_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,9 @@ pub async fn upload_file(
}

let data = data.freeze();
let primary = (version_files.iter().all(|x| !x.primary) && !ignore_primary)
let primary = (validation_result.is_passed()
&& version_files.iter().all(|x| !x.primary)
&& !ignore_primary)
|| force_primary
|| total_files_len == 1;

Expand Down Expand Up @@ -911,6 +913,12 @@ pub async fn upload_file(
));
}

if let ValidationResult::Warning(msg) = validation_result {
if primary {
return Err(CreateError::InvalidInput(msg.to_string()));
}
}

version_files.push(VersionFileBuilder {
filename: file_name.to_string(),
url: format!("{cdn_url}/{file_path_encode}"),
Expand Down
7 changes: 6 additions & 1 deletion src/search/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ pub async fn search_for_project(

let offset: usize = info.offset.as_deref().unwrap_or("0").parse()?;
let index = info.index.as_deref().unwrap_or("relevance");
let limit = info.limit.as_deref().unwrap_or("10").parse::<usize>()?.min(100);
let limit = info
.limit
.as_deref()
.unwrap_or("10")
.parse::<usize>()?
.min(100);

let sort = get_sort_index(config, index)?;
let meilisearch_index = client.get_index(sort.0).await?;
Expand Down

0 comments on commit 49cf0c8

Please sign in to comment.