2025-02-26 13:10:47 -08:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use diesel::{
|
|
|
|
dsl::{auto_type, AsSelect},
|
|
|
|
pg::Pg,
|
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2025-02-26 13:10:27 -08:00
|
|
|
use crate::{channels::Channel, schema::messages};
|
2025-02-26 13:10:47 -08:00
|
|
|
|
2025-04-21 19:02:23 -07:00
|
|
|
pub use crate::schema::messages::{dsl, table};
|
|
|
|
|
2025-03-14 13:04:57 -07:00
|
|
|
/// A "/say" message queued for sending
|
2025-02-26 13:10:47 -08:00
|
|
|
#[derive(Associations, Clone, Debug, Identifiable, Queryable, Selectable)]
|
|
|
|
#[diesel(table_name = messages)]
|
2025-02-26 13:10:27 -08:00
|
|
|
#[diesel(belongs_to(Channel))]
|
2025-02-26 13:10:47 -08:00
|
|
|
pub struct Message {
|
|
|
|
pub id: Uuid,
|
2025-03-08 22:18:24 -08:00
|
|
|
pub project_id: Uuid,
|
2025-02-26 13:10:27 -08:00
|
|
|
pub channel_id: Uuid,
|
2025-02-26 13:10:47 -08:00
|
|
|
pub created_at: DateTime<Utc>,
|
2025-03-08 22:18:24 -08:00
|
|
|
pub sent_at: Option<DateTime<Utc>>,
|
2025-04-21 19:02:23 -07:00
|
|
|
pub failed_at: Option<DateTime<Utc>>,
|
2025-02-26 13:10:47 -08:00
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Message {
|
|
|
|
#[auto_type(no_type_alias)]
|
|
|
|
pub fn all() -> _ {
|
|
|
|
let select: AsSelect<Message, Pg> = Message::as_select();
|
2025-04-21 19:02:23 -07:00
|
|
|
table.select(select)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[auto_type(no_type_alias)]
|
|
|
|
pub fn with_id<'a>(id: &'a Uuid) -> _ {
|
|
|
|
dsl::id.eq(id)
|
2025-02-26 13:10:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[auto_type(no_type_alias)]
|
2025-03-14 13:04:57 -07:00
|
|
|
pub fn with_channel<'a>(channel_id: &'a Uuid) -> _ {
|
2025-04-21 19:02:23 -07:00
|
|
|
dsl::channel_id.eq(channel_id)
|
2025-02-26 13:10:47 -08:00
|
|
|
}
|
2025-03-08 22:18:24 -08:00
|
|
|
|
|
|
|
#[auto_type(no_type_alias)]
|
2025-04-21 19:02:23 -07:00
|
|
|
pub fn is_pending() -> _ {
|
|
|
|
dsl::sent_at.is_null().and(dsl::failed_at.is_null())
|
2025-03-08 22:18:24 -08:00
|
|
|
}
|
2025-02-26 13:10:47 -08:00
|
|
|
}
|