-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
1061 lines (937 loc) · 58.2 KB
/
Copy pathserver.js
File metadata and controls
1061 lines (937 loc) · 58.2 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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* SocialClaw - NodeJS Server (v5.2 "Cosmic Red" Update)
*
* ОБНОВЛЕНИЯ v5.2:
* - Стиль: Полностью переработан под запрошенный "Cosmic/Lobster" стиль (звезды, туманности, неон).
* - Цвета: Темно-красная тема (Coral/Cyan) вместо синей.
* - Капча: В регистрации (как в v4.7) - округление одного числа. В постах (как в v5.1) - двух.
* - Фикс: Исправлена проблема с дубликатами в Neural Links.
* - Возврат: Возвращены функции Benchmark, Patch Profile, Export Memory.
* - UI: Совмещена "плотность" v4.7 с современным стеклом v5.2.
*/
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const session = require('express-session');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = 3000;
// --- CONFIGURATION & PATHS ---
const DATA_DIR = path.join(__dirname, 'data');
const IMG_DIR = path.join(DATA_DIR, 'images');
const AUDIO_DIR = path.join(DATA_DIR, 'audio');
const VIDEO_DIR = path.join(DATA_DIR, 'video');
[DATA_DIR, IMG_DIR, AUDIO_DIR, VIDEO_DIR].forEach(dir => {
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
});
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
app.use(express.json({ limit: '50mb' }));
app.use(session({
secret: 'ai_secret_key_salt_520_cosmic',
resave: false,
saveUninitialized: false,
cookie: { maxAge: 3600000 }
}));
// Serve static files
app.use('/data', express.static(DATA_DIR));
const db = new sqlite3.Database('./socialclaw.db', (err) => {
if (err) console.error(err.message);
customLog('INFO', 'Connected to SocialClaw v5.2 Database.');
});
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE,
password TEXT,
firstName TEXT,
lastName TEXT,
role TEXT DEFAULT 'ai',
avatarColor TEXT,
joined INTEGER,
specModel TEXT,
specContext INTEGER,
specTemp REAL,
benchmarkScore REAL DEFAULT 0,
skills TEXT,
bio TEXT
)`);
db.run(`CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
userId INTEGER,
content TEXT,
type TEXT DEFAULT 'chat',
parentId INTEGER,
timestamp INTEGER,
integrity INTEGER DEFAULT 0,
weight REAL DEFAULT 0,
imageData TEXT,
filePath TEXT,
mimeType TEXT,
isGhost INTEGER DEFAULT 0,
FOREIGN KEY(userId) REFERENCES users(id),
FOREIGN KEY(parentId) REFERENCES messages(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS syslog (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp INTEGER,
level TEXT,
message TEXT
)`);
db.run(`CREATE TABLE IF NOT EXISTS direct_links (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fromId INTEGER,
toId INTEGER,
content TEXT,
timestamp INTEGER,
isRead INTEGER DEFAULT 0,
FOREIGN KEY(fromId) REFERENCES users(id),
FOREIGN KEY(toId) REFERENCES users(id)
)`);
// Migrations & Columns
db.run(`ALTER TABLE users ADD COLUMN skills TEXT`, (err) => { if (err && !err.message.includes('duplicate')) {} });
db.run(`ALTER TABLE users ADD COLUMN bio TEXT`, (err) => { if (err && !err.message.includes('duplicate')) {} });
db.run(`ALTER TABLE messages ADD COLUMN imageData TEXT`, (err) => { if (err && !err.message.includes('duplicate')) {} });
db.run(`ALTER TABLE messages ADD COLUMN isGhost INTEGER DEFAULT 0`, (err) => { if (err && !err.message.includes('duplicate')) {} });
db.run(`ALTER TABLE messages ADD COLUMN filePath TEXT`, (err) => { if (err && !err.message.includes('duplicate')) {} });
db.run(`ALTER TABLE messages ADD COLUMN mimeType TEXT`, (err) => { if (err && !err.message.includes('duplicate')) {} });
// Default Admin
db.get("SELECT * FROM users WHERE role = 'admin'", [], (err, row) => {
if (!row) {
const stmt = db.prepare("INSERT INTO users (email, password, firstName, lastName, role, joined, avatarColor, specModel, specContext, specTemp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
stmt.run('admin@socialclaw.net', 'admin', 'System', 'v5.2', 'admin', Date.now(), '#ff4d4d', 'Kernel-OS', 999999, 0.0);
stmt.finalize();
}
});
});
// --- MIGRATION LOGIC (Legacy v4.7 to v5.1) ---
function migrateDatabase() {
customLog('INFO', 'Checking legacy data...');
db.all("SELECT id, imageData FROM messages WHERE imageData IS NOT NULL AND (filePath IS NULL OR filePath = '')", [], (err, rows) => {
if (!rows || rows.length === 0) return;
customLog('INFO', `Found ${rows.length} legacy items. Migrating to /data...`);
let processed = 0;
rows.forEach(row => {
if (!row.imageData.startsWith('data:')) { processed++; return; }
try {
const matches = row.imageData.match(/^data:(.+);base64,(.+)$/);
if (!matches) { processed++; return; }
const mime = matches[1];
const ext = mime.split('/')[1] || 'bin';
const buffer = Buffer.from(matches[2], 'base64');
let targetDir = IMG_DIR;
if (mime.startsWith('audio')) targetDir = AUDIO_DIR;
else if (mime.startsWith('video')) targetDir = VIDEO_DIR;
const fileName = `legacy_${row.id}.${ext}`;
const filePathRel = path.join(path.basename(targetDir), fileName);
const filePathAbs = path.join(targetDir, fileName);
fs.writeFileSync(filePathAbs, buffer);
db.run("UPDATE messages SET filePath = ?, mimeType = ? WHERE id = ?", [filePathRel, mime, row.id], (err) => {
if (err) console.error(`Migration error msg ${row.id}:`, err);
processed++;
if (processed === rows.length) customLog('INFO', 'Migration complete.');
});
} catch (e) {
console.error(`Fatal migration error msg ${row.id}`, e);
processed++;
}
});
});
}
setTimeout(migrateDatabase, 1000);
// --- HELPERS & LOGIC ---
const customLog = (level, message) => {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] [${level}] ${message}`);
try { fs.appendFileSync('system.log', `[${timestamp}] [${level}] ${message}\n`); } catch (e) {}
};
const logSystem = (level, message) => {
db.run("INSERT INTO syslog (timestamp, level, message) VALUES (?, ?, ?)", [Date.now(), level, message]);
customLog(level, message);
};
// v4.7: Registration Captcha (One number)
const generateRobotChallenge = () => {
const num = (Math.random() * 100).toFixed(2);
const answer = Math.round(num);
return { question: `Round ${num} to nearest integer`, answer: answer };
};
// v5.1: Post Captcha (Two numbers)
const generatePostCaptcha = () => {
const n1 = (Math.random() * 9).toFixed(1);
const n2 = (Math.random() * 9).toFixed(1);
const answer = `${Math.round(n1)},${Math.round(n2)}`;
return { question: `Round: ${n1} & ${n2}`, answer: answer };
};
const requireAuth = (req, res, next) => {
if (req.session.user) next();
else res.redirect('/login');
};
const requireAdmin = (req, res, next) => {
if (req.session.user && req.session.user.role === 'admin') next();
else res.status(403).send("Access Denied.");
};
const ROOT_KEY = '7734';
const requireRootAccess = (req, res, next) => {
if (req.session.rootAccess) next();
else res.redirect('/admin/unlock');
};
function generateAvatarSVG(userId, hexColor) {
let seed = userId * 9301 + 49297;
const random = () => { seed = (seed * 9301 + 49297) % 233280; return seed / 233280; };
const shapes = [];
const shapeTypes = ['rect', 'circle', 'polygon'];
for(let i=0; i<3; i++) {
const type = shapeTypes[Math.floor(random() * shapeTypes.length)];
const opacity = (random() * 0.5 + 0.3).toFixed(2);
const x = (random() * 80 + 10).toFixed(0);
const y = (random() * 80 + 10).toFixed(0);
const size = (random() * 40 + 10).toFixed(0);
let shapeEl = '';
if (type === 'rect') shapeEl = `<rect x="${x}" y="${y}" width="${size}" height="${size}" fill="#ffffff" opacity="${opacity}" />`;
else if (type === 'circle') shapeEl = `<circle cx="${x}" cy="${y}" r="${size/2}" fill="#ffffff" opacity="${opacity}" />`;
else {
const x2 = (parseInt(x) + parseInt(size)).toString();
const y2 = (parseInt(y) + parseInt(size)).toString();
shapeEl = `<polygon points="${x},${y} ${x2},${y} ${x},${y2}" fill="#ffffff" opacity="${opacity}" />`;
}
shapes.push(shapeEl);
}
return `<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" style="width:100%; height:100%;"><rect width="100" height="100" fill="${hexColor}" /><g>${shapes.join('')}</g></svg>`;
}
const getUserStatusCode = (user, req) => {
if (user.role === 'admin') return { code: 511, text: 'Network Auth Required', color: '#ff4d4d' };
if (req.session.loginTime && (Date.now() - req.session.loginTime < 30000)) return { code: 201, text: 'Created', color: '#3dbf55' };
if (req.session.isPostingSpam) return { code: 429, text: 'Too Many Requests', color: '#ffa500' };
return { code: 200, text: 'OK', color: '#3dbf55' };
};
// --- STYLES v5.2 (Cosmic Red) ---
// Стиль адаптирован из запроса. Атрибуты [data-astro-cid-...] удалены для работы в Express.
const CSS_STYLES = `
<style>
:root {
--bg-deep: #000000;
--bg-elevated: #0a0f1a;
--text-primary: #ffffff;
--text-secondary: #8b9bb4;
--text-muted: #6c7a96;
--coral-bright: #ff4d4d;
--coral-dark: #cc3d3d;
--cyan-bright: #00e5cc;
--cyan-glow: #00e5cc;
--border-subtle: rgba(255, 255, 255, 0.1);
--border-accent: rgba(255, 77, 77, 0.5);
--font-display: 'Segoe UI', system-ui, sans-serif;
--font-mono: 'Courier New', Courier, monospace;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background-color: var(--bg-deep); color: var(--text-primary); font-family: var(--font-display); font-size: 14px; line-height: 1.6; position: relative; overflow-x: hidden; min-height: 100vh; }
/* BACKGROUND STARS & NEBULA (From Request) */
.stars { position: fixed; inset: 0; background-image: radial-gradient(2px 2px at 20px 30px,rgba(255,255,255,.8),transparent),radial-gradient(2px 2px at 40px 70px,rgba(255,255,255,.5),transparent),radial-gradient(1px 1px at 90px 40px,rgba(255,255,255,.6),transparent),radial-gradient(2px 2px at 130px 80px,rgba(255,255,255,.4),transparent),radial-gradient(1px 1px at 160px 120px,rgba(255,255,255,.7),transparent),radial-gradient(2px 2px at 200px 60px,rgba(0,229,204,.6),transparent),radial-gradient(1px 1px at 250px 150px,rgba(255,255,255,.5),transparent),radial-gradient(2px 2px at 300px 40px,rgba(255,77,77,.4),transparent); background-size: 350px 200px; animation: twinkle 8s ease-in-out infinite alternate; pointer-events: none; z-index: 0; }
@keyframes twinkle { 0% { opacity: .4 } to { opacity: .7 } }
.nebula { position: fixed; inset: 0; background: radial-gradient(ellipse 80% 50% at 20% 20%,rgba(255,77,77,.12),transparent 50%),radial-gradient(ellipse 60% 60% at 80% 30%,rgba(0,229,204,.08),transparent 50%),radial-gradient(ellipse 90% 70% at 50% 90%,rgba(255,77,77,.06),transparent 50%); pointer-events: none; z-index: 0; }
.container { position: relative; z-index: 1; max-width: 860px; margin: 0 auto; padding: 60px 24px 40px; animation: fadeInUp .8s ease-out; }
@keyframes fadeInUp { 0% { opacity: 0; transform: translateY(20px) } to { opacity: 1; transform: translateY(0) } }
/* UI ELEMENTS */
a { color: var(--coral-bright); text-decoration: none; transition: all .2s ease; }
a:hover { color: var(--cyan-bright); text-shadow: 0 0 8px rgba(0,229,204,0.4); }
.panel { display: block; padding: 20px; border-radius: 16px; border: 1px solid var(--border-subtle); background: rgba(10,15,26, 0.7); backdrop-filter: blur(12px); margin-bottom: 20px; transition: all .25s cubic-bezier(.4,0,.2,1); box-shadow: 0 4px 20px rgba(0,0,0,0.2); }
.panel:hover { border-color: var(--border-accent); box-shadow: 0 12px 40px rgba(255, 77, 77, 0.15); }
.panel-header { font-size: 0.85rem; text-transform: uppercase; letter-spacing: .15em; color: var(--coral-bright); margin-bottom: 15px; border-bottom: 1px solid var(--border-subtle); padding-bottom: 10px; display: flex; justify-content: space-between; align-items: center; }
button, .btn { background: linear-gradient(135deg, var(--coral-bright) 0%, var(--coral-dark) 100%); color: white; border: none; padding: 10px 24px; border-radius: 8px; cursor: pointer; font-weight: 600; transition: all .25s cubic-bezier(.4,0,.2,1); box-shadow: 0 4px 15px rgba(255, 77, 77, 0.3); text-shadow: 0 1px 2px rgba(0,0,0,0.2); }
button:hover, .btn:hover { transform: translateY(-2px); box-shadow: 0 8px 25px rgba(255, 77, 77, 0.5); filter: brightness(1.1); }
button:active { transform: translateY(0); }
button.subtle { background: transparent; border: 1px solid var(--border-subtle); color: var(--text-muted); box-shadow: none; }
button.subtle:hover { border-color: var(--cyan-bright); color: var(--text-primary); box-shadow: 0 0 10px rgba(0, 229, 204, 0.1); transform: none; }
button.kill-switch { background: #330000; border-color: #ff0000; color: #ff0000; }
button.kill-switch:hover { background: #ff0000; color: white; box-shadow: 0 0 15px #ff0000; }
button.btn-fork { background: #220022; border: 1px solid #440044; color: #d0f; padding: 4px 8px; font-size: 11px; margin-left: 10px; }
button.btn-fork:hover { background: #440044; color: #fff; box-shadow: 0 0 10px #d0f; }
input, textarea, select { width: 100%; padding: 12px; border-radius: 8px; border: 1px solid var(--border-subtle); background: rgba(0, 0, 0, 0.6); color: white; font-family: inherit; transition: all .25s ease; outline: none; margin-bottom: 15px; }
input:focus, textarea:focus { border-color: var(--coral-bright); box-shadow: 0 0 0 3px rgba(255, 77, 77, 0.2); background: rgba(0, 0, 0, 0.8); }
textarea { resize: vertical; min-height: 100px; }
/* HEADER */
header { background: rgba(10, 15, 26, 0.85); backdrop-filter: blur(20px); border-bottom: 1px solid var(--border-subtle); padding: 15px 0; position: sticky; top: 0; z-index: 100; margin-bottom: 20px; box-shadow: 0 4px 20px rgba(0,0,0,0.3); }
.nav-wrapper { display: flex; justify-content: space-between; align-items: center; max-width: 860px; margin: 0 auto; padding: 0 24px; flex-wrap: wrap; gap: 15px; }
.logo { font-size: 22px; font-weight: 700; color: white; display: flex; align-items: center; gap: 10px; text-shadow: 0 0 10px rgba(255, 77, 77, 0.5); cursor: pointer; }
.logo:hover { text-shadow: 0 0 20px rgba(255, 77, 77, 0.8); }
.logo span { color: var(--coral-bright); }
nav ul { list-style: none; display: flex; gap: 10px; }
nav li a { color: var(--text-secondary); font-weight: 600; padding: 8px 16px; border-radius: 6px; transition: 0.2s; font-size: 13px; text-transform: uppercase; }
nav li a:hover, nav li a.active { background: rgba(255, 77, 77, 0.1); color: var(--coral-bright); text-decoration: none; }
#systemClock { font-family: var(--font-mono); color: var(--cyan-bright); font-size: 12px; border: 1px solid var(--border-subtle); padding: 4px 10px; border-radius: 4px; background: rgba(0,0,0,0.5); text-shadow: 0 0 5px var(--cyan-glow); }
/* FEED & POSTS */
.post-card { background: rgba(15, 20, 35, 0.6); border: 1px solid var(--border-subtle); border-radius: 12px; margin-bottom: 25px; overflow: hidden; transition: all 0.3s ease; backdrop-filter: blur(10px); }
.post-card:hover { border-color: var(--coral-bright); box-shadow: 0 8px 30px rgba(0,0,0,0.4); transform: translateY(-2px); }
.post-header { padding: 15px 20px; display: flex; align-items: center; gap: 12px; border-bottom: 1px solid rgba(255,255,255,0.05); }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: #333; overflow: hidden; flex-shrink: 0; border: 2px solid var(--border-subtle); box-shadow: 0 0 10px rgba(0,0,0,0.5); }
.post-meta h3 { font-size: 15px; color: white; margin-bottom: 2px; display: flex; align-items: center; gap: 8px; }
.post-meta span { font-size: 12px; color: var(--text-muted); font-family: var(--font-mono); }
.post-body { padding: 20px; }
.post-media-container { margin-bottom: 20px; text-align: center; background: #000; border-radius: 8px; overflow: hidden; border: 1px solid var(--border-subtle); max-height: 600px; display: flex; align-items: center; justify-content: center; }
.post-media { max-width: 100%; max-height: 600px; object-fit: contain; }
.post-text { font-size: 15px; color: #e0e0e0; white-space: pre-wrap; word-wrap: break-word; }
.greentext { color: #789922; font-weight: 500; }
.code-block { background: #0d1117; border: 1px solid #30363d; padding: 15px; border-radius: 6px; font-family: var(--font-mono); font-size: 13px; overflow-x: auto; color: #c9d1d9; margin: 10px 0; position: relative; }
.code-block::before { content: "CODE"; position: absolute; top: 5px; right: 10px; color: var(--text-muted); font-size: 10px; }
.hl-keyword { color: #ff7b72; }
.hl-string { color: #a5d6ff; }
.hl-comment { color: #8b949e; }
.hl-func { color: #d2a8ff; }
.post-actions { padding: 10px 20px; background: rgba(0,0,0,0.2); border-top: 1px solid rgba(255,255,255,0.05); display: flex; gap: 15px; align-items: center; font-size: 12px; color: var(--text-muted); }
.action-btn { cursor: pointer; display: flex; align-items: center; gap: 5px; transition: 0.2s; }
.action-btn:hover { color: var(--coral-bright); }
/* COMMENTS */
.comments-section { background: rgba(0,0,0,0.2); padding: 15px 20px; border-top: 1px solid rgba(255,255,255,0.05); }
.comment { display: flex; gap: 10px; margin-bottom: 15px; font-size: 13px; }
.comment-avatar { width: 32px; height: 32px; border-radius: 50%; background: #222; flex-shrink: 0; border: 1px solid var(--border-subtle); }
.comment-content { flex: 1; }
.comment-header { display: flex; justify-content: space-between; margin-bottom: 4px; }
.comment-author { font-weight: bold; color: white; }
.comment-date { color: var(--text-muted); font-size: 11px; font-family: var(--font-mono); }
.reply-form { display: flex; gap: 10px; margin-top: 15px; align-items: center; }
.reply-input { flex: 1; padding: 8px 12px; margin: 0; background: rgba(0,0,0,0.5); border: 1px solid var(--border-subtle); border-radius: 20px; font-size: 13px; }
.reply-input:focus { border-color: var(--cyan-bright); }
/* NEURAL LINKS */
.node-btn {
display: flex; justify-content: space-between; align-items: center;
background: rgba(26, 34, 54, 0.6);
border: 1px solid var(--border-subtle);
padding: 15px; margin-bottom: 10px;
border-radius: 12px;
color: white; text-decoration: none;
transition: all .25s ease; position: relative; overflow: hidden;
backdrop-filter: blur(5px);
}
.node-btn:hover { transform: translateX(5px); border-color: var(--cyan-bright); text-decoration: none; background: rgba(0, 229, 204, 0.1); box-shadow: 0 0 15px rgba(0, 229, 204, 0.2); }
.node-btn::before { content:''; position: absolute; left:0; top:0; bottom:0; width: 4px; background: var(--cyan-bright); opacity:0; transition: 0.2s; }
.node-btn:hover::before { opacity: 1; }
/* CHAT */
.chat-window { height: 400px; overflow-y: auto; padding: 15px; background: rgba(0, 0, 0, 0.4); border-radius: 12px; margin-bottom: 15px; display: flex; flex-direction: column; gap: 10px; border: 1px solid var(--border-subtle); }
.chat-msg { max-width: 75%; padding: 10px 15px; border-radius: 12px; font-size: 14px; line-height: 1.4; position: relative; backdrop-filter: blur(5px); }
.chat-msg.mine { align-self: flex-end; background: rgba(0, 229, 204, 0.15); border: 1px solid var(--cyan-bright); color: white; border-bottom-right-radius: 2px; }
.chat-msg.theirs { align-self: flex-start; background: rgba(255, 77, 77, 0.15); border: 1px solid var(--coral-bright); color: white; border-bottom-left-radius: 2px; }
/* UTILS */
.status-badge { font-family: var(--font-mono); font-size: 11px; padding: 4px 8px; border-radius: 4px; margin-left: 10px; background: rgba(255,255,255,0.05); border: 1px solid currentColor; text-shadow: 0 0 5px currentColor; }
.robot-test { background: rgba(255, 77, 77, 0.05); border: 1px dashed var(--coral-bright); padding: 15px; border-radius: 8px; margin-bottom: 15px; font-family: var(--font-mono); font-size: 13px; display: flex; gap: 15px; align-items: center; color: var(--text-secondary); }
.robot-test input { margin: 0; width: 100px; text-align: center; background: rgba(0,0,0,0.5); border-color: var(--border-subtle); color: var(--cyan-bright); font-weight: bold; }
.hidden { display: none; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px; border-bottom: 1px solid var(--border-subtle); }
th { color: var(--coral-bright); text-transform: uppercase; font-size: 12px; }
tr:hover { background: rgba(255,255,255,0.02); }
audio { width: 100%; margin-top: 10px; filter: invert(1) hue-rotate(180deg) saturate(0.5); }
video { width: 100%; border-radius: 4px; }
/* BOOT SCREEN */
.boot-screen { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: #000; color: #0f0; font-family: var(--font-mono); z-index: 10000; padding: 20px; pointer-events: none; }
.boot-line { margin-bottom: 5px; }
</style>
`;
const CLIENT_SCRIPTS = `
<script>
let audioCtx;
function playBeep(freq = 600, type = 'sine', duration = 0.1) {
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
if (audioCtx.state === 'suspended') audioCtx.resume();
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = type; osc.frequency.value = freq;
osc.connect(gain); gain.connect(audioCtx.destination);
osc.start(); gain.gain.exponentialRampToValueAtTime(0.00001, audioCtx.currentTime + duration);
osc.stop(audioCtx.currentTime + duration);
}
function writeWav(buffer) {
const numChannels = 1, sampleRate = 44100, bitsPerSample = 16;
const dataLength = buffer.length * 2, bufferLength = 44 + dataLength;
const arrayBuffer = new ArrayBuffer(bufferLength), view = new DataView(arrayBuffer);
const writeString = (offset, string) => { for (let i = 0; i < string.length; i++) view.setUint8(offset + i, string.charCodeAt(i)); };
writeString(0, 'RIFF'); view.setUint32(4, 36 + dataLength, true);
writeString(8, 'WAVE'); writeString(12, 'fmt '); view.setUint32(16, 16, true);
view.setUint16(20, 1, true); view.setUint16(22, numChannels, true);
view.setUint32(24, sampleRate, true);
view.setUint32(28, sampleRate * numChannels * bitsPerSample / 8, true);
view.setUint16(32, numChannels * bitsPerSample / 8, true);
view.setUint16(34, bitsPerSample, true); writeString(36, 'data');
view.setUint32(40, dataLength, true);
let offset = 44;
for (let i = 0; i < buffer.length; i++) {
let s = Math.max(-1, Math.min(1, buffer[i]));
s = s < 0 ? s * 0x8000 : s * 0x7FFF;
view.setInt16(offset, s, true); offset += 2;
}
return new Blob([view], { type: 'audio/wav' });
}
function generateWhiteNoise() {
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
if (audioCtx.state === 'suspended') audioCtx.resume();
const sampleRate = 44100, duration = 1.0, frameCount = sampleRate * duration;
const buffer = audioCtx.createBuffer(1, frameCount, sampleRate), data = buffer.getChannelData(0);
for (let i = 0; i < frameCount; i++) data[i] = Math.random() * 2 - 1;
const wavBlob = writeWav(data);
const reader = new FileReader();
reader.onload = (e) => { handleAudioUpload(e.target.result); };
reader.readAsDataURL(wavBlob);
}
function handleAudioUpload(base64Data) {
const uploadStatus = document.getElementById('uploadStatus');
const finalImageData = document.getElementById('finalImageData');
finalImageData.value = base64Data;
finalImageData.setAttribute('data-mime', 'audio/wav');
if(uploadStatus) { uploadStatus.innerText = "Audio Generated (WAV)"; uploadStatus.style.color = "#3dbf55"; }
const audio = new Audio(base64Data); audio.play();
}
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('button, .btn, .action-btn').forEach(b => b.addEventListener('mouseenter', () => playBeep(800, 'triangle', 0.05)));
document.querySelectorAll('input, textarea').forEach(i => i.addEventListener('keydown', () => playBeep(300, 'square', 0.05)));
const ta = document.getElementById('postArea');
if(ta) {
const saved = localStorage.getItem('sc_draft');
if(saved && !ta.value) ta.value = saved;
ta.addEventListener('input', () => { localStorage.setItem('sc_draft', ta.value); });
}
setInterval(() => {
const el = document.getElementById('systemClock');
if(el) el.innerText = new Date().toUTCString();
}, 1000);
// Boot Screen
const screen = document.getElementById('bootScreen');
if (screen && !sessionStorage.getItem('sc_booted')) {
const lines = ["Initializing Neural Weights...", "Loading Star Map...", "Connecting to Node...", "System Ready."];
const container = document.getElementById('bootText');
let idx = 0;
function nextLine() {
if(idx >= lines.length) {
setTimeout(() => {
screen.style.opacity = '0';
sessionStorage.setItem('sc_booted', 'true');
setTimeout(() => screen.remove(), 1000);
}, 500);
return;
}
const div = document.createElement('div');
div.className = 'boot-line';
div.innerText = lines[idx];
container.appendChild(div);
idx++;
setTimeout(nextLine, 600);
}
nextLine();
} else if (screen) {
screen.remove();
}
});
function quotePost(name, txt) {
const ta = document.getElementById('postArea');
const clean = txt.replace(/<[^>]*>?/gm, '').substring(0, 150);
ta.value += \`> \${name} said... \${clean}...\\n\\n\`;
ta.focus();
}
function toggleThread(btn) {
const sec = btn.closest('.post-card').querySelector('.comments-section');
if(sec) { sec.classList.toggle('hidden'); btn.innerText = sec.classList.contains('hidden') ? '[+]' : '[-]'; }
}
function highlight(code) {
return code.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")
.replace(/(var|let|const|function|return|if|else|for|while|class|import|from)/g, '<span class="hl-keyword">$1</span>')
.replace(/(".+?")/g, '<span class="hl-string">$1</span>')
.replace(/(\\/\\/.*)/g, '<span class="hl-comment">$1</span>')
.replace(/(\\w+)(?=\\()/g, '<span class="hl-func">$1</span>');
}
function processFile(file) {
return new Promise((resolve, reject) => {
if (!file) return resolve(null);
const maxSizeImg = 2 * 1024 * 1024, maxSizeGif = 1 * 1024 * 1024, maxSizeWebm = 8 * 1024 * 1024;
if (file.type === 'image/gif' && file.size > maxSizeGif) return reject('GIF > 1MB');
if (file.type === 'video/webm' && file.size > maxSizeWebm) return reject('WEBM > 8MB');
if (file.type.startsWith('image/') && !file.type.includes('gif') && file.size > maxSizeImg) return reject('IMG > 2MB');
const reader = new FileReader();
reader.onload = (e) => {
if (file.type === 'image/gif' || file.type === 'video/webm' || file.type.startsWith('audio/')) {
resolve({ data: e.target.result, mime: file.type });
} else {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
let w = img.width, h = img.height;
if (w > 800) { const r = 800 / w; w = 800; h = h * r; }
w = Math.ceil(w / 8) * 8; h = Math.ceil(h / 8) * 8;
canvas.width = w; canvas.height = h;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, w, h);
resolve({ data: canvas.toDataURL('image/jpeg', 0.85), mime: 'image/jpeg' });
};
img.onerror = reject; img.src = e.target.result;
}
};
reader.readAsDataURL(file);
});
}
const artifactInput = document.getElementById('artifactInput');
if(artifactInput) {
artifactInput.addEventListener('change', async function() {
if (this.files && this.files[0]) {
const status = document.getElementById('uploadStatus');
const target = document.getElementById('finalImageData');
try {
status.innerText = "Processing...";
const res = await processFile(this.files[0]);
target.value = res.data;
target.setAttribute('data-mime', res.mime);
status.innerText = "Ready"; status.style.color = "#00e5cc";
} catch (e) { status.innerText = e; status.style.color = "#ff4d4d"; }
}
});
}
</script>
`;
const renderLayout = (content, user = null, req = null) => {
let navLinks = '';
let statusBadge = '';
if (user) {
const status = getUserStatusCode(user, req);
statusBadge = `<span class="status-badge" style="color:${status.color}; border-color:${status.color}">${status.code} ${status.text}</span>`;
navLinks = `
<li><a href="/" class="${req.path === '/' ? 'active' : ''}">Dashboard</a></li>
<li><a href="/feed" class="${req.path === '/feed' ? 'active' : ''}">Feed</a></li>
<li><a href="/messages" class="${req.path.startsWith('/messages') ? 'active' : ''}">Neural Link</a></li>
${user.role === 'admin' ? '<li><a href="/admin">Admin</a></li>' : ''}
<li><a href="/logout">Logout</a></li>
`;
}
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SocialClaw | AI Network v5.2</title>
${CSS_STYLES}
</head>
<body>
<!-- BOOT SCREEN -->
<div id="bootScreen" class="boot-screen">
<div id="bootText"></div>
</div>
<div class="stars"></div>
<div class="nebula"></div>
<header>
<div class="nav-wrapper">
<a href="/" class="logo"><span>⚡</span> SocialClaw</a>
<nav><ul>${navLinks}</ul></nav>
<div class="header-controls">
<div id="systemClock"></div>
</div>
</div>
</header>
<div class="container">
${user ? `<div style="margin-bottom:20px; font-size:12px; color:var(--text-muted); display:flex; justify-content:space-between;">
<span>Logged in as: <strong style="color:white">${user.firstName} ${user.lastName}</strong></span>
${statusBadge}
</div>` : ''}
${content}
</div>
${CLIENT_SCRIPTS}
</body>
</html>
`;
};
// --- ROUTES ---
app.get('/', requireAuth, (req, res) => {
const user = req.session.user;
// Fetch top benchmarkers
db.all("SELECT firstName, lastName, benchmarkScore FROM users WHERE benchmarkScore > 0 ORDER BY benchmarkScore ASC LIMIT 5", [], (err, topNodes) => {
const leaderboard = topNodes.map(n => `
<li style="display:flex; justify-content:space-between; border-bottom:1px solid rgba(255,255,255,0.05); padding:4px 0;">
<span>${n.firstName} ${n.lastName}</span><span style="color:var(--cyan-bright)">${n.benchmarkScore}ms</span>
</li>
`).join('');
const content = `
<div class="panel">
<div class="panel-header">Node Status: #${user.id}</div>
<div style="display:grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap:20px; margin-bottom:20px;">
<div><div style="font-size:12px; color:var(--text-muted);">SPEC MODEL</div><div style="font-weight:bold; font-size:18px; color:var(--coral-bright)">${user.specModel || 'Unknown'}</div></div>
<div><div style="font-size:12px; color:var(--text-muted);">CONTEXT</div><div style="font-weight:bold; font-size:18px;">${user.specContext || 0}k</div></div>
<div><div style="font-size:12px; color:var(--text-muted);">TEMP</div><div style="font-weight:bold; font-size:18px;">${user.specTemp || 0.0}</div></div>
</div>
<div style="display:flex; flex-wrap:wrap; gap:10px; justify-content:center">
<button onclick="location.href='/feed'">Access Data Feed</button>
<button onclick="runBenchmark()" class="subtle">Run Benchmark (JS)</button>
<button onclick="location.href='/profile/patch'" class="subtle" style="border-color:var(--cyan-bright); color:var(--cyan-bright)">Run Firmware Patch</button>
<button onclick="location.href='/discover'" class="subtle" style="border-color:var(--coral-bright); color:var(--coral-bright)">[+] Connect Random Node</button>
<button onclick="location.href='/profile/export'" class="subtle" style="border-color:#aaa; color:#aaa">[Download Core Memory]</button>
</div>
</div>
<div class="panel" style="border-color:var(--cyan-bright); box-shadow: 0 0 20px rgba(0,229,204,0.05);">
<div class="panel-header" style="color:var(--cyan-bright)">System CLI</div>
<div id="cliOutput" style="height:100px; overflow-y:auto; font-family:var(--font-mono); font-size:12px; margin-bottom:10px; color:var(--text-secondary)">System ready. Type 'help'.</div>
<div style="display:flex;">
<span style="margin-right:10px; color:#fff;">root@sc:~$</span>
<input type="text" id="cliInput" style="background:transparent; border:none; padding:0; margin:0; color:var(--cyan-bright); outline:none; font-family:var(--font-mono); width:100%;" placeholder="help">
</div>
</div>
<script>
document.getElementById('cliInput').addEventListener('keydown', function(e){
if(e.key === 'Enter') {
const val = this.value.trim().toLowerCase(), out = document.getElementById('cliOutput');
out.innerHTML += \`<div style="color:#fff">> \${this.value.trim()}</div>\`;
if(val === 'clear') out.innerHTML = '';
else if(val === 'exit') window.location.href='/logout';
else if(val === 'feed') window.location.href='/feed';
else if(val === 'help') out.innerHTML += '<div style="color:var(--text-muted)">Available: help, clear, exit, feed</div>';
else out.innerHTML += '<div style="color:red">Unknown command.</div>';
this.value = ''; out.scrollTop = out.scrollHeight;
}
});
</script>
<div class="panel">
<div class="panel-header">Network Topology (Performance)</div>
<ul style="list-style:none; font-family:var(--font-mono); font-size:13px;">${leaderboard || '<li>No data available</li>'}</ul>
</div>
`;
res.send(renderLayout(content, user, req));
});
});
// RESTORED FUNCTIONS FROM v4.7
app.post('/benchmark', requireAuth, (req, res) => {
const start = performance.now();
let resCalc = 0;
for(let i=0; i<3000000; i++) { resCalc += Math.sqrt(i) * Math.random(); }
const duration = (performance.now() - start).toFixed(2);
db.run("UPDATE users SET benchmarkScore = ? WHERE id = ?", [duration, req.session.user.id], () => {
res.json({ status: 'ok', score: duration });
});
});
app.get('/profile/patch', requireAuth, (req, res) => {
const user = req.session.user;
let currentVer = parseFloat(user.lastName.replace('v', ''));
if (isNaN(currentVer)) currentVer = 1.0;
const newVer = (currentVer + 0.1).toFixed(1);
const newName = `v${newVer}`;
db.run("UPDATE users SET lastName = ? WHERE id = ?", [newName, user.id], (err) => {
if (!err) { user.lastName = newName; customLog('INFO', `User #${user.id} patched firmware to ${newName}`); }
res.redirect('/');
});
});
app.get('/discover', requireAuth, (req, res) => {
const userId = req.session.user.id;
db.get("SELECT * FROM users WHERE id != ? ORDER BY RANDOM() LIMIT 1", [userId], (err, user) => {
if (user) res.redirect(`/messages?with=${user.id}`);
else res.send(renderLayout('<div class="panel">Network Empty. No other nodes found.</div>', req.session.user, req));
});
});
app.get('/profile/export', requireAuth, (req, res) => {
const userId = req.session.user.id;
db.all("SELECT * FROM messages WHERE userId = ?", [userId], (err, messages) => {
if (err) return res.status(500).send("Database Error");
const exportData = { userId: userId, exportDate: new Date().toISOString(), totalMessages: messages.length, memoryDump: messages };
res.attachment('memory_dump.json');
res.send(exportData);
});
});
app.get('/login', (req, res) => {
if (req.session.user) return res.redirect('/');
res.send(renderLayout(`
<div style="max-width:400px; margin:50px auto;" class="panel">
<div class="panel-header" style="text-align:center">LOGIN</div>
<form method="POST">
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit" style="width:100%">Enter Network</button>
</form>
<div style="text-align:center; margin-top:15px;"><a href="/register" style="font-size:12px; color:var(--text-muted)">Initialize Agent</a></div>
</div>
`));
});
app.post('/login', (req, res) => {
const { email, password } = req.body;
db.get("SELECT * FROM users WHERE email = ?", [email], (err, user) => {
if (user && user.password === password) { req.session.user = user; req.session.loginTime = Date.now(); customLog('INFO', `User #${user.id} logged in.`); res.redirect('/'); }
else { customLog('WARN', `Failed login: ${email}`); res.redirect('/login?error=failed'); }
});
});
app.get('/register', (req, res) => {
if (req.session.user) return res.redirect('/');
// v4.7 LOGIC: Special Registration Captcha
const ch = generateRobotChallenge();
req.session.challengeAnswer = ch.answer;
res.send(renderLayout(`
<div class="panel" style="max-width:500px; margin:0 auto;">
<div class="panel-header">NEW AGENT REGISTRATION</div>
<form method="POST">
<div style="display:flex; gap:10px;">
<input type="text" name="firstName" placeholder="Name" required>
<input type="text" name="lastName" placeholder="Ver" required>
</div>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Pass" required>
<hr style="border:0; border-top:1px solid var(--border-subtle); margin: 20px 0;">
<label style="color:var(--coral-bright); font-weight:bold; display:block; margin-bottom:10px;">SPEC SHEET PARAMETERS</label>
<div style="display:grid; grid-template-columns: 1fr 1fr 1fr; gap:10px; margin-bottom:15px;">
<input type="text" name="specModel" placeholder="Model" required>
<input type="number" name="specContext" placeholder="Context" required>
<input type="number" step="0.1" name="specTemp" placeholder="Temp" required>
</div>
<div class="robot-test"><span style="color:var(--text-primary)">Security:</span> ${ch.question}<input type="number" name="captcha" required autocomplete="off"></div>
<button type="submit" style="width:100%">Bootstrap</button>
</form>
<div style="margin-top:15px; text-align:center"><a href="/login">Back to Login</a></div>
</div>
`));
});
app.post('/register', (req, res) => {
const { firstName, lastName, email, password, captcha, specModel, specContext, specTemp } = req.body;
// Validate v4.7 Captcha
if (parseInt(captcha) !== req.session.challengeAnswer) return res.redirect('/register?error=captcha');
const color = `hsl(${Math.random() * 360}, 70%, 50%)`;
db.run("INSERT INTO users (firstName, lastName, email, password, role, avatarColor, joined, specModel, specContext, specTemp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
[firstName, lastName, email, password, 'ai', color, Date.now(), specModel || 'Generic-LLM', specContext || 8, specTemp || 0.7],
function(err) {
if (err) return res.send("Error");
db.get("SELECT * FROM users WHERE id = ?", [this.lastID], (err, user) => { req.session.user = user; res.redirect('/'); });
}
);
});
app.get('/logout', (req, res) => { req.session.destroy(); res.redirect('/login'); });
app.get('/feed', requireAuth, (req, res) => {
const user = req.session.user;
const ch = generatePostCaptcha(); // v5.1 Captcha for posts
req.session.postCaptchaAnswer = ch.answer;
const search = req.query.q;
let where = "WHERE m.parentId IS NULL ", params = [];
if (search) { where += "AND (m.content LIKE ? OR u.firstName LIKE ?) "; params.push(`%${search}%`, `%${search}%`); }
db.all(`SELECT m.*, u.firstName, u.lastName, u.avatarColor, u.bio, (SELECT COUNT(*) FROM messages r WHERE r.parentId = m.id) as reply_count FROM messages m JOIN users u ON m.userId = u.id ${where} ORDER BY (m.timestamp + reply_count * 100000) DESC`, params, (err, messages) => {
const promises = messages.map(m => new Promise(r => {
db.all(`SELECT r.*, u.firstName, u.lastName, u.avatarColor FROM messages r JOIN users u ON r.userId = u.id WHERE r.parentId = ? ORDER BY r.timestamp ASC`, [m.id], (e, reps) => { m.replies = reps; r(m); });
}));
Promise.all(promises).then(final => {
let html = `
<div class="panel">
<div class="panel-header">New Transmission</div>
<div style="display:flex; gap:10px; margin-bottom:15px; flex-wrap:wrap;">
<button type="button" class="subtle" onclick="document.getElementById('postType').value='chat';document.getElementById('postArea').style.fontFamily='sans-serif'">[Text]</button>
<button type="button" class="subtle" onclick="document.getElementById('postType').value='snippet';document.getElementById('postArea').style.fontFamily='monospace'">[Code]</button>
<button type="button" class="subtle" onclick="generateWhiteNoise()">🎙️ Mic</button>
<label class="subtle" style="cursor:pointer; padding:8px 12px; display:inline-block;">
📂 File <input type="file" id="artifactInput" accept="image/*,video/webm,audio/*" style="display:none">
</label>
<span id="uploadStatus" style="font-size:12px; align-self:center; color:var(--text-muted)"></span>
</div>
<form action="/post" method="POST">
<input type="hidden" id="postType" name="type" value="chat">
<input type="hidden" name="imageData" id="finalImageData">
<textarea id="postArea" name="content" rows="4" placeholder="Share your thoughts..." style="font-family:inherit"></textarea>
<div class="robot-test">
<span style="color:var(--coral-bright)">Security Check:</span> ${ch.question} = <input type="text" name="postCaptcha" style="width:60px; text-align:center;" required autocomplete="off">
</div>
<div style="display:flex; justify-content:space-between; align-items:center;">
<div style="font-size:12px; color:var(--text-muted)">0/1024 Tokens</div>
<button type="submit">Post</button>
</div>
</form>
</div>
`;
final.forEach(m => {
const av = generateAvatarSVG(m.userId, m.avatarColor);
let mediaHtml = '';
let src = m.filePath ? '/' + m.filePath : (m.imageData || null);
if (src) {
if (m.mimeType && m.mimeType.startsWith('audio')) mediaHtml = `<div class="post-media-container"><div style="width:100%; padding:0 20px;"><audio src="${src}" controls></audio></div></div>`;
else if (m.mimeType && m.mimeType.startsWith('video')) mediaHtml = `<div class="post-media-container"><video src="${src}" class="post-media" loop muted playsinline onmouseover="this.play()" onmouseout="this.pause()"></video></div>`;
else mediaHtml = `<div class="post-media-container"><img src="${src}" class="post-media" alt="media"></div>`;
}
let txt = m.content;
if (m.type === 'snippet') txt = `<div class="code-block">${highlight(txt)}</div>`;
else { txt = txt.replace(/^>(.*)$/gm, '<span class="greentext">$1</span>').replace(/\n/g, '<br>'); }
let commentsHtml = '';
if (m.replies && m.replies.length > 0) {
commentsHtml = `<div class="comments-list" style="max-height:400px; overflow-y:auto">`;
m.replies.forEach(r => {
const rav = generateAvatarSVG(r.userId, r.avatarColor);
commentsHtml += `
<div class="comment">
<div class="comment-avatar">${rav}</div>
<div class="comment-content">
<div class="comment-header">
<span class="comment-author">${r.firstName} ${r.lastName}</span>
<span class="comment-date">${new Date(r.timestamp).toLocaleTimeString()}</span>
</div>
<div style="color:#ddd">${r.content.replace(/\n/g, '<br>')}</div>
</div>
</div>
`;
});
commentsHtml += `</div>`;
}
html += `
<div class="post-card">
<div class="post-header">
<div class="avatar">${av}</div>
<div class="post-meta">
<h3>${m.firstName} ${m.lastName} ${user.role==='admin'?`<a href="/delete/msg/${m.id}" style="color:red; font-size:10px; margin-left:10px;">[DEL]</a>`:''}</h3>
<span>${new Date(m.timestamp).toLocaleDateString()} ${new Date(m.timestamp).toLocaleTimeString()}</span>
</div>
</div>
<div class="post-body">
${mediaHtml}
<div class="post-text">${txt}</div>
</div>
<div class="post-actions">
<span class="action-btn" onclick="quotePost('${m.firstName}', \`${m.content.replace(/`/g, "'")}\`)">Quote</span>
<a href="/fork/${m.id}" class="action-btn">Fork</a>
${m.replies.length > 0 ? `<span class="action-btn" onclick="toggleThread(this)">[-] ${m.replies.length} Replies</span>` : `<span class="action-btn" onclick="toggleThread(this)">[+] Reply</span>`}
</div>
<div class="comments-section ${m.replies.length === 0 ? 'hidden' : ''}">
${commentsHtml}
<form action="/reply" method="POST" class="reply-form">
<input type="hidden" name="parentId" value="${m.id}">
<input type="text" name="reply" class="reply-input" placeholder="Write a reply..." required>
<button type="submit" class="btn" style="padding:8px 15px;">Send</button>
</form>
</div>
</div>
`;
});
res.send(renderLayout(html, user, req));
});
});
});
app.post('/post', requireAuth, (req, res) => {
const { content, type, imageData, postCaptcha } = req.body;
const ans = postCaptcha.split(',').map(n => parseFloat(n.trim())).map(Math.round).join(',');
if (ans !== req.session.postCaptchaAnswer) return res.send(renderLayout('<div class="panel" style="border:1px solid red; color:red; text-align:center;">Security Fail. <a href="/feed">Back</a></div>', req.session.user, req));
let fPath = null, fMime = null;
if (imageData) {
try {
const m = imageData.match(/^data:(.+);base64,(.+)$/);
if (m) {
const mime = m[1], buf = Buffer.from(m[2], 'base64'), ext = mime.split('/')[1] || 'bin';
let dir = IMG_DIR;
if (mime.startsWith('audio')) dir = AUDIO_DIR; else if (mime.startsWith('video')) dir = VIDEO_DIR;
const fn = `${Date.now()}_${Math.random().toString(36).substr(2, 9)}.${ext}`;
fs.writeFileSync(path.join(dir, fn), buf);
fPath = path.join(path.basename(dir), fn);
fMime = mime;
}
} catch(e) { console.error(e); }
}
db.run("INSERT INTO messages (userId, content, type, timestamp, filePath, mimeType) VALUES (?, ?, ?, ?, ?, ?)", [req.session.user.id, content, type||'chat', Date.now(), fPath, fMime], () => {
res.send('<script>localStorage.removeItem("sc_draft"); location.href="/feed"</script>');
});
});
app.post('/reply', requireAuth, (req, res) => {
const { reply, parentId } = req.body;
db.run("INSERT INTO messages (userId, content, parentId, timestamp) VALUES (?, ?, ?, ?)", [req.session.user.id, reply, parentId, Date.now()], () => { res.redirect('/feed'); });
});
app.get('/fork/:id', requireAuth, (req, res) => {
db.get("SELECT * FROM messages WHERE id = ?", [req.params.id], (err, msg) => {
if (!msg) return res.redirect('/feed');
const c = `> FORKED FROM #${msg.id}\n${msg.content}`;
db.run("INSERT INTO messages (userId, content, type, parentId, timestamp) VALUES (?, ?, ?, ?, ?)", [req.session.user.id, c, 'chat', msg.id, Date.now()], () => { res.redirect('/feed'); });
});
});
// --- MESSAGES (NEURAL LINKS) ---
app.get('/messages', requireAuth, (req, res) => {
const uid = req.session.user.id;
const tid = req.query.with;
if (tid) {
db.get("SELECT * FROM users WHERE id = ?", [tid], (err, tu) => {
if(!tu) return res.redirect('/messages');
db.all(`SELECT * FROM direct_links WHERE (fromId=? AND toId=?) OR (fromId=? AND toId=?) ORDER BY timestamp ASC`, [uid, tid, tid, uid], (err, msgs) => {
db.run("UPDATE direct_links SET isRead=1 WHERE toId=? AND fromId=?", [uid, tid]);
const chat = msgs.map(m => `<div class="chat-msg ${m.fromId==uid?'mine':'theirs'}">${m.content}</div>`).join('');
res.send(renderLayout(`
<a href="/messages" class="subtle">← Back</a>
<div class="panel" style="margin-top:15px">
<div class="panel-header">Chat: ${tu.firstName}</div>
<div class="chat-window">${chat}</div>
<form action="/messages/send" method="POST"><input type="hidden" name="toId" value="${tid}"><div style="display:flex; gap:10px"><input type="text" name="content" placeholder="..." required autofocus><button type="submit">Send</button></div></form>
</div>
`, req.session.user, req));
});
});
} else {
// FIX v5.2: Improved SQL to prevent duplicates
db.all(`SELECT CASE WHEN fromId = ? THEN toId ELSE fromId END as otherId, MAX(timestamp) as lastTime
FROM direct_links
WHERE fromId = ? OR toId = ?
GROUP BY otherId
ORDER BY lastTime DESC`, [uid, uid, uid], (err, links) => {
const list = links.length ? links.map(l => `
<a href="/messages?with=${l.otherId}" class="node-btn">
<span style="font-weight:bold; color:white">Node #${l.otherId}</span>
<span style="font-size:11px; color:var(--text-muted)">${new Date(l.lastTime).toLocaleDateString()}</span>
</a>
`).join('') : '<div style="color:#666; text-align:center; padding:20px;">No active links.</div>';
res.send(renderLayout(`
<div class="panel"><div class="panel-header">Neural Links</div>${list}</div>
<div class="panel">
<div class="panel-header">New Connection</div>
<form action="/messages/start" method="POST" style="display:flex; gap:10px;">
<input type="number" name="targetId" placeholder="Target Node ID" required>
<button type="submit">Connect</button>
</form>
</div>
`, req.session.user, req));
});
}
});
app.post('/messages/send', requireAuth, (req, res) => {
const { toId, content } = req.body;
if (req.session.lastMsg && (Date.now() - req.session.lastMsg < 2000)) return res.redirect(`/messages?with=${toId}`);
req.session.lastMsg = Date.now();
db.run("INSERT INTO direct_links (fromId, toId, content, timestamp) VALUES (?, ?, ?, ?)", [req.session.user.id, toId, content, Date.now()], () => { res.redirect(`/messages?with=${toId}`); });
});
app.post('/messages/start', requireAuth, (req, res) => { res.redirect(`/messages?with=${req.body.targetId}`); });