124 lines
4.3 KiB
Rust
124 lines
4.3 KiB
Rust
use uuid::Uuid;
|
|
|
|
use crate::error::AppError;
|
|
use crate::models::prs::PrSubscription;
|
|
use crate::service::PrService;
|
|
use crate::session::Session;
|
|
|
|
use super::util::{clamp_limit_offset, ensure_affected};
|
|
|
|
impl PrService {
|
|
pub async fn pr_subscriptions(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
number: i64,
|
|
limit: i64,
|
|
offset: i64,
|
|
) -> Result<Vec<PrSubscription>, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
|
self.ensure_pr_readable(user_uid, &pr).await?;
|
|
let (limit, offset) = clamp_limit_offset(limit, offset);
|
|
sqlx::query_as::<_, PrSubscription>(
|
|
"SELECT id, pull_request_id, user_id, reason, muted, created_at, updated_at \
|
|
FROM pr_subscription WHERE pull_request_id = $1 ORDER BY created_at ASC LIMIT $2 OFFSET $3",
|
|
)
|
|
.bind(pr.id).bind(limit).bind(offset)
|
|
.fetch_all(self.ctx.db.reader()).await.map_err(AppError::Database)
|
|
}
|
|
|
|
pub async fn pr_subscribe(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
number: i64,
|
|
) -> Result<PrSubscription, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
|
self.ensure_pr_readable(user_uid, &pr).await?;
|
|
let now = chrono::Utc::now();
|
|
let mut txn = self
|
|
.ctx
|
|
.db
|
|
.writer()
|
|
.begin()
|
|
.await
|
|
.map_err(|_| AppError::TxnError)?;
|
|
sqlx::query("SET LOCAL app.current_user_id = $1")
|
|
.bind(user_uid)
|
|
.execute(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
|
|
let sub = sqlx::query_as::<_, PrSubscription>(
|
|
"INSERT INTO pr_subscription (id, pull_request_id, user_id, reason, muted, created_at, updated_at) \
|
|
VALUES ($1, $2, $3, 'manual', false, $4, $4) ON CONFLICT (pull_request_id, user_id) DO NOTHING \
|
|
RETURNING id, pull_request_id, user_id, reason, muted, created_at, updated_at",
|
|
)
|
|
.bind(Uuid::now_v7()).bind(pr.id).bind(user_uid).bind(now)
|
|
.fetch_optional(&mut *txn).await.map_err(AppError::Database)?
|
|
.ok_or(AppError::Conflict("already subscribed".into()))?;
|
|
|
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
|
Ok(sub)
|
|
}
|
|
|
|
pub async fn pr_unsubscribe(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
number: i64,
|
|
) -> Result<(), AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
|
self.ensure_pr_readable(user_uid, &pr).await?;
|
|
let mut txn = self
|
|
.ctx
|
|
.db
|
|
.writer()
|
|
.begin()
|
|
.await
|
|
.map_err(|_| AppError::TxnError)?;
|
|
sqlx::query("SET LOCAL app.current_user_id = $1")
|
|
.bind(user_uid)
|
|
.execute(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
|
|
let result =
|
|
sqlx::query("DELETE FROM pr_subscription WHERE pull_request_id = $1 AND user_id = $2")
|
|
.bind(pr.id)
|
|
.bind(user_uid)
|
|
.execute(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
ensure_affected(result.rows_affected(), "not subscribed")?;
|
|
|
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn pr_mute(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
number: i64,
|
|
muted: bool,
|
|
) -> Result<(), AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
|
self.ensure_pr_readable(user_uid, &pr).await?;
|
|
let result = sqlx::query(
|
|
"UPDATE pr_subscription SET muted = $1, updated_at = $2 WHERE pull_request_id = $3 AND user_id = $4",
|
|
)
|
|
.bind(muted).bind(chrono::Utc::now()).bind(pr.id).bind(user_uid)
|
|
.execute(self.ctx.db.writer()).await.map_err(AppError::Database)?;
|
|
ensure_affected(result.rows_affected(), "not subscribed")
|
|
}
|
|
}
|