| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use goingson_core::{CoreError, Email, EmailId, ProjectId, Result, UserId}; |
| 5 |
use rusqlite::{Connection, params}; |
| 6 |
|
| 7 |
use crate::utils::execute; |
| 8 |
|
| 9 |
use super::query; |
| 10 |
|
| 11 |
|
| 12 |
pub(super) fn mark_read(conn: &Connection, id: EmailId, user_id: UserId) -> Result<bool> { |
| 13 |
let result = execute( |
| 14 |
conn, |
| 15 |
"UPDATE emails SET is_read = 1 WHERE id = ? AND user_id = ?", |
| 16 |
params![id.to_string(), user_id.to_string()], |
| 17 |
)?; |
| 18 |
Ok(result > 0) |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
pub(super) fn mark_unread(conn: &Connection, id: EmailId, user_id: UserId) -> Result<bool> { |
| 23 |
let result = execute( |
| 24 |
conn, |
| 25 |
"UPDATE emails SET is_read = 0 WHERE id = ? AND user_id = ?", |
| 26 |
params![id.to_string(), user_id.to_string()], |
| 27 |
)?; |
| 28 |
Ok(result > 0) |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
pub(super) fn archive(conn: &Connection, id: EmailId, user_id: UserId) -> Result<bool> { |
| 33 |
let result = execute( |
| 34 |
conn, |
| 35 |
"UPDATE emails SET is_archived = 1 WHERE id = ? AND user_id = ?", |
| 36 |
params![id.to_string(), user_id.to_string()], |
| 37 |
)?; |
| 38 |
Ok(result > 0) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
pub(super) fn unarchive(conn: &Connection, id: EmailId, user_id: UserId) -> Result<bool> { |
| 43 |
let result = execute( |
| 44 |
conn, |
| 45 |
"UPDATE emails SET is_archived = 0 WHERE id = ? AND user_id = ?", |
| 46 |
params![id.to_string(), user_id.to_string()], |
| 47 |
)?; |
| 48 |
Ok(result > 0) |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
pub(super) fn update_source_folder( |
| 53 |
conn: &Connection, |
| 54 |
id: EmailId, |
| 55 |
user_id: UserId, |
| 56 |
new_folder: &str, |
| 57 |
) -> Result<bool> { |
| 58 |
let result = execute( |
| 59 |
conn, |
| 60 |
"UPDATE emails SET source_folder = ? WHERE id = ? AND user_id = ?", |
| 61 |
params![new_folder, id.to_string(), user_id.to_string()], |
| 62 |
)?; |
| 63 |
Ok(result > 0) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
pub(super) fn set_full_body( |
| 68 |
conn: &Connection, |
| 69 |
id: EmailId, |
| 70 |
user_id: UserId, |
| 71 |
body: &str, |
| 72 |
) -> Result<()> { |
| 73 |
execute( |
| 74 |
conn, |
| 75 |
"UPDATE emails SET body = ?, body_truncated = 0 WHERE id = ? AND user_id = ?", |
| 76 |
params![body, id.to_string(), user_id.to_string()], |
| 77 |
)?; |
| 78 |
Ok(()) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
pub(super) fn mark_all_read(conn: &Connection, user_id: UserId) -> Result<u64> { |
| 83 |
let result = execute( |
| 84 |
conn, |
| 85 |
"UPDATE emails SET is_read = 1 WHERE user_id = ? AND is_read = 0", |
| 86 |
params![user_id.to_string()], |
| 87 |
)?; |
| 88 |
Ok(result as u64) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
pub(super) fn link_to_project( |
| 93 |
conn: &Connection, |
| 94 |
id: EmailId, |
| 95 |
user_id: UserId, |
| 96 |
project_id: Option<ProjectId>, |
| 97 |
) -> Result<bool> { |
| 98 |
let result = execute( |
| 99 |
conn, |
| 100 |
"UPDATE emails SET project_id = ? WHERE id = ? AND user_id = ?", |
| 101 |
params![ |
| 102 |
project_id.map(|p| p.to_string()), |
| 103 |
id.to_string(), |
| 104 |
user_id.to_string() |
| 105 |
], |
| 106 |
)?; |
| 107 |
Ok(result > 0) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
pub(super) fn count_unread(conn: &Connection, user_id: UserId) -> Result<i64> { |
| 112 |
conn.query_row( |
| 113 |
"SELECT COUNT(*) FROM emails WHERE user_id = ? AND is_read = 0", |
| 114 |
params![user_id.to_string()], |
| 115 |
|row| row.get(0), |
| 116 |
) |
| 117 |
.map_err(CoreError::database) |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
pub(super) fn update_labels( |
| 122 |
conn: &Connection, |
| 123 |
id: EmailId, |
| 124 |
user_id: UserId, |
| 125 |
labels: &[String], |
| 126 |
) -> Result<Option<Email>> { |
| 127 |
let labels_json = serde_json::to_string(labels).unwrap_or_else(|_| "[]".to_string()); |
| 128 |
let result = execute( |
| 129 |
conn, |
| 130 |
"UPDATE emails SET labels = ? WHERE id = ? AND user_id = ?", |
| 131 |
params![&labels_json, id.to_string(), user_id.to_string()], |
| 132 |
)?; |
| 133 |
if result > 0 { |
| 134 |
query::get_by_id(conn, id, user_id) |
| 135 |
} else { |
| 136 |
Ok(None) |
| 137 |
} |
| 138 |
} |
| 139 |
|