| 1 | use rusqlite::{Connection, Row, params, types::Type}; |
| 2 | |
| 3 | use crate::attachment_images::{OUTBOX_OWNER, delete_images, load_images, replace_images}; |
| 4 | use crate::error::Result; |
| 5 | use crate::models::{OutboxRecord, OutboxState, OutboxTarget}; |
| 6 | use crate::store::Store; |
| 7 | use crate::time::{from_millis, to_millis}; |
| 8 | |
| 9 | const OUTBOX_COLUMNS: &str = |
| 10 | "id, conversation_id, target, thread_root_id, payload, state, last_error, created_at"; |
| 11 | |
| 12 | impl Store { |
| 13 | pub fn put_outbox(&self, record: &OutboxRecord) -> Result<()> { |
| 14 | let mut connection = self.lock()?; |
| 15 | let transaction = connection.transaction()?; |
| 16 | transaction.execute( |
| 17 | &format!( |
| 18 | "INSERT INTO outbox ({OUTBOX_COLUMNS}) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8) |
| 19 | ON CONFLICT (id) DO UPDATE SET |
| 20 | conversation_id = excluded.conversation_id, target = excluded.target, |
| 21 | thread_root_id = excluded.thread_root_id, payload = excluded.payload, |
| 22 | state = excluded.state, last_error = excluded.last_error, |
| 23 | created_at = excluded.created_at" |
| 24 | ), |
| 25 | params![ |
| 26 | record.id, |
| 27 | record.conversation_id, |
| 28 | record.target.as_str(), |
| 29 | record.thread_root_id, |
| 30 | record.payload, |
| 31 | record.state.as_str(), |
| 32 | record.last_error, |
| 33 | to_millis(record.created_at), |
| 34 | ], |
| 35 | )?; |
| 36 | replace_images(&transaction, OUTBOX_OWNER, &record.id, &record.images)?; |
| 37 | Ok(transaction.commit()?) |
| 38 | } |
| 39 | |
| 40 | pub fn outbox_for_conversation(&self, conversation_id: &str) -> Result<Vec<OutboxRecord>> { |
| 41 | let connection = self.read()?; |
| 42 | let mut statement = connection.prepare_cached(&format!( |
| 43 | "SELECT {OUTBOX_COLUMNS} FROM outbox WHERE conversation_id = ?1 |
| 44 | ORDER BY created_at ASC, id ASC" |
| 45 | ))?; |
| 46 | let records = statement |
| 47 | .query_map([conversation_id], outbox_from_row)? |
| 48 | .collect::<rusqlite::Result<Vec<_>>>()?; |
| 49 | with_images(&connection, records) |
| 50 | } |
| 51 | |
| 52 | pub fn sending_outbox(&self) -> Result<Vec<OutboxRecord>> { |
| 53 | let connection = self.read()?; |
| 54 | let mut statement = connection.prepare_cached(&format!( |
| 55 | "SELECT {OUTBOX_COLUMNS} FROM outbox WHERE state = ?1 ORDER BY created_at ASC, id ASC" |
| 56 | ))?; |
| 57 | let records = statement |
| 58 | .query_map([OutboxState::Sending.as_str()], outbox_from_row)? |
| 59 | .collect::<rusqlite::Result<Vec<_>>>()?; |
| 60 | with_images(&connection, records) |
| 61 | } |
| 62 | |
| 63 | pub fn failed_outbox_conversations(&self) -> Result<Vec<String>> { |
| 64 | let connection = self.read()?; |
| 65 | let mut statement = connection.prepare_cached( |
| 66 | "SELECT DISTINCT conversation_id FROM outbox WHERE state = ?1 ORDER BY conversation_id", |
| 67 | )?; |
| 68 | let ids = statement |
| 69 | .query_map([OutboxState::Failed.as_str()], |row| row.get(0))? |
| 70 | .collect::<rusqlite::Result<Vec<_>>>()?; |
| 71 | Ok(ids) |
| 72 | } |
| 73 | |
| 74 | pub fn mark_outbox_failed(&self, id: &str, error: &str) -> Result<()> { |
| 75 | let connection = self.lock()?; |
| 76 | connection.execute( |
| 77 | "UPDATE outbox SET state = ?2, last_error = ?3 WHERE id = ?1", |
| 78 | params![id, OutboxState::Failed.as_str(), error], |
| 79 | )?; |
| 80 | Ok(()) |
| 81 | } |
| 82 | |
| 83 | pub fn delete_outbox(&self, id: &str) -> Result<()> { |
| 84 | let mut connection = self.lock()?; |
| 85 | let transaction = connection.transaction()?; |
| 86 | transaction.execute("DELETE FROM outbox WHERE id = ?1", [id])?; |
| 87 | delete_images(&transaction, OUTBOX_OWNER, id)?; |
| 88 | Ok(transaction.commit()?) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | fn with_images(connection: &Connection, records: Vec<OutboxRecord>) -> Result<Vec<OutboxRecord>> { |
| 93 | records |
| 94 | .into_iter() |
| 95 | .map(|mut record| { |
| 96 | record.images = load_images(connection, OUTBOX_OWNER, &record.id)?; |
| 97 | Ok(record) |
| 98 | }) |
| 99 | .collect() |
| 100 | } |
| 101 | |
| 102 | fn outbox_from_row(row: &Row<'_>) -> rusqlite::Result<OutboxRecord> { |
| 103 | let target: String = row.get(2)?; |
| 104 | let state: String = row.get(5)?; |
| 105 | Ok(OutboxRecord { |
| 106 | id: row.get(0)?, |
| 107 | conversation_id: row.get(1)?, |
| 108 | target: OutboxTarget::parse(&target).ok_or_else(|| unknown_value(2, "outbox target"))?, |
| 109 | thread_root_id: row.get(3)?, |
| 110 | payload: row.get(4)?, |
| 111 | images: Vec::new(), |
| 112 | state: OutboxState::parse(&state).ok_or_else(|| unknown_value(5, "outbox state"))?, |
| 113 | last_error: row.get(6)?, |
| 114 | created_at: from_millis(row.get(7)?), |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | fn unknown_value(column: usize, what: &str) -> rusqlite::Error { |
| 119 | rusqlite::Error::FromSqlConversionFailure(column, Type::Text, format!("unknown {what}").into()) |
| 120 | } |