Skip to main content

wekan_cli/client/attachments/
recovery.rs

1use crate::client::{
2    ClientError, WekanClient, datetime::validate_response_date, transport::decode_success,
3};
4use reqwest::header::ACCEPT;
5use secrecy::{ExposeSecret, SecretString};
6use serde::{Deserialize, Serialize};
7
8#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
9#[serde(
10    deny_unknown_fields,
11    rename_all(deserialize = "camelCase", serialize = "snake_case")
12)]
13pub struct DeletedAttachment {
14    pub attachment_id: String,
15    pub attachment_name: String,
16    pub attachment_type: String,
17    pub board_id: String,
18    pub swimlane_id: Option<String>,
19    pub list_id: Option<String>,
20    pub card_id: Option<String>,
21    pub deleted_at: String,
22    pub deleted_by: Option<String>,
23    pub delete_batch_id: Option<String>,
24}
25#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
26#[serde(
27    deny_unknown_fields,
28    rename_all(deserialize = "camelCase", serialize = "snake_case")
29)]
30pub struct SoftDeleteAttachmentResponse {
31    pub deleted: bool,
32    pub batch_id: Option<String>,
33}
34#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
35#[serde(deny_unknown_fields)]
36pub struct RestoreAttachmentResponse {
37    pub restored: bool,
38}
39
40impl WekanClient {
41    pub async fn deleted_board_attachments(
42        &self,
43        board: &str,
44        token: &SecretString,
45    ) -> Result<Vec<DeletedAttachment>, ClientError> {
46        let mut endpoint = self.board_endpoint(board, Some("attachments"))?;
47        endpoint
48            .path_segments_mut()
49            .expect("HTTP URL supports path segments")
50            .push("deleted");
51        let body = self
52            .execute_success_request(
53                self.http
54                    .get(endpoint)
55                    .header(ACCEPT, "application/json")
56                    .bearer_auth(token.expose_secret()),
57            )
58            .await?;
59        let attachments: Vec<DeletedAttachment> =
60            decode_success(&body, "deleted board attachments", true)?;
61        for attachment in &attachments {
62            if attachment.board_id != board || attachment.attachment_id.trim().is_empty() {
63                return Err(super::protocol(
64                    "the deleted attachment did not match the requested board",
65                ));
66            }
67            validate_response_date(&attachment.deleted_at)
68                .map_err(|()| super::protocol("the deleted attachment timestamp is invalid"))?;
69        }
70        Ok(attachments)
71    }
72    pub async fn soft_delete_board_attachment(
73        &self,
74        board: &str,
75        attachment: &str,
76        token: &SecretString,
77    ) -> Result<SoftDeleteAttachmentResponse, ClientError> {
78        let mut endpoint = self.board_endpoint(board, Some("attachments"))?;
79        endpoint
80            .path_segments_mut()
81            .expect("HTTP URL supports path segments")
82            .push(attachment);
83        let body = self
84            .execute_success_request(
85                self.http
86                    .delete(endpoint)
87                    .header(ACCEPT, "application/json")
88                    .bearer_auth(token.expose_secret()),
89            )
90            .await?;
91        decode_success(&body, "board attachment deletion", true)
92    }
93    pub async fn restore_board_attachment(
94        &self,
95        board: &str,
96        attachment: &str,
97        token: &SecretString,
98    ) -> Result<RestoreAttachmentResponse, ClientError> {
99        let mut endpoint = self.board_endpoint(board, Some("attachments"))?;
100        endpoint
101            .path_segments_mut()
102            .expect("HTTP URL supports path segments")
103            .extend([attachment, "restore"]);
104        let body = self
105            .execute_success_request(
106                self.http
107                    .post(endpoint)
108                    .header(ACCEPT, "application/json")
109                    .bearer_auth(token.expose_secret()),
110            )
111            .await?;
112        decode_success(&body, "board attachment restore", true)
113    }
114}