-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_storage.js
More file actions
199 lines (171 loc) · 5.37 KB
/
simple_storage.js
File metadata and controls
199 lines (171 loc) · 5.37 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
198
const fs = require('fs');
const path = require('path');
// Storage file paths - use Railway volume if available, otherwise current directory
const STORAGE_DIR = process.env.RAILWAY_VOLUME_MOUNT_PATH
? path.join(process.env.RAILWAY_VOLUME_MOUNT_PATH)
: __dirname;
// Ensure storage directory exists
if (!fs.existsSync(STORAGE_DIR)) {
fs.mkdirSync(STORAGE_DIR, { recursive: true });
}
const USERS_FILE = path.join(STORAGE_DIR, 'users_data.json');
const SESSIONS_FILE = path.join(STORAGE_DIR, 'sessions_data.json');
console.log(`💾 Storage location: ${STORAGE_DIR}`);
// In-memory storage
let users = {};
let sessions = {};
// Load from file on startup
function loadStorage() {
try {
if (fs.existsSync(USERS_FILE)) {
users = JSON.parse(fs.readFileSync(USERS_FILE, 'utf8'));
console.log(`✅ Loaded ${Object.keys(users).length} users from storage`);
}
} catch (error) {
console.error(`⚠️ Could not load users: ${error}`);
}
try {
if (fs.existsSync(SESSIONS_FILE)) {
sessions = JSON.parse(fs.readFileSync(SESSIONS_FILE, 'utf8'));
console.log(`✅ Loaded ${Object.keys(sessions).length} sessions from storage`);
}
} catch (error) {
console.error(`⚠️ Could not load sessions: ${error}`);
}
}
function saveUsers() {
try {
fs.writeFileSync(USERS_FILE, JSON.stringify(users, null, 2));
} catch (error) {
console.error(`⚠️ Could not save users: ${error}`);
}
}
function saveSessions() {
try {
fs.writeFileSync(SESSIONS_FILE, JSON.stringify(sessions, null, 2));
} catch (error) {
console.error(`⚠️ Could not save sessions: ${error}`);
}
}
// Load on module import
loadStorage();
class SimpleUserStorage {
static saveUser(uid, tokens, chats = null) {
if (!users[uid]) {
users[uid] = {
uid,
created_at: new Date().toISOString()
};
}
users[uid] = {
...users[uid],
tokens,
updated_at: new Date().toISOString()
};
if (chats) {
users[uid].available_chats = chats;
}
saveUsers();
console.log(`💾 Saved data for user ${uid.substring(0, 10)}...`);
}
static getUser(uid) {
return users[uid] || null;
}
static isAuthenticated(uid) {
const user = users[uid];
return user && user.tokens && user.tokens.access_token;
}
static deleteUser(uid) {
if (users[uid]) {
delete users[uid];
saveUsers();
return true;
}
return false;
}
static updateUserSettings(uid, settings) {
if (users[uid]) {
users[uid].settings = {
...users[uid].settings,
...settings,
updated_at: new Date().toISOString()
};
saveUsers();
console.log(`⚙️ Updated settings for user ${uid.substring(0, 10)}...`);
return true;
}
return false;
}
static getUserSettings(uid) {
const user = users[uid];
if (user && user.settings) {
return user.settings;
}
// Return defaults
return {
timezone: 'America/Los_Angeles' // Default to PT
};
}
}
class SimpleSessionStorage {
static getOrCreateSession(sessionId, uid) {
if (!sessions[sessionId]) {
sessions[sessionId] = {
session_id: sessionId,
uid,
message_mode: "idle", // idle, recording, processing
segments_count: 0,
accumulated_text: "",
target_chat: null,
created_at: new Date().toISOString()
};
console.log(`🆕 Created new session: ${sessionId}`);
}
return sessions[sessionId];
}
static updateSession(sessionId, updates) {
if (sessions[sessionId]) {
updates.last_segment_at = new Date().toISOString();
Object.assign(sessions[sessionId], updates);
console.log(`💾 Updated session ${sessionId}:`, updates);
} else {
console.error(`⚠️ Session ${sessionId} not found for update!`);
}
}
static getSessionIdleTime(sessionId) {
if (!sessions[sessionId]) {
return null;
}
const lastSegment = sessions[sessionId].last_segment_at;
if (!lastSegment) {
return null;
}
try {
const lastTime = new Date(lastSegment);
const idleSeconds = (Date.now() - lastTime.getTime()) / 1000;
return idleSeconds;
} catch (error) {
return null;
}
}
static resetSession(sessionId) {
if (sessions[sessionId]) {
sessions[sessionId].message_mode = "idle";
sessions[sessionId].segments_count = 0;
sessions[sessionId].accumulated_text = "";
sessions[sessionId].target_chat = null;
console.log(`🔄 Reset session ${sessionId}`);
}
}
static getAllSessions() {
return sessions;
}
}
module.exports = {
SimpleUserStorage,
SimpleSessionStorage,
users,
sessions,
saveUsers,
saveSessions
};