-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-db.ts
More file actions
867 lines (782 loc) · 30.6 KB
/
Copy pathserver-db.ts
File metadata and controls
867 lines (782 loc) · 30.6 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
import { MongoClient, Db } from 'mongodb';
import { User, Chat, Message, AppTheme, UserStatus } from './src/types';
// AES-256 Symmetric Encryption for Cortex Secure Backups (.crypt files)
const CRYPT_ALGORITHM = 'aes-256-cbc';
const CRYPT_KEY = crypto.scryptSync('CortexSecureBackupPassword_2026', 'cortex_salt', 32);
const CRYPT_IV = Buffer.alloc(16, 0); // Deterministic secure static IV for complete format compatibility
export function encryptData(text: string): string {
const cipher = crypto.createCipheriv(CRYPT_ALGORITHM, CRYPT_KEY, CRYPT_IV);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
export function decryptData(encryptedHex: string): string {
const decipher = crypto.createDecipheriv(CRYPT_ALGORITHM, CRYPT_KEY, CRYPT_IV);
let decrypted = decipher.update(encryptedHex, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// Simple password hashing/simulation helper for MVP
function hashPassword(pwd: string): string {
let hash = 0;
for (let i = 0; i < pwd.length; i++) {
const char = pwd.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash; // Convert to 32bit integer
}
return 'hash_' + Math.abs(hash).toString(16);
}
interface LocalDB {
users: Array<User & { passwordHash: string }>;
chats: Chat[];
messages: Message[];
statuses: UserStatus[];
}
const DB_FILE = path.join(process.cwd(), 'app_db.json');
class DatabaseManager {
private mongoClient: MongoClient | null = null;
private mongoDb: Db | null = null;
private useMongo = false;
// Local file fallback DB memory state
private localData: LocalDB = {
users: [],
chats: [],
messages: [],
statuses: []
};
constructor() {
this.loadLocal();
this.connectMongo().then(() => {
this.checkAndLoadBackupFile();
});
}
public async checkAndLoadBackupFile() {
try {
const backupPath = path.join(process.cwd(), 'backup.crypt');
if (fs.existsSync(backupPath)) {
console.log('==================================================');
console.log('📦 Found auto-restore backup file: backup.crypt');
console.log('==================================================');
const encryptedHex = fs.readFileSync(backupPath, 'utf-8').trim();
if (!encryptedHex) {
console.warn('Backup file is empty, aborting auto-restore.');
return;
}
const decryptedText = decryptData(encryptedHex);
const parsedData = JSON.parse(decryptedText);
await this.restoreBackup(parsedData);
console.log('✅ Successfully auto-restored DB from backup.crypt!');
const loadedPath = path.join(process.cwd(), 'backup.crypt.loaded');
fs.renameSync(backupPath, loadedPath);
console.log(`Renamed active backup to backup.crypt.loaded to finalize initialization.`);
}
} catch (err) {
console.error('⚠️ Failed loading backup.crypt on startup:', err);
}
}
public async restoreBackup(data: LocalDB) {
if (this.useMongo && this.mongoDb) {
if (data.users && data.users.length > 0) {
await this.mongoDb.collection('users').deleteMany({});
await this.mongoDb.collection('users').insertMany(data.users);
}
if (data.chats && data.chats.length > 0) {
await this.mongoDb.collection('chats').deleteMany({});
await this.mongoDb.collection('chats').insertMany(data.chats);
}
if (data.messages && data.messages.length > 0) {
await this.mongoDb.collection('messages').deleteMany({});
await this.mongoDb.collection('messages').insertMany(data.messages);
}
if (data.statuses && data.statuses.length > 0) {
await this.mongoDb.collection('statuses').deleteMany({});
await this.mongoDb.collection('statuses').insertMany(data.statuses);
}
}
// Always sync memory mapping and local fallback json
this.localData = {
users: data.users || [],
chats: data.chats || [],
messages: data.messages || [],
statuses: data.statuses || []
};
this.saveLocal();
}
public async getBackupData(): Promise<LocalDB> {
if (this.useMongo && this.mongoDb) {
const users = await this.mongoDb.collection('users').find({}).toArray() as any;
const chats = await this.mongoDb.collection('chats').find({}).toArray() as any;
const messages = await this.mongoDb.collection('messages').find({}).toArray() as any;
const statuses = await this.mongoDb.collection('statuses').find({}).toArray() as any;
return { users, chats, messages, statuses };
}
return this.localData;
}
private loadLocal() {
try {
if (fs.existsSync(DB_FILE)) {
const fileContent = fs.readFileSync(DB_FILE, 'utf-8');
this.localData = JSON.parse(fileContent);
// Ensure default arrays are present
if (!this.localData.users) this.localData.users = [];
if (!this.localData.chats) this.localData.chats = [];
if (!this.localData.messages) this.localData.messages = [];
if (!this.localData.statuses) this.localData.statuses = [];
} else {
this.saveLocal();
}
} catch (err) {
console.error('Error reading local db.json, starting fresh', err);
}
}
private saveLocal() {
try {
fs.writeFileSync(DB_FILE, JSON.stringify(this.localData, null, 2), 'utf-8');
} catch (err) {
console.error('Error writing local db.json', err);
}
}
private async connectMongo() {
const uri = process.env.MONGODB_URI;
if (uri) {
console.log('Detected MONGODB_URI. Attempting to connect to MongoDB...', uri.split('@')[1] || uri);
try {
this.mongoClient = new MongoClient(uri);
await this.mongoClient.connect();
this.mongoDb = this.mongoClient.db('whatsapp_mvp');
this.useMongo = true;
console.log('Connected to MongoDB successfully!');
// Populate local fallback database into MongoDB to migrate any existing mock/active data
await this.migrateLocalToMongo();
} catch (err) {
console.error('MongoDB connection failed. Continuing with local JSON DB file fallback.', err);
this.useMongo = false;
}
} else {
console.log('No MONGODB_URI environment variable found. Using high-fidelity local JSON database (app_db.json).');
}
}
private async migrateLocalToMongo() {
if (!this.useMongo || !this.mongoDb) return;
try {
const userCount = await this.mongoDb.collection('users').countDocuments();
if (userCount === 0 && this.localData.users.length > 0) {
console.log('Migrating local users to MongoDB...');
await this.mongoDb.collection('users').insertMany(this.localData.users);
}
const chatCount = await this.mongoDb.collection('chats').countDocuments();
if (chatCount === 0 && this.localData.chats.length > 0) {
console.log('Migrating local chats to MongoDB...');
await this.mongoDb.collection('chats').insertMany(this.localData.chats);
}
const msgCount = await this.mongoDb.collection('messages').countDocuments();
if (msgCount === 0 && this.localData.messages.length > 0) {
console.log('Migrating local messages to MongoDB...');
await this.mongoDb.collection('messages').insertMany(this.localData.messages);
}
} catch (e) {
console.error('Migration error', e);
}
}
// Users Auth APIs
async registerUser(username: string, passwordPlain: string, avatar: string, theme: AppTheme = 'dark-blue', bio?: string): Promise<User | null> {
const cleanUsername = username.trim().toLowerCase();
if (!cleanUsername || !passwordPlain) return null;
const pwdHash = hashPassword(passwordPlain);
const cleanBio = bio ? bio.trim().substring(0, 20) : 'Hey there! I use chat';
if (this.useMongo && this.mongoDb) {
const existing = await this.mongoDb.collection('users').findOne({ username: cleanUsername });
if (existing) return null;
const userId = 'usr_' + Math.random().toString(36).substr(2, 9);
const newUserDoc = {
id: userId,
username: cleanUsername,
passwordHash: pwdHash,
avatar,
theme,
isOnline: false,
bio: cleanBio,
isPublic: false,
friends: [] as string[],
friendRequests: [] as any[],
createdAt: new Date().toISOString()
};
await this.mongoDb.collection('users').insertOne(newUserDoc);
const { passwordHash, ...userObj } = newUserDoc;
return userObj as User;
} else {
const existing = this.localData.users.find(u => u.username === cleanUsername);
if (existing) return null;
const userId = 'usr_' + Math.random().toString(36).substr(2, 9);
const newUser = {
id: userId,
username: cleanUsername,
passwordHash: pwdHash,
avatar,
theme,
isOnline: false,
bio: cleanBio,
isPublic: false,
friends: [] as string[],
friendRequests: [] as any[],
createdAt: new Date().toISOString()
};
this.localData.users.push(newUser);
this.saveLocal();
const { passwordHash, ...userObj } = newUser;
return userObj as User;
}
}
async loginUser(username: string, passwordPlain: string): Promise<User | null> {
const cleanUsername = username.trim().toLowerCase();
const pwdHash = hashPassword(passwordPlain);
if (this.useMongo && this.mongoDb) {
const userDoc = await this.mongoDb.collection('users').findOne({ username: cleanUsername, passwordHash: pwdHash });
if (!userDoc) return null;
const { _id, passwordHash, ...userObj } = userDoc as any;
return {
...userObj,
bio: userObj.bio || 'Hey there! I use chat',
friends: userObj.friends || [],
friendRequests: userObj.friendRequests || []
} as User;
} else {
const user = this.localData.users.find(u => u.username === cleanUsername && u.passwordHash === pwdHash);
if (!user) return null;
const { passwordHash, ...userObj } = user;
return {
...userObj,
bio: userObj.bio || 'Hey there! I use chat',
friends: userObj.friends || [],
friendRequests: userObj.friendRequests || []
} as User;
}
}
async getUser(userId: string): Promise<User | null> {
if (this.useMongo && this.mongoDb) {
const userDoc = await this.mongoDb.collection('users').findOne({ id: userId });
if (!userDoc) return null;
const { _id, passwordHash, ...userObj } = userDoc as any;
return {
...userObj,
bio: userObj.bio || 'Hey there! I use chat',
friends: userObj.friends || [],
friendRequests: userObj.friendRequests || []
} as User;
} else {
const user = this.localData.users.find(u => u.id === userId);
if (!user) return null;
const { passwordHash, ...userObj } = user;
return {
...userObj,
bio: userObj.bio || 'Hey there! I use chat',
friends: userObj.friends || [],
friendRequests: userObj.friendRequests || []
} as User;
}
}
async updateUserTheme(userId: string, theme: AppTheme): Promise<boolean> {
if (this.useMongo && this.mongoDb) {
const result = await this.mongoDb.collection('users').updateOne({ id: userId }, { $set: { theme } });
return result.modifiedCount > 0;
} else {
const user = this.localData.users.find(u => u.id === userId);
if (user) {
user.theme = theme;
this.saveLocal();
return true;
}
return false;
}
}
async setUserOnlineStatus(userId: string, isOnline: boolean): Promise<void> {
if (this.useMongo && this.mongoDb) {
await this.mongoDb.collection('users').updateOne({ id: userId }, { $set: { isOnline } });
} else {
const user = this.localData.users.find(u => u.id === userId);
if (user) {
user.isOnline = isOnline;
this.saveLocal();
}
}
}
async getAllUsers(): Promise<User[]> {
if (this.useMongo && this.mongoDb) {
const cursor = this.mongoDb.collection('users').find();
const docs = await cursor.toArray();
return docs.map((d: any) => {
const { _id, passwordHash, ...userObj } = d;
return {
...userObj,
bio: userObj.bio || 'Hey there! I use chat',
friends: userObj.friends || [],
friendRequests: userObj.friendRequests || []
} as User;
});
} else {
return this.localData.users.map(u => {
const { passwordHash, ...userObj } = u;
return {
...userObj,
bio: userObj.bio || 'Hey there! I use chat',
friends: userObj.friends || [],
friendRequests: userObj.friendRequests || []
} as User;
});
}
}
async sendFriendRequest(fromUserId: string, toUsername: string): Promise<{ success: boolean; error?: string }> {
const sender = await this.getUser(fromUserId);
if (!sender) return { success: false, error: 'Sender not found' };
const targetUsernameClean = toUsername.trim().toLowerCase();
if (sender.username === targetUsernameClean) {
return { success: false, error: 'You cannot send a friend request to yourself' };
}
let target: any = null;
if (this.useMongo && this.mongoDb) {
target = await this.mongoDb.collection('users').findOne({ username: targetUsernameClean });
} else {
target = this.localData.users.find(u => u.username === targetUsernameClean);
}
if (!target) return { success: false, error: `User with username "${toUsername}" not found` };
const targetFriends = target.friends || [];
if (targetFriends.includes(fromUserId)) {
return { success: false, error: 'You are already friends' };
}
const targetRequests = target.friendRequests || [];
if (targetRequests.some((r: any) => r.fromId === fromUserId)) {
return { success: false, error: 'Friend request was already sent' };
}
const newRequest = {
fromId: fromUserId,
fromUsername: sender.username,
fromAvatar: sender.avatar
};
if (this.useMongo && this.mongoDb) {
await this.mongoDb.collection('users').updateOne(
{ id: target.id },
{ $push: { friendRequests: newRequest } } as any
);
} else {
if (!target.friendRequests) target.friendRequests = [];
target.friendRequests.push(newRequest);
this.saveLocal();
}
return { success: true };
}
async acceptFriendRequest(userId: string, fromUserId: string): Promise<{ success: boolean; error?: string }> {
const self = await this.getUser(userId);
const sender = await this.getUser(fromUserId);
if (!self || !sender) return { success: false, error: 'User profiles not found' };
const updatedRequests = (self.friendRequests || []).filter((r: any) => r.fromId !== fromUserId);
const selfFriends = Array.from(new Set([...(self.friends || []), fromUserId]));
const senderFriends = Array.from(new Set([...(sender.friends || []), userId]));
if (this.useMongo && this.mongoDb) {
await this.mongoDb.collection('users').updateOne(
{ id: userId },
{ $set: { friendRequests: updatedRequests, friends: selfFriends } }
);
await this.mongoDb.collection('users').updateOne(
{ id: fromUserId },
{ $set: { friends: senderFriends } }
);
} else {
const selfRaw = this.localData.users.find(u => u.id === userId);
const senderRaw = this.localData.users.find(u => u.id === fromUserId);
if (selfRaw) {
selfRaw.friendRequests = updatedRequests;
selfRaw.friends = selfFriends;
}
if (senderRaw) {
senderRaw.friends = senderFriends;
}
this.saveLocal();
}
return { success: true };
}
async declineFriendRequest(userId: string, fromUserId: string): Promise<{ success: boolean; error?: string }> {
const self = await this.getUser(userId);
if (!self) return { success: false, error: 'User profile not found' };
const updatedRequests = (self.friendRequests || []).filter((r: any) => r.fromId !== fromUserId);
if (this.useMongo && this.mongoDb) {
await this.mongoDb.collection('users').updateOne(
{ id: userId },
{ $set: { friendRequests: updatedRequests } }
);
} else {
const selfRaw = this.localData.users.find(u => u.id === userId);
if (selfRaw) {
selfRaw.friendRequests = updatedRequests;
}
this.saveLocal();
}
return { success: true };
}
// Chats APIs
async getChatsForUser(userId: string): Promise<Chat[]> {
if (this.useMongo && this.mongoDb) {
const cursor = this.mongoDb.collection('chats').find({ members: userId });
const docs = await cursor.toArray();
return docs.map((d: any) => {
const { _id, ...chatObj } = d;
return chatObj as Chat;
});
} else {
return this.localData.chats.filter(c => c.members.includes(userId));
}
}
async createChat(members: string[], isGroup: boolean, name?: string, creatorId?: string): Promise<Chat> {
const chatId = 'cht_' + Math.random().toString(36).substr(2, 9);
const primaryAdmin = creatorId || members[0] || '';
const newChat: Chat = {
id: chatId,
name: isGroup ? (name || 'Group Chat') : undefined,
isGroup,
members,
admins: isGroup ? [primaryAdmin] : undefined,
createdAt: new Date().toISOString()
};
if (this.useMongo && this.mongoDb) {
await this.mongoDb.collection('chats').insertOne({ ...newChat });
} else {
this.localData.chats.push(newChat);
this.saveLocal();
}
return newChat;
}
// Messages APIs
async getMessagesForChat(chatId: string): Promise<Message[]> {
// Delete expired messages first
await this.cleanupExpiredMessages();
if (this.useMongo && this.mongoDb) {
const cursor = this.mongoDb.collection('messages').find({ chatId }).sort({ createdAt: 1 });
const docs = await cursor.toArray();
return docs.map((d: any) => {
const { _id, ...msgObj } = d;
return msgObj as Message;
});
} else {
return this.localData.messages.filter(m => m.chatId === chatId);
}
}
async storeMessage(message: Omit<Message, 'id' | 'createdAt' | 'expiresAt'>): Promise<Message> {
const msgId = 'msg_' + Math.random().toString(36).substr(2, 9);
const now = new Date();
const createdAt = now.toISOString();
// Expiration date is exactly 48 hours later (2 days)
const expiresAt = new Date(now.getTime() + 48 * 60 * 60 * 1000).toISOString();
const fullMessage: Message = {
...message,
id: msgId,
createdAt,
expiresAt
};
if (this.useMongo && this.mongoDb) {
await this.mongoDb.collection('messages').insertOne({ ...fullMessage });
// Update lastMessage and lastMessageAt on parent chat
await this.mongoDb.collection('chats').updateOne(
{ id: message.chatId },
{ $set: { lastMessage: message.text || (message.mediaType === 'image' ? '📷 Image' : '🎥 Video'), lastMessageAt: createdAt } }
);
} else {
this.localData.messages.push(fullMessage);
const chat = this.localData.chats.find(c => c.id === message.chatId);
if (chat) {
chat.lastMessage = message.text || (message.mediaType === 'image' ? '📷 Image' : '🎥 Video');
chat.lastMessageAt = createdAt;
}
this.saveLocal();
}
return fullMessage;
}
async addMessageReaction(messageId: string, userId: string, emoji: string): Promise<Message | null> {
if (this.useMongo && this.mongoDb) {
const msg = await this.mongoDb.collection('messages').findOne({ id: messageId });
if (!msg) return null;
const reactions = msg.reactions || {};
reactions[userId] = emoji;
const result = await this.mongoDb.collection('messages').findOneAndUpdate(
{ id: messageId },
{ $set: { reactions } },
{ returnDocument: 'after' }
);
if (!result) return null;
const actualDoc = (result as any).value ? (result as any).value : result;
const { _id, ...msgObj } = actualDoc as any;
return msgObj as Message;
} else {
const msg = this.localData.messages.find(m => m.id === messageId);
if (msg) {
if (!msg.reactions) msg.reactions = {};
msg.reactions[userId] = emoji;
this.saveLocal();
return msg;
}
return null;
}
}
// Deletes expired messages (older than 48 hours from creation, i.e., current time > expiresAt)
async cleanupExpiredMessages(): Promise<string[]> {
const nowStr = new Date().toISOString();
let deletedIds: string[] = [];
if (this.useMongo && this.mongoDb) {
try {
// Find messages that are expired
const cursor = this.mongoDb.collection('messages').find({ expiresAt: { $lt: nowStr } });
const expired = await cursor.toArray();
deletedIds = expired.map((m: any) => m.id);
if (deletedIds.length > 0) {
console.log(`[Database] Cleaning up ${deletedIds.length} expired messages from MongoDB.`);
await this.mongoDb.collection('messages').deleteMany({ expiresAt: { $lt: nowStr } });
}
} catch (err) {
console.error('Error during Mongo clean up', err);
}
} else {
const nonExpired: Message[] = [];
this.localData.messages.forEach(m => {
if (new Date(m.expiresAt).getTime() < Date.now()) {
deletedIds.push(m.id);
} else {
nonExpired.push(m);
}
});
if (deletedIds.length > 0) {
console.log(`[Database] Cleaning up ${deletedIds.length} expired messages from Local JSON DB.`);
this.localData.messages = nonExpired;
this.saveLocal();
}
}
return deletedIds;
}
// STATUSES APIS (Expires after 24 hours)
async createStatus(userId: string, username: string, avatar: string, text: string, backgroundColor: string): Promise<UserStatus> {
const statusId = 'st_' + Math.random().toString(36).substr(2, 9);
const now = new Date();
const createdAt = now.toISOString();
const newStatus: UserStatus = {
id: statusId,
userId,
username,
avatar,
text,
backgroundColor,
createdAt
};
if (this.useMongo && this.mongoDb) {
await this.mongoDb.collection('statuses').insertOne({ ...newStatus });
} else {
if (!this.localData.statuses) this.localData.statuses = [];
this.localData.statuses.push(newStatus);
this.saveLocal();
}
return newStatus;
}
async getActiveStatuses(): Promise<UserStatus[]> {
await this.cleanupExpiredStatuses();
if (this.useMongo && this.mongoDb) {
const cursor = this.mongoDb.collection('statuses').find().sort({ createdAt: -1 });
const docs = await cursor.toArray();
return docs.map((d: any) => {
const { _id, ...statusObj } = d;
return statusObj as UserStatus;
});
} else {
if (!this.localData.statuses) this.localData.statuses = [];
return [...this.localData.statuses].sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
}
}
async deleteStatus(statusId: string, userId: string): Promise<boolean> {
if (this.useMongo && this.mongoDb) {
const result = await this.mongoDb.collection('statuses').deleteOne({ id: statusId, userId });
return result.deletedCount > 0;
} else {
if (!this.localData.statuses) this.localData.statuses = [];
const lenBefore = this.localData.statuses.length;
this.localData.statuses = this.localData.statuses.filter(s => !(s.id === statusId && s.userId === userId));
const deleted = this.localData.statuses.length < lenBefore;
if (deleted) {
this.saveLocal();
}
return deleted;
}
}
async cleanupExpiredStatuses(): Promise<void> {
const now = Date.now();
const limit = 24 * 60 * 60 * 1000; // 24 hours
if (this.useMongo && this.mongoDb) {
try {
const threshold = new Date(now - limit).toISOString();
await this.mongoDb.collection('statuses').deleteMany({ createdAt: { $lt: threshold } });
} catch (err) {
console.error('Error cleaning up statuses in mongo', err);
}
} else {
if (!this.localData.statuses) this.localData.statuses = [];
this.localData.statuses = this.localData.statuses.filter(s => {
return (now - new Date(s.createdAt).getTime()) < limit;
});
this.saveLocal();
}
}
// GROUP CHAT ROLES & MEMBERSHIP APIS
async updateGroupAdmins(chatId: string, admins: string[]): Promise<Chat | null> {
if (this.useMongo && this.mongoDb) {
const result = await this.mongoDb.collection('chats').findOneAndUpdate(
{ id: chatId },
{ $set: { admins } },
{ returnDocument: 'after' }
);
if (!result) return null;
const actualDoc = (result as any).value ? (result as any).value : result;
const { _id, ...chatObj } = actualDoc as any;
return chatObj as Chat;
} else {
const chat = this.localData.chats.find(c => c.id === chatId);
if (chat) {
chat.admins = admins;
this.saveLocal();
return chat;
}
return null;
}
}
async removeGroupMember(chatId: string, memberId: string): Promise<Chat | null> {
if (this.useMongo && this.mongoDb) {
const chatDoc = await this.mongoDb.collection('chats').findOne({ id: chatId });
if (!chatDoc) return null;
const members = (chatDoc.members || []).filter((m: string) => m !== memberId);
const admins = (chatDoc.admins || []).filter((a: string) => a !== memberId);
const result = await this.mongoDb.collection('chats').findOneAndUpdate(
{ id: chatId },
{ $set: { members, admins } },
{ returnDocument: 'after' }
);
if (!result) return null;
const actualDoc = (result as any).value ? (result as any).value : result;
const { _id, ...chatObj } = actualDoc as any;
return chatObj as Chat;
} else {
const chat = this.localData.chats.find(c => c.id === chatId);
if (chat) {
chat.members = (chat.members || []).filter(m => m !== memberId);
chat.admins = (chat.admins || []).filter(a => a !== memberId);
this.saveLocal();
return chat;
}
return null;
}
}
async addGroupMember(chatId: string, memberId: string): Promise<Chat | null> {
if (this.useMongo && this.mongoDb) {
const chatDoc = await this.mongoDb.collection('chats').findOne({ id: chatId });
if (!chatDoc) return null;
const members = Array.from(new Set([...(chatDoc.members || []), memberId]));
const result = await this.mongoDb.collection('chats').findOneAndUpdate(
{ id: chatId },
{ $set: { members } },
{ returnDocument: 'after' }
);
if (!result) return null;
const actualDoc = (result as any).value ? (result as any).value : result;
const { _id, ...chatObj } = actualDoc as any;
return chatObj as Chat;
} else {
const chat = this.localData.chats.find(c => c.id === chatId);
if (chat) {
chat.members = Array.from(new Set([...(chat.members || []), memberId]));
this.saveLocal();
return chat;
}
return null;
}
}
// PROFILE MANAGEMENT APIS
async updateUserProfile(userId: string, updates: { bio?: string; avatar?: string; isPublic?: boolean }): Promise<User | null> {
const setObj: any = {};
if (updates.bio !== undefined) setObj.bio = updates.bio.substring(0, 100); // Expanded from 20 to 100 for better statuses/bios!
if (updates.avatar !== undefined) setObj.avatar = updates.avatar;
if (updates.isPublic !== undefined) setObj.isPublic = updates.isPublic;
if (this.useMongo && this.mongoDb) {
await this.mongoDb.collection('users').updateOne({ id: userId }, { $set: setObj });
return this.getUser(userId);
} else {
const user = this.localData.users.find(u => u.id === userId);
if (user) {
if (updates.bio !== undefined) user.bio = updates.bio.substring(0, 100);
if (updates.avatar !== undefined) user.avatar = updates.avatar;
if (updates.isPublic !== undefined) user.isPublic = updates.isPublic;
this.saveLocal();
return this.getUser(userId);
}
return null;
}
}
// ADMINISTRATIVE DEV APIS
async adminGetAllChats(): Promise<Chat[]> {
if (this.useMongo && this.mongoDb) {
const cursor = this.mongoDb.collection('chats').find({});
const docs = await cursor.toArray();
return docs.map((doc: any) => {
const { _id, ...rest } = doc;
return rest as Chat;
});
} else {
return [...this.localData.chats];
}
}
async adminDeleteUser(userId: string): Promise<boolean> {
if (this.useMongo && this.mongoDb) {
const res1 = await this.mongoDb.collection('users').deleteOne({ id: userId });
await this.mongoDb.collection('messages').deleteMany({ senderId: userId });
// Remove from friend arrays of other users
await this.mongoDb.collection('users').updateMany(
{},
{ $pull: { friends: userId, friendRequests: { fromId: userId } } as any }
);
// Remove from groups
await this.mongoDb.collection('chats').updateMany(
{},
{ $pull: { members: userId, admins: userId } as any }
);
return (res1.deletedCount || 0) > 0;
} else {
const idx = this.localData.users.findIndex(u => u.id === userId);
if (idx !== -1) {
this.localData.users.splice(idx, 1);
this.localData.messages = this.localData.messages.filter(m => m.senderId !== userId);
this.localData.users.forEach(u => {
u.friends = (u.friends || []).filter(fId => fId !== userId);
u.friendRequests = (u.friendRequests || []).filter(r => r.fromId !== userId);
});
this.localData.chats.forEach(c => {
c.members = (c.members || []).filter(mId => mId !== userId);
c.admins = (c.admins || []).filter(aId => aId !== userId);
});
this.saveLocal();
return true;
}
return false;
}
}
async adminDeleteChat(chatId: string): Promise<boolean> {
if (this.useMongo && this.mongoDb) {
const res1 = await this.mongoDb.collection('chats').deleteOne({ id: chatId });
await this.mongoDb.collection('messages').deleteMany({ chatId });
return (res1.deletedCount || 0) > 0;
} else {
const idx = this.localData.chats.findIndex(c => c.id === chatId);
if (idx !== -1) {
this.localData.chats.splice(idx, 1);
this.localData.messages = this.localData.messages.filter(m => m.chatId !== chatId);
this.saveLocal();
return true;
}
return false;
}
}
}
export const db = new DatabaseManager();