use chrono::{DateTime, Utc}; use diesel::{ dsl::{auto_type, AsSelect}, pg::Pg, prelude::*, }; use uuid::Uuid; use crate::{channels::Channel, schema::messages}; pub use crate::schema::messages::{dsl, table}; /// A "/say" message queued for sending #[derive(Associations, Clone, Debug, Identifiable, Queryable, Selectable)] #[diesel(table_name = messages)] #[diesel(belongs_to(Channel))] pub struct Message { pub id: Uuid, pub project_id: Uuid, pub channel_id: Uuid, pub created_at: DateTime, pub sent_at: Option>, pub failed_at: Option>, pub message: String, } impl Message { #[auto_type(no_type_alias)] pub fn all() -> _ { let select: AsSelect = Message::as_select(); table.select(select) } #[auto_type(no_type_alias)] pub fn with_id<'a>(id: &'a Uuid) -> _ { dsl::id.eq(id) } #[auto_type(no_type_alias)] pub fn with_channel<'a>(channel_id: &'a Uuid) -> _ { dsl::channel_id.eq(channel_id) } #[auto_type(no_type_alias)] pub fn is_pending() -> _ { dsl::sent_at.is_null().and(dsl::failed_at.is_null()) } }