use crate::store::{run_sqlite, Store, StoreResult};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeliveryEventKind {
IssueEdit,
Comment,
Ignored,
}
impl DeliveryEventKind {
pub fn as_str(self) -> &'static str {
match self {
Self::IssueEdit => "issue_edit",
Self::Comment => "comment",
Self::Ignored => "ignored",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeliveryStatus {
Pending,
Processed,
Ignored,
NoTarget,
Error,
}
impl DeliveryStatus {
pub fn as_str(self) -> &'static str {
match self {
Self::Pending => "pending",
Self::Processed => "processed",
Self::Ignored => "ignored",
Self::NoTarget => "no_target",
Self::Error => "error",
}
}
pub fn from_db(value: &str) -> Self {
match value {
"processed" => Self::Processed,
"ignored" => Self::Ignored,
"no_target" => Self::NoTarget,
"error" => Self::Error,
_ => Self::Pending,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeliveryRecord {
pub inserted: bool,
pub existing_status: Option<DeliveryStatus>,
}
#[derive(Debug, Clone)]
pub struct DeliveryCompletion {
pub delivery_id: String,
pub provider: String,
pub status: DeliveryStatus,
pub target_kind: Option<String>,
pub target_id: Option<String>,
pub outcome: Option<String>,
pub processed_at: i64,
}
impl Store {
pub async fn record_delivery(
&self,
delivery_id: String,
provider: String,
event_kind: Option<String>,
received_at: i64,
) -> StoreResult<DeliveryRecord> {
run_sqlite(&self.sqlite, move |store| {
store.record_delivery(&delivery_id, &provider, event_kind.as_deref(), received_at)
})
.await
}
pub async fn complete_delivery(&self, completion: DeliveryCompletion) -> StoreResult<()> {
run_sqlite(&self.sqlite, move |store| {
store.complete_delivery(&completion)
})
.await
}
pub async fn delivery_count(&self) -> StoreResult<i64> {
run_sqlite(&self.sqlite, |store| store.delivery_count()).await
}
}