|
| 1 | +use crate::{ |
| 2 | + date::WpGmtDateTime, |
| 3 | + impl_as_query_value_for_new_type, |
| 4 | + url_query::{AppendUrlQueryPairs, QueryPairs, QueryPairsExtension}, |
| 5 | + users::UserId, |
| 6 | +}; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use serde_repr::*; |
| 9 | +use std::collections::HashMap; |
| 10 | + |
| 11 | +use super::WpComSiteId; |
| 12 | + |
| 13 | +#[derive(Debug, PartialEq, Eq, Serialize, uniffi::Record)] |
| 14 | +pub struct CreateBotConversationParams { |
| 15 | + pub message: String, |
| 16 | + pub user_id: UserId, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, PartialEq, Serialize, Deserialize, uniffi::Record)] |
| 20 | +pub struct BotConversation { |
| 21 | + pub chat_id: u64, |
| 22 | + pub wpcom_user_id: UserId, |
| 23 | + pub external_id: Option<String>, |
| 24 | + pub external_id_provider: Option<String>, |
| 25 | + pub session_id: String, |
| 26 | + pub bot_slug: String, |
| 27 | + pub bot_version: String, |
| 28 | + pub created_at: WpGmtDateTime, |
| 29 | + pub zendesk_ticket_id: Option<String>, |
| 30 | + pub messages: Vec<BotMessage>, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Debug, PartialEq, Serialize, Deserialize, uniffi::Record)] |
| 34 | +pub struct BotMessage { |
| 35 | + pub message_id: u64, |
| 36 | + pub content: String, |
| 37 | + pub role: String, |
| 38 | + #[serde(rename = "ts")] |
| 39 | + pub created_at: WpGmtDateTime, |
| 40 | + pub context: MessageContext, |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Debug, PartialEq, Serialize, Deserialize, uniffi::Enum)] |
| 44 | +#[serde(untagged, rename_all = "lowercase")] |
| 45 | +pub enum MessageContext { |
| 46 | + User(UserMessageContext), |
| 47 | + Bot(BotMessageContext), |
| 48 | +} |
| 49 | + |
| 50 | +#[derive(Debug, PartialEq, Serialize, Deserialize, uniffi::Record)] |
| 51 | +pub struct BotMessageContext { |
| 52 | + pub question_tags: HashMap<String, String>, |
| 53 | + pub sources: Vec<BotMessageContextSource>, |
| 54 | + pub flags: HashMap<String, bool>, |
| 55 | +} |
| 56 | + |
| 57 | +#[derive(Debug, PartialEq, Serialize, Deserialize, uniffi::Record)] |
| 58 | +pub struct BotMessageContextSource { |
| 59 | + pub title: String, |
| 60 | + pub url: String, |
| 61 | + pub heading: String, |
| 62 | + pub content: String, |
| 63 | + pub blog_id: WpComSiteId, |
| 64 | + pub post_id: u64, |
| 65 | + pub score: f64, |
| 66 | + pub last_indexed_at: WpGmtDateTime, |
| 67 | +} |
| 68 | + |
| 69 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 70 | +#[serde(rename_all = "snake_case")] |
| 71 | +pub struct UserMessageContext { |
| 72 | + #[serde(alias = "selectedSiteId")] |
| 73 | + pub selected_site_id: WpComSiteId, |
| 74 | + pub wpcom_user_id: UserId, |
| 75 | + pub wpcom_user_name: String, |
| 76 | + pub user_paid_support_eligibility: UserPaidSupportEligibility, |
| 77 | + pub plan: UserPaidSupportPlan, |
| 78 | + pub products: Vec<String>, |
| 79 | + pub plan_interface: bool, |
| 80 | +} |
| 81 | + |
| 82 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 83 | +pub struct UserPaidSupportEligibility { |
| 84 | + pub is_user_eligible: bool, |
| 85 | + pub wapuu_assistant_enabled: bool, |
| 86 | +} |
| 87 | + |
| 88 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 89 | +pub struct UserPaidSupportPlan { |
| 90 | + pub plan_name: String, |
| 91 | + pub is_free: bool, |
| 92 | +} |
| 93 | + |
| 94 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 95 | +pub struct BotConversationSummary { |
| 96 | + pub chat_id: u64, |
| 97 | + pub created_at: WpGmtDateTime, |
| 98 | + pub last_message: BotMessageSummary, |
| 99 | +} |
| 100 | + |
| 101 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 102 | +pub struct BotMessageSummary { |
| 103 | + pub content: String, |
| 104 | + pub role: String, |
| 105 | + pub created_at: WpGmtDateTime, |
| 106 | +} |
| 107 | + |
| 108 | +#[derive(Debug, PartialEq, Eq, Deserialize, uniffi::Record)] |
| 109 | +pub struct GetBotConversationParams { |
| 110 | + // The number of the page to retrieve, limited to 100. |
| 111 | + #[uniffi(default = None)] |
| 112 | + pub page_number: Option<u64>, |
| 113 | + |
| 114 | + // The number of items per page. |
| 115 | + #[uniffi(default = None)] |
| 116 | + pub items_per_page: Option<u64>, |
| 117 | + |
| 118 | + // If true, include the feedback rating value for each message in the response. |
| 119 | + #[uniffi(default = false)] |
| 120 | + pub include_feedback: bool, |
| 121 | +} |
| 122 | + |
| 123 | +impl AppendUrlQueryPairs for GetBotConversationParams { |
| 124 | + fn append_query_pairs(&self, query_pairs_mut: &mut QueryPairs) { |
| 125 | + query_pairs_mut |
| 126 | + .append_option_query_value_pair("page", self.page_number.as_ref()) |
| 127 | + .append_option_query_value_pair("per_page", self.items_per_page.as_ref()) |
| 128 | + .append_query_value_pair("include_feedback", &self.include_feedback.to_string()); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 133 | +pub struct AddMessageToBotConversationParams { |
| 134 | + pub message: String, |
| 135 | + pub context: HashMap<String, String>, // TODO: Once it's possible, this hashmap should default to empty |
| 136 | +} |
| 137 | + |
| 138 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 139 | +pub struct CreateBotConversationFeedbackParams { |
| 140 | + pub rating_value: FeedbackRating, |
| 141 | +} |
| 142 | + |
| 143 | +#[derive(Debug, PartialEq, Eq, uniffi::Enum, Serialize_repr, Deserialize_repr)] |
| 144 | +#[repr(u8)] |
| 145 | +pub enum FeedbackRating { |
| 146 | + Positive = 1, |
| 147 | + Negative = 0, |
| 148 | +} |
| 149 | + |
| 150 | +impl_as_query_value_for_new_type!(BotId); |
| 151 | +uniffi::custom_newtype!(BotId, String); |
| 152 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 153 | +#[serde(transparent)] |
| 154 | +pub struct BotId(pub String); |
| 155 | + |
| 156 | +impl std::fmt::Display for BotId { |
| 157 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 158 | + write!(f, "{}", self.0) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl_as_query_value_for_new_type!(ChatId); |
| 163 | +uniffi::custom_newtype!(ChatId, u64); |
| 164 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 165 | +pub struct ChatId(pub u64); |
| 166 | + |
| 167 | +impl std::str::FromStr for ChatId { |
| 168 | + type Err = std::num::ParseIntError; |
| 169 | + |
| 170 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 171 | + s.parse().map(Self) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +impl std::fmt::Display for ChatId { |
| 176 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 177 | + write!(f, "{}", self.0) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +impl_as_query_value_for_new_type!(MessageId); |
| 182 | +uniffi::custom_newtype!(MessageId, u64); |
| 183 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 184 | +pub struct MessageId(pub u64); |
| 185 | + |
| 186 | +impl std::str::FromStr for MessageId { |
| 187 | + type Err = std::num::ParseIntError; |
| 188 | + |
| 189 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 190 | + s.parse().map(Self) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +impl std::fmt::Display for MessageId { |
| 195 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 196 | + write!(f, "{}", self.0) |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +#[cfg(test)] |
| 201 | +mod tests { |
| 202 | + use super::*; |
| 203 | + |
| 204 | + #[test] |
| 205 | + fn test_bot_create_conversation_response_deserialization() { |
| 206 | + let json = include_str!("../../tests/wpcom/support_bots/create-conversation-response.json"); |
| 207 | + let conversation: BotConversation = |
| 208 | + serde_json::from_str(json).expect("Failed to deserialize bot conversation"); |
| 209 | + assert_eq!(conversation.chat_id, 1965886); |
| 210 | + } |
| 211 | + |
| 212 | + #[test] |
| 213 | + fn test_bot_conversation_deserialization() { |
| 214 | + let json = include_str!("../../tests/wpcom/support_bots/single-conversation.json"); |
| 215 | + let conversation: BotConversation = |
| 216 | + serde_json::from_str(json).expect("Failed to deserialize bot conversation"); |
| 217 | + assert_eq!(conversation.chat_id, 1965758); |
| 218 | + } |
| 219 | + |
| 220 | + #[test] |
| 221 | + fn test_bot_conversation_summary_deserialization() { |
| 222 | + let json = include_str!("../../tests/wpcom/support_bots/converation-list.json"); |
| 223 | + let conversations: Vec<BotConversationSummary> = |
| 224 | + serde_json::from_str(json).expect("Failed to deserialize bot conversation summary"); |
| 225 | + assert_eq!(conversations.len(), 1); |
| 226 | + assert_eq!(conversations[0].chat_id, 1965758); |
| 227 | + } |
| 228 | + |
| 229 | + #[test] |
| 230 | + fn test_add_message_to_bot_conversation_response_deserialization() { |
| 231 | + let json = include_str!( |
| 232 | + "../../tests/wpcom/support_bots/add-message-to-conversation-response.json" |
| 233 | + ); |
| 234 | + let conversation: BotConversation = |
| 235 | + serde_json::from_str(json).expect("Failed to deserialize bot conversation"); |
| 236 | + assert_eq!(conversation.chat_id, 1965758); |
| 237 | + } |
| 238 | +} |
0 commit comments