open-lark 0.14.0

Enterprise-grade Lark/Feishu Open API SDK with comprehensive Chinese documentation and advanced error handling
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
use reqwest::Method;
use serde::{Deserialize, Serialize};

use crate::core::{
    api_req::ApiRequest,
    api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
    config::Config,
    constants::AccessTokenType,
    endpoints::{cloud_docs::*, EndpointBuilder},
    http::Transport,
    query_params::QueryParams,
    req_option::RequestOption,
    SDKResult,
};

/// 获取云文档权限设置请求 (v2)
#[derive(Debug, Serialize, Default, Clone)]
pub struct GetPermissionPublicV2Request {
    #[serde(skip)]
    api_request: ApiRequest,
    /// 文档token
    #[serde(skip)]
    token: String,
    /// 文档类型
    #[serde(skip)]
    obj_type: String,
}

impl GetPermissionPublicV2Request {
    pub fn builder() -> GetPermissionPublicV2RequestBuilder {
        GetPermissionPublicV2RequestBuilder::default()
    }

    pub fn new(token: impl ToString, obj_type: impl ToString) -> Self {
        Self {
            token: token.to_string(),
            obj_type: obj_type.to_string(),
            ..Default::default()
        }
    }

    /// 获取文档权限设置
    pub fn for_doc(token: impl ToString) -> Self {
        Self::new(token, "doc")
    }

    /// 获取电子表格权限设置
    pub fn for_sheet(token: impl ToString) -> Self {
        Self::new(token, "sheet")
    }

    /// 获取多维表格权限设置
    pub fn for_bitable(token: impl ToString) -> Self {
        Self::new(token, "bitable")
    }

    /// 获取知识库权限设置
    pub fn for_wiki(token: impl ToString) -> Self {
        Self::new(token, "wiki")
    }
}

#[derive(Default)]
pub struct GetPermissionPublicV2RequestBuilder {
    request: GetPermissionPublicV2Request,
}

impl GetPermissionPublicV2RequestBuilder {
    /// 文档token
    pub fn token(mut self, token: impl ToString) -> Self {
        self.request.token = token.to_string();
        self
    }

    /// 文档类型
    pub fn obj_type(mut self, obj_type: impl ToString) -> Self {
        self.request.obj_type = obj_type.to_string();
        self
    }

    /// 设置为文档类型
    pub fn as_doc(mut self) -> Self {
        self.request.obj_type = "doc".to_string();
        self
    }

    /// 设置为电子表格类型
    pub fn as_sheet(mut self) -> Self {
        self.request.obj_type = "sheet".to_string();
        self
    }

    /// 设置为多维表格类型
    pub fn as_bitable(mut self) -> Self {
        self.request.obj_type = "bitable".to_string();
        self
    }

    /// 设置为知识库类型
    pub fn as_wiki(mut self) -> Self {
        self.request.obj_type = "wiki".to_string();
        self
    }

    pub fn build(mut self) -> GetPermissionPublicV2Request {
        self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
        self.request
    }
}

crate::impl_executable_builder_owned!(
    GetPermissionPublicV2RequestBuilder,
    crate::service::cloud_docs::permission::PermissionService,
    GetPermissionPublicV2Request,
    BaseResponse<GetPermissionPublicV2Response>,
    get_permission_public_v2
);

/// 公开访问设置 (v2)
#[derive(Debug, Deserialize)]
pub struct PublicSettingsV2 {
    /// 链接分享设置
    pub link_share_setting: String,
    /// 密码保护开关
    pub password_switch: bool,
    /// 是否允许复制
    pub allow_copy: bool,
    /// 是否允许评论
    pub allow_comment: bool,
    /// 是否允许保存副本
    pub allow_save_copy: bool,
    /// 访问权限
    pub access_setting: Option<String>,
    /// 水印设置
    pub watermark_setting: Option<String>,
    /// 是否允许分享到组织外
    pub allow_share_partner_tenant: Option<bool>,
    /// 外部分享设置
    pub external_access_entity: Option<serde_json::Value>,
    /// 评论权限设置
    pub comment_entity: Option<serde_json::Value>,
    /// 分享范围设置
    pub share_scope: Option<String>,
    /// 到期时间
    pub expire_time: Option<i64>,
}

/// 获取云文档权限设置响应 (v2)
#[derive(Debug, Deserialize)]
pub struct GetPermissionPublicV2Response {
    /// 公开访问设置
    pub permission_public: PublicSettingsV2,
}

impl ApiResponseTrait for GetPermissionPublicV2Response {
    fn data_format() -> ResponseFormat {
        ResponseFormat::Data
    }
}

/// 获取云文档权限设置 (v2)
pub async fn get_permission_public_v2(
    request: GetPermissionPublicV2Request,
    config: &Config,
    option: Option<RequestOption>,
) -> SDKResult<BaseResponse<GetPermissionPublicV2Response>> {
    let mut api_req = request.api_request;
    api_req.http_method = Method::GET;
    api_req.api_path =
        EndpointBuilder::replace_param(DRIVE_V2_PERMISSIONS_PUBLIC, "token", &request.token);

    // 添加查询参数
    api_req
        .query_params
        .insert(QueryParams::TYPE, request.obj_type);

    api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];

    let api_resp = Transport::request(api_req, config, option).await?;
    Ok(api_resp)
}

impl PublicSettingsV2 {
    /// 是否开启了链接分享
    pub fn is_link_share_enabled(&self) -> bool {
        self.link_share_setting == "tenant_readable"
            || self.link_share_setting == "tenant_editable"
            || self.link_share_setting == "anyone_readable"
            || self.link_share_setting == "anyone_editable"
    }

    /// 是否允许组织内访问
    pub fn is_tenant_accessible(&self) -> bool {
        self.link_share_setting == "tenant_readable" || self.link_share_setting == "tenant_editable"
    }

    /// 是否允许任何人访问
    pub fn is_anyone_accessible(&self) -> bool {
        self.link_share_setting == "anyone_readable" || self.link_share_setting == "anyone_editable"
    }

    /// 是否允许编辑
    pub fn is_editable(&self) -> bool {
        self.link_share_setting == "tenant_editable" || self.link_share_setting == "anyone_editable"
    }

    /// 是否仅可读
    pub fn is_readonly(&self) -> bool {
        self.link_share_setting == "tenant_readable" || self.link_share_setting == "anyone_readable"
    }

    /// 是否开启密码保护
    pub fn has_password_protection(&self) -> bool {
        self.password_switch
    }

    /// 是否允许分享到组织外
    pub fn allows_external_share(&self) -> bool {
        self.allow_share_partner_tenant.unwrap_or(false)
    }

    /// 是否有过期时间
    pub fn has_expire_time(&self) -> bool {
        self.expire_time.is_some()
    }

    /// 是否已过期
    pub fn is_expired(&self) -> bool {
        if let Some(expire_time) = self.expire_time {
            let now = chrono::Utc::now().timestamp();
            now > expire_time
        } else {
            false
        }
    }

    /// 获取分享级别描述
    pub fn share_level_description(&self) -> &'static str {
        match self.link_share_setting.as_str() {
            "closed" => "关闭分享",
            "tenant_readable" => "组织内可读",
            "tenant_editable" => "组织内可编辑",
            "anyone_readable" => "任何人可读",
            "anyone_editable" => "任何人可编辑",
            _ => "未知设置",
        }
    }

    /// 获取权限摘要
    pub fn permissions_summary(&self) -> String {
        let mut features = Vec::new();

        if self.allow_copy {
            features.push("允许复制");
        }
        if self.allow_comment {
            features.push("允许评论");
        }
        if self.allow_save_copy {
            features.push("允许保存副本");
        }
        if self.password_switch {
            features.push("密码保护");
        }
        if self.allows_external_share() {
            features.push("组织外分享");
        }

        if features.is_empty() {
            "基础权限".to_string()
        } else {
            features.join(", ")
        }
    }

    /// 获取安全级别
    pub fn security_level(&self) -> &'static str {
        if self.link_share_setting == "closed" {
            "最安全"
        } else if self.password_switch {
            "较安全"
        } else if self.is_tenant_accessible() && !self.allows_external_share() {
            "中等安全"
        } else if self.is_anyone_accessible() || self.allows_external_share() {
            "较低安全"
        } else {
            "未知"
        }
    }

    /// 获取分享范围描述
    pub fn share_scope_description(&self) -> Option<&str> {
        self.share_scope.as_deref()
    }

    /// 获取过期时间格式化字符串
    pub fn expire_time_formatted(&self) -> Option<String> {
        self.expire_time.map(|timestamp| {
            let datetime =
                chrono::DateTime::from_timestamp(timestamp, 0).unwrap_or_else(chrono::Utc::now);
            datetime.format("%Y-%m-%d %H:%M:%S").to_string()
        })
    }

    /// 获取剩余有效时间(秒)
    pub fn remaining_valid_time(&self) -> Option<i64> {
        if let Some(expire_time) = self.expire_time {
            let now = chrono::Utc::now().timestamp();
            if expire_time > now {
                Some(expire_time - now)
            } else {
                Some(0) // 已过期
            }
        } else {
            None // 永久有效
        }
    }

    /// 获取高级功能状态
    pub fn advanced_features_status(&self) -> Vec<String> {
        let mut features = Vec::new();

        if let Some(access_setting) = &self.access_setting {
            features.push(format!("访问设置: {access_setting}"));
        }

        if let Some(watermark) = &self.watermark_setting {
            features.push(format!("水印: {watermark}"));
        }

        if self.comment_entity.is_some() {
            features.push("自定义评论权限".to_string());
        }

        if self.external_access_entity.is_some() {
            features.push("外部访问配置".to_string());
        }

        features
    }
}

impl GetPermissionPublicV2Response {
    /// 获取公开设置
    pub fn public_settings(&self) -> &PublicSettingsV2 {
        &self.permission_public
    }

    /// 是否允许外部访问
    pub fn allows_external_access(&self) -> bool {
        self.permission_public.is_link_share_enabled()
    }

    /// 获取设置摘要
    pub fn settings_summary(&self) -> String {
        format!(
            "{} - {} (安全级别: {})",
            self.permission_public.share_level_description(),
            self.permission_public.permissions_summary(),
            self.permission_public.security_level()
        )
    }

    /// 是否有高级配置
    pub fn has_advanced_config(&self) -> bool {
        self.permission_public.external_access_entity.is_some()
            || self.permission_public.comment_entity.is_some()
            || self.permission_public.share_scope.is_some()
    }

    /// 安全性建议
    pub fn security_recommendations(&self) -> Vec<String> {
        let mut recommendations = Vec::new();

        if !self.permission_public.password_switch && self.permission_public.is_anyone_accessible()
        {
            recommendations.push("建议开启密码保护以提高安全性".to_string());
        }

        if self.permission_public.allow_copy && self.permission_public.is_anyone_accessible() {
            recommendations.push("建议限制复制权限以防止内容泄露".to_string());
        }

        if self.permission_public.is_editable() && self.permission_public.is_anyone_accessible() {
            recommendations.push("建议将编辑权限限制在组织内".to_string());
        }

        if self.permission_public.allows_external_share() {
            recommendations.push("开启了组织外分享,请确保内容安全".to_string());
        }

        if self.permission_public.is_expired() {
            recommendations.push("文档分享已过期,需要重新设置".to_string());
        } else if let Some(remaining) = self.permission_public.remaining_valid_time() {
            if remaining < 86400 {
                // 少于24小时
                recommendations.push("文档分享即将过期,请注意及时续期".to_string());
            }
        }

        if recommendations.is_empty() {
            recommendations.push("当前权限设置合理".to_string());
        }

        recommendations
    }

    /// 获取高级功能报告
    pub fn advanced_features_report(&self) -> String {
        let features = self.permission_public.advanced_features_status();
        if features.is_empty() {
            "未启用高级功能".to_string()
        } else {
            format!("高级功能: {}", features.join(", "))
        }
    }

    /// 过期状态检查
    pub fn expiration_status(&self) -> String {
        if let Some(expire_time) = self.permission_public.expire_time_formatted() {
            if self.permission_public.is_expired() {
                format!("已过期: {expire_time}")
            } else if let Some(remaining) = self.permission_public.remaining_valid_time() {
                let days = remaining / 86400;
                let hours = (remaining % 86400) / 3600;
                if days > 0 {
                    format!("剩余: {days}{hours}小时 (过期时间: {expire_time})")
                } else {
                    format!("剩余: {hours}小时 (过期时间: {expire_time})")
                }
            } else {
                format!("过期时间: {expire_time}")
            }
        } else {
            "永久有效".to_string()
        }
    }
}

#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
    use super::*;

    #[test]
    fn test_get_permission_public_v2_request_builder() {
        let request = GetPermissionPublicV2Request::builder()
            .token("doccnxxxxxx")
            .as_doc()
            .build();

        assert_eq!(request.token, "doccnxxxxxx");
        assert_eq!(request.obj_type, "doc");
    }

    #[test]
    fn test_convenience_methods() {
        let request = GetPermissionPublicV2Request::for_doc("doccnxxxxxx");
        assert_eq!(request.obj_type, "doc");

        let request = GetPermissionPublicV2Request::for_sheet("shtcnxxxxxx");
        assert_eq!(request.obj_type, "sheet");

        let request = GetPermissionPublicV2Request::for_bitable("bblcnxxxxxx");
        assert_eq!(request.obj_type, "bitable");

        let request = GetPermissionPublicV2Request::for_wiki("wikicnxxxxxx");
        assert_eq!(request.obj_type, "wiki");
    }

    #[test]
    fn test_public_settings_v2_methods() {
        let settings = PublicSettingsV2 {
            link_share_setting: "tenant_editable".to_string(),
            password_switch: true,
            allow_copy: true,
            allow_comment: true,
            allow_save_copy: false,
            access_setting: Some("advanced".to_string()),
            watermark_setting: Some("visible".to_string()),
            allow_share_partner_tenant: Some(true),
            external_access_entity: Some(serde_json::json!({})),
            comment_entity: None,
            share_scope: Some("limited".to_string()),
            expire_time: Some(chrono::Utc::now().timestamp() + 86400), // 24小时后过期
        };

        assert!(settings.is_link_share_enabled());
        assert!(settings.is_tenant_accessible());
        assert!(!settings.is_anyone_accessible());
        assert!(settings.is_editable());
        assert!(!settings.is_readonly());
        assert!(settings.has_password_protection());
        assert!(settings.allows_external_share());
        assert!(settings.has_expire_time());
        assert!(!settings.is_expired());
        assert_eq!(settings.share_level_description(), "组织内可编辑");
        assert_eq!(settings.security_level(), "较安全");
        assert_eq!(settings.share_scope_description(), Some("limited"));

        let features = settings.advanced_features_status();
        assert!(!features.is_empty());
        assert!(features.iter().any(|f| f.contains("访问设置")));
        assert!(features.iter().any(|f| f.contains("水印")));
    }

    #[test]
    fn test_expiration_logic() {
        let expired_settings = PublicSettingsV2 {
            link_share_setting: "anyone_readable".to_string(),
            password_switch: false,
            allow_copy: false,
            allow_comment: false,
            allow_save_copy: false,
            access_setting: None,
            watermark_setting: None,
            allow_share_partner_tenant: None,
            external_access_entity: None,
            comment_entity: None,
            share_scope: None,
            expire_time: Some(chrono::Utc::now().timestamp() - 3600), // 1小时前过期
        };

        assert!(expired_settings.is_expired());
        assert_eq!(expired_settings.remaining_valid_time(), Some(0));

        let permanent_settings = PublicSettingsV2 {
            link_share_setting: "tenant_readable".to_string(),
            password_switch: false,
            allow_copy: false,
            allow_comment: false,
            allow_save_copy: false,
            access_setting: None,
            watermark_setting: None,
            allow_share_partner_tenant: None,
            external_access_entity: None,
            comment_entity: None,
            share_scope: None,
            expire_time: None,
        };

        assert!(!permanent_settings.is_expired());
        assert_eq!(permanent_settings.remaining_valid_time(), None);
    }
}