files_sdk/files/
file_comments.rs

1//! File comment operations
2//!
3//! This module provides operations for commenting on files:
4//! - List comments on a file
5//! - Create new comments
6//! - Update existing comments
7//! - Delete comments
8//! - React to comments
9
10use crate::{FilesClient, Result};
11use serde::{Deserialize, Serialize};
12use serde_json::json;
13
14/// Represents a comment on a file
15#[derive(Debug, Serialize, Deserialize, Clone)]
16pub struct FileCommentEntity {
17    /// File comment ID
18    pub id: Option<i64>,
19
20    /// Comment body text
21    pub body: Option<String>,
22
23    /// Reactions to this comment
24    pub reactions: Option<Vec<FileCommentReactionEntity>>,
25}
26
27/// Represents a reaction to a file comment
28#[derive(Debug, Serialize, Deserialize, Clone)]
29pub struct FileCommentReactionEntity {
30    /// Reaction ID
31    pub id: Option<i64>,
32
33    /// Emoji used for the reaction
34    pub emoji: Option<String>,
35}
36
37/// Handler for file comment operations
38#[derive(Debug, Clone)]
39pub struct FileCommentHandler {
40    client: FilesClient,
41}
42
43impl FileCommentHandler {
44    /// Creates a new FileCommentHandler
45    pub fn new(client: FilesClient) -> Self {
46        Self { client }
47    }
48
49    /// List comments for a file
50    ///
51    /// # Arguments
52    ///
53    /// * `path` - Path to the file
54    ///
55    /// # Examples
56    ///
57    /// ```rust,no_run
58    /// # use files_sdk::{FilesClient, FileCommentHandler};
59    /// # #[tokio::main]
60    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
61    /// let client = FilesClient::builder().api_key("key").build()?;
62    /// let handler = FileCommentHandler::new(client);
63    ///
64    /// let comments = handler.list("/path/to/file.txt").await?;
65    /// for comment in comments {
66    ///     println!("{}", comment.body.unwrap_or_default());
67    /// }
68    /// # Ok(())
69    /// # }
70    /// ```
71    pub async fn list(&self, path: &str) -> Result<Vec<FileCommentEntity>> {
72        let endpoint = format!("/file_comments/files{}", path);
73        let response = self.client.get_raw(&endpoint).await?;
74        Ok(serde_json::from_value(response)?)
75    }
76
77    /// Create a new file comment
78    ///
79    /// # Arguments
80    ///
81    /// * `path` - Path to the file
82    /// * `body` - Comment text
83    pub async fn create(&self, path: &str, body: &str) -> Result<FileCommentEntity> {
84        let body_json = json!({
85            "body": body,
86            "path": path,
87        });
88
89        let response = self.client.post_raw("/file_comments", body_json).await?;
90        Ok(serde_json::from_value(response)?)
91    }
92
93    /// Update a file comment
94    ///
95    /// # Arguments
96    ///
97    /// * `id` - Comment ID
98    /// * `body` - New comment text
99    pub async fn update(&self, id: i64, body: &str) -> Result<FileCommentEntity> {
100        let body_json = json!({
101            "body": body,
102        });
103
104        let endpoint = format!("/file_comments/{}", id);
105        let response = self.client.patch_raw(&endpoint, body_json).await?;
106        Ok(serde_json::from_value(response)?)
107    }
108
109    /// Delete a file comment
110    ///
111    /// # Arguments
112    ///
113    /// * `id` - Comment ID
114    pub async fn delete(&self, id: i64) -> Result<()> {
115        let endpoint = format!("/file_comments/{}", id);
116        self.client.delete_raw(&endpoint).await?;
117        Ok(())
118    }
119
120    /// Add a reaction to a file comment
121    ///
122    /// # Arguments
123    ///
124    /// * `file_comment_id` - ID of the comment to react to
125    /// * `emoji` - Emoji for the reaction
126    pub async fn add_reaction(
127        &self,
128        file_comment_id: i64,
129        emoji: &str,
130    ) -> Result<FileCommentReactionEntity> {
131        let body = json!({
132            "file_comment_id": file_comment_id,
133            "emoji": emoji,
134        });
135
136        let response = self
137            .client
138            .post_raw("/file_comment_reactions", body)
139            .await?;
140        Ok(serde_json::from_value(response)?)
141    }
142
143    /// Delete a reaction from a file comment
144    ///
145    /// # Arguments
146    ///
147    /// * `id` - Reaction ID
148    pub async fn delete_reaction(&self, id: i64) -> Result<()> {
149        let endpoint = format!("/file_comment_reactions/{}", id);
150        self.client.delete_raw(&endpoint).await?;
151        Ok(())
152    }
153}
154
155#[cfg(test)]
156mod tests {
157    use super::*;
158
159    #[test]
160    fn test_handler_creation() {
161        let client = FilesClient::builder().api_key("test-key").build().unwrap();
162        let _handler = FileCommentHandler::new(client);
163    }
164}