forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
198 lines (155 loc) · 5.39 KB
/
Copy pathdb.py
File metadata and controls
198 lines (155 loc) · 5.39 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
Redis-based storage for Linear tokens and user settings.
Supports both local development (file fallback) and production (Redis).
"""
import json
import os
from datetime import datetime
from typing import Optional, Dict, Any
# Try to import redis, fall back to file-based if not available
try:
import redis
REDIS_AVAILABLE = True
except ImportError:
REDIS_AVAILABLE = False
# Redis connection
_redis_client = None
def _get_redis() -> Optional['redis.Redis']:
"""Get or create Redis connection."""
global _redis_client
if not REDIS_AVAILABLE:
return None
redis_url = os.getenv("REDIS_URL") or os.getenv("REDIS_PRIVATE_URL")
if not redis_url:
return None
if _redis_client is None:
try:
_redis_client = redis.from_url(redis_url, decode_responses=True)
_redis_client.ping() # Test connection
print("✅ Connected to Redis")
except Exception as e:
print(f"⚠️ Redis connection failed: {e}, falling back to file storage")
return None
return _redis_client
# File-based fallback for local development
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
TOKENS_FILE = os.path.join(DATA_DIR, "tokens.json")
USER_SETTINGS_FILE = os.path.join(DATA_DIR, "user_settings.json")
def _ensure_data_dir():
"""Ensure the data directory exists."""
os.makedirs(DATA_DIR, exist_ok=True)
def _load_json(filepath: str) -> Dict[str, Any]:
"""Load JSON from file, return empty dict if not exists."""
_ensure_data_dir()
if os.path.exists(filepath):
with open(filepath, "r") as f:
return json.load(f)
return {}
def _save_json(filepath: str, data: Dict[str, Any]):
"""Save data to JSON file."""
_ensure_data_dir()
with open(filepath, "w") as f:
json.dump(data, f, indent=2)
# ============================================
# Token Management
# ============================================
def store_linear_tokens(uid: str, access_token: str, refresh_token: str, expires_at: int):
"""Store Linear tokens for a user."""
r = _get_redis()
token_data = {
"access_token": access_token,
"refresh_token": refresh_token,
"expires_at": expires_at,
"updated_at": datetime.utcnow().isoformat()
}
if r:
# Use Redis
key = f"linear:tokens:{uid}"
r.set(key, json.dumps(token_data))
# Set expiry to 365 days (Linear tokens are long-lived)
r.expire(key, 60 * 60 * 24 * 365)
else:
# Fallback to file
tokens = _load_json(TOKENS_FILE)
tokens[uid] = token_data
_save_json(TOKENS_FILE, tokens)
def get_linear_tokens(uid: str) -> Optional[Dict[str, Any]]:
"""Get Linear tokens for a user."""
r = _get_redis()
if r:
key = f"linear:tokens:{uid}"
data = r.get(key)
if data:
return json.loads(data)
return None
else:
tokens = _load_json(TOKENS_FILE)
return tokens.get(uid)
def delete_linear_tokens(uid: str):
"""Delete Linear tokens for a user."""
r = _get_redis()
if r:
key = f"linear:tokens:{uid}"
r.delete(key)
else:
tokens = _load_json(TOKENS_FILE)
if uid in tokens:
del tokens[uid]
_save_json(TOKENS_FILE, tokens)
def is_token_expired(uid: str) -> bool:
"""Check if the user's token is expired."""
tokens = get_linear_tokens(uid)
if not tokens:
return True
expires_at = tokens.get("expires_at", 0)
# Add 60 seconds buffer
return datetime.utcnow().timestamp() > (expires_at - 60)
# ============================================
# User Settings Management
# ============================================
def store_user_setting(uid: str, key: str, value: Any):
"""Store a setting for a user."""
r = _get_redis()
if r:
redis_key = f"linear:settings:{uid}"
settings = r.get(redis_key)
settings = json.loads(settings) if settings else {}
settings[key] = value
r.set(redis_key, json.dumps(settings))
else:
settings = _load_json(USER_SETTINGS_FILE)
if uid not in settings:
settings[uid] = {}
settings[uid][key] = value
_save_json(USER_SETTINGS_FILE, settings)
def get_user_setting(uid: str, key: str) -> Optional[Any]:
"""Get a setting for a user."""
r = _get_redis()
if r:
redis_key = f"linear:settings:{uid}"
settings = r.get(redis_key)
if settings:
return json.loads(settings).get(key)
return None
else:
settings = _load_json(USER_SETTINGS_FILE)
return settings.get(uid, {}).get(key)
def get_user_settings(uid: str) -> Dict[str, Any]:
"""Get all settings for a user."""
r = _get_redis()
if r:
redis_key = f"linear:settings:{uid}"
settings = r.get(redis_key)
return json.loads(settings) if settings else {}
else:
settings = _load_json(USER_SETTINGS_FILE)
return settings.get(uid, {})
def store_default_team(uid: str, team_id: str, team_name: str):
"""Store the default team for a user."""
store_user_setting(uid, "default_team", {
"id": team_id,
"name": team_name
})
def get_default_team(uid: str) -> Optional[Dict[str, str]]:
"""Get the default team for a user."""
return get_user_setting(uid, "default_team")