|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | + |
| 5 | +use crate::{date::WpGmtDateTime, impl_as_query_value_for_new_type}; |
| 6 | + |
| 7 | +#[derive(Debug, PartialEq, Eq, Serialize, uniffi::Record)] |
| 8 | +pub struct CreateSupportTicketParams { |
| 9 | + pub subject: String, |
| 10 | + pub message: String, |
| 11 | + pub application: String, |
| 12 | + #[uniffi(default = None)] |
| 13 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 14 | + pub wpcom_site_id: Option<u64>, |
| 15 | + #[uniffi(default = [])] |
| 16 | + pub tags: Vec<String>, |
| 17 | + #[uniffi(default = [])] |
| 18 | + pub attachments: Vec<String>, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 22 | +pub struct SupportConversationSummary { |
| 23 | + pub id: ConversationId, |
| 24 | + pub title: String, |
| 25 | + pub description: String, |
| 26 | + pub status: String, |
| 27 | + pub created_at: WpGmtDateTime, |
| 28 | + pub updated_at: WpGmtDateTime, |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 32 | +pub struct SupportConversation { |
| 33 | + pub id: ConversationId, |
| 34 | + pub title: String, |
| 35 | + pub description: String, |
| 36 | + pub status: String, |
| 37 | + pub created_at: WpGmtDateTime, |
| 38 | + pub updated_at: WpGmtDateTime, |
| 39 | + pub messages: Vec<SupportMessage>, |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 43 | +pub struct SupportMessage { |
| 44 | + pub id: u64, |
| 45 | + pub content: String, |
| 46 | + pub author: SupportMessageAuthor, |
| 47 | + pub role: String, |
| 48 | + pub author_is_current_user: bool, |
| 49 | + pub created_at: WpGmtDateTime, |
| 50 | + pub attachments: Vec<SupportAttachment>, |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 54 | +pub struct SupportUserIdentity { |
| 55 | + pub id: u64, |
| 56 | + pub email: String, |
| 57 | + pub display_name: String, |
| 58 | + pub avatar_url: String, |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 62 | +pub struct SupportAttachment { |
| 63 | + pub id: u64, |
| 64 | + pub filename: String, |
| 65 | + pub content_type: String, |
| 66 | + pub size: u64, |
| 67 | + pub url: String, |
| 68 | + pub metadata: HashMap<String, AttachmentMetadataValue>, |
| 69 | +} |
| 70 | + |
| 71 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Enum)] |
| 72 | +#[serde(untagged)] |
| 73 | +pub enum SupportMessageAuthor { |
| 74 | + User(SupportUserIdentity), |
| 75 | + SupportAgent(SupportAgentIdentity), |
| 76 | +} |
| 77 | + |
| 78 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Enum, strum_macros::Display)] |
| 79 | +#[strum(serialize_all = "snake_case")] |
| 80 | +pub enum AttachmentMetadataKey { |
| 81 | + Width, |
| 82 | + Height, |
| 83 | + Other(String), |
| 84 | +} |
| 85 | + |
| 86 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Enum)] |
| 87 | +#[serde(untagged)] |
| 88 | +pub enum AttachmentMetadataValue { |
| 89 | + String(String), |
| 90 | + Number(u64), |
| 91 | + Boolean(bool), |
| 92 | +} |
| 93 | + |
| 94 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)] |
| 95 | +pub struct SupportAgentIdentity { |
| 96 | + pub id: u64, |
| 97 | + pub name: String, |
| 98 | +} |
| 99 | + |
| 100 | +#[derive(Debug, PartialEq, Eq, Serialize, uniffi::Record)] |
| 101 | +pub struct AddMessageToSupportConversationParams { |
| 102 | + pub message: String, |
| 103 | + #[uniffi(default = [])] |
| 104 | + pub attachments: Vec<String>, |
| 105 | +} |
| 106 | + |
| 107 | +impl_as_query_value_for_new_type!(ConversationId); |
| 108 | +uniffi::custom_newtype!(ConversationId, u64); |
| 109 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 110 | +pub struct ConversationId(pub u64); |
| 111 | + |
| 112 | +impl std::str::FromStr for ConversationId { |
| 113 | + type Err = std::num::ParseIntError; |
| 114 | + |
| 115 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 116 | + s.parse().map(Self) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl std::fmt::Display for ConversationId { |
| 121 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 122 | + write!(f, "{}", self.0) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +#[cfg(test)] |
| 127 | +mod tests { |
| 128 | + use super::*; |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_support_conversation_deserialization() { |
| 132 | + let json = include_str!("../../tests/wpcom/support_tickets/single-conversation.json"); |
| 133 | + let conversation: SupportConversation = |
| 134 | + serde_json::from_str(json).expect("Failed to deserialize support conversation"); |
| 135 | + assert_eq!(conversation.messages.len(), 7); |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn test_support_conversation_list_deserialization() { |
| 140 | + let json = include_str!("../../tests/wpcom/support_tickets/conversation-list.json"); |
| 141 | + let conversation_list: Vec<SupportConversationSummary> = |
| 142 | + serde_json::from_str(json).expect("Failed to deserialize support conversation list"); |
| 143 | + assert_eq!(conversation_list.len(), 11); |
| 144 | + } |
| 145 | +} |
0 commit comments