| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
use axum::{Form, extract::State, response::IntoResponse}; |
| 13 |
use serde::Deserialize; |
| 14 |
use sqlx::PgPool; |
| 15 |
|
| 16 |
use crate::{ |
| 17 |
auth::AuthUser, |
| 18 |
db::{self, GitRepoId}, |
| 19 |
error::{AppError, Result}, |
| 20 |
}; |
| 21 |
|
| 22 |
#[derive(Deserialize)] |
| 23 |
pub(in crate::routes) struct MuteForm { |
| 24 |
repo_id: String, |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
owner: String, |
| 29 |
|
| 30 |
|
| 31 |
muted: Option<String>, |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#[tracing::instrument(skip_all, name = "api::set_repo_notifications")] |
| 41 |
pub(in crate::routes) async fn set_repo_notifications( |
| 42 |
State(db): State<PgPool>, |
| 43 |
AuthUser(user): AuthUser, |
| 44 |
Form(form): Form<MuteForm>, |
| 45 |
) -> Result<impl IntoResponse> { |
| 46 |
let repo_id: GitRepoId = form |
| 47 |
.repo_id |
| 48 |
.parse() |
| 49 |
.map_err(|_| AppError::BadRequest("Invalid repository".to_string()))?; |
| 50 |
|
| 51 |
let Some(repo) = db::git_repos::get_repo_by_id(&db, repo_id).await? else { |
| 52 |
return Err(AppError::NotFound); |
| 53 |
}; |
| 54 |
|
| 55 |
if repo.visibility == db::Visibility::Private |
| 56 |
&& repo.user_id != user.id |
| 57 |
&& !db::repo_collaborators::is_collaborator(&db, repo.id, user.id).await? |
| 58 |
{ |
| 59 |
|
| 60 |
return Err(AppError::NotFound); |
| 61 |
} |
| 62 |
|
| 63 |
let muted = form.muted.as_deref() == Some("true"); |
| 64 |
db::lists::set_repo_muted( |
| 65 |
&db, |
| 66 |
*repo.id.as_uuid(), |
| 67 |
user.id, |
| 68 |
db::ListKind::Issues, |
| 69 |
muted, |
| 70 |
) |
| 71 |
.await?; |
| 72 |
|
| 73 |
Ok(axum::response::Redirect::to(&format!( |
| 74 |
"/git/{}/{}", |
| 75 |
form.owner, repo.name |
| 76 |
))) |
| 77 |
} |
| 78 |
|