feat(nutrition): meals belong to the person who logged them
- meals.user_id (v1 databases gain it on open, DEFAULT '' keeps their meals the single user's); insert_meal, get_meals_between and delete_meal take the person, and delete only removes that person's meal. - turn_state.set_user / ToolContext.user_id carry the asker; logMeal, fetchMeals and deleteMeal use it. The v2 brain sets it for each reply. - Tests: two people never see or delete each other's meals, no user is v1's single user, and a v1 database opens with its meals kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -27,7 +27,9 @@ CREATE TABLE IF NOT EXISTS meals (
|
||||
sodium_mg REAL,
|
||||
potassium_mg REAL,
|
||||
micros_json TEXT,
|
||||
confidence REAL
|
||||
confidence REAL,
|
||||
-- Who logged it (v2: Discord user ID). '' is v1's single user.
|
||||
user_id TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
-- Conversation summaries for diary/memory system
|
||||
@@ -127,6 +129,11 @@ class Database:
|
||||
with self._lock:
|
||||
cur = self.conn.cursor()
|
||||
cur.executescript(_SCHEMA_SQL)
|
||||
# v1 databases have no meals.user_id; their meals are the single user's ('').
|
||||
cols = {r[1] for r in cur.execute("PRAGMA table_info(meals)").fetchall()}
|
||||
if "user_id" not in cols:
|
||||
cur.execute("ALTER TABLE meals ADD COLUMN user_id TEXT NOT NULL DEFAULT ''")
|
||||
cur.execute("CREATE INDEX IF NOT EXISTS meals_user_ts ON meals(user_id, ts_utc)")
|
||||
if self.is_vss_enabled:
|
||||
cur.executescript(_VSS_SCHEMA_SQL)
|
||||
self.conn.commit()
|
||||
@@ -290,13 +297,14 @@ class Database:
|
||||
potassium_mg: Optional[float] = None,
|
||||
micros_json: Optional[str] = None,
|
||||
confidence: Optional[float] = None,
|
||||
user_id: str = "",
|
||||
) -> int:
|
||||
with self._lock:
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO meals(ts_utc, source_app, description, calories_kcal, protein_g, carbs_g, fat_g, fiber_g, sugar_g, sodium_mg, potassium_mg, micros_json, confidence)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO meals(ts_utc, source_app, description, calories_kcal, protein_g, carbs_g, fat_g, fiber_g, sugar_g, sodium_mg, potassium_mg, micros_json, confidence, user_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
ts_utc,
|
||||
@@ -312,28 +320,31 @@ class Database:
|
||||
potassium_mg,
|
||||
micros_json,
|
||||
confidence,
|
||||
user_id or "",
|
||||
),
|
||||
)
|
||||
self.conn.commit()
|
||||
return int(cur.lastrowid)
|
||||
|
||||
def get_meals_between(self, ts_utc_min: str, ts_utc_max: str) -> list[sqlite3.Row]:
|
||||
def get_meals_between(self, ts_utc_min: str, ts_utc_max: str, user_id: str = "") -> list[sqlite3.Row]:
|
||||
"""Meals of one person (``user_id``; '' is v1's single user) in the time range."""
|
||||
with self._lock:
|
||||
cur = self.conn.cursor()
|
||||
rows = cur.execute(
|
||||
"""
|
||||
SELECT * FROM meals
|
||||
WHERE ts_utc >= ? AND ts_utc <= ?
|
||||
WHERE ts_utc >= ? AND ts_utc <= ? AND user_id = ?
|
||||
ORDER BY ts_utc ASC
|
||||
""",
|
||||
(ts_utc_min, ts_utc_max),
|
||||
(ts_utc_min, ts_utc_max, user_id or ""),
|
||||
).fetchall()
|
||||
return rows
|
||||
|
||||
def delete_meal(self, meal_id: int) -> bool:
|
||||
def delete_meal(self, meal_id: int, user_id: str = "") -> bool:
|
||||
"""Delete a meal only when it belongs to ``user_id``."""
|
||||
with self._lock:
|
||||
cur = self.conn.cursor()
|
||||
cur.execute("DELETE FROM meals WHERE id = ?", (meal_id,))
|
||||
cur.execute("DELETE FROM meals WHERE id = ? AND user_id = ?", (meal_id, user_id or ""))
|
||||
self.conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user