feat(service): expand service layer with new domain operations
- Add IM service modules: audit, channel roles, custom emojis, forum tags, integrations, invitations, repo links, slash commands, stages, voice, webhooks - Add PR service modules: review requests, templates - Add repo service modules: contributors, release assets, git extras (archive, branch rename, commit extras, diff/merge, tag, tree) - Add user service: social (follow/block) - Add internal auth service - Update existing service modules with expanded functionality - Remove deleted IM modules: articles, delivery trace, drafts, follows, messages, polls, presence, reactions, threads
This commit is contained in:
@@ -5,7 +5,7 @@ use crate::models::prs::PrAssignee;
|
||||
use crate::service::PrService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected};
|
||||
use super::util::{clamp_limit_offset, ensure_affected, set_local_user_id};
|
||||
|
||||
impl PrService {
|
||||
pub async fn pr_assignees(
|
||||
@@ -48,8 +48,7 @@ impl PrService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -98,8 +97,7 @@ impl PrService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::models::prs::{PrLabel, PrLabelRelation};
|
||||
use crate::service::PrService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected, required_text};
|
||||
use super::util::{clamp_limit_offset, ensure_affected, required_text, set_local_user_id};
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct CreatePrLabelParams {
|
||||
@@ -140,8 +140,7 @@ impl PrService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -182,8 +181,7 @@ impl PrService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
@@ -7,7 +7,9 @@ pub mod files;
|
||||
pub mod labels;
|
||||
pub mod merge_strategy;
|
||||
pub mod reactions;
|
||||
pub mod review_requests;
|
||||
pub mod reviews;
|
||||
pub mod status;
|
||||
pub mod subscriptions;
|
||||
pub mod templates;
|
||||
pub mod util;
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::models::common::Role;
|
||||
use crate::models::prs::PrReviewRequest;
|
||||
use crate::service::PrService;
|
||||
use crate::session::Session;
|
||||
|
||||
impl PrService {
|
||||
pub async fn pr_requested_reviewers(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
number: i64,
|
||||
) -> Result<Vec<PrReviewRequest>, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_readable(user_uid, &repo).await?;
|
||||
let pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
||||
self.ensure_pr_readable(user_uid, &pr).await?;
|
||||
sqlx::query_as::<_, PrReviewRequest>(
|
||||
"SELECT * FROM pr_review_request WHERE pull_request_id = $1 ORDER BY created_at ASC",
|
||||
)
|
||||
.bind(pr.id)
|
||||
.fetch_all(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn pr_request_reviewers(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
number: i64,
|
||||
reviewer_ids: Vec<Uuid>,
|
||||
) -> Result<Vec<PrReviewRequest>, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role_at_least(user_uid, &repo, Role::Member)
|
||||
.await?;
|
||||
let pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
||||
self.ensure_pr_editable(user_uid, &pr).await?;
|
||||
|
||||
let mut created = Vec::new();
|
||||
for reviewer_id in reviewer_ids {
|
||||
let result = sqlx::query_as::<_, PrReviewRequest>(
|
||||
"INSERT INTO pr_review_request (id, pull_request_id, reviewer_id, requested_by, created_at) \
|
||||
VALUES ($1, $2, $3, $4, NOW()) ON CONFLICT (pull_request_id, reviewer_id) DO NOTHING RETURNING *",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(pr.id)
|
||||
.bind(reviewer_id)
|
||||
.bind(user_uid)
|
||||
.fetch_optional(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
if let Some(r) = result {
|
||||
created.push(r);
|
||||
}
|
||||
}
|
||||
Ok(created)
|
||||
}
|
||||
|
||||
pub async fn pr_remove_requested_reviewer(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
number: i64,
|
||||
reviewer_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role_at_least(user_uid, &repo, Role::Member)
|
||||
.await?;
|
||||
let pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
||||
self.ensure_pr_editable(user_uid, &pr).await?;
|
||||
sqlx::query(
|
||||
"DELETE FROM pr_review_request WHERE pull_request_id = $1 AND reviewer_id = $2",
|
||||
)
|
||||
.bind(pr.id)
|
||||
.bind(reviewer_id)
|
||||
.execute(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ use crate::models::prs::PrSubscription;
|
||||
use crate::service::PrService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected};
|
||||
use super::util::{clamp_limit_offset, ensure_affected, set_local_user_id};
|
||||
|
||||
impl PrService {
|
||||
pub async fn pr_subscriptions(
|
||||
@@ -47,8 +47,7 @@ impl PrService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -83,8 +82,7 @@ impl PrService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::models::common::Role;
|
||||
use crate::models::prs::PrTemplate;
|
||||
use crate::service::PrService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, merge_optional_text, required_text};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct CreatePrTemplateParams {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub title_template: Option<String>,
|
||||
pub body_template: String,
|
||||
pub labels: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UpdatePrTemplateParams {
|
||||
pub name: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub title_template: Option<String>,
|
||||
pub body_template: Option<String>,
|
||||
pub labels: Option<Vec<String>>,
|
||||
pub active: Option<bool>,
|
||||
}
|
||||
|
||||
impl PrService {
|
||||
pub async fn pr_templates(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<PrTemplate>, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_readable(user_uid, &repo).await?;
|
||||
let (limit, offset) = clamp_limit_offset(limit, offset);
|
||||
sqlx::query_as::<_, PrTemplate>(
|
||||
"SELECT * FROM pr_template WHERE repo_id = $1 AND active = true ORDER BY name ASC LIMIT $2 OFFSET $3",
|
||||
)
|
||||
.bind(repo.id)
|
||||
.bind(limit)
|
||||
.bind(offset)
|
||||
.fetch_all(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn pr_create_template(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
params: CreatePrTemplateParams,
|
||||
) -> Result<PrTemplate, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role_at_least(user_uid, &repo, Role::Member)
|
||||
.await?;
|
||||
let name = required_text(params.name, "name")?;
|
||||
sqlx::query_as::<_, PrTemplate>(
|
||||
"INSERT INTO pr_template (id, repo_id, name, description, title_template, body_template, labels, created_by, created_at, updated_at) \
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW(), NOW()) RETURNING *",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(repo.id)
|
||||
.bind(&name)
|
||||
.bind(params.description.as_deref())
|
||||
.bind(params.title_template.as_deref())
|
||||
.bind(¶ms.body_template)
|
||||
.bind(¶ms.labels)
|
||||
.bind(user_uid)
|
||||
.fetch_one(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn pr_update_template(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
template_id: Uuid,
|
||||
params: UpdatePrTemplateParams,
|
||||
) -> Result<PrTemplate, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role_at_least(user_uid, &repo, Role::Admin)
|
||||
.await?;
|
||||
let current: PrTemplate =
|
||||
sqlx::query_as("SELECT * FROM pr_template WHERE id = $1 AND repo_id = $2")
|
||||
.bind(template_id)
|
||||
.bind(repo.id)
|
||||
.fetch_optional(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("template not found".into()))?;
|
||||
|
||||
let name =
|
||||
merge_optional_text(params.name, Some(current.name.clone())).unwrap_or(current.name);
|
||||
let description = merge_optional_text(params.description, current.description);
|
||||
let title_template = merge_optional_text(params.title_template, current.title_template);
|
||||
let body_template = merge_optional_text(params.body_template, Some(current.body_template))
|
||||
.unwrap_or_default();
|
||||
let labels = params.labels.unwrap_or(current.labels);
|
||||
let active = params.active.unwrap_or(current.active);
|
||||
|
||||
sqlx::query_as::<_, PrTemplate>(
|
||||
"UPDATE pr_template SET name = $1, description = $2, title_template = $3, body_template = $4, \
|
||||
labels = $5, active = $6, updated_at = NOW() WHERE id = $7 AND repo_id = $8 RETURNING *",
|
||||
)
|
||||
.bind(&name)
|
||||
.bind(&description)
|
||||
.bind(&title_template)
|
||||
.bind(&body_template)
|
||||
.bind(&labels)
|
||||
.bind(active)
|
||||
.bind(template_id)
|
||||
.bind(repo.id)
|
||||
.fetch_one(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn pr_delete_template(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
template_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role_at_least(user_uid, &repo, Role::Admin)
|
||||
.await?;
|
||||
sqlx::query("DELETE FROM pr_template WHERE id = $1 AND repo_id = $2")
|
||||
.bind(template_id)
|
||||
.bind(repo.id)
|
||||
.execute(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
pub use crate::service::util::{
|
||||
clamp_limit_offset, ensure_affected, merge_optional_text, parse_enum, required_text, role_level,
|
||||
clamp_limit_offset, ensure_affected, merge_optional_text, parse_enum, required_text,
|
||||
role_level, set_local_user_id,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user