Skip to main content

max / goingson

2.0 KB · 61 lines History Blame Raw
1 //! Shared tool context: the database handle and the fixed desktop user.
2 //!
3 //! go-mcp is a peer writer against the same `goingson.db` the desktop app uses.
4 //! Every write flows through the normal repository layer, so the 33 SQL sync
5 //! triggers fire and a running GoingsOn picks the changes up. There is no back
6 //! door around the changelog.
7
8 use std::path::PathBuf;
9
10 use goingson_core::UserId;
11 use goingson_db_sqlite::{SqliteProjectRepository, SqliteTaskRepository};
12 use sqlx::SqlitePool;
13 use uuid::Uuid;
14
15 /// Fixed single-user desktop id. Must match `DESKTOP_USER_ID` in the GoingsOn
16 /// Tauri app (`src-tauri/src/state.rs`); tasks are keyed on it, so a mismatch
17 /// would write rows the app never shows.
18 pub const DESKTOP_USER_ID: UserId = UserId::from_uuid(Uuid::from_u128(1));
19
20 /// Everything a tool needs to reach the database. Cheap to clone (the pool is
21 /// an `Arc` internally); tools hold it behind an `Arc` anyway.
22 pub struct Ctx {
23 pub pool: SqlitePool,
24 pub user_id: UserId,
25 }
26
27 impl Ctx {
28 pub fn new(pool: SqlitePool) -> Self {
29 Self {
30 pool,
31 user_id: DESKTOP_USER_ID,
32 }
33 }
34
35 pub fn tasks(&self) -> SqliteTaskRepository {
36 SqliteTaskRepository::new(self.pool.clone())
37 }
38
39 pub fn projects(&self) -> SqliteProjectRepository {
40 SqliteProjectRepository::new(self.pool.clone())
41 }
42 }
43
44 /// Default location of `goingson.db`, matching Tauri's `app_data_dir` for the
45 /// `com.goingson.app` bundle. Resolves the same path the desktop app opens so
46 /// `go-mcp` with no `--db` flag targets the real database.
47 pub fn default_db_path() -> Option<PathBuf> {
48 let home = std::env::var_os("HOME")?;
49 let home = PathBuf::from(home);
50
51 #[cfg(target_os = "macos")]
52 let base = home.join("Library/Application Support");
53
54 #[cfg(not(target_os = "macos"))]
55 let base = std::env::var_os("XDG_DATA_HOME")
56 .map(PathBuf::from)
57 .unwrap_or_else(|| home.join(".local/share"));
58
59 Some(base.join("com.goingson.app").join("goingson.db"))
60 }
61