| 1 |
|
| 2 |
|
| 3 |
use axum::{ |
| 4 |
Form, Json, |
| 5 |
extract::{Path, State}, |
| 6 |
http::header::HeaderMap, |
| 7 |
response::{IntoResponse, Response}, |
| 8 |
}; |
| 9 |
use serde::{Deserialize, Serialize}; |
| 10 |
|
| 11 |
use crate::config::Config; |
| 12 |
use crate::{AppStorage, Integrations}; |
| 13 |
use sqlx::PgPool; |
| 14 |
|
| 15 |
use crate::{ |
| 16 |
auth::AuthUser, |
| 17 |
db::{self, GitRepoId, ProjectId, ProjectType, Slug, UserId, Visibility}, |
| 18 |
error::{AppError, Result, ResultExt}, |
| 19 |
helpers::{htmx_toast_response, is_htmx_request}, |
| 20 |
types::ListResponse, |
| 21 |
validation, |
| 22 |
}; |
| 23 |
|
| 24 |
use super::verify_project_ownership; |
| 25 |
use crate::extractors::{ValidatedForm, ValidatedJson}; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#[derive(Debug, Deserialize)] |
| 31 |
pub(super) struct CreateProjectRequest { |
| 32 |
pub slug: Slug, |
| 33 |
pub title: String, |
| 34 |
pub description: Option<String>, |
| 35 |
#[serde(default)] |
| 36 |
pub features: Vec<String>, |
| 37 |
pub category: Option<String>, |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
#[derive(Debug, Serialize)] |
| 42 |
pub(super) struct ProjectResponse { |
| 43 |
pub id: ProjectId, |
| 44 |
pub slug: String, |
| 45 |
pub title: String, |
| 46 |
pub description: Option<String>, |
| 47 |
pub project_type: ProjectType, |
| 48 |
pub features: Vec<String>, |
| 49 |
pub is_public: bool, |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
#[tracing::instrument(skip_all, name = "projects::create_project")] |
| 54 |
pub(super) async fn create_project( |
| 55 |
State(db): State<PgPool>, |
| 56 |
State(integrations): State<Integrations>, |
| 57 |
headers: HeaderMap, |
| 58 |
AuthUser(user): AuthUser, |
| 59 |
ValidatedForm(req): ValidatedForm<CreateProjectRequest>, |
| 60 |
) -> Result<Response> { |
| 61 |
user.check_not_suspended()?; |
| 62 |
|
| 63 |
|
| 64 |
if !user.can_create_projects { |
| 65 |
return Err(AppError::Forbidden); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
validation::validate_project_title(&req.title)?; |
| 70 |
if let Some(ref desc) = req.description { |
| 71 |
validation::validate_project_description(desc)?; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
let category_id = if let Some(ref cat_name) = req.category { |
| 76 |
let trimmed = cat_name.trim(); |
| 77 |
if trimmed.is_empty() { |
| 78 |
None |
| 79 |
} else { |
| 80 |
let cat = db::categories::get_or_create_category(&db, trimmed).await?; |
| 81 |
Some(cat.id) |
| 82 |
} |
| 83 |
} else { |
| 84 |
None |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
for f in &req.features { |
| 89 |
f.parse::<db::ProjectFeature>() |
| 90 |
.map_err(|_| AppError::validation(format!("Invalid feature: {f}")))?; |
| 91 |
} |
| 92 |
|
| 93 |
let project = db::projects::create_project( |
| 94 |
&db, |
| 95 |
user.id, |
| 96 |
&req.slug, |
| 97 |
&req.title, |
| 98 |
req.description.as_deref(), |
| 99 |
&req.features, |
| 100 |
) |
| 101 |
.await?; |
| 102 |
|
| 103 |
|
| 104 |
if let Some(cat_id) = category_id { |
| 105 |
db::projects::set_project_category(&db, project.id, user.id, Some(cat_id)).await?; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
if let Err(e) = db::mailing_lists::create_default_lists(&db, project.id, &req.title).await { |
| 110 |
tracing::warn!(project_id = %project.id, error = ?e, "failed to create default mailing lists"); |
| 111 |
} |
| 112 |
|
| 113 |
db::users::bump_cache_generation(&db, user.id).await?; |
| 114 |
db::projects::bump_cache_generation(&db, project.id).await?; |
| 115 |
|
| 116 |
|
| 117 |
if let Some(ref mt) = integrations.mt_client { |
| 118 |
let mt = mt.clone(); |
| 119 |
let db = db.clone(); |
| 120 |
let project_id = project.id; |
| 121 |
let slug = project.slug.to_string(); |
| 122 |
let title = project.title.clone(); |
| 123 |
let desc = project.description.clone(); |
| 124 |
let username = user.username.to_string(); |
| 125 |
let display_name = user.display_name.clone(); |
| 126 |
let user_id = user.id; |
| 127 |
tokio::spawn(async move { |
| 128 |
match mt |
| 129 |
.create_community(&crate::mt_client::CreateCommunityRequest { |
| 130 |
name: title, |
| 131 |
slug, |
| 132 |
description: desc, |
| 133 |
owner_mnw_id: *user_id, |
| 134 |
owner_username: username, |
| 135 |
owner_display_name: display_name, |
| 136 |
}) |
| 137 |
.await |
| 138 |
{ |
| 139 |
Ok(resp) => { |
| 140 |
if let Err(e) = |
| 141 |
db::projects::set_mt_community_id(&db, project_id, resp.community_id).await |
| 142 |
{ |
| 143 |
tracing::warn!(error = ?e, "failed to store MT community ID"); |
| 144 |
} |
| 145 |
} |
| 146 |
Err(e) => tracing::warn!(error = ?e, "MT community provisioning failed"), |
| 147 |
} |
| 148 |
}); |
| 149 |
} |
| 150 |
|
| 151 |
if is_htmx_request(&headers) { |
| 152 |
|
| 153 |
let mut response = Response::new(axum::body::Body::empty()); |
| 154 |
response.headers_mut().insert( |
| 155 |
"HX-Redirect", |
| 156 |
format!("/dashboard/project/{}", project.slug) |
| 157 |
.parse() |
| 158 |
.expect("static redirect path is valid"), |
| 159 |
); |
| 160 |
return Ok(response); |
| 161 |
} |
| 162 |
|
| 163 |
Ok(Json(ProjectResponse { |
| 164 |
id: project.id, |
| 165 |
slug: project.slug.to_string(), |
| 166 |
title: project.title, |
| 167 |
description: project.description, |
| 168 |
project_type: project.project_type, |
| 169 |
features: project.features, |
| 170 |
is_public: project.is_public, |
| 171 |
}) |
| 172 |
.into_response()) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
#[tracing::instrument(skip_all, name = "projects::list_projects")] |
| 177 |
pub(super) async fn list_projects( |
| 178 |
State(db): State<PgPool>, |
| 179 |
AuthUser(user): AuthUser, |
| 180 |
) -> Result<impl IntoResponse> { |
| 181 |
let projects = db::projects::get_projects_by_user(&db, user.id).await?; |
| 182 |
|
| 183 |
let data: Vec<ProjectResponse> = projects |
| 184 |
.into_iter() |
| 185 |
.map(|p| ProjectResponse { |
| 186 |
id: p.id, |
| 187 |
slug: p.slug.to_string(), |
| 188 |
title: p.title, |
| 189 |
description: p.description, |
| 190 |
project_type: p.project_type, |
| 191 |
features: p.features, |
| 192 |
is_public: p.is_public, |
| 193 |
}) |
| 194 |
.collect(); |
| 195 |
|
| 196 |
Ok(Json(ListResponse { data })) |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
#[derive(Debug, Deserialize)] |
| 201 |
pub(super) struct UpdateProjectRequest { |
| 202 |
pub title: Option<String>, |
| 203 |
pub description: Option<String>, |
| 204 |
pub features: Option<Vec<String>>, |
| 205 |
pub is_public: Option<bool>, |
| 206 |
pub category: Option<String>, |
| 207 |
|
| 208 |
pub pricing_model: Option<String>, |
| 209 |
|
| 210 |
pub price_dollars: Option<f64>, |
| 211 |
|
| 212 |
pub pwyw_min_dollars: Option<f64>, |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
#[tracing::instrument(skip_all, name = "projects::update_project", fields(project_id))] |
| 217 |
pub(super) async fn update_project( |
| 218 |
State(db): State<PgPool>, |
| 219 |
AuthUser(user): AuthUser, |
| 220 |
Path(id): Path<ProjectId>, |
| 221 |
ValidatedJson(req): ValidatedJson<UpdateProjectRequest>, |
| 222 |
) -> Result<impl IntoResponse> { |
| 223 |
tracing::Span::current().record("project_id", tracing::field::display(&id)); |
| 224 |
user.check_not_suspended()?; |
| 225 |
verify_project_ownership(&db, id, user.id).await?; |
| 226 |
|
| 227 |
|
| 228 |
if let Some(ref title) = req.title { |
| 229 |
validation::validate_project_title(title)?; |
| 230 |
} |
| 231 |
if let Some(ref desc) = req.description { |
| 232 |
validation::validate_project_description(desc)?; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
if let Some(ref cat_name) = req.category { |
| 237 |
let trimmed = cat_name.trim(); |
| 238 |
if trimmed.is_empty() { |
| 239 |
db::projects::set_project_category(&db, id, user.id, None).await?; |
| 240 |
} else { |
| 241 |
let cat = db::categories::get_or_create_category(&db, trimmed).await?; |
| 242 |
db::projects::set_project_category(&db, id, user.id, Some(cat.id)).await?; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
if let Some(ref features) = req.features { |
| 248 |
for f in features { |
| 249 |
f.parse::<db::ProjectFeature>() |
| 250 |
.map_err(|_| AppError::validation(format!("Invalid feature: {f}")))?; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
let updated = db::projects::update_project( |
| 255 |
&db, |
| 256 |
id, |
| 257 |
user.id, |
| 258 |
req.title.as_deref(), |
| 259 |
req.description.as_deref(), |
| 260 |
req.features.as_deref(), |
| 261 |
req.is_public, |
| 262 |
) |
| 263 |
.await?; |
| 264 |
|
| 265 |
if let Some(ref model_str) = req.pricing_model { |
| 266 |
let kind: db::PricingKind = model_str |
| 267 |
.parse() |
| 268 |
.map_err(|_| AppError::validation(format!("Invalid pricing_model: {model_str}")))?; |
| 269 |
|
| 270 |
let price_cents = if kind == db::PricingKind::BuyOnce { |
| 271 |
let dollars = req |
| 272 |
.price_dollars |
| 273 |
.ok_or_else(|| AppError::validation("price_dollars required for buy_once"))?; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
let cents = crate::pricing::validate_dollars_f64("price_dollars", dollars)?; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
db::PriceCents::buy_once(cents)? |
| 282 |
} else { |
| 283 |
db::PriceCents::ZERO |
| 284 |
}; |
| 285 |
|
| 286 |
let pwyw_min_cents = if kind == db::PricingKind::Pwyw { |
| 287 |
let dollars = req.pwyw_min_dollars.unwrap_or(0.0); |
| 288 |
let cents = crate::pricing::validate_dollars_f64("pwyw_min_dollars", dollars)?; |
| 289 |
Some(db::PriceCents::new(cents)?) |
| 290 |
} else { |
| 291 |
None |
| 292 |
}; |
| 293 |
|
| 294 |
db::projects::update_project_pricing(&db, id, user.id, kind, price_cents, pwyw_min_cents) |
| 295 |
.await?; |
| 296 |
} |
| 297 |
|
| 298 |
db::projects::bump_cache_generation(&db, id).await?; |
| 299 |
|
| 300 |
Ok(Json(ProjectResponse { |
| 301 |
id: updated.id, |
| 302 |
slug: updated.slug.to_string(), |
| 303 |
title: updated.title, |
| 304 |
description: updated.description, |
| 305 |
project_type: updated.project_type, |
| 306 |
features: updated.features, |
| 307 |
is_public: updated.is_public, |
| 308 |
})) |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
#[derive(Debug, Deserialize)] |
| 313 |
pub(super) struct UpdateProjectThemeRequest { |
| 314 |
|
| 315 |
pub theme_id: Option<String>, |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
#[tracing::instrument(skip_all, name = "projects::update_project_theme", fields(project_id))] |
| 322 |
pub(super) async fn update_project_theme( |
| 323 |
State(db): State<PgPool>, |
| 324 |
AuthUser(user): AuthUser, |
| 325 |
Path(id): Path<ProjectId>, |
| 326 |
Form(req): Form<UpdateProjectThemeRequest>, |
| 327 |
) -> Result<impl IntoResponse> { |
| 328 |
tracing::Span::current().record("project_id", tracing::field::display(&id)); |
| 329 |
user.check_not_suspended()?; |
| 330 |
verify_project_ownership(&db, id, user.id).await?; |
| 331 |
|
| 332 |
let theme_id = crate::theming::normalize_theme_id(req.theme_id.as_deref()) |
| 333 |
.map_err(|t| AppError::validation(format!("Unknown theme: {t}")))?; |
| 334 |
db::projects::set_project_theme(&db, id, user.id, theme_id.as_deref()).await?; |
| 335 |
db::projects::bump_cache_generation(&db, id).await?; |
| 336 |
|
| 337 |
Ok(htmx_toast_response("Theme saved", "success")) |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
#[tracing::instrument(skip_all, name = "projects::delete_project", fields(project_id))] |
| 345 |
pub(super) async fn delete_project( |
| 346 |
State(db): State<PgPool>, |
| 347 |
State(config): State<Config>, |
| 348 |
State(storage): State<AppStorage>, |
| 349 |
AuthUser(user): AuthUser, |
| 350 |
Path(id): Path<ProjectId>, |
| 351 |
) -> Result<impl IntoResponse> { |
| 352 |
tracing::Span::current().record("project_id", tracing::field::display(&id)); |
| 353 |
user.check_not_suspended()?; |
| 354 |
let project = verify_project_ownership(&db, id, user.id).await?; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
let item_keys = db::items::get_project_item_s3_keys(&db, id).await?; |
| 361 |
let version_keys = db::items::get_project_version_s3_keys(&db, id).await?; |
| 362 |
let gallery_keys = db::gallery_images::s3_keys_for_project(&db, id).await?; |
| 363 |
|
| 364 |
let mut all_keys: Vec<(String, String)> = Vec::new(); |
| 365 |
|
| 366 |
all_keys.extend( |
| 367 |
version_keys |
| 368 |
.into_iter() |
| 369 |
.map(|k| (k, crate::storage::S3Bucket::Main.as_str().to_string())), |
| 370 |
); |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
for k in item_keys.into_iter().chain(gallery_keys) { |
| 377 |
all_keys.extend(crate::storage::both_bucket_delete(&k)); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
if let Some(ref url) = project.cover_image_url |
| 382 |
&& let Some(key) = crate::storage::extract_s3_key_from_url( |
| 383 |
url, |
| 384 |
&config.cdn_base_url, |
| 385 |
storage |
| 386 |
.s3 |
| 387 |
.as_deref() |
| 388 |
.map(crate::storage::StorageBackend::bucket), |
| 389 |
config.storage.as_ref().map(|c| c.endpoint.as_str()), |
| 390 |
) |
| 391 |
{ |
| 392 |
all_keys.extend(crate::storage::both_bucket_delete(&key)); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
db::pending_s3_deletions::enqueue_deletions(&db, &all_keys, "project_delete").await?; |
| 402 |
|
| 403 |
|
| 404 |
let storage_bytes = db::items::get_project_storage_bytes(&db, id).await?; |
| 405 |
if storage_bytes > 0 |
| 406 |
&& let Err(e) = db::creator_tiers::decrement_storage_used(&db, user.id, storage_bytes).await |
| 407 |
{ |
| 408 |
tracing::warn!(error = ?e, bytes = storage_bytes, "failed to decrement storage for project delete"); |
| 409 |
} |
| 410 |
|
| 411 |
db::projects::delete_project(&db, id, user.id).await?; |
| 412 |
db::users::bump_cache_generation(&db, user.id).await?; |
| 413 |
Ok(htmx_toast_response("Project deleted", "success")) |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
#[derive(Debug, Deserialize)] |
| 420 |
pub(super) struct LinkRepoRequest { |
| 421 |
pub name: String, |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
#[tracing::instrument(skip_all, name = "projects::link_repo")] |
| 426 |
pub(super) async fn link_repo( |
| 427 |
State(db): State<PgPool>, |
| 428 |
AuthUser(user): AuthUser, |
| 429 |
Path(id): Path<ProjectId>, |
| 430 |
Json(req): Json<LinkRepoRequest>, |
| 431 |
) -> Result<impl IntoResponse> { |
| 432 |
user.check_not_suspended()?; |
| 433 |
verify_project_ownership(&db, id, user.id).await?; |
| 434 |
|
| 435 |
let repo = db::git_repos::get_repo_by_user_and_name(&db, user.id, &req.name) |
| 436 |
.await? |
| 437 |
.ok_or(AppError::validation("Repository not found".to_string()))?; |
| 438 |
|
| 439 |
db::git_repos::link_repo_to_project(&db, repo.id, id).await?; |
| 440 |
db::projects::bump_cache_generation(&db, id).await?; |
| 441 |
|
| 442 |
Ok(htmx_toast_response("Repository linked", "success")) |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
#[tracing::instrument(skip_all, name = "projects::unlink_repo")] |
| 447 |
pub(super) async fn unlink_repo( |
| 448 |
State(db): State<PgPool>, |
| 449 |
AuthUser(user): AuthUser, |
| 450 |
Path((id, repo_name)): Path<(ProjectId, String)>, |
| 451 |
) -> Result<impl IntoResponse> { |
| 452 |
user.check_not_suspended()?; |
| 453 |
verify_project_ownership(&db, id, user.id).await?; |
| 454 |
|
| 455 |
let repo = db::git_repos::get_repo_by_user_and_name(&db, user.id, &repo_name) |
| 456 |
.await? |
| 457 |
.ok_or(AppError::validation("Repository not found".to_string()))?; |
| 458 |
|
| 459 |
db::git_repos::unlink_repo_from_project(&db, repo.id).await?; |
| 460 |
db::projects::bump_cache_generation(&db, id).await?; |
| 461 |
|
| 462 |
Ok(htmx_toast_response("Repository unlinked", "success")) |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
#[derive(Debug, Deserialize)] |
| 469 |
pub(super) struct CreateRepoRequest { |
| 470 |
pub name: String, |
| 471 |
pub visibility: Option<Visibility>, |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
#[derive(Debug, Serialize)] |
| 476 |
pub(super) struct RepoResponse { |
| 477 |
pub id: GitRepoId, |
| 478 |
pub name: String, |
| 479 |
pub visibility: Visibility, |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
#[tracing::instrument(skip_all, name = "projects::create_repo")] |
| 484 |
pub(super) async fn create_repo( |
| 485 |
State(db): State<PgPool>, |
| 486 |
State(config): State<Config>, |
| 487 |
AuthUser(user): AuthUser, |
| 488 |
Json(req): Json<CreateRepoRequest>, |
| 489 |
) -> Result<impl IntoResponse> { |
| 490 |
user.check_not_suspended()?; |
| 491 |
user.check_not_sandbox()?; |
| 492 |
|
| 493 |
|
| 494 |
let name = req.name.trim(); |
| 495 |
if name.is_empty() || name.len() > 64 { |
| 496 |
return Err(AppError::validation( |
| 497 |
"Repository name must be 1-64 characters".to_string(), |
| 498 |
)); |
| 499 |
} |
| 500 |
if name.starts_with('.') || name == ".." { |
| 501 |
return Err(AppError::validation("Invalid repository name".to_string())); |
| 502 |
} |
| 503 |
if !name |
| 504 |
.chars() |
| 505 |
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.') |
| 506 |
{ |
| 507 |
return Err(AppError::validation( |
| 508 |
"Repository name may only contain letters, numbers, hyphens, underscores, and dots" |
| 509 |
.to_string(), |
| 510 |
)); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
let visibility = req.visibility.unwrap_or(Visibility::Public); |
| 515 |
|
| 516 |
|
| 517 |
let git_root = config.build.git_repos_path.as_deref().ok_or_else(|| { |
| 518 |
AppError::validation("Git repositories are not configured on this server".to_string()) |
| 519 |
})?; |
| 520 |
|
| 521 |
|
| 522 |
if db::git_repos::get_repo_by_user_and_name(&db, user.id, name) |
| 523 |
.await? |
| 524 |
.is_some() |
| 525 |
{ |
| 526 |
return Err(AppError::validation( |
| 527 |
"A repository with that name already exists".to_string(), |
| 528 |
)); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
let username = user.username.to_string(); |
| 533 |
let owner_dir = std::path::Path::new(git_root).join(&username); |
| 534 |
let repo_dir = owner_dir.join(format!("{name}.git")); |
| 535 |
|
| 536 |
if repo_dir.exists() { |
| 537 |
return Err(AppError::validation( |
| 538 |
"A repository with that name already exists on disk".to_string(), |
| 539 |
)); |
| 540 |
} |
| 541 |
|
| 542 |
std::fs::create_dir_all(&owner_dir).context("create git owner directory")?; |
| 543 |
|
| 544 |
crate::git::init_bare_repo(&repo_dir).context("init bare git repo")?; |
| 545 |
|
| 546 |
|
| 547 |
if let Some(token) = &config.build.trigger_token { |
| 548 |
let hooks_dir = repo_dir.join("hooks"); |
| 549 |
let hook_path = hooks_dir.join("post-receive"); |
| 550 |
let hook_content = crate::build_runner::post_receive_hook(token, &username, name); |
| 551 |
if let Err(e) = std::fs::write(&hook_path, &hook_content) { |
| 552 |
tracing::warn!(error = ?e, "failed to install post-receive hook"); |
| 553 |
} else { |
| 554 |
#[cfg(unix)] |
| 555 |
{ |
| 556 |
use std::os::unix::fs::PermissionsExt; |
| 557 |
let _ = |
| 558 |
std::fs::set_permissions(&hook_path, std::fs::Permissions::from_mode(0o755)); |
| 559 |
} |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
let db_repo = |
| 565 |
db::git_repos::create_repo_with_visibility(&db, user.id, name, visibility).await?; |
| 566 |
|
| 567 |
Ok(Json(RepoResponse { |
| 568 |
id: db_repo.id, |
| 569 |
name: db_repo.name, |
| 570 |
visibility: db_repo.visibility, |
| 571 |
})) |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
#[derive(Debug, Deserialize)] |
| 576 |
pub(super) struct UpdateRepoVisibilityRequest { |
| 577 |
pub visibility: Visibility, |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
#[tracing::instrument(skip_all, name = "projects::update_repo_visibility")] |
| 582 |
pub(super) async fn update_repo_visibility( |
| 583 |
State(db): State<PgPool>, |
| 584 |
AuthUser(user): AuthUser, |
| 585 |
Path(repo_id): Path<GitRepoId>, |
| 586 |
Json(req): Json<UpdateRepoVisibilityRequest>, |
| 587 |
) -> Result<impl IntoResponse> { |
| 588 |
user.check_not_suspended()?; |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
let repo = db::git_repos::get_repo_by_id(&db, repo_id) |
| 593 |
.await? |
| 594 |
.ok_or(AppError::NotFound)?; |
| 595 |
|
| 596 |
if repo.user_id != user.id { |
| 597 |
return Err(AppError::Forbidden); |
| 598 |
} |
| 599 |
|
| 600 |
db::git_repos::update_visibility(&db, repo_id, req.visibility).await?; |
| 601 |
|
| 602 |
Ok(htmx_toast_response("Visibility updated", "success")) |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
#[derive(Debug, Deserialize)] |
| 609 |
pub(super) struct AddMemberForm { |
| 610 |
pub username: String, |
| 611 |
pub split_percent: i16, |
| 612 |
pub role: Option<db::ProjectRole>, |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
#[tracing::instrument(skip_all, name = "api::add_project_member")] |
| 617 |
pub(super) async fn add_project_member( |
| 618 |
State(db): State<PgPool>, |
| 619 |
AuthUser(session_user): AuthUser, |
| 620 |
Path(project_id): Path<ProjectId>, |
| 621 |
Form(form): Form<AddMemberForm>, |
| 622 |
) -> Result<Response> { |
| 623 |
let _project = verify_project_ownership(&db, project_id, session_user.id).await?; |
| 624 |
|
| 625 |
|
| 626 |
if form.split_percent < 1 || form.split_percent > 99 { |
| 627 |
return Err(AppError::validation( |
| 628 |
"Split must be between 1% and 99%".to_string(), |
| 629 |
)); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
let username = db::Username::new(&form.username)?; |
| 634 |
let member_user = db::users::get_user_by_username(&db, &username) |
| 635 |
.await? |
| 636 |
.ok_or_else(|| AppError::validation(format!("User '{}' not found", form.username)))?; |
| 637 |
|
| 638 |
|
| 639 |
if member_user.id == session_user.id { |
| 640 |
return Err(AppError::validation( |
| 641 |
"You are already the project owner".to_string(), |
| 642 |
)); |
| 643 |
} |
| 644 |
|
| 645 |
let role = form.role.unwrap_or(db::ProjectRole::Member); |
| 646 |
|
| 647 |
db::project_members::add_project_member( |
| 648 |
&db, |
| 649 |
project_id, |
| 650 |
member_user.id, |
| 651 |
role, |
| 652 |
form.split_percent, |
| 653 |
session_user.id, |
| 654 |
) |
| 655 |
.await?; |
| 656 |
|
| 657 |
|
| 658 |
db::projects::bump_cache_generation(&db, project_id).await?; |
| 659 |
|
| 660 |
Ok(htmx_toast_response( |
| 661 |
&format!( |
| 662 |
"Added @{} with {}% split", |
| 663 |
member_user.username, form.split_percent |
| 664 |
), |
| 665 |
"success", |
| 666 |
) |
| 667 |
.into_response()) |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
#[tracing::instrument(skip_all, name = "api::remove_project_member")] |
| 672 |
pub(super) async fn remove_project_member( |
| 673 |
State(db): State<PgPool>, |
| 674 |
AuthUser(session_user): AuthUser, |
| 675 |
Path((project_id, user_id)): Path<(ProjectId, db::UserId)>, |
| 676 |
) -> Result<Response> { |
| 677 |
verify_project_ownership(&db, project_id, session_user.id).await?; |
| 678 |
|
| 679 |
let removed = db::project_members::remove_project_member(&db, project_id, user_id).await?; |
| 680 |
|
| 681 |
if !removed { |
| 682 |
return Err(AppError::NotFound); |
| 683 |
} |
| 684 |
|
| 685 |
db::projects::bump_cache_generation(&db, project_id).await?; |
| 686 |
|
| 687 |
Ok(htmx_toast_response("Member removed", "success").into_response()) |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
#[derive(Debug, Deserialize)] |
| 693 |
pub(super) struct AddCollaboratorForm { |
| 694 |
pub username: String, |
| 695 |
#[serde(default = "default_true")] |
| 696 |
pub can_push: bool, |
| 697 |
} |
| 698 |
|
| 699 |
fn default_true() -> bool { |
| 700 |
true |
| 701 |
} |
| 702 |
|
| 703 |
#[derive(Debug, Serialize)] |
| 704 |
pub(super) struct CollaboratorResponse { |
| 705 |
pub user_id: UserId, |
| 706 |
pub username: String, |
| 707 |
pub can_push: bool, |
| 708 |
pub created_at: String, |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
async fn verify_repo_ownership( |
| 713 |
db: &PgPool, |
| 714 |
repo_id: GitRepoId, |
| 715 |
user_id: UserId, |
| 716 |
) -> Result<db::DbGitRepo> { |
| 717 |
let repo = db::git_repos::get_repo_by_id(db, repo_id) |
| 718 |
.await? |
| 719 |
.ok_or(AppError::NotFound)?; |
| 720 |
|
| 721 |
if repo.user_id != user_id { |
| 722 |
return Err(AppError::Forbidden); |
| 723 |
} |
| 724 |
|
| 725 |
Ok(repo) |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
#[tracing::instrument(skip_all, name = "api::add_repo_collaborator")] |
| 730 |
pub(super) async fn add_repo_collaborator( |
| 731 |
State(db): State<PgPool>, |
| 732 |
AuthUser(user): AuthUser, |
| 733 |
Path(repo_id): Path<GitRepoId>, |
| 734 |
Form(form): Form<AddCollaboratorForm>, |
| 735 |
) -> Result<Response> { |
| 736 |
user.check_not_suspended()?; |
| 737 |
|
| 738 |
let _repo = verify_repo_ownership(&db, repo_id, user.id).await?; |
| 739 |
|
| 740 |
let username = db::Username::new(&form.username)?; |
| 741 |
let collab_user = db::users::get_user_by_username(&db, &username) |
| 742 |
.await? |
| 743 |
.ok_or_else(|| AppError::validation(format!("User '{}' not found", form.username)))?; |
| 744 |
|
| 745 |
if collab_user.id == user.id { |
| 746 |
return Err(AppError::validation( |
| 747 |
"You are already the repo owner".to_string(), |
| 748 |
)); |
| 749 |
} |
| 750 |
|
| 751 |
db::repo_collaborators::add_collaborator(&db, repo_id, collab_user.id, form.can_push) |
| 752 |
.await |
| 753 |
.map_err(|e| { |
| 754 |
if let AppError::Database(ref db_err) = e |
| 755 |
&& db_err |
| 756 |
.to_string() |
| 757 |
.contains("repo_collaborators_repo_id_user_id_key") |
| 758 |
{ |
| 759 |
return AppError::validation("This user is already a collaborator".to_string()); |
| 760 |
} |
| 761 |
e |
| 762 |
})?; |
| 763 |
|
| 764 |
Ok(htmx_toast_response( |
| 765 |
&format!("Added @{} as collaborator", collab_user.username), |
| 766 |
"success", |
| 767 |
) |
| 768 |
.into_response()) |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
#[tracing::instrument(skip_all, name = "api::remove_repo_collaborator")] |
| 773 |
pub(super) async fn remove_repo_collaborator( |
| 774 |
State(db): State<PgPool>, |
| 775 |
AuthUser(user): AuthUser, |
| 776 |
Path((repo_id, collab_user_id)): Path<(GitRepoId, UserId)>, |
| 777 |
) -> Result<Response> { |
| 778 |
user.check_not_suspended()?; |
| 779 |
|
| 780 |
let _repo = verify_repo_ownership(&db, repo_id, user.id).await?; |
| 781 |
|
| 782 |
let removed = db::repo_collaborators::remove_collaborator(&db, repo_id, collab_user_id).await?; |
| 783 |
|
| 784 |
if !removed { |
| 785 |
return Err(AppError::NotFound); |
| 786 |
} |
| 787 |
|
| 788 |
Ok(htmx_toast_response("Collaborator removed", "success").into_response()) |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
#[tracing::instrument(skip_all, name = "api::list_repo_collaborators")] |
| 793 |
pub(super) async fn list_repo_collaborators( |
| 794 |
State(db): State<PgPool>, |
| 795 |
AuthUser(user): AuthUser, |
| 796 |
Path(repo_id): Path<GitRepoId>, |
| 797 |
) -> Result<impl IntoResponse> { |
| 798 |
let _repo = verify_repo_ownership(&db, repo_id, user.id).await?; |
| 799 |
|
| 800 |
let collabs = db::repo_collaborators::list_collaborators(&db, repo_id).await?; |
| 801 |
|
| 802 |
let data: Vec<CollaboratorResponse> = collabs |
| 803 |
.into_iter() |
| 804 |
.map(|c| CollaboratorResponse { |
| 805 |
user_id: c.user_id, |
| 806 |
username: c.username, |
| 807 |
can_push: c.can_push, |
| 808 |
created_at: c.created_at.format("%b %d, %Y").to_string(), |
| 809 |
}) |
| 810 |
.collect(); |
| 811 |
|
| 812 |
Ok(Json(ListResponse { data })) |
| 813 |
} |
| 814 |
|