|
1 | 1 | use crate::database;
|
2 |
| -use crate::database::models::DBCollection; |
3 | 2 | use crate::database::models::project_item::ProjectQueryResult;
|
4 | 3 | use crate::database::models::version_item::VersionQueryResult;
|
| 4 | +use crate::database::models::{DBCollection, DBOrganization, DBTeamMember}; |
5 | 5 | use crate::database::redis::RedisPool;
|
6 | 6 | use crate::database::{DBProject, DBVersion, models};
|
7 | 7 | use crate::models::users::User;
|
8 | 8 | use crate::routes::ApiError;
|
| 9 | +use futures::TryStreamExt; |
9 | 10 | use itertools::Itertools;
|
10 | 11 | use sqlx::PgPool;
|
11 | 12 |
|
@@ -132,8 +133,6 @@ pub async fn filter_enlisted_projects_ids(
|
132 | 133 | if let Some(user) = user_option {
|
133 | 134 | let user_id: models::ids::DBUserId = user.id.into();
|
134 | 135 |
|
135 |
| - use futures::TryStreamExt; |
136 |
| - |
137 | 136 | sqlx::query!(
|
138 | 137 | "
|
139 | 138 | SELECT m.id id, m.team_id team_id FROM team_members tm
|
@@ -364,3 +363,36 @@ pub async fn filter_visible_collections(
|
364 | 363 |
|
365 | 364 | Ok(return_collections)
|
366 | 365 | }
|
| 366 | + |
| 367 | +pub async fn is_visible_organization( |
| 368 | + organization: &DBOrganization, |
| 369 | + viewing_user: &Option<User>, |
| 370 | + pool: &PgPool, |
| 371 | + redis: &RedisPool, |
| 372 | +) -> Result<bool, ApiError> { |
| 373 | + let members = |
| 374 | + DBTeamMember::get_from_team_full(organization.team_id, pool, redis) |
| 375 | + .await?; |
| 376 | + |
| 377 | + // This is meant to match the same projects as the `Project::is_searchable` method, but we're not using |
| 378 | + // it here because that'd entail pulling in all projects for the organization |
| 379 | + let has_searchable_projects = sqlx::query_scalar!( |
| 380 | + "SELECT TRUE FROM mods WHERE organization_id = $1 AND status IN ('public', 'archived') LIMIT 1", |
| 381 | + organization.id as database::models::ids::DBOrganizationId |
| 382 | + ) |
| 383 | + .fetch_optional(pool) |
| 384 | + .await? |
| 385 | + .flatten() |
| 386 | + .unwrap_or(false); |
| 387 | + |
| 388 | + let visible = has_searchable_projects |
| 389 | + || members.iter().filter(|member| member.accepted).count() > 1 |
| 390 | + || viewing_user.as_ref().is_some_and(|viewing_user| { |
| 391 | + viewing_user.role.is_mod() |
| 392 | + || members |
| 393 | + .iter() |
| 394 | + .any(|member| member.user_id == viewing_user.id.into()) |
| 395 | + }); |
| 396 | + |
| 397 | + Ok(visible) |
| 398 | +} |
0 commit comments