1use std::path::Path;
4
5use serde_json::{json, Value};
6
7use super::{Body, ClickUpClient, Query};
8use crate::{
9 error::{ClickUpError, Result},
10 model::Checklist,
11 task_ref::TaskRef,
12};
13
14#[derive(Debug, Clone, Copy, PartialEq)]
16pub enum Dependency {
17 WaitingOn,
19 Blocks,
21}
22
23impl Dependency {
24 fn key(self) -> &'static str {
25 match self {
26 Self::WaitingOn => "depends_on",
27 Self::Blocks => "dependency_of",
28 }
29 }
30}
31
32fn enc(segment: &str) -> String {
33 url::form_urlencoded::byte_serialize(segment.as_bytes())
34 .collect::<String>()
35 .replace('+', "%20")
36}
37
38impl ClickUpClient {
39 pub async fn add_tag(&self, task: &TaskRef, tag: &str) -> Result<()> {
41 self.post::<Value>(
42 &format!("/v2/task/{}/tag/{}", task.path_id(), enc(tag)),
43 &self.task_query(task)?,
44 json!({}),
45 )
46 .await?;
47 Ok(())
48 }
49
50 pub async fn remove_tag(&self, task: &TaskRef, tag: &str) -> Result<()> {
52 self.delete(
53 &format!("/v2/task/{}/tag/{}", task.path_id(), enc(tag)),
54 &self.task_query(task)?,
55 )
56 .await?;
57 Ok(())
58 }
59
60 pub async fn add_link(&self, task: &TaskRef, other: &TaskRef) -> Result<()> {
62 let other_id = self.task_id(other).await?;
63 self.post::<Value>(
64 &format!("/v2/task/{}/link/{other_id}", task.path_id()),
65 &self.task_query(task)?,
66 json!({}),
67 )
68 .await?;
69 Ok(())
70 }
71
72 pub async fn remove_link(&self, task: &TaskRef, other: &TaskRef) -> Result<()> {
74 let other_id = self.task_id(other).await?;
75 self.delete(
76 &format!("/v2/task/{}/link/{other_id}", task.path_id()),
77 &self.task_query(task)?,
78 )
79 .await?;
80 Ok(())
81 }
82
83 pub async fn add_dependency(
85 &self,
86 task: &TaskRef,
87 other: &TaskRef,
88 kind: Dependency,
89 ) -> Result<()> {
90 let other_id = self.task_id(other).await?;
91 self.post::<Value>(
92 &format!("/v2/task/{}/dependency", task.path_id()),
93 &self.task_query(task)?,
94 json!({ kind.key(): other_id }),
95 )
96 .await?;
97 Ok(())
98 }
99
100 pub async fn remove_dependency(
102 &self,
103 task: &TaskRef,
104 other: &TaskRef,
105 kind: Dependency,
106 ) -> Result<()> {
107 let other_id = self.task_id(other).await?;
108 let q = self.task_query(task)?.push(kind.key(), other_id);
109 self.delete(&format!("/v2/task/{}/dependency", task.path_id()), &q)
110 .await?;
111 Ok(())
112 }
113
114 pub async fn create_checklist(
116 &self,
117 task: &TaskRef,
118 name: &str,
119 items: &[String],
120 ) -> Result<Checklist> {
121 let v: Value = self
122 .post(
123 &format!("/v2/task/{}/checklist", task.path_id()),
124 &self.task_query(task)?,
125 json!({ "name": name }),
126 )
127 .await?;
128 let mut checklist: Checklist =
129 serde_json::from_value(v.get("checklist").cloned().unwrap_or(v))?;
130 for item in items {
131 checklist = self.add_checklist_item(&checklist.id, item).await?;
132 }
133 Ok(checklist)
134 }
135
136 pub async fn add_checklist_item(&self, checklist_id: &str, name: &str) -> Result<Checklist> {
138 let v: Value = self
139 .post(
140 &format!("/v2/checklist/{checklist_id}/checklist_item"),
141 &Query::new(),
142 json!({ "name": name }),
143 )
144 .await?;
145 Ok(serde_json::from_value(
146 v.get("checklist").cloned().unwrap_or(v),
147 )?)
148 }
149
150 pub async fn update_checklist_item(
152 &self,
153 checklist_id: &str,
154 item_id: &str,
155 resolved: Option<bool>,
156 name: Option<&str>,
157 ) -> Result<Value> {
158 let mut body = json!({});
159 if let Some(r) = resolved {
160 body["resolved"] = json!(r);
161 }
162 if let Some(n) = name {
163 body["name"] = json!(n);
164 }
165 self.put(
166 &format!("/v2/checklist/{checklist_id}/checklist_item/{item_id}"),
167 &Query::new(),
168 body,
169 )
170 .await
171 }
172
173 pub async fn upload_attachment(&self, task: &TaskRef, path: &Path) -> Result<Value> {
175 let bytes = tokio::fs::read(path).await?;
176 let name = path
177 .file_name()
178 .and_then(|n| n.to_str())
179 .ok_or_else(|| ClickUpError::Invalid(format!("bad file name: {}", path.display())))?
180 .to_string();
181 let mime = mime_guess::from_path(path)
182 .first_or_octet_stream()
183 .to_string();
184 self.call(
185 reqwest::Method::POST,
186 &format!("/v2/task/{}/attachment", task.path_id()),
187 &self.task_query(task)?,
188 Some(Body::File {
189 field: "attachment",
190 name,
191 bytes,
192 mime,
193 }),
194 )
195 .await
196 }
197}
198
199#[cfg(test)]
200mod tests {
201 #[test]
202 fn encodes_tag_names() {
203 assert_eq!(super::enc("needs review"), "needs%20review");
204 assert_eq!(super::enc("a/b"), "a%2Fb");
205 }
206}