open_lark/service/cloud_docs/permission/member/
create.rs1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3
4use crate::core::{
5 api_req::ApiRequest,
6 api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
7 config::Config,
8 constants::AccessTokenType,
9 http::Transport,
10 req_option::RequestOption,
11 SDKResult,
12};
13
14use super::batch_create::Permission;
15
16#[derive(Debug, Serialize, Default, Clone)]
18pub struct CreatePermissionMemberRequest {
19 #[serde(skip)]
20 api_request: ApiRequest,
21 #[serde(skip)]
23 token: String,
24 #[serde(skip)]
26 obj_type: String,
27 member_type: String,
29 member_id: String,
31 perm: Permission,
33 #[serde(skip_serializing_if = "Option::is_none")]
35 need_notification: Option<bool>,
36}
37
38impl CreatePermissionMemberRequest {
39 pub fn builder() -> CreatePermissionMemberRequestBuilder {
40 CreatePermissionMemberRequestBuilder::default()
41 }
42
43 pub fn new(
44 token: impl ToString,
45 obj_type: impl ToString,
46 member_type: impl ToString,
47 member_id: impl ToString,
48 permission: Permission,
49 ) -> Self {
50 Self {
51 token: token.to_string(),
52 obj_type: obj_type.to_string(),
53 member_type: member_type.to_string(),
54 member_id: member_id.to_string(),
55 perm: permission,
56 ..Default::default()
57 }
58 }
59
60 pub fn for_user(
62 token: impl ToString,
63 obj_type: impl ToString,
64 user_id: impl ToString,
65 permission: Permission,
66 ) -> Self {
67 Self::new(token, obj_type, "user", user_id, permission)
68 }
69
70 pub fn for_chat(
72 token: impl ToString,
73 obj_type: impl ToString,
74 chat_id: impl ToString,
75 permission: Permission,
76 ) -> Self {
77 Self::new(token, obj_type, "chat", chat_id, permission)
78 }
79
80 pub fn for_department(
82 token: impl ToString,
83 obj_type: impl ToString,
84 department_id: impl ToString,
85 permission: Permission,
86 ) -> Self {
87 Self::new(token, obj_type, "department", department_id, permission)
88 }
89}
90
91#[derive(Default)]
92pub struct CreatePermissionMemberRequestBuilder {
93 request: CreatePermissionMemberRequest,
94}
95
96impl CreatePermissionMemberRequestBuilder {
97 pub fn token(mut self, token: impl ToString) -> Self {
99 self.request.token = token.to_string();
100 self
101 }
102
103 pub fn obj_type(mut self, obj_type: impl ToString) -> Self {
105 self.request.obj_type = obj_type.to_string();
106 self
107 }
108
109 pub fn as_doc(mut self) -> Self {
111 self.request.obj_type = "doc".to_string();
112 self
113 }
114
115 pub fn as_sheet(mut self) -> Self {
117 self.request.obj_type = "sheet".to_string();
118 self
119 }
120
121 pub fn as_bitable(mut self) -> Self {
123 self.request.obj_type = "bitable".to_string();
124 self
125 }
126
127 pub fn as_wiki(mut self) -> Self {
129 self.request.obj_type = "wiki".to_string();
130 self
131 }
132
133 pub fn member(mut self, member_type: impl ToString, member_id: impl ToString) -> Self {
135 self.request.member_type = member_type.to_string();
136 self.request.member_id = member_id.to_string();
137 self
138 }
139
140 pub fn user(mut self, user_id: impl ToString) -> Self {
142 self.request.member_type = "user".to_string();
143 self.request.member_id = user_id.to_string();
144 self
145 }
146
147 pub fn chat(mut self, chat_id: impl ToString) -> Self {
149 self.request.member_type = "chat".to_string();
150 self.request.member_id = chat_id.to_string();
151 self
152 }
153
154 pub fn department(mut self, department_id: impl ToString) -> Self {
156 self.request.member_type = "department".to_string();
157 self.request.member_id = department_id.to_string();
158 self
159 }
160
161 pub fn permission(mut self, permission: Permission) -> Self {
163 self.request.perm = permission;
164 self
165 }
166
167 pub fn as_owner(mut self) -> Self {
169 self.request.perm = Permission::FullAccess;
170 self
171 }
172
173 pub fn as_editor(mut self) -> Self {
175 self.request.perm = Permission::Edit;
176 self
177 }
178
179 pub fn as_commenter(mut self) -> Self {
181 self.request.perm = Permission::Comment;
182 self
183 }
184
185 pub fn as_viewer(mut self) -> Self {
187 self.request.perm = Permission::View;
188 self
189 }
190
191 pub fn need_notification(mut self, need: bool) -> Self {
193 self.request.need_notification = Some(need);
194 self
195 }
196
197 pub fn with_notification(mut self) -> Self {
199 self.request.need_notification = Some(true);
200 self
201 }
202
203 pub fn without_notification(mut self) -> Self {
205 self.request.need_notification = Some(false);
206 self
207 }
208
209 pub fn build(mut self) -> CreatePermissionMemberRequest {
210 self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
211 self.request
212 }
213}
214
215crate::impl_executable_builder_owned!(
216 CreatePermissionMemberRequestBuilder,
217 crate::service::cloud_docs::permission::PermissionService,
218 CreatePermissionMemberRequest,
219 BaseResponse<CreatePermissionMemberResponse>,
220 create_member
221);
222
223#[derive(Debug, Deserialize)]
225pub struct PermissionMemberCreated {
226 pub member_type: String,
228 pub member_id: String,
230 pub perm: Permission,
232 pub create_time: Option<i64>,
234 pub notified: Option<bool>,
236}
237
238#[derive(Debug, Deserialize)]
240pub struct CreatePermissionMemberResponse {
241 pub member: PermissionMemberCreated,
243}
244
245impl ApiResponseTrait for CreatePermissionMemberResponse {
246 fn data_format() -> ResponseFormat {
247 ResponseFormat::Data
248 }
249}
250
251pub async fn create_permission_member(
253 request: CreatePermissionMemberRequest,
254 config: &Config,
255 option: Option<RequestOption>,
256) -> SDKResult<BaseResponse<CreatePermissionMemberResponse>> {
257 let mut api_req = request.api_request;
258 api_req.http_method = Method::POST;
259 api_req.api_path = format!(
260 "/open-apis/drive/v1/permissions/{}/members?type={}",
261 request.token, request.obj_type
262 );
263
264 if let Some(need_notification) = request.need_notification {
266 api_req.api_path = format!(
267 "{}&need_notification={}",
268 api_req.api_path, need_notification
269 );
270 }
271
272 api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
273
274 let api_resp = Transport::request(api_req, config, option).await?;
275 Ok(api_resp)
276}
277
278impl PermissionMemberCreated {
279 pub fn member_id(&self) -> &str {
281 &self.member_id
282 }
283
284 pub fn member_type(&self) -> &str {
286 &self.member_type
287 }
288
289 pub fn permission(&self) -> &Permission {
291 &self.perm
292 }
293
294 pub fn is_user(&self) -> bool {
296 self.member_type == "user"
297 }
298
299 pub fn is_chat(&self) -> bool {
301 self.member_type == "chat"
302 }
303
304 pub fn is_department(&self) -> bool {
306 self.member_type == "department"
307 }
308
309 pub fn can_edit(&self) -> bool {
311 self.perm.can_edit()
312 }
313
314 pub fn is_owner(&self) -> bool {
316 self.perm.is_owner()
317 }
318
319 pub fn was_notified(&self) -> bool {
321 self.notified.unwrap_or(false)
322 }
323
324 pub fn has_create_time(&self) -> bool {
326 self.create_time.is_some()
327 }
328
329 pub fn create_time_formatted(&self) -> Option<String> {
331 self.create_time
332 .map(|timestamp| format!("创建时间: {timestamp}"))
333 }
334
335 pub fn member_type_description(&self) -> String {
337 match self.member_type.as_str() {
338 "user" => "用户".to_string(),
339 "chat" => "群组".to_string(),
340 "department" => "部门".to_string(),
341 _ => "未知".to_string(),
342 }
343 }
344
345 pub fn permission_description(&self) -> String {
347 self.perm.description().to_string()
348 }
349
350 pub fn summary(&self) -> String {
352 let mut parts = vec![
353 format!("{} ({})", self.member_id, self.member_type_description()),
354 format!("权限: {}", self.permission_description()),
355 ];
356
357 if let Some(time) = self.create_time_formatted() {
358 parts.push(time);
359 }
360
361 if self.was_notified() {
362 parts.push("已通知".to_string());
363 }
364
365 parts.join(", ")
366 }
367}
368
369impl CreatePermissionMemberResponse {
370 pub fn member_id(&self) -> &str {
372 self.member.member_id()
373 }
374
375 pub fn member_type(&self) -> &str {
377 self.member.member_type()
378 }
379
380 pub fn permission(&self) -> &Permission {
382 self.member.permission()
383 }
384
385 pub fn is_created(&self) -> bool {
387 !self.member.member_id.is_empty()
388 }
389
390 pub fn success_summary(&self) -> String {
392 format!("协作者添加成功: {}", self.member.summary())
393 }
394
395 pub fn was_notified(&self) -> bool {
397 self.member.was_notified()
398 }
399
400 pub fn permission_level(&self) -> u8 {
402 self.member.perm.level()
403 }
404}
405
406#[cfg(test)]
407mod tests {
408 use super::*;
409
410 #[test]
411 fn test_create_permission_member_request_builder() {
412 let request = CreatePermissionMemberRequest::builder()
413 .token("doccnxxxxxx")
414 .as_doc()
415 .user("user123")
416 .as_editor()
417 .with_notification()
418 .build();
419
420 assert_eq!(request.token, "doccnxxxxxx");
421 assert_eq!(request.obj_type, "doc");
422 assert_eq!(request.member_type, "user");
423 assert_eq!(request.member_id, "user123");
424 assert!(matches!(request.perm, Permission::Edit));
425 assert_eq!(request.need_notification, Some(true));
426 }
427
428 #[test]
429 fn test_create_permission_member_convenience_methods() {
430 let request = CreatePermissionMemberRequest::for_user(
431 "doccnxxxxxx",
432 "doc",
433 "user123",
434 Permission::Edit,
435 );
436 assert_eq!(request.member_type, "user");
437 assert_eq!(request.member_id, "user123");
438
439 let request = CreatePermissionMemberRequest::for_chat(
440 "doccnxxxxxx",
441 "doc",
442 "chat456",
443 Permission::View,
444 );
445 assert_eq!(request.member_type, "chat");
446 assert_eq!(request.member_id, "chat456");
447
448 let request = CreatePermissionMemberRequest::for_department(
449 "doccnxxxxxx",
450 "doc",
451 "dept789",
452 Permission::Comment,
453 );
454 assert_eq!(request.member_type, "department");
455 assert_eq!(request.member_id, "dept789");
456 }
457
458 #[test]
459 fn test_permission_member_created_methods() {
460 let member = PermissionMemberCreated {
461 member_type: "user".to_string(),
462 member_id: "user123".to_string(),
463 perm: Permission::Edit,
464 create_time: Some(1234567890),
465 notified: Some(true),
466 };
467
468 assert!(member.is_user());
469 assert!(!member.is_chat());
470 assert!(!member.is_department());
471 assert!(member.can_edit());
472 assert!(!member.is_owner());
473 assert!(member.was_notified());
474 assert!(member.has_create_time());
475 assert_eq!(member.member_type_description(), "用户");
476 assert_eq!(member.permission_description(), "编辑者");
477 }
478}