-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsb-users.js
More file actions
263 lines (231 loc) · 11.4 KB
/
sb-users.js
File metadata and controls
263 lines (231 loc) · 11.4 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// ============================================================
// ROLL COOKBOOK — sb-users.js
// User management, auth, settings, profiles, deletion requests
// ============================================================
async function checkUsername(username) {
const data = await sbFetch('GET', 'users', null,
`username=eq.${username.toLowerCase()}&select=id,username`);
return { status: 'Success', exists: data.length > 0, username: username.toLowerCase() };
}
async function createUser(username) {
const name = username.toLowerCase().trim();
await sbFetch('POST', 'users', { username: name });
const userRes = await sbFetch('GET', 'users', null, `username=eq.${name}&select=id`);
if (!userRes || !userRes[0]) return { status: 'Error', message: 'Failed to create user' };
const userId = userRes[0].id;
await sbFetch('POST', 'user_settings', { user_id: userId, theme: 'light' });
// Copy template recipes
try {
const templates = await sbFetch('GET', 'templates', null, 'select=*');
if (templates && templates.length > 0) {
const recipes = templates.map(t => ({
user_id: userId, title: t.title, category: t.category,
ingredients: t.ingredients || '', instructions: t.instructions || '',
notes: t.notes || '', cook_time: t.cook_time || '',
image_url: t.image_url || '', favourite: false, rating: null
}));
await sbFetch('POST', 'recipes', recipes);
}
} catch(e) { console.error('Template copy failed:', e); }
// NOTE: Auto-follow rollert2 removed — users start with no follows
return { status: 'Success' };
}
async function getUserId(username) {
const data = await sbFetch('GET', 'users', null,
`username=eq.${username.toLowerCase()}&select=id`);
return data && data[0] ? data[0].id : null;
}
async function getUserProfile(username) {
const data = await sbFetch('GET', 'users', null,
`username=eq.${username.toLowerCase()}&select=id,username,avatar_url,bio,is_private,created_at,displayed_achievements`);
return data && data[0] ? data[0] : null;
}
async function updateUserProfile(username, updates) {
const userId = await getUserId(username);
if (!userId) return { status: 'Error' };
const allowed = {};
if (updates.bio !== undefined) allowed.bio = updates.bio;
if (updates.is_private !== undefined) allowed.is_private = updates.is_private;
if (updates.displayed_achievements !== undefined) allowed.displayed_achievements = updates.displayed_achievements;
console.log('Updating user profile with:', allowed);
await sbFetch('PATCH', `users?id=eq.${userId}`, allowed);
return { status: 'Success' };
}
async function getUserSettings(username) {
const userId = await getUserId(username);
if (!userId) return { status: 'Success', settings: {} };
const data = await sbFetch('GET', 'user_settings', null, `user_id=eq.${userId}&select=*`);
if (!data || data.length === 0) {
await sbFetch('POST', 'user_settings', { user_id: userId, theme: 'light' });
return { status: 'Success', settings: {} };
}
return { status: 'Success', settings: data[0] };
}
async function updateUserSettings(username, settings) {
const userId = await getUserId(username);
if (!userId) return { status: 'Error', message: 'User not found' };
await sbFetch('PATCH', `user_settings?user_id=eq.${userId}`, settings);
return { status: 'Success' };
}
// Save unlocked achievements to Supabase for cross-device sync
async function saveUnlockedAchievements(username, achievements) {
try {
// Save achievements to user_settings table
await updateUserSettings(username, { achievements: achievements });
return { status: 'Success' };
} catch (error) {
console.error('Error saving achievements to Supabase:', error);
// Check if error is about missing column
if (error.message && error.message.includes('column') && error.message.includes('does not exist')) {
console.warn('Achievements column does not exist in user_settings table. Please add "achievements" column (type: jsonb) to user_settings table.');
return { status: 'Error', message: 'Achievements column missing in database' };
}
return { status: 'Error', message: error.message };
}
}
// Load unlocked achievements from Supabase
async function loadUnlockedAchievements(username) {
try {
const settings = await getUserSettings(username);
console.log('Settings loaded in loadUnlockedAchievements:', settings);
return {
status: 'Success',
achievements: (settings.settings && settings.settings.achievements) ? settings.settings.achievements : {}
};
} catch (error) {
console.error('Error loading achievements from Supabase:', error);
// Check if error is about missing column
if (error.message && error.message.includes('column') && error.message.includes('does not exist')) {
console.warn('Achievements column does not exist in user_settings table. Please add "achievements" column (type: jsonb) to user_settings table.');
return { status: 'Error', message: 'Achievements column missing in database', achievements: {} };
}
return { status: 'Error', message: error.message, achievements: {} };
}
}
async function uploadProfilePicture(base64Data, fileName, username) {
try {
const parts = base64Data.split(',');
const byteString = atob(parts[1]);
const mimeString = parts[0].split(':')[1].split(';')[0];
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) ia[i] = byteString.charCodeAt(i);
const blob = new Blob([ab], { type: mimeString });
const path = `avatars/${username}.jpg`;
const res = await fetch(`${SUPABASE_URL}/storage/v1/object/profile-pictures/${path}`, {
method: 'PUT',
headers: {
'apikey': SUPABASE_KEY, 'Authorization': `Bearer ${SUPABASE_KEY}`,
'Content-Type': 'image/jpeg', 'x-upsert': 'true'
},
body: blob
});
if (!res.ok) throw new Error('Storage error: ' + await res.text());
const publicUrl = `${SUPABASE_URL}/storage/v1/object/public/profile-pictures/${path}?t=${Date.now()}`;
const userId = await getUserId(username);
if (userId) await sbFetch('PATCH', `users?id=eq.${userId}`, { avatar_url: publicUrl });
return { status: 'Success', url: publicUrl };
} catch(e) { return { status: 'Error', message: e.message }; }
}
async function getUserAvatar(username) {
const data = await sbFetch('GET', 'users', null, `username=eq.${username}&select=avatar_url`);
return data && data[0] ? data[0].avatar_url || null : null;
}
async function getProfileStats(username) {
const userId = await getUserId(username);
if (!userId) return { status: 'Success', recipeCount: 0, favCount: 0, followingCount: 0, followerCount: 0 };
const [recipes, favs, following, followers] = await Promise.all([
sbFetch('GET', 'recipes', null, `user_id=eq.${userId}&select=id`),
sbFetch('GET', 'recipes', null, `user_id=eq.${userId}&favourite=eq.true&select=id`),
sbFetch('GET', 'follows', null, `follower_id=eq.${userId}&select=followee_id`),
sbFetch('GET', 'follows', null, `followee_id=eq.${userId}&select=follower_id`)
]);
return {
status: 'Success',
recipeCount: (recipes || []).length,
favCount: (favs || []).length,
followingCount: (following || []).length,
followerCount: (followers || []).length
};
}
// ── DELETION REQUESTS ─────────────────────────────────────────
async function requestAccountDeletion(username) {
const userId = await getUserId(username);
if (!userId) return { status: 'Error', message: 'User not found' };
const deleteAfter = new Date(Date.now() + 1 * 60 * 60 * 1000).toISOString();
// Upsert deletion request
const existing = await sbFetch('GET', 'deletion_requests', null, `username=eq.${username}&select=id`);
if (existing && existing.length > 0) {
await sbFetch('PATCH', `deletion_requests?username=eq.${username}`,
{ cancelled: false, requested_at: new Date().toISOString(), delete_after: deleteAfter });
} else {
await sbFetch('POST', 'deletion_requests', { username, user_id: userId, delete_after: deleteAfter });
}
// Notify rollert2
const adminId = await getUserId('rollert2');
if (adminId && adminId !== userId) {
await sbFetch('POST', 'notifications', {
to_user_id: adminId, from_user_id: userId,
type: 'deletion_request',
meta: JSON.stringify({ username, delete_after: deleteAfter })
});
}
return { status: 'Success', deleteAfter };
}
async function cancelDeletionRequest(username) {
await sbFetch('PATCH', `deletion_requests?username=eq.${username}`, { cancelled: true });
return { status: 'Success' };
}
async function getDeletionRequest(username) {
const data = await sbFetch('GET', 'deletion_requests', null,
`username=eq.${username}&cancelled=eq.false&select=*`);
return data && data[0] ? data[0] : null;
}
async function getPendingDeletionRequests() {
const data = await sbFetch('GET', 'deletion_requests', null,
`cancelled=eq.false&select=*&order=requested_at.desc`);
return data || [];
}
// ── TOUR ─────────────────────────────────────────────────────
async function getTourStatus(username) {
const userId = await getUserId(username);
if (!userId) return { status: 'Success', complete: false, version: '0' };
const data = await sbFetch('GET', 'user_settings', null,
`user_id=eq.${userId}&select=tour_complete,tour_version`);
if (!data || data.length === 0) return { status: 'Success', complete: false, version: '0' };
return { status: 'Success', complete: data[0].tour_complete, version: data[0].tour_version };
}
async function completeTour(username) {
const userId = await getUserId(username);
if (!userId) return;
await sbFetch('PATCH', `user_settings?user_id=eq.${userId}`,
{ tour_complete: true, tour_version: '3' });
}
// ── ADMIN ─────────────────────────────────────────────────────
async function getAdminSettings() {
const data = await sbFetch('GET', 'admin_settings', null, 'select=*&limit=1');
if (!data || data.length === 0) return { status: 'Success', settings: {} };
return { status: 'Success', settings: data[0] };
}
async function updateAdminSettings(settings) {
try {
let data = await sbFetch('GET', 'admin_settings', null, 'select=id&limit=1');
if (!data || data.length === 0) {
await sbFetch('POST', 'admin_settings', { timer_tone: 'default' });
data = await sbFetch('GET', 'admin_settings', null, 'select=id&limit=1');
}
if (!data || data.length === 0) return { status: 'Error', message: 'Could not find admin settings' };
const clean = { ...settings };
delete clean.updated_at;
await sbFetch('PATCH', `admin_settings?id=eq.${data[0].id}`, clean);
return { status: 'Success' };
} catch(e) { return { status: 'Error', message: e.message }; }
}
// Check saved users still exist (for login screen cleanup)
async function validateSavedUsers(usernames) {
if (!usernames || usernames.length === 0) return [];
const data = await sbFetch('GET', 'users', null,
`username=in.(${usernames.join(',')})&select=username`).catch(() => []);
const valid = new Set((data || []).map(u => u.username));
return usernames.filter(u => valid.has(u));
}