openlark-docs 0.17.0

飞书开放平台云文档服务模块 - 文档、表格、知识库API (202 APIs, 100% 覆盖,不含旧版本)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/// 文档服务通用数据模型
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// use openlark_core::api::Response;

/// 文档资源基础信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentBase {
    /// 文档ID
    pub document_id: String,
    /// 文档标题
    pub title: String,
    /// 文档类型
    pub doc_type: DocumentType,
    /// 创建时间
    pub create_time: DateTime<Utc>,
    /// 修改时间
    pub modify_time: DateTime<Utc>,
    /// 创建者信息
    pub creator: UserInfo,
    /// 修改者信息
    pub modifier: Option<UserInfo>,
    /// 文档状态
    pub status: DocumentStatus,
}

/// 文档类型枚举
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum DocumentType {
    /// 文本文档
    Doc,
    /// 表格
    Sheet,
    /// 幻灯片
    Slide,
    /// 思维笔记
    Mindnote,
    /// 流程图
    Flowchart,
    /// 多人实时文档
    Bitable,
    /// Wiki页面
    Wiki,
    /// 文件夹
    Folder,
    /// 其他
    Other(String),
}

/// 文档状态
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum DocumentStatus {
    /// 正常
    Normal,
    /// 回收站
    Recycle,
    /// 删除
    Deleted,
    /// 加密
    Encrypted,
    /// 归档
    Archived,
}

/// 用户信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct UserInfo {
    /// 用户ID
    pub user_id: String,
    /// 用户名
    pub name: String,
    /// 邮箱
    pub email: Option<String>,
    /// 头像URL
    pub avatar_url: Option<String>,
    /// 部门信息
    pub department: Option<DepartmentInfo>,
}

/// 部门信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DepartmentInfo {
    /// 部门ID
    pub department_id: String,
    /// 部门名称
    pub name: String,
}

/// 文件信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FileInfo {
    /// 文件ID
    pub file_id: String,
    /// 文件名
    pub name: String,
    /// 文件大小(字节)
    pub size: u64,
    /// MIME类型
    pub mime_type: String,
    /// 文件URL
    pub url: String,
    /// 下载Token
    pub download_token: Option<String>,
    /// 缩略图URL
    pub thumbnail_url: Option<String>,
}

/// 权限信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Permission {
    /// 权限类型
    pub permission_type: PermissionType,
    /// 是否可读
    pub can_read: bool,
    /// 是否可写
    pub can_write: bool,
    /// 是否可删除
    pub can_delete: bool,
    /// 是否可分享
    pub can_share: bool,
    /// 权限到期时间
    pub expire_time: Option<DateTime<Utc>>,
}

/// 权限类型
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum PermissionType {
    /// 所有者
    Owner,
    /// 管理员
    Admin,
    /// 编辑者
    Editor,
    /// 评论者
    Commenter,
    /// 查看者
    Viewer,
}

/// 分享信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ShareInfo {
    /// 分享链接
    pub share_url: String,
    /// 分享Token
    pub share_token: String,
    /// 权限类型
    pub permission_type: PermissionType,
    /// 是否需要密码
    pub need_password: bool,
    /// 过期时间
    pub expire_time: Option<DateTime<Utc>>,
    /// 访问次数限制
    pub visit_limit: Option<u32>,
    /// 当前访问次数
    pub visit_count: u32,
}

/// 版本信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VersionInfo {
    /// 版本号
    pub version: i32,
    /// 版本名称
    pub version_name: String,
    /// 创建时间
    pub create_time: DateTime<Utc>,
    /// 创建者
    pub creator: UserInfo,
    /// 版本描述
    pub description: Option<String>,
    /// 是否为当前版本
    pub is_current: bool,
    /// 文件大小
    pub size: u64,
}

/// 搜索结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    /// 文档信息
    pub document: DocumentBase,
    /// 匹配的片段
    pub snippets: Vec<String>,
    /// 相关度分数
    pub score: f64,
}

/// 文档统计信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DocumentStats {
    /// 浏览次数
    pub view_count: u64,
    /// 编辑次数
    pub edit_count: u64,
    /// 评论数量
    pub comment_count: u64,
    /// 分享次数
    pub share_count: u64,
    /// 下载次数
    pub download_count: u64,
    /// 最后统计时间
    pub last_stats_time: DateTime<Utc>,
}

/// 文档操作记录
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationRecord {
    /// 记录ID
    pub record_id: String,
    /// 操作类型
    pub operation_type: OperationType,
    /// 操作时间
    pub operation_time: DateTime<Utc>,
    /// 操作者
    pub operator: UserInfo,
    /// 操作描述
    pub description: Option<String>,
    /// IP地址
    pub ip_address: Option<String>,
    /// 设备信息
    pub device_info: Option<String>,
}

/// 操作类型
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum OperationType {
    /// 创建
    Create,
    /// 读取
    Read,
    /// 更新
    Update,
    /// 删除
    Delete,
    /// 分享
    Share,
    /// 下载
    Download,
    /// 复制
    Copy,
    /// 移动
    Move,
    /// 重命名
    Rename,
    /// 权限变更
    PermissionChange,
    /// 其他
    Other(String),
}

/// 文档导入/导出任务
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ImportExportTask {
    /// 任务ID
    pub task_id: String,
    /// 任务类型
    pub task_type: TaskType,
    /// 任务状态
    pub status: TaskStatus,
    /// 进度百分比 (0-100)
    pub progress: u8,
    /// 开始时间
    pub start_time: DateTime<Utc>,
    /// 结束时间
    pub end_time: Option<DateTime<Utc>>,
    /// 错误信息
    pub error_message: Option<String>,
    /// 结果URL
    pub result_url: Option<String>,
}

/// 任务类型
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum TaskType {
    /// 导入
    Import,
    /// 导出
    Export,
    /// 转换
    Convert,
}

/// 任务状态
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum TaskStatus {
    /// 等待中
    Pending,
    /// 处理中
    Processing,
    /// 已完成
    Completed,
    /// 已失败
    Failed,
    /// 已取消
    Canceled,
}

/// 通用响应结构
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CommonResponse<T: PartialEq> {
    /// 是否成功
    pub success: bool,
    /// 响应数据
    pub data: Option<T>,
    /// 错误信息
    pub error: Option<ErrorInfo>,
    /// 请求ID
    pub request_id: String,
}

/// 错误信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ErrorInfo {
    /// 错误代码
    pub code: i32,
    /// 错误消息
    pub message: String,
    /// 错误详情
    pub details: Option<HashMap<String, serde_json::Value>>,
}

/// 分页请求参数
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PageRequest {
    /// 页码(从1开始)
    pub page: u32,
    /// 每页大小
    pub page_size: u32,
    /// 排序字段
    pub sort_field: Option<String>,
    /// 排序方向
    pub sort_direction: Option<SortDirection>,
}

/// 排序方向
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum SortDirection {
    /// 升序
    Asc,
    /// 降序
    Desc,
}

/// 分页响应
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PageResponse<T: PartialEq> {
    /// 数据列表
    pub items: Vec<T>,
    /// 总条数
    pub total: u64,
    /// 当前页码
    pub page: u32,
    /// 每页大小
    pub page_size: u32,
    /// 总页数
    pub total_page: u32,
    /// 是否有下一页
    pub has_next: bool,
}

impl Default for PageRequest {
    fn default() -> Self {
        Self {
            page: 1,
            page_size: 20,
            sort_field: None,
            sort_direction: Some(SortDirection::Desc),
        }
    }
}

// Docs和Docx项目的特定模型
// 注意:这些模型的定义在各自的模块中,这里不重复导入

#[cfg(test)]
mod tests {
    use super::*;

    fn test_roundtrip<T: Serialize + for<'de> Deserialize<'de> + PartialEq + std::fmt::Debug>(
        original: &T,
    ) {
        let json = serde_json::to_string(original).expect("序列化失败");
        let deserialized: T = serde_json::from_str(&json).expect("反序列化失败");
        assert_eq!(original, &deserialized, "roundtrip 后数据不一致");
    }

    #[test]
    fn test_document_type_serialization() {
        test_roundtrip(&DocumentType::Doc);
        test_roundtrip(&DocumentType::Sheet);
        test_roundtrip(&DocumentType::Other("custom".to_string()));
    }

    #[test]
    fn test_document_status_serialization() {
        test_roundtrip(&DocumentStatus::Normal);
        test_roundtrip(&DocumentStatus::Archived);
    }

    #[test]
    fn test_permission_type_serialization() {
        test_roundtrip(&PermissionType::Owner);
        test_roundtrip(&PermissionType::Viewer);
    }

    #[test]
    fn test_operation_type_serialization() {
        test_roundtrip(&OperationType::Create);
        test_roundtrip(&OperationType::Other("custom_op".to_string()));
    }

    #[test]
    fn test_task_type_serialization() {
        test_roundtrip(&TaskType::Import);
        test_roundtrip(&TaskType::Export);
    }

    #[test]
    fn test_task_status_serialization() {
        test_roundtrip(&TaskStatus::Pending);
        test_roundtrip(&TaskStatus::Completed);
    }

    #[test]
    fn test_sort_direction_serialization() {
        test_roundtrip(&SortDirection::Asc);
        test_roundtrip(&SortDirection::Desc);
    }

    #[test]
    fn test_user_info_serialization() {
        let user = UserInfo {
            user_id: "user123".to_string(),
            name: "测试用户".to_string(),
            email: Some("test@example.com".to_string()),
            avatar_url: None,
            department: Some(DepartmentInfo {
                department_id: "dept456".to_string(),
                name: "技术部".to_string(),
            }),
        };
        test_roundtrip(&user);
    }

    #[test]
    fn test_department_info_serialization() {
        let dept = DepartmentInfo {
            department_id: "dept789".to_string(),
            name: "产品部".to_string(),
        };
        test_roundtrip(&dept);
    }

    #[test]
    fn test_file_info_serialization() {
        let file = FileInfo {
            file_id: "file123".to_string(),
            name: "test.pdf".to_string(),
            size: 1024000,
            mime_type: "application/pdf".to_string(),
            url: "https://example.com/file".to_string(),
            download_token: Some("token123".to_string()),
            thumbnail_url: None,
        };
        test_roundtrip(&file);
    }

    #[test]
    fn test_permission_serialization() {
        let perm = Permission {
            permission_type: PermissionType::Editor,
            can_read: true,
            can_write: true,
            can_delete: false,
            can_share: true,
            expire_time: None,
        };
        test_roundtrip(&perm);
    }

    #[test]
    fn test_share_info_serialization() {
        let share = ShareInfo {
            share_url: "https://share.example.com/doc".to_string(),
            share_token: "token456".to_string(),
            permission_type: PermissionType::Viewer,
            need_password: false,
            expire_time: None,
            visit_limit: Some(100),
            visit_count: 10,
        };
        test_roundtrip(&share);
    }

    #[test]
    fn test_version_info_serialization() {
        let version = VersionInfo {
            version: 1,
            version_name: "v1.0".to_string(),
            create_time: Utc::now(),
            creator: UserInfo {
                user_id: "user001".to_string(),
                name: "张三".to_string(),
                email: None,
                avatar_url: None,
                department: None,
            },
            description: Some("初始版本".to_string()),
            is_current: true,
            size: 2048,
        };
        test_roundtrip(&version);
    }

    #[test]
    fn test_document_stats_serialization() {
        let stats = DocumentStats {
            view_count: 100,
            edit_count: 20,
            comment_count: 5,
            share_count: 10,
            download_count: 3,
            last_stats_time: Utc::now(),
        };
        test_roundtrip(&stats);
    }

    #[test]
    fn test_import_export_task_serialization() {
        let task = ImportExportTask {
            task_id: "task789".to_string(),
            task_type: TaskType::Convert,
            status: TaskStatus::Processing,
            progress: 50,
            start_time: Utc::now(),
            end_time: None,
            error_message: None,
            result_url: Some("https://result.example.com".to_string()),
        };
        test_roundtrip(&task);
    }

    #[test]
    fn test_error_info_serialization() {
        let mut details = HashMap::new();
        details.insert("field".to_string(), serde_json::json!("value"));

        let error = ErrorInfo {
            code: 404,
            message: "Not Found".to_string(),
            details: Some(details),
        };
        test_roundtrip(&error);
    }

    #[test]
    fn test_page_request_serialization() {
        let req = PageRequest {
            page: 1,
            page_size: 20,
            sort_field: Some("create_time".to_string()),
            sort_direction: Some(SortDirection::Desc),
        };
        test_roundtrip(&req);
    }

    #[test]
    fn test_page_request_default() {
        let default_req = PageRequest::default();
        assert_eq!(default_req.page, 1);
        assert_eq!(default_req.page_size, 20);
    }

    #[test]
    fn test_common_response_serialization() {
        let response: CommonResponse<String> = CommonResponse {
            success: true,
            data: Some("test data".to_string()),
            error: None,
            request_id: "req123".to_string(),
        };
        test_roundtrip(&response);
    }

    #[test]
    fn test_page_response_serialization() {
        let response: PageResponse<String> = PageResponse {
            items: vec!["item1".to_string(), "item2".to_string()],
            total: 100,
            page: 1,
            page_size: 20,
            total_page: 5,
            has_next: true,
        };
        test_roundtrip(&response);
    }
}