420dedbc1e
- 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
121 lines
4.3 KiB
Rust
121 lines
4.3 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use crate::error::AppError;
|
|
use crate::models::common::Role;
|
|
use crate::models::workspaces::{Workspace, WorkspaceCustomBranding};
|
|
use crate::service::WorkspaceService;
|
|
use crate::session::Session;
|
|
|
|
use super::util::{merge_optional_text, set_local_user_id};
|
|
|
|
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
|
pub struct UpdateBrandingParams {
|
|
pub logo_url: Option<String>,
|
|
pub favicon_url: Option<String>,
|
|
pub primary_color: Option<String>,
|
|
pub accent_color: Option<String>,
|
|
pub custom_css: Option<String>,
|
|
pub support_url: Option<String>,
|
|
pub enabled: Option<bool>,
|
|
}
|
|
|
|
impl WorkspaceService {
|
|
pub async fn workspace_branding(
|
|
&self,
|
|
ctx: &Session,
|
|
ws: &Workspace,
|
|
) -> Result<WorkspaceCustomBranding, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
self.ensure_workspace_role_at_least(user_uid, &ws, Role::Admin)
|
|
.await?;
|
|
self.ensure_workspace_branding(ws.id).await
|
|
}
|
|
|
|
pub async fn workspace_update_branding(
|
|
&self,
|
|
ctx: &Session,
|
|
ws: &Workspace,
|
|
params: UpdateBrandingParams,
|
|
) -> Result<WorkspaceCustomBranding, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
self.ensure_workspace_role_at_least(user_uid, &ws, Role::Admin)
|
|
.await?;
|
|
|
|
let current = self.ensure_workspace_branding(ws.id).await?;
|
|
let now = chrono::Utc::now();
|
|
|
|
let mut txn = self
|
|
.ctx
|
|
.db
|
|
.writer()
|
|
.begin()
|
|
.await
|
|
.map_err(|_| AppError::TxnError)?;
|
|
sqlx::query(set_local_user_id(user_uid))
|
|
.execute(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
|
|
let result = sqlx::query_as::<_, WorkspaceCustomBranding>(
|
|
"UPDATE workspace_custom_branding SET logo_url = $1, favicon_url = $2, primary_color = $3, \
|
|
accent_color = $4, custom_css = $5, support_url = $6, enabled = $7, updated_at = $8 \
|
|
WHERE workspace_id = $9 \
|
|
RETURNING workspace_id, logo_url, favicon_url, primary_color, accent_color, \
|
|
custom_css, support_url, enabled, created_at, updated_at",
|
|
)
|
|
.bind(merge_optional_text(params.logo_url, current.logo_url))
|
|
.bind(merge_optional_text(params.favicon_url, current.favicon_url))
|
|
.bind(merge_optional_text(params.primary_color, current.primary_color))
|
|
.bind(merge_optional_text(params.accent_color, current.accent_color))
|
|
.bind(merge_optional_text(params.custom_css, current.custom_css))
|
|
.bind(merge_optional_text(params.support_url, current.support_url))
|
|
.bind(params.enabled.unwrap_or(current.enabled))
|
|
.bind(now)
|
|
.bind(ws.id)
|
|
.fetch_one(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
|
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
|
Ok(result)
|
|
}
|
|
|
|
async fn ensure_workspace_branding(
|
|
&self,
|
|
workspace_id: Uuid,
|
|
) -> Result<WorkspaceCustomBranding, AppError> {
|
|
if let Some(branding) = self.find_workspace_branding(workspace_id).await? {
|
|
return Ok(branding);
|
|
}
|
|
let now = chrono::Utc::now();
|
|
sqlx::query(
|
|
"INSERT INTO workspace_custom_branding (workspace_id, enabled, created_at, updated_at) \
|
|
VALUES ($1, false, $2, $2) ON CONFLICT (workspace_id) DO NOTHING",
|
|
)
|
|
.bind(workspace_id)
|
|
.bind(now)
|
|
.execute(self.ctx.db.writer())
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
self.find_workspace_branding(workspace_id)
|
|
.await?
|
|
.ok_or(AppError::NotFound("workspace branding not found".into()))
|
|
}
|
|
|
|
async fn find_workspace_branding(
|
|
&self,
|
|
workspace_id: Uuid,
|
|
) -> Result<Option<WorkspaceCustomBranding>, AppError> {
|
|
sqlx::query_as::<_, WorkspaceCustomBranding>(
|
|
"SELECT workspace_id, logo_url, favicon_url, primary_color, accent_color, \
|
|
custom_css, support_url, enabled, created_at, updated_at \
|
|
FROM workspace_custom_branding WHERE workspace_id = $1",
|
|
)
|
|
.bind(workspace_id)
|
|
.fetch_optional(self.ctx.db.reader())
|
|
.await
|
|
.map_err(AppError::Database)
|
|
}
|
|
}
|