1use chrono::{DateTime, Utc};
4pub use kival_types::CommentStatus;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7use uuid::Uuid;
8
9use crate::ListResponse;
10
11pub const COMMENT_BODY_MAX_CHARS: usize = 20_000;
13
14pub const COMMENT_MENTION_MAX_USERS: usize = 50;
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
19pub struct CommentAuthor {
20 pub id: Uuid,
22 pub username: String,
24 pub display_name: String,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
30pub struct CommentMention {
31 pub user_id: Uuid,
33 pub username: String,
35 pub display_name: String,
37}
38
39#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
41pub struct Comment {
42 pub id: Uuid,
44 pub workspace_id: Uuid,
46 pub object_id: Uuid,
48 pub thread_id: Uuid,
50 pub parent_comment_id: Option<Uuid>,
52 pub author: CommentAuthor,
54 pub status: CommentStatus,
56 pub body: Option<String>,
58 pub mentions: Vec<CommentMention>,
60 pub created_at: DateTime<Utc>,
62 pub updated_at: DateTime<Utc>,
64 pub edited_at: Option<DateTime<Utc>>,
66 pub deleted_at: Option<DateTime<Utc>>,
68 pub deleted_by: Option<Uuid>,
70 pub expired_at: Option<DateTime<Utc>>,
72 pub retention_expires_at: Option<DateTime<Utc>>,
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
78pub struct CommentThread {
79 pub id: Uuid,
81 pub workspace_id: Uuid,
83 pub object_id: Uuid,
85 pub created_by: Uuid,
87 pub created_at: DateTime<Utc>,
89 pub updated_at: DateTime<Utc>,
91 pub resolved_at: Option<DateTime<Utc>>,
93 pub resolved_by: Option<Uuid>,
95 pub retention_expires_at: Option<DateTime<Utc>>,
97 pub comments: Vec<Comment>,
99 #[serde(skip_serializing_if = "Option::is_none")]
101 pub comments_next_cursor: Option<String>,
102}
103
104#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
106pub struct CommentMentionCandidate {
107 pub user_id: Uuid,
109 pub username: String,
111 pub display_name: String,
113}
114
115#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
117pub struct CommentMentionCandidateParams {
118 #[serde(default)]
120 pub q: String,
121 pub limit: Option<i64>,
123}
124
125impl CommentMentionCandidateParams {
126 pub fn checked_limit(&self) -> Result<i64, &'static str> {
132 match self.limit {
133 Some(limit) if limit < 1 => Err("limit must be at least 1"),
134 Some(limit) => Ok(limit.min(20)),
135 None => Ok(8),
136 }
137 }
138}
139
140#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
142#[serde(deny_unknown_fields)]
143pub struct CreateCommentRequest {
144 pub body: String,
146 #[serde(default)]
148 pub mentioned_user_ids: Vec<Uuid>,
149}
150
151#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
153#[serde(deny_unknown_fields)]
154pub struct UpdateCommentRequest {
155 pub body: String,
157 #[serde(default)]
159 pub mentioned_user_ids: Vec<Uuid>,
160}
161
162#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
164pub struct CommentResponse {
165 pub comment: Comment,
167}
168
169#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
171pub struct CommentThreadResponse {
172 pub thread: CommentThread,
174}
175
176pub type CommentThreadListResponse = ListResponse<CommentThread>;
178
179pub type CommentListResponse = ListResponse<Comment>;
181
182pub type CommentMentionCandidateListResponse = ListResponse<CommentMentionCandidate>;
184
185#[cfg(test)]
186mod tests {
187 use super::*;
188
189 #[test]
190 fn mention_candidate_query_limits_are_validated() {
191 assert_eq!(CommentMentionCandidateParams::default().checked_limit(), Ok(8));
192 assert!(
193 CommentMentionCandidateParams { q: String::new(), limit: Some(0) }
194 .checked_limit()
195 .is_err()
196 );
197 assert_eq!(
198 CommentMentionCandidateParams { q: String::new(), limit: Some(25) }.checked_limit(),
199 Ok(20),
200 );
201 }
202
203 #[test]
204 fn create_request_rejects_unknown_fields() {
205 let result = serde_json::from_value::<CreateCommentRequest>(serde_json::json!({
206 "body": "hello",
207 "unexpected": true,
208 }));
209
210 assert!(result.is_err());
211 }
212}