File size: 1,986 Bytes
d3d504c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Optional anonymous activity preference memory; no diary text or location."""
import hashlib
import os
import sqlite3
from contextlib import closing
from datetime import datetime, timedelta, timezone
from pathlib import Path

RETENTION_DAYS = 30


def database_path() -> Path:
    return Path(os.getenv("MINDILY_MEMORY_DB", Path(__file__).resolve().parent / "data" / "memory.sqlite3"))


def _connect():
    path = database_path()
    path.parent.mkdir(parents=True, exist_ok=True)
    conn = sqlite3.connect(path, timeout=10)
    conn.execute("""CREATE TABLE IF NOT EXISTS memory (
        token_hash TEXT PRIMARY KEY,
        preferred_kind TEXT NOT NULL,
        updated_at TEXT NOT NULL
    )""")
    conn.commit()
    return conn


def _hash(token: str) -> str:
    return hashlib.sha256(token.encode("utf-8")).hexdigest()


def _purge_expired(conn) -> None:
    cutoff = (datetime.now(timezone.utc) - timedelta(days=RETENTION_DAYS)).isoformat()
    conn.execute("DELETE FROM memory WHERE updated_at < ?", (cutoff,))


def remember(token: str, preferred_kind: str) -> None:
    with closing(_connect()) as conn:
        with conn:
            _purge_expired(conn)
            conn.execute("""INSERT INTO memory VALUES (?, ?, ?)
                ON CONFLICT(token_hash) DO UPDATE SET preferred_kind=excluded.preferred_kind,
                updated_at=excluded.updated_at""", (
                    _hash(token), preferred_kind, datetime.now(timezone.utc).isoformat()))


def recall(token: str) -> str | None:
    with closing(_connect()) as conn:
        with conn:
            _purge_expired(conn)
            row = conn.execute("SELECT preferred_kind FROM memory WHERE token_hash=?", (_hash(token),)).fetchone()
        return row[0] if row else None


def forget(token: str) -> bool:
    with closing(_connect()) as conn:
        with conn:
            _purge_expired(conn)
            return bool(conn.execute("DELETE FROM memory WHERE token_hash=?", (_hash(token),)).rowcount)