fishpi_sdk/client/
comment.rs1use crate::client::core::FishPi;
2use crate::domain::comment::CommentPost;
3use crate::ids::CommentId;
4use crate::utils::error::Error;
5use crate::wire::comment::{WireCommentRemove, WireCommentUpdate, WireCommentVote};
6use serde_json::json;
7
8pub struct CommentApi(pub(crate) FishPi);
10
11impl FishPi {
12 pub fn comment(&self) -> CommentApi {
14 CommentApi(self.clone())
15 }
16}
17
18impl CommentApi {
19 pub async fn post(&self, data: CommentPost) -> Result<(), Error> {
20 self.0.post_raw("comment", &data).await?;
21 Ok(())
22 }
23
24 pub async fn update(
25 &self,
26 id: impl Into<CommentId>,
27 data: CommentPost,
28 ) -> Result<String, Error> {
29 let id = id.into();
30 let value = self.0.put_raw(&format!("comment/{}", id), &data).await?;
31 let wire: WireCommentUpdate =
32 serde_json::from_value(value).map_err(|e| Error::decode(e.to_string()))?;
33 Ok(wire.comment_content)
34 }
35
36 pub async fn vote(&self, id: impl Into<CommentId>, like: bool) -> Result<bool, Error> {
37 let id = id.into();
38 let action = if like { "up" } else { "down" };
39 let value = self
40 .0
41 .post_raw(
42 &format!("vote/{}/comment", action),
43 &json!({ "dataId": id.as_str() }),
44 )
45 .await?;
46 let wire: WireCommentVote =
47 serde_json::from_value(value).map_err(|e| Error::decode(e.to_string()))?;
48 let previous_vote = wire.vote_type.unwrap_or(-1);
49 Ok(like && previous_vote != 0)
50 }
51
52 pub async fn thank(&self, id: impl Into<CommentId>) -> Result<(), Error> {
53 let id = id.into();
54 self.0
55 .post_raw("comment/thank", &json!({ "commentId": id.as_str() }))
56 .await?;
57 Ok(())
58 }
59
60 pub async fn remove(&self, id: impl Into<CommentId>) -> Result<String, Error> {
61 let id = id.into();
62 let value = self
63 .0
64 .post_raw(&format!("comment/{}/remove", id), &json!({}))
65 .await?;
66 let wire: WireCommentRemove =
67 serde_json::from_value(value).map_err(|e| Error::decode(e.to_string()))?;
68 Ok(wire.comment_id)
69 }
70}