use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use uuid::Uuid; /// Row as stored in the database. /// Timestamps are stored as ISO 8601 strings because `sqlx::Any` does not /// provide a blanket `Encode`/`Decode` impl for `chrono::DateTime`. #[derive(Debug, Clone, sqlx::FromRow)] pub struct EmoteRow { pub uuid: String, pub name: String, pub alias: Option, pub image_key: String, /// ISO 8601 string, e.g. "2022-02-13T11:27:38.219685+00:00" pub created: String, /// ISO 8601 string pub modified: String, } impl EmoteRow { pub fn created_dt(&self) -> DateTime { DateTime::parse_from_rfc3339(&self.created) .map(|dt| dt.with_timezone(&Utc)) .unwrap_or_else(|_| Utc::now()) } pub fn modified_dt(&self) -> DateTime { DateTime::parse_from_rfc3339(&self.modified) .map(|dt| dt.with_timezone(&Utc)) .unwrap_or_else(|_| Utc::now()) } } /// JSON representation returned by the API. #[derive(Debug, Serialize, Deserialize)] pub struct EmoteResponse { pub name: String, pub url: String, pub created: DateTime, pub modified: DateTime, } /// Payload for updating an existing emote. #[derive(Debug, Deserialize)] pub struct UpdateEmoteRequest { pub name: Option, pub alias: Option, pub image_key: Option, } pub fn new_uuid() -> String { Uuid::new_v4().to_string() }