| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use rusqlite::{OptionalExtension, params}; |
| 4 | |
| 5 | use crate::attachment_images::{DRAFT_OWNER, delete_images, load_images, replace_images}; |
| 6 | use crate::error::Result; |
| 7 | use crate::models::DraftRecord; |
| 8 | use crate::store::Store; |
| 9 | use crate::time::{from_millis, to_millis}; |
| 10 | |
| 11 | impl Store { |
| 12 | pub fn save_draft(&self, record: &DraftRecord) -> Result<()> { |
| 13 | let mut connection = self.lock()?; |
| 14 | let transaction = connection.transaction()?; |
| 15 | transaction.execute( |
| 16 | "INSERT INTO drafts (conversation_id, payload, preview, updated_at) VALUES (?1, ?2, ?3, ?4) |
| 17 | ON CONFLICT (conversation_id) DO UPDATE SET |
| 18 | payload = excluded.payload, preview = excluded.preview, |
| 19 | updated_at = excluded.updated_at", |
| 20 | params![ |
| 21 | record.conversation_id, |
| 22 | record.payload, |
| 23 | record.preview, |
| 24 | to_millis(record.updated_at), |
| 25 | ], |
| 26 | )?; |
| 27 | replace_images( |
| 28 | &transaction, |
| 29 | DRAFT_OWNER, |
| 30 | &record.conversation_id, |
| 31 | &record.images, |
| 32 | )?; |
| 33 | Ok(transaction.commit()?) |
| 34 | } |
| 35 | |
| 36 | pub fn save_draft_text(&self, record: &DraftRecord) -> Result<()> { |
| 37 | self.lock()?.execute( |
| 38 | "INSERT INTO drafts (conversation_id, payload, preview, updated_at) VALUES (?1, ?2, ?3, ?4) |
| 39 | ON CONFLICT (conversation_id) DO UPDATE SET |
| 40 | payload = excluded.payload, preview = excluded.preview, |
| 41 | updated_at = excluded.updated_at", |
| 42 | params![ |
| 43 | record.conversation_id, |
| 44 | record.payload, |
| 45 | record.preview, |
| 46 | to_millis(record.updated_at), |
| 47 | ], |
| 48 | )?; |
| 49 | Ok(()) |
| 50 | } |
| 51 | |
| 52 | pub fn draft(&self, conversation_id: &str) -> Result<Option<DraftRecord>> { |
| 53 | let connection = self.lock()?; |
| 54 | let found = connection |
| 55 | .query_row( |
| 56 | "SELECT payload, preview, updated_at FROM drafts WHERE conversation_id = ?1", |
| 57 | [conversation_id], |
| 58 | |row| { |
| 59 | Ok(DraftRecord { |
| 60 | conversation_id: conversation_id.to_owned(), |
| 61 | payload: row.get(0)?, |
| 62 | preview: row.get(1)?, |
| 63 | images: Vec::new(), |
| 64 | updated_at: from_millis(row.get(2)?), |
| 65 | }) |
| 66 | }, |
| 67 | ) |
| 68 | .optional()?; |
| 69 | found |
| 70 | .map(|mut record| { |
| 71 | record.images = load_images(&connection, DRAFT_OWNER, conversation_id)?; |
| 72 | Ok(record) |
| 73 | }) |
| 74 | .transpose() |
| 75 | } |
| 76 | |
| 77 | pub fn delete_draft(&self, conversation_id: &str) -> Result<()> { |
| 78 | let mut connection = self.lock()?; |
| 79 | let transaction = connection.transaction()?; |
| 80 | transaction.execute( |
| 81 | "DELETE FROM drafts WHERE conversation_id = ?1", |
| 82 | [conversation_id], |
| 83 | )?; |
| 84 | delete_images(&transaction, DRAFT_OWNER, conversation_id)?; |
| 85 | Ok(transaction.commit()?) |
| 86 | } |
| 87 | |
| 88 | pub fn draft_previews(&self) -> Result<HashMap<String, String>> { |
| 89 | let connection = self.lock()?; |
| 90 | let mut statement = |
| 91 | connection.prepare_cached("SELECT conversation_id, preview FROM drafts")?; |
| 92 | let previews = statement |
| 93 | .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))? |
| 94 | .collect::<rusqlite::Result<_>>()?; |
| 95 | Ok(previews) |
| 96 | } |
| 97 | } |