|
| 1 | +//! 笔记组 commands(v0.11.0 REQ-195~198 系统层)。 |
| 2 | +//! |
| 3 | +//! @ai-context: 本层只做参数校验、调用数据层、错误映射(AGENTS.md §6); |
| 4 | +//! 路由决策在 group_route.rs(纯函数),组化编排在 note_group_assign.rs。 |
| 5 | +//! @ai-context: REQ-198 可见可改——override_group_route 落"修改即记忆"标记, |
| 6 | +//! 改判样本(route_reason)留痕供后续阈值校准。 |
| 7 | +
|
| 8 | +use tauri::State; |
| 9 | + |
| 10 | +use crate::commands::{normalize_title, AppState}; |
| 11 | +use crate::types::{NewNoteGroup, Note, NoteGroup}; |
| 12 | +use crate::video_profile_domain::DomainKind; |
| 13 | + |
| 14 | +/// 组类别白名单(非法值拒绝——前端枚举同口径)。 |
| 15 | +const GROUP_KINDS: [&str; 3] = ["course", "topic", "standalone"]; |
| 16 | + |
| 17 | +/// 组类别校验(白名单前置,防任意字符串入库)。 |
| 18 | +fn require_kind(kind: &str) -> Result<(), String> { |
| 19 | + if GROUP_KINDS.contains(&kind) { |
| 20 | + Ok(()) |
| 21 | + } else { |
| 22 | + Err(format!("不支持的组类别: {}(支持: {})", kind, GROUP_KINDS.join("/"))) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// 领域标签校验(kebab-case 白名单;空 → None)。 |
| 27 | +fn parse_domain(domain_tag: Option<String>) -> Result<Option<String>, String> { |
| 28 | + match domain_tag { |
| 29 | + Some(t) if !t.trim().is_empty() => DomainKind::parse(t.trim()) |
| 30 | + .map(|k| Some(k.as_str().to_string())) |
| 31 | + .ok_or_else(|| format!("不支持的领域标签: {}", t)), |
| 32 | + _ => Ok(None), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// 列出笔记组(含组内笔记数;terrain 可选过滤 container/feed)。 |
| 37 | +#[tauri::command] |
| 38 | +pub fn list_note_groups( |
| 39 | + state: State<'_, AppState>, |
| 40 | + terrain: Option<String>, |
| 41 | +) -> Result<Vec<NoteGroup>, String> { |
| 42 | + let terrain = terrain.as_deref().filter(|t| !t.trim().is_empty()); |
| 43 | + if let Some(t) = terrain { |
| 44 | + if t != "container" && t != "feed" { |
| 45 | + return Err(format!("不支持的地形: {}(支持: container/feed)", t)); |
| 46 | + } |
| 47 | + } |
| 48 | + state.db.list_groups(terrain).map_err(|e| e.to_string()) |
| 49 | +} |
| 50 | + |
| 51 | +/// 单个组详情(不存在 → 错误;前端按 id 刷新用)。 |
| 52 | +#[tauri::command] |
| 53 | +pub fn get_note_group(state: State<'_, AppState>, id: i64) -> Result<NoteGroup, String> { |
| 54 | + if id <= 0 { |
| 55 | + return Err("无效的组 id".to_string()); |
| 56 | + } |
| 57 | + state |
| 58 | + .db |
| 59 | + .get_group(id) |
| 60 | + .map_err(|e| e.to_string())? |
| 61 | + .ok_or_else(|| format!("笔记组不存在: {}", id)) |
| 62 | +} |
| 63 | + |
| 64 | +/// 组内笔记列表(组详情面板数据源)。 |
| 65 | +#[tauri::command] |
| 66 | +pub fn list_group_notes(state: State<'_, AppState>, group_id: i64) -> Result<Vec<Note>, String> { |
| 67 | + if group_id <= 0 { |
| 68 | + return Err("无效的组 id".to_string()); |
| 69 | + } |
| 70 | + state.db.list_notes_by_group(group_id).map_err(|e| e.to_string()) |
| 71 | +} |
| 72 | + |
| 73 | +/// 用户自建主题组(manual——契约一:粒度对齐领域,领域标签必填)。 |
| 74 | +#[tauri::command] |
| 75 | +pub fn create_topic_group( |
| 76 | + state: State<'_, AppState>, |
| 77 | + name: String, |
| 78 | + domain_tag: String, |
| 79 | + terrain: Option<String>, |
| 80 | +) -> Result<NoteGroup, String> { |
| 81 | + let name = normalize_title(name, "未命名组"); |
| 82 | + let domain = parse_domain(Some(domain_tag))? |
| 83 | + .ok_or_else(|| "主题组必须指定领域标签".to_string())?; |
| 84 | + let terrain = terrain.unwrap_or_else(|| "container".to_string()); |
| 85 | + if terrain != "container" && terrain != "feed" { |
| 86 | + return Err(format!("不支持的地形: {}", terrain)); |
| 87 | + } |
| 88 | + // 同领域同地形已有主题组 → 直接复用(契约一防重复抽屉) |
| 89 | + if let Some(existing) = state.db.find_topic_group(&domain, &terrain).map_err(|e| e.to_string())? { |
| 90 | + return Ok(existing); |
| 91 | + } |
| 92 | + let label = DomainKind::parse(&domain).map(|k| k.label()).unwrap_or("主题组"); |
| 93 | + state |
| 94 | + .db |
| 95 | + .create_group(&NewNoteGroup { |
| 96 | + name: if name == "未命名组" { label.to_string() } else { name }, |
| 97 | + terrain, |
| 98 | + kind: "topic".to_string(), |
| 99 | + domain_tag: Some(domain), |
| 100 | + source: "manual".to_string(), |
| 101 | + series_key: None, |
| 102 | + route_reason: None, |
| 103 | + }) |
| 104 | + .map_err(|e| e.to_string()) |
| 105 | +} |
| 106 | + |
| 107 | +/// 重命名组。 |
| 108 | +#[tauri::command] |
| 109 | +pub fn rename_note_group( |
| 110 | + state: State<'_, AppState>, |
| 111 | + id: i64, |
| 112 | + name: String, |
| 113 | +) -> Result<bool, String> { |
| 114 | + if id <= 0 { |
| 115 | + return Err("无效的组 id".to_string()); |
| 116 | + } |
| 117 | + let name = normalize_title(name, "未命名组"); |
| 118 | + state.db.rename_group(id, &name).map_err(|e| e.to_string()) |
| 119 | +} |
| 120 | + |
| 121 | +/// 路由改判(REQ-198 修改即记忆):改组类别/领域,留痕 route_reason。 |
| 122 | +#[tauri::command] |
| 123 | +pub fn override_group_route( |
| 124 | + state: State<'_, AppState>, |
| 125 | + id: i64, |
| 126 | + kind: String, |
| 127 | + domain_tag: Option<String>, |
| 128 | +) -> Result<bool, String> { |
| 129 | + if id <= 0 { |
| 130 | + return Err("无效的组 id".to_string()); |
| 131 | + } |
| 132 | + require_kind(&kind)?; |
| 133 | + let domain = parse_domain(domain_tag)?; |
| 134 | + // 主题组改判必须带领域(契约一:主题组粒度对齐领域) |
| 135 | + if kind == "topic" && domain.is_none() { |
| 136 | + return Err("改判为主题组必须指定领域标签".to_string()); |
| 137 | + } |
| 138 | + let reason = format!( |
| 139 | + "用户改判:{}{}", |
| 140 | + kind, |
| 141 | + domain.as_deref().map(|d| format!("({})", d)).unwrap_or_default() |
| 142 | + ); |
| 143 | + state |
| 144 | + .db |
| 145 | + .override_group_route(id, &kind, domain.as_deref(), &reason) |
| 146 | + .map_err(|e| e.to_string()) |
| 147 | +} |
| 148 | + |
| 149 | +/// 移动笔记到组(group_id=None 移出组——手动纠错路由误判的兜底路径)。 |
| 150 | +#[tauri::command] |
| 151 | +pub fn move_note_to_group( |
| 152 | + state: State<'_, AppState>, |
| 153 | + note_id: i64, |
| 154 | + group_id: Option<i64>, |
| 155 | +) -> Result<bool, String> { |
| 156 | + if note_id <= 0 { |
| 157 | + return Err("无效的笔记 id".to_string()); |
| 158 | + } |
| 159 | + if let Some(gid) = group_id { |
| 160 | + if gid <= 0 { |
| 161 | + return Err("无效的组 id".to_string()); |
| 162 | + } |
| 163 | + if state.db.get_group(gid).map_err(|e| e.to_string())?.is_none() { |
| 164 | + return Err(format!("笔记组不存在: {}", gid)); |
| 165 | + } |
| 166 | + } |
| 167 | + state.db.update_note_group(note_id, group_id).map_err(|e| e.to_string()) |
| 168 | +} |
0 commit comments