feat(api): google tasks oauth, sync engine, and background worker
- ApiClient with manual OAuth2 Device Flow (no yup-oauth2 dependency) - Devide auth: POST device/code -> show URL+code -> poll token endpoint - Token persistence in ~/.config/task_app/token.json - CRUD: create_task, update_task, delete_task, move_task via Google Tasks API - fetch_lists and fetch_tasks for initial sync import - Db wraps Connection in std::sync::Mutex for thread-safe sharing via Arc - Sync engine: background thread with tokio runtime, processes queue every 30s - process_sync_queue drains sync_queue and calls API methods - trigger_sync() called after every local mutation (create/update/delete/reorder) - Network status propagated to UI (Online/Offline/Syncing) - Initial sync skeleton ready for full import flow
This commit is contained in:
+55
-30
@@ -1,10 +1,12 @@
|
||||
use rusqlite::{params, Connection, Result as SqlResult};
|
||||
use std::sync::Mutex;
|
||||
|
||||
use chrono::NaiveDateTime;
|
||||
use rusqlite::{params, Connection, Result as SqlResult};
|
||||
|
||||
use crate::domain::models::*;
|
||||
|
||||
pub struct Db {
|
||||
conn: Connection,
|
||||
conn: Mutex<Connection>,
|
||||
}
|
||||
|
||||
impl Db {
|
||||
@@ -38,11 +40,12 @@ impl Db {
|
||||
created_at TEXT NOT NULL
|
||||
);",
|
||||
)?;
|
||||
Ok(Self { conn })
|
||||
Ok(Self { conn: Mutex::new(conn) })
|
||||
}
|
||||
|
||||
pub fn get_lists(&self) -> Vec<TaskList> {
|
||||
let mut stmt = self.conn
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT id, title FROM task_lists ORDER BY title")
|
||||
.unwrap();
|
||||
stmt.query_map([], |row| {
|
||||
@@ -57,7 +60,8 @@ impl Db {
|
||||
}
|
||||
|
||||
pub fn insert_list(&self, list: &TaskList) -> SqlResult<()> {
|
||||
self.conn.execute(
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO task_lists (id, title) VALUES (?1, ?2)",
|
||||
params![list.id, list.title],
|
||||
)?;
|
||||
@@ -65,13 +69,15 @@ impl Db {
|
||||
}
|
||||
|
||||
pub fn delete_list(&self, list_id: &str) -> SqlResult<()> {
|
||||
self.conn.execute("DELETE FROM tasks WHERE list_id = ?1", params![list_id])?;
|
||||
self.conn.execute("DELETE FROM task_lists WHERE id = ?1", params![list_id])?;
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute("DELETE FROM tasks WHERE list_id = ?1", params![list_id])?;
|
||||
conn.execute("DELETE FROM task_lists WHERE id = ?1", params![list_id])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_tasks(&self, list_id: &str) -> Vec<Task> {
|
||||
let mut stmt = self.conn
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let mut stmt = conn
|
||||
.prepare(
|
||||
"SELECT id, list_id, title, notes, status, due, position
|
||||
FROM tasks WHERE list_id = ?1 ORDER BY position ASC",
|
||||
@@ -104,19 +110,18 @@ impl Db {
|
||||
TaskStatus::Completed => "completed",
|
||||
TaskStatus::NeedsAction => "needsAction",
|
||||
};
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let position = if task.position == 0 {
|
||||
let max: i64 = self.conn
|
||||
.query_row(
|
||||
"SELECT COALESCE(MAX(position), -1) + 1 FROM tasks WHERE list_id = ?1",
|
||||
params![task.list_id],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.unwrap_or(0);
|
||||
max
|
||||
conn.query_row(
|
||||
"SELECT COALESCE(MAX(position), -1) + 1 FROM tasks WHERE list_id = ?1",
|
||||
params![task.list_id],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.unwrap_or(0)
|
||||
} else {
|
||||
task.position
|
||||
};
|
||||
self.conn.execute(
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO tasks (id, list_id, title, notes, status, due, position, updated_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
|
||||
params![
|
||||
@@ -139,7 +144,8 @@ impl Db {
|
||||
TaskStatus::Completed => "completed",
|
||||
TaskStatus::NeedsAction => "needsAction",
|
||||
};
|
||||
self.conn.execute(
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"UPDATE tasks SET title=?1, notes=?2, status=?3, due=?4, position=?5, updated_at=?6
|
||||
WHERE id=?7",
|
||||
params![
|
||||
@@ -156,12 +162,14 @@ impl Db {
|
||||
}
|
||||
|
||||
pub fn delete_task(&self, task_id: &str) -> SqlResult<()> {
|
||||
self.conn.execute("DELETE FROM tasks WHERE id = ?1", params![task_id])?;
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute("DELETE FROM tasks WHERE id = ?1", params![task_id])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn reorder_task(&self, task_id: &str, new_position: i64) -> SqlResult<()> {
|
||||
let (old_position, list_id): (i64, String) = self.conn.query_row(
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let (old_position, list_id): (i64, String) = conn.query_row(
|
||||
"SELECT position, list_id FROM tasks WHERE id = ?1",
|
||||
params![task_id],
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
@@ -172,20 +180,20 @@ impl Db {
|
||||
}
|
||||
|
||||
if new_position > old_position {
|
||||
self.conn.execute(
|
||||
conn.execute(
|
||||
"UPDATE tasks SET position = position - 1
|
||||
WHERE list_id = ?1 AND position > ?2 AND position <= ?3",
|
||||
params![list_id, old_position, new_position],
|
||||
)?;
|
||||
} else {
|
||||
self.conn.execute(
|
||||
conn.execute(
|
||||
"UPDATE tasks SET position = position + 1
|
||||
WHERE list_id = ?1 AND position >= ?2 AND position < ?3",
|
||||
params![list_id, new_position, old_position],
|
||||
)?;
|
||||
}
|
||||
|
||||
self.conn.execute(
|
||||
conn.execute(
|
||||
"UPDATE tasks SET position = ?1 WHERE id = ?2",
|
||||
params![new_position, task_id],
|
||||
)?;
|
||||
@@ -194,7 +202,9 @@ impl Db {
|
||||
}
|
||||
|
||||
pub fn replace_all_lists(&self, lists: &[TaskList]) -> SqlResult<()> {
|
||||
self.conn.execute("DELETE FROM task_lists", [])?;
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute("DELETE FROM task_lists", [])?;
|
||||
drop(conn);
|
||||
for list in lists {
|
||||
self.insert_list(list)?;
|
||||
}
|
||||
@@ -202,7 +212,9 @@ impl Db {
|
||||
}
|
||||
|
||||
pub fn replace_all_tasks(&self, list_id: &str, tasks: &[Task]) -> SqlResult<()> {
|
||||
self.conn.execute("DELETE FROM tasks WHERE list_id = ?1", params![list_id])?;
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute("DELETE FROM tasks WHERE list_id = ?1", params![list_id])?;
|
||||
drop(conn);
|
||||
for (i, task) in tasks.iter().enumerate() {
|
||||
let mut t = task.clone();
|
||||
if t.position == 0 {
|
||||
@@ -215,8 +227,14 @@ impl Db {
|
||||
}
|
||||
|
||||
pub fn push_sync(&self, action: SyncAction, task_id: &str, list_id: &str, payload: &str) -> SqlResult<()> {
|
||||
let action_str = serde_json::to_string(&action).unwrap_or_default();
|
||||
self.conn.execute(
|
||||
let action_str = match action {
|
||||
SyncAction::Create => "Create",
|
||||
SyncAction::Update => "Update",
|
||||
SyncAction::Delete => "Delete",
|
||||
SyncAction::Reorder => "Reorder",
|
||||
};
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO sync_queue (action, task_id, list_id, payload, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
params![
|
||||
@@ -231,13 +249,20 @@ impl Db {
|
||||
}
|
||||
|
||||
pub fn drain_sync(&self) -> Vec<SyncQueueItem> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let items: Vec<SyncQueueItem> = {
|
||||
let mut stmt = self.conn
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT id, action, task_id, list_id, payload, created_at FROM sync_queue ORDER BY id")
|
||||
.unwrap();
|
||||
stmt.query_map([], |row| {
|
||||
let action_str: String = row.get(1)?;
|
||||
let action: SyncAction = serde_json::from_str(&action_str).unwrap_or(SyncAction::Update);
|
||||
let action = match action_str.as_str() {
|
||||
"\"Create\"" | "Create" => SyncAction::Create,
|
||||
"\"Update\"" | "Update" => SyncAction::Update,
|
||||
"\"Delete\"" | "Delete" => SyncAction::Delete,
|
||||
"\"Reorder\"" | "Reorder" => SyncAction::Reorder,
|
||||
_ => SyncAction::Update,
|
||||
};
|
||||
Ok(SyncQueueItem {
|
||||
id: row.get(0)?,
|
||||
action,
|
||||
@@ -253,7 +278,7 @@ impl Db {
|
||||
};
|
||||
|
||||
if !items.is_empty() {
|
||||
self.conn.execute("DELETE FROM sync_queue", []).unwrap();
|
||||
conn.execute("DELETE FROM sync_queue", []).unwrap();
|
||||
}
|
||||
|
||||
items
|
||||
|
||||
Reference in New Issue
Block a user