Skip to main content

clickupc_core/client/
comment.rs

1//! Task comments (cursor pagination), threaded replies.
2
3use serde::Deserialize;
4use serde_json::{json, Value};
5
6use super::{ClickUpClient, Query};
7use crate::{error::Result, model::Comment, task_ref::TaskRef};
8
9pub const COMMENT_PAGE_SIZE: usize = 25;
10
11#[derive(Deserialize)]
12struct CommentsResp {
13    #[serde(default)]
14    comments: Vec<Comment>,
15}
16
17impl ClickUpClient {
18    /// `GET /v2/task/{id}/comment` — newest first, 25 per page. Pass the
19    /// previous page's last comment as `before` to get the next page.
20    pub async fn task_comments_page(
21        &self,
22        task: &TaskRef,
23        before: Option<&Comment>,
24    ) -> Result<Vec<Comment>> {
25        let mut q = self.task_query(task)?;
26        if let Some(c) = before {
27            q = q.push_opt("start", c.date).push("start_id", &c.id);
28        }
29        Ok(self
30            .get::<CommentsResp>(&format!("/v2/task/{}/comment", task.path_id()), &q)
31            .await?
32            .comments)
33    }
34
35    /// Pages through comments until `limit` (or all when `None`).
36    pub async fn task_comments(
37        &self,
38        task: &TaskRef,
39        limit: Option<usize>,
40    ) -> Result<Vec<Comment>> {
41        let mut out: Vec<Comment> = Vec::new();
42        loop {
43            let page = self.task_comments_page(task, out.last()).await?;
44            let n = page.len();
45            out.extend(page);
46            if limit.is_some_and(|l| out.len() >= l) {
47                out.truncate(limit.unwrap_or(usize::MAX));
48                break;
49            }
50            if n < COMMENT_PAGE_SIZE {
51                break;
52            }
53        }
54        Ok(out)
55    }
56
57    /// `POST /v2/task/{id}/comment`
58    pub async fn add_comment(
59        &self,
60        task: &TaskRef,
61        text: &str,
62        assignee: Option<i64>,
63        notify_all: bool,
64    ) -> Result<Value> {
65        let mut body = json!({ "comment_text": text, "notify_all": notify_all });
66        if let Some(a) = assignee {
67            body["assignee"] = json!(a);
68        }
69        self.post(
70            &format!("/v2/task/{}/comment", task.path_id()),
71            &self.task_query(task)?,
72            body,
73        )
74        .await
75    }
76
77    /// `PUT /v2/comment/{id}`
78    pub async fn update_comment(
79        &self,
80        comment_id: &str,
81        text: &str,
82        resolved: Option<bool>,
83    ) -> Result<()> {
84        let mut body = json!({ "comment_text": text });
85        if let Some(r) = resolved {
86            body["resolved"] = json!(r);
87        }
88        self.put::<Value>(&format!("/v2/comment/{comment_id}"), &Query::new(), body)
89            .await?;
90        Ok(())
91    }
92
93    /// `DELETE /v2/comment/{id}`
94    pub async fn delete_comment(&self, comment_id: &str) -> Result<()> {
95        self.delete(&format!("/v2/comment/{comment_id}"), &Query::new())
96            .await?;
97        Ok(())
98    }
99
100    /// `GET /v2/comment/{id}/reply`
101    pub async fn comment_replies(&self, comment_id: &str) -> Result<Vec<Comment>> {
102        Ok(self
103            .get::<CommentsResp>(&format!("/v2/comment/{comment_id}/reply"), &Query::new())
104            .await?
105            .comments)
106    }
107
108    /// `POST /v2/comment/{id}/reply`
109    pub async fn reply_comment(
110        &self,
111        comment_id: &str,
112        text: &str,
113        notify_all: bool,
114    ) -> Result<Value> {
115        self.post(
116            &format!("/v2/comment/{comment_id}/reply"),
117            &Query::new(),
118            json!({ "comment_text": text, "notify_all": notify_all }),
119        )
120        .await
121    }
122}