Files
appks/service/im/channel_roles.rs
zhenyi 420dedbc1e 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
2026-06-10 18:49:32 +08:00

112 lines
3.5 KiB
Rust

use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::error::AppError;
use crate::models::channels::ChannelMemberRole;
use crate::service::ImService;
use super::session::ImSession;
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
pub struct CreateChannelRoleParams {
pub name: String,
pub description: Option<String>,
pub permissions: Vec<String>,
pub assignable: bool,
}
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
pub struct UpdateChannelRoleParams {
pub name: Option<String>,
pub description: Option<String>,
pub permissions: Option<Vec<String>>,
pub assignable: Option<bool>,
}
impl ImService {
pub async fn channel_role_list(
&self,
_ctx: &ImSession,
channel_id: Uuid,
) -> Result<Vec<ChannelMemberRole>, AppError> {
sqlx::query_as::<_, ChannelMemberRole>(
"SELECT id, channel_id, name, description, permissions, assignable, \
created_by, created_at, updated_at \
FROM channel_member_role WHERE channel_id = $1 ORDER BY created_at",
)
.bind(channel_id)
.fetch_all(self.ctx.db.reader())
.await
.map_err(AppError::Database)
}
pub async fn channel_role_create(
&self,
ctx: &ImSession,
channel_id: Uuid,
params: CreateChannelRoleParams,
) -> Result<ChannelMemberRole, AppError> {
let now = chrono::Utc::now();
sqlx::query_as::<_, ChannelMemberRole>(
"INSERT INTO channel_member_role \
(id, channel_id, name, description, permissions, assignable, created_by, created_at, updated_at) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $8) \
RETURNING id, channel_id, name, description, permissions, assignable, \
created_by, created_at, updated_at",
)
.bind(Uuid::now_v7())
.bind(channel_id)
.bind(&params.name)
.bind(params.description.as_deref())
.bind(&params.permissions)
.bind(params.assignable)
.bind(ctx.user)
.bind(now)
.fetch_one(self.ctx.db.writer())
.await
.map_err(AppError::Database)
}
pub async fn channel_role_update(
&self,
_ctx: &ImSession,
role_id: Uuid,
params: UpdateChannelRoleParams,
) -> Result<ChannelMemberRole, AppError> {
let now = chrono::Utc::now();
sqlx::query_as::<_, ChannelMemberRole>(
"UPDATE channel_member_role SET \
name = COALESCE($1, name), \
description = COALESCE($2, description), \
permissions = COALESCE($3, permissions), \
assignable = COALESCE($4, assignable), \
updated_at = $5 \
WHERE id = $6 \
RETURNING id, channel_id, name, description, permissions, assignable, \
created_by, created_at, updated_at",
)
.bind(params.name.as_deref())
.bind(params.description.as_deref())
.bind(params.permissions.as_ref())
.bind(params.assignable)
.bind(now)
.bind(role_id)
.fetch_one(self.ctx.db.writer())
.await
.map_err(AppError::Database)
}
pub async fn channel_role_delete(
&self,
_ctx: &ImSession,
role_id: Uuid,
) -> Result<(), AppError> {
sqlx::query("DELETE FROM channel_member_role WHERE id = $1")
.bind(role_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
Ok(())
}
}