Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
1224 lines (1199 loc) · 75.9 KB
/
Copy pathapp.js
File metadata and controls
1224 lines (1199 loc) · 75.9 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
import { uploadRelay } from './storage-upload.js';
import { recordingOptions } from './video-media.js';
import { sb, SUPABASE_URL, SUPABASE_KEY, SNAP_BUCKET, $, $$, el, esc, app, toast, ago, initial, avatarHTML, isMediaUrl, icon,
safeMediaUrl, state, presenceUsers, isOnline, setFriendActivity, activityText, processImage, processCanvas, processVideo, makeStoryPreview, makeRelayMedia, makeAvatar,
idb, dataUrlToBytes } from './core.js';
import { db } from './db.js';
import { initPush, registerSW, enablePush, disablePush, pushPreference, pushRegistrationActive, releasePushForCurrentUser } from './push.js';
import { startRtc, fetchSnap } from './rtc.js';
import { loadOrCreateKeys, encryptSharedRelay, wrapSharedRelayKey } from './crypto.js';
import { FILTERS, drawFiltered, filterImageBlob } from './filters.js';
import { renderConvs, openConversation, onIncomingDM, onIncomingCall, detachAll, chatUnread, reconnectOpenChat, onMessageInsert, onSnapInsert, noteSentSnap, markSnapDelivered, markSnapOpened, markSnapRemoved, markMessageDelivered, sendStoryReply, sendClipShare, bootChat, syncMessages, clearAllLocalConversations, receiveRelayTransfer, syncTransfers, callCapture, flipCallCamera } from './chat.js';
import { openGroupById, createGroupFlow, onIncomingGroupCall, onIncomingGroupData, renderGroupList, closeCurrentGroup, bootGroups, sendSnapToGroupChat, clearAllGroupConversations } from './groups.js';
import { viewClips, closeClips } from './clips.js';
import { saveMemory, viewMemories, closeMemories } from './memories.js';
const uuid = () => (crypto.randomUUID ? crypto.randomUUID() : (Date.now() + '-' + Math.random().toString(16).slice(2)));
const RELAY_LIMIT = 10; // outstanding encrypted relay items allowed per sender/recipient pair
const RELAY_TTL_DAYS = 7; // an offline snap self-destructs a week after it's sent if never opened
// The database RPC below independently verifies this immutable auth-user ID.
// This client check only controls whether the management UI is shown.
const MAYFLY_ADMIN_ID = '2f43626a-3056-402d-9daf-b0de5193a2f8';
window.addEventListener('unhandledrejection', (e) => console.error('[mayfly] unhandled rejection:', e.reason));
// Mobile browser chrome can change the visible viewport while a thread is being
// pulled or scrolled. Drive chat layout from VisualViewport so its composer stays
// inside the actually visible area rather than the larger layout viewport.
const syncVisualViewport = () => {
const h = window.visualViewport?.height || window.innerHeight;
document.documentElement.style.setProperty('--app-vh', Math.round(h) + 'px');
};
syncVisualViewport();
window.addEventListener('resize', syncVisualViewport);
window.visualViewport?.addEventListener('resize', syncVisualViewport);
window.visualViewport?.addEventListener('scroll', syncVisualViewport);
// ===================== presence =====================
let presenceCh = null, activityCh = null;
let activityTimer = null, lastActivityWrite = 0;
const touchActivity = (force = false) => { if(!state.me||(!force&&Date.now()-lastActivityWrite<55_000))return;lastActivityWrite=Date.now();db.touchActivity().then(()=>{},()=>{}); };
const refreshFriendActivity = async (rerender=false) => { const {data}=await db.friendActivity();setFriendActivity(data||[]);window.dispatchEvent(new CustomEvent('mf-activity-updated'));if(rerender&&$('#friendlist'))renderFriends(); };
const startPresence = () => {
presenceCh = sb.channel('mayfly-presence', { config: { presence: { key: state.me.id } } });
presenceCh.on('presence', { event: 'sync' }, () => {
const st = presenceCh.presenceState();
for (const k in presenceUsers) delete presenceUsers[k];
for (const key in st) for (const m of st[key]) if (m.user_id) presenceUsers[m.user_id] = { username: m.username };
if ($('#friendlist')) renderFriends(); // refresh online dots
reconnectOpenChat(); // connect an open chat once the friend comes online
});
presenceCh.subscribe(async (status) => {
if (status === 'SUBSCRIBED') { await presenceCh.track({ user_id: state.me.id, username: state.profile.username });touchActivity(true);refreshFriendActivity().then(()=>{},()=>{}); }
});
clearInterval(activityTimer);activityTimer=setInterval(()=>touchActivity(),60_000);
activityCh=sb.channel('mayfly-friend-activity').on('postgres_changes',{event:'*',schema:'public',table:'mf_user_activity'},()=>refreshFriendActivity(true)).subscribe();
};
// A service worker cannot hold a Realtime websocket open while the operating
// system freezes a background PWA. It can, however, wake the page with a push.
// Rebuild the shared socket after a longer suspension so presence and signaling
// recover immediately instead of waiting for the next heartbeat timeout.
let backgroundedAt = 0, lastRealtimeRecovery = 0, recoveryTimer = null;
const recoverRealtime = async (forceSocket = false) => {
if (!state.me || Date.now() - lastRealtimeRecovery < 1500) return;
lastRealtimeRecovery = Date.now();
try {
const connected = sb.realtime.isConnected?.() ?? true;
if (forceSocket || !connected) {
await sb.realtime.disconnect();
sb.realtime.connect();
}
// Channels rejoin automatically with the shared socket. Re-track as soon
// as possible as well; this is harmless when the presence channel is
// already joined and shortens the stale-offline window after a resume.
if (presenceCh?.state === 'joined') {
await presenceCh.track({ user_id: state.me.id, username: state.profile?.username });
}
db.touchDevice().then(() => {}, () => {});
touchActivity();refreshFriendActivity().then(()=>{},()=>{});
syncMessages().then(setChatDot, () => {});
syncTransfers().then(setChatDot, () => {});
reconnectOpenChat();
clearTimeout(recoveryTimer);
recoveryTimer = setTimeout(() => {
if (presenceCh?.state === 'joined' && state.me) {
presenceCh.track({ user_id: state.me.id, username: state.profile?.username }).catch(() => {});
}
}, 1200);
} catch (e) {}
};
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') { backgroundedAt = Date.now();touchActivity(); }
else recoverRealtime(backgroundedAt > 0 && Date.now() - backgroundedAt > 30_000);
}, { passive: true });
window.addEventListener('pageshow', (event) => recoverRealtime(!!event.persisted), { passive: true });
window.addEventListener('online', () => recoverRealtime(true), { passive: true });
window.addEventListener('focus', () => recoverRealtime(false), { passive: true });
// Chrome emits these Page Lifecycle events around tab freezing. Other browsers
// simply ignore them and use visibility/pageshow above.
document.addEventListener('freeze', () => { backgroundedAt = Date.now(); }, { passive: true });
document.addEventListener('resume', () => recoverRealtime(true), { passive: true });
navigator.serviceWorker?.addEventListener('message', (event) => {
if (event.data?.type === 'mf-wake') recoverRealtime(true);
});
// ===================== camera-first capture =====================
let stream = null, facing = 'user', borrowedCamera = false, cameraRequest = 0;
let cameraInputCleanup = () => {};
const stopStream = () => { cameraRequest++; if (stream && !borrowedCamera) stream.getTracks().forEach(t => t.stop()); stream = null; borrowedCamera = false; };
window.addEventListener('call-media-acquiring', stopStream);
window.addEventListener('call-media-changed', () => {
if (!$('#cam')) return;
const call = callCapture();
if (borrowedCamera && stream === call.stream) { facing = call.facing; return; }
if (!borrowedCamera && stream && call.active && !call.stream) return;
startCamera();
});
const startCamera = async () => {
const v = $('#cam'); if (!v) return;
stopStream();
const request = cameraRequest;
const call = callCapture();
if (call.stream) {
stream = call.stream; borrowedCamera = true; facing = call.facing;
v.srcObject = stream; v.play?.().catch(() => {});
$('#camerr').textContent = '';
return;
}
let acquired;
try {
// Chrome and Edge record the combined stream reliably as WebM (VP8 + Opus).
// If microphone permission is denied, preserve the working video-only fallback.
try {
acquired = await navigator.mediaDevices.getUserMedia({
video: { facingMode: facing },
audio: call.active ? false : { echoCancellation: true, noiseSuppression: true },
});
} catch (audioError) {
acquired = await navigator.mediaDevices.getUserMedia({ video: { facingMode: facing }, audio: false });
}
if (request !== cameraRequest || !v.isConnected) { acquired.getTracks().forEach(t => t.stop()); return; }
stream = acquired;
v.srcObject = stream; v.play?.().catch(() => {});
$('#camerr').textContent = '';
} catch (e) {
if (request !== cameraRequest || !v.isConnected) return;
$('#camerr').innerHTML = 'Camera unavailable. <b>Tap the photo icon</b> to pick from your gallery instead.';
}
};
const viewCamera = (defaultRecipientId = null, groupId = null) => {
cameraInputCleanup();
stopStream();
app.innerHTML = `<main class="camwrap">
<div class="viewport">
<video id="cam" autoplay playsinline muted disablepictureinpicture controlslist="nodownload noplaybackrate noremoteplayback"></video>
<canvas id="filterpreview" hidden aria-hidden="true"></canvas>
<div class="camerr" id="camerr"></div>
</div>
<select class="filterselect" id="filter" aria-label="Photo filter">${FILTERS.map(f => `<option value="${f.id}">${f.label}</option>`).join('')}</select>
<div class="cambar">
<button class="cbtn ghost" id="pick" title="From gallery" aria-label="Pick from gallery">🖼️</button>
<button class="shutter" id="shoot" aria-label="Take photo; hold to record video"></button>
<button class="cbtn ghost" id="flip" title="Flip camera" aria-label="Flip camera">🔄</button>
<input id="file" type="file" accept="image/*,video/*" hidden>
</div>
</main>`;
const finishShot = (shot) => compose(shot, defaultRecipientId, groupId);
let activeFilter = 'normal', previewFrame = null, previewLastDraw = 0;
const filter = $('#filter');
const preview = $('#filterpreview');
const drawLivePreview = (now) => {
const video = $('#cam');
if (activeFilter === 'normal' || !preview.isConnected) return;
if (!video?.videoWidth) { previewFrame = requestAnimationFrame(drawLivePreview); return; }
if (!previewLastDraw || now - previewLastDraw >= 180) {
const scale = Math.min(1, 640 / video.videoWidth);
preview.width = Math.max(1, Math.round(video.videoWidth * scale));
preview.height = Math.max(1, Math.round(video.videoHeight * scale));
const ctx = preview.getContext('2d');
if (facing === 'user') { ctx.translate(preview.width, 0); ctx.scale(-1, 1); }
drawFiltered(ctx, video, activeFilter);
previewLastDraw = now;
}
previewFrame = requestAnimationFrame(drawLivePreview);
};
const updateFilter = () => {
activeFilter = filter.value;
$('#cam').style.filter = 'none';
cancelAnimationFrame(previewFrame); previewFrame = null; previewLastDraw = 0;
preview.hidden = activeFilter === 'normal';
if (activeFilter !== 'normal') previewFrame = requestAnimationFrame(drawLivePreview);
};
filter.onchange = updateFilter;
$('#flip').onclick = () => { if (borrowedCamera) return flipCallCamera(); facing = facing === 'user' ? 'environment' : 'user'; startCamera(); };
$('#pick').onclick = () => $('#file').click();
$('#file').onchange = async () => {
const f = $('#file').files[0]; if (!f) return;
try {
if (f.type.startsWith('video/')) finishShot(await processVideo(f));
else finishShot(await processImage(await filterImageBlob(f, activeFilter)));
}
catch (e) { toast('Could not read that media.'); }
};
const shoot = $('#shoot');
shoot.onclick = null; // pointer handling below distinguishes a tap from a hold.
const takePhoto = async () => {
const v = $('#cam'); if (!v || !v.videoWidth) return toast('Camera not ready.');
const c = Object.assign(document.createElement('canvas'), { width: v.videoWidth, height: v.videoHeight });
const ctx = c.getContext('2d');
if (facing === 'user') { ctx.translate(c.width, 0); ctx.scale(-1, 1); }
drawFiltered(ctx, v, activeFilter);
try { finishShot(await processCanvas(c)); }
catch (e) { toast('Could not prepare that photo.'); }
};
const captureVideoPreview = () => {
const v = $('#cam'); if (!v?.videoWidth) return null;
const scale = 24 / Math.max(v.videoWidth, v.videoHeight);
const c = Object.assign(document.createElement('canvas'), {
width: Math.max(1, Math.round(v.videoWidth * scale)), height: Math.max(1, Math.round(v.videoHeight * scale)),
});
drawFiltered(c.getContext('2d'), v, activeFilter);
return c.toDataURL('image/jpeg', 0.5);
};
let holdTimer = null, recorder = null, longPress = false, preparingVideo = false;
const stopRecording = () => {
if (recorder?.state === 'recording') recorder.stop();
};
const startRecording = () => {
if (!stream || !window.MediaRecorder) return toast('Video recording is not available in this browser.');
if (activeFilter !== 'normal') toast('Filters apply to photos; video records unfiltered.');
const chunks = [];
const preview = captureVideoPreview();
if (recorder || preparingVideo) return;
const options = recordingOptions();
const captureMime = options?.mimeType || '';
try { recorder = new MediaRecorder(stream, options); }
catch (e) { return toast('Could not start video recording.'); }
const recording = recorder;
let discarded = false;
recording.onerror = () => { discarded = true; toast('Recording failed. Please try again.'); };
recorder.ondataavailable = (e) => { if (e.data?.size) chunks.push(e.data); };
recorder.onstop = async () => {
shoot.classList.remove('recording');
const blob = new Blob(chunks, { type: recording.mimeType || captureMime });
recorder = null;
if (discarded || !shoot.isConnected) return;
if (!blob.size) return toast('The recording was empty. Please try again.');
preparingVideo = true; shoot.disabled = true;
try {
const shot = await processVideo(blob);
if (preview) shot.preview = preview;
if (!shoot.isConnected) { URL.revokeObjectURL(shot.localPreviewUrl); return; }
finishShot(shot);
}
catch (e) { toast(e.message || 'Could not prepare that video.'); }
finally { preparingVideo = false; shoot.disabled = false; }
};
try { recording.start(250); shoot.classList.add('recording'); }
catch (e) { recorder = null; toast('Could not start video recording.'); }
};
shoot.onpointerdown = (e) => {
if (preparingVideo) return;
e.preventDefault(); shoot.setPointerCapture?.(e.pointerId); longPress = false;
holdTimer = setTimeout(() => { longPress = true; startRecording(); }, 300);
};
const releaseShutter = () => {
clearTimeout(holdTimer);
const wasLongPress = longPress; longPress = false;
if (wasLongPress) stopRecording(); else takePhoto();
};
shoot.onpointerup = releaseShutter;
shoot.onpointercancel = releaseShutter;
// Chrome on some Android devices exposes the hardware volume key as VolumeUp;
// other mobile browsers reserve it for system volume and never dispatch this event.
let volumeHeld = false;
const isVolumeUp = (e) => e.key === 'VolumeUp' || e.key === 'AudioVolumeUp';
const volumeDown = (e) => {
if (!isVolumeUp(e) || volumeHeld || preparingVideo) return;
e.preventDefault(); volumeHeld = true; longPress = false;
holdTimer = setTimeout(() => { longPress = true; startRecording(); }, 300);
};
const volumeUp = (e) => {
if (!isVolumeUp(e) || !volumeHeld) return;
e.preventDefault(); volumeHeld = false; releaseShutter();
};
document.addEventListener('keydown', volumeDown);
document.addEventListener('keyup', volumeUp);
cameraInputCleanup = () => {
document.removeEventListener('keydown', volumeDown);
document.removeEventListener('keyup', volumeUp);
clearTimeout(holdTimer);
if (recorder?.state === 'recording') recorder.stop();
cancelAnimationFrame(previewFrame);
};
startCamera();
};
// ===================== compose: caption, timer, choose friends, send =====================
// A Snap normally stays in the recipient's chat. A positive timer makes it view-once.
let timer = 0;
const compose = async (shot, defaultRecipientId = null, defaultGroupId = null, initialStory = false, initialCaption = '') => {
stopStream();
const isVideo = shot.mime?.startsWith('video/');
timer = 0;
// Preview the original Blob URL. `full` remains the encoded payload used for delivery.
const previewUrl = isVideo ? (shot.localPreviewUrl || shot.full) : shot.full;
const releasePreview = () => { if (shot.localPreviewUrl) URL.revokeObjectURL(shot.localPreviewUrl); };
app.innerHTML = `<main class="composewrap">
<div class="preview ${isVideo ? 'video' : ''}" ${isVideo ? '' : `style="background-image:url('${safeMediaUrl(previewUrl)}')"`}>
${isVideo ? `<video class="composevideo" src="${safeMediaUrl(previewUrl)}" autoplay muted loop playsinline></video>` : ''}
<input id="cap" class="capinput" value="${esc(initialCaption)}" placeholder="Add a caption…" maxlength="120" autocomplete="off">
<div class="timerpick"><button class="tchip ${timer === 0 ? 'on' : ''}" data-t="0">Keep</button>${[3, 5, 10].map(t => `<button class="tchip ${t === timer ? 'on' : ''}" data-t="${t}">${t}s</button>`).join('')}</div>
<button class="retake" id="retake" aria-label="Retake">✕</button>
</div>
<div class="sendrow">
<button class="pill" id="savememory" type="button">Save to Memories</button>
<div class="sendto">Send to…</div>
<input id="recipsearch" class="recipsearch" type="search" placeholder="Search friends" autocomplete="off" aria-label="Search friends">
<div id="recipmeta" class="recipmeta"></div>
<div id="recips" class="recips"><div class="spin">Loading friends…</div></div>
<button class="btn send" id="send" disabled>Send ▸</button>
</div>
</main>`;
$('#retake').onclick = () => { releasePreview(); viewCamera(defaultRecipientId); };
$('#savememory').onclick = async () => {
const button = $('#savememory'); button.disabled = true; button.textContent = 'Saving…';
try { await saveMemory(shot, $('#cap').value); button.textContent = 'Saved to Memories'; toast('Saved only in this browser.'); }
catch (e) { button.disabled = false; button.textContent = 'Save to Memories'; toast('Could not save that Memory.'); }
};
if (isVideo) {
const previewPlayer = $('.composevideo');
previewPlayer.play().then(() => {
previewPlayer.muted = false;
return previewPlayer.play();
}).catch(() => {
// Autoplay sound can be blocked by the browser; keep the visual preview playing.
previewPlayer.muted = true;
previewPlayer.play().catch(() => {});
});
}
$$('.tchip').forEach(b => b.onclick = () => { timer = +b.dataset.t; $$('.tchip').forEach(x => x.classList.toggle('on', x === b)); });
const chosen = new Set(), chosenGroups = new Set();
const RECENT_FRIEND_LIMIT = 50;
const ALL_FRIENDS_LIMIT = 100;
let toStory = initialStory, allFriends = false;
const send = $('#send');
const [{ data: friends }, { data: groups }] = await Promise.all([db.friends(), db.myGroups()]);
const box = $('#recips'); if (!box) return;
const list = (friends || []).map(f => {
const friend = otherOf(f);
return friend && { ...friend, friendSince: f.created_at };
}).filter(Boolean).sort((a, b) => new Date(b.friendSince) - new Date(a.friendSince));
const groupList = groups || [];
const selectedFriendIds = () => allFriends
? list.slice(0, ALL_FRIENDS_LIMIT).map(u => u.id)
: [...chosen];
// A selected group counts as one destination — the Snap goes to its chat, not to
// each member individually.
const refreshSend = () => {
const n = selectedFriendIds().length + chosenGroups.size + (toStory ? 1 : 0);
send.disabled = !n;
send.textContent = n ? `Send to ${n} ▸` : 'Send ▸';
};
// A Snap started from a chat or a friend row keeps that person selected.
if (list.some(u => u.id === defaultRecipientId)) chosen.add(defaultRecipientId);
if (groupList.some(g => g.id === defaultGroupId)) chosenGroups.add(defaultGroupId);
const search = $('#recipsearch');
const meta = $('#recipmeta');
const renderRecipients = () => {
const query = search.value.trim().toLowerCase();
const matches = query ? list.filter(u => u.username.toLowerCase().includes(query)) : list;
const visible = matches.slice(0, RECENT_FRIEND_LIMIT);
box.innerHTML = '';
// "My Story" — broadcast to all friends for 24h (always available)
const storyChip = el(`<button class="recip story ${toStory ? 'on' : ''}"><span class="ring">⚡</span><span>My Story</span></button>`);
storyChip.onclick = () => {
if (isVideo) return toast('Video snaps can be sent directly to friends, not to Stories yet.');
toStory = !toStory; renderRecipients(); refreshSend();
};
box.appendChild(storyChip);
groupList.forEach(g => {
const chip = el(`<button class="recip ${chosenGroups.has(g.id) ? 'on' : ''}"><span class="avatar">👥</span><span>${esc(g.name || 'Group')}</span></button>`);
chip.onclick = () => {
chosenGroups.has(g.id) ? chosenGroups.delete(g.id) : chosenGroups.add(g.id);
renderRecipients(); refreshSend();
};
box.appendChild(chip);
});
if (list.length) {
const allChip = el(`<button class="recip allfriends ${allFriends ? 'on' : ''}"><span class="avatar">👥</span><span>All friends${list.length > ALL_FRIENDS_LIMIT ? ` (first ${ALL_FRIENDS_LIMIT})` : ''}</span></button>`);
allChip.onclick = () => {
allFriends = !allFriends;
if (allFriends) chosen.clear();
renderRecipients(); refreshSend();
};
box.appendChild(allChip);
}
if (!list.length) box.appendChild(el(`<div class="empty" style="width:100%">No friends yet — <a href="#/friends">add some →</a> or just post to your Story.</div>`));
visible.forEach(u => {
const chip = el(`<button class="recip ${chosen.has(u.id) ? 'on' : ''}" data-uid="${u.id}">${avatarHTML(u.username, u.avatar)}<span>${esc(u.username)}</span>${isOnline(u.id) ? '<i class="dot"></i>' : ''}</button>`);
chip.onclick = () => {
allFriends = false;
chosen.has(u.id) ? chosen.delete(u.id) : chosen.add(u.id);
renderRecipients(); refreshSend();
};
box.appendChild(chip);
});
if (matches.length > RECENT_FRIEND_LIMIT) {
box.appendChild(el(`<div class="recipmore">Showing ${RECENT_FRIEND_LIMIT} of ${matches.length}${query ? ' matches' : ' recent friends'} — search to narrow the list.</div>`));
}
meta.textContent = allFriends && list.length > ALL_FRIENDS_LIMIT
? `All-friends sends are limited to your ${ALL_FRIENDS_LIMIT} most recent friends at a time.`
: (query ? `${matches.length} friend${matches.length === 1 ? '' : 's'} found` : `Showing your ${Math.min(RECENT_FRIEND_LIMIT, list.length)} most recent friends`);
};
search.oninput = renderRecipients;
renderRecipients();
refreshSend();
// Build a File from the captured snap for group P2P delivery.
const snapFile = async () => shot.rawBlob
? new File([shot.rawBlob], 'snap', { type: shot.mime || 'video/webm' })
: new File([await dataUrlToBytes(shot.full)], 'snap.jpg', { type: shot.mime || 'image/jpeg' });
send.onclick = async () => {
const directIds = selectedFriendIds();
if (allFriends && list.length > ALL_FRIENDS_LIMIT && !confirm(`Send this Snap to your ${ALL_FRIENDS_LIMIT} most recent friends? You have ${list.length} friends total, so the rest will not receive this one.`)) return;
send.disabled = true;
const caption = $('#cap').value.trim();
// Friends receive an individual Snap; groups receive it in their chat only.
let targets = list.filter(u => directIds.includes(u.id));
const selectedGroups = groupList.filter(g => chosenGroups.has(g.id));
// Video must remain downloadable after the sender closes or backgrounds the app.
const offlineTargets = targets.filter(u => isVideo || !isOnline(u.id));
targets = targets.filter(u => !isVideo && isOnline(u.id));
let ok = 0, blocked = 0, toomany = 0;
// A live P2P send that fails still owes the friend a snap, so it joins the relay batch.
const relayTargets = offlineTargets.slice();
if (toStory) { const s = await postStory(shot, caption); if (s) ok++; }
const SEND_BATCH_SIZE = 5;
let done = 0;
for (let i = 0; i < targets.length; i += SEND_BATCH_SIZE) {
const batch = targets.slice(i, i + SEND_BATCH_SIZE);
send.textContent = `Sending ${done + 1}–${Math.min(done + batch.length, targets.length)} of ${targets.length}…`;
const results = await Promise.all(batch.map(u => sendLiveSnap(shot, u, caption, timer)));
results.forEach((r, index) => {
if (r && r.id) { ok++; noteSentSnap(batch[index].id, r.id, r.kind); }
else if (r === 'cap') blocked++;
else if (r === 'toomany') toomany++;
else relayTargets.push(batch[index]);
});
done += batch.length;
}
if (relayTargets.length) {
send.textContent = `Preparing one encrypted relay for ${relayTargets.length} friend${relayTargets.length === 1 ? '' : 's'}...`;
const relay = await sendSharedRelay(shot, relayTargets, caption, timer, (sent,total) => { if (send.isConnected) send.textContent = `Uploading ${Math.round(sent / total * 100)}%…`; });
relay.sent.forEach(({ uid, id, kind }) => { ok++; noteSentSnap(uid, id, kind); });
blocked += relay.blocked; toomany += relay.toomany;
}
if (selectedGroups.length) {
const file = await snapFile();
for (const g of selectedGroups) {
send.textContent = `Sending to ${g.name || 'group'}…`;
if (await sendSnapToGroupChat(g.id, file, timer)) ok++;
}
}
// one toast wins (it replaces), so prefer the most useful message
if (toomany) toast(`You've hit ${RELAY_LIMIT} unopened offline snaps${ok ? ` · sent ${ok}` : ''}. Some couldn't be sent until they're opened or expire.`);
else if (ok) toast(`Sent 🐛`);
else if (blocked) toast('Some friends already have an unopened snap from you.');
if (!ok) { toast('This Snap was not sent. Please try again.'); refreshSend(); return; }
releasePreview();
location.hash = defaultRecipientId ? '#/c/' + defaultRecipientId : (defaultGroupId ? '#/group/' + defaultGroupId : '#/chats');
};
};
// Live recipients keep the original media in the sender's browser and pull it P2P.
const sendLiveSnap = async (shot, u, caption, secs) => {
const base = { sender_id: state.me.id, recipient_id: u.id, preview: shot.preview,
caption, w: shot.w, h: shot.h, timer: secs };
const kind = shot.mime?.startsWith('video/') ? 'video' : 'photo';
try {
if (!isOnline(u.id)) return false;
const { data } = await db.devicesForUser(u.id);
const devices = (data || []).length ? data : [{ id: null }];
const logicalId = uuid(), ids = devices.map(() => uuid());
await Promise.all(ids.map(id => idb.set('snap:' + id, shot.rawBlob || shot.full)));
const rows = devices.map((device, i) => ({ ...base, id: ids[i], logical_id: logicalId, sender_device_id: state.deviceId, recipient_device_id: device.id, delivery: `live:${encodeURIComponent(shot.mime || 'image/jpeg')}` }));
const results = await Promise.all(rows.map(row => db.addSnap(row)));
const failed = results.find(result => result.error);
if (failed) { await Promise.all(ids.map(id => idb.del('snap:' + id))); throw failed.error; }
db.bumpStreak(u.id).then(() => {}, () => {});
return { id: ids[0], kind };
} catch (e) { console.error('[mayfly] send failed', e); return false; }
};
// One encrypted media payload, with a small recipient-specific wrapped key per row.
const sendSharedRelay = async (shot, targets, caption, secs, onProgress = null) => {
const result = { sent: [], blocked: 0, toomany: 0 };
const checked = await Promise.all(targets.map(async (u) => {
const { count } = await db.pendingRelayTo(u.id);
if (count && count >= RELAY_LIMIT) return { u, ok: false, toomany: true };
const { data } = await db.devicesForUser(u.id);
const devices = (data || []).map(device => {
try { return { id: device.id, pubkey: JSON.parse(device.pubkey) }; } catch (e) { return null; }
}).filter(Boolean);
// Legacy recipients retain the account-key envelope until they register
// their first device with the multi-device release.
if (!devices.length) try { if (u.pubkey) devices.push({ id: null, pubkey: JSON.parse(u.pubkey) }); } catch (e) {}
return { u, ok: devices.length > 0, devices };
}));
const recipients = checked.filter(item => item.ok);
result.blocked = checked.filter(item => item.blocked).length;
result.toomany = checked.filter(item => item.toomany).length;
if (!recipients.length) return result;
const id = uuid();
const expires_at = new Date(Date.now() + RELAY_TTL_DAYS * 24 * 3600 * 1000).toISOString();
let payloadCreated = false;
try {
const source = shot.rawBlob || await fetch(shot.full).then(r => r.blob());
const { bytes, mime } = await makeRelayMedia(source);
const { ciphertext, content_iv, rawKey } = await encryptSharedRelay(bytes);
const { error: payloadError } = await db.addRelayPayload({ id, sender_id: state.me.id, content_iv, mime, expires_at });
if (payloadError) throw payloadError;
payloadCreated = true;
const upload = await uploadRelay({ sb, projectUrl: SUPABASE_URL, publishableKey: SUPABASE_KEY, bucket: SNAP_BUCKET, path: id, body: ciphertext, onProgress });
if (upload.error) throw upload.error;
const rows = await Promise.all(recipients.flatMap(({ u, devices }) => {
const logicalId = uuid();
return devices.map(async (device) => {
try {
const key = await wrapSharedRelayKey(device.pubkey, rawKey);
const snapId = uuid();
const { error } = await db.addSnap({
id: snapId, sender_id: state.me.id, recipient_id: u.id,
preview: shot.preview, caption, w: shot.w, h: shot.h, timer: secs,
delivery: `relay-shared:${encodeURIComponent(mime)}`,
relay_id: id, logical_id: logicalId, sender_device_id: state.deviceId, recipient_device_id: device.id,
wrapped_key: key.wrapped_key, iv: key.iv, eph_pub: key.eph_pub, expires_at,
});
if (error) throw error;
return { uid: u.id, id: snapId, kind: mime.startsWith('video/') ? 'video' : 'photo' };
} catch (e) { console.error('[mayfly] shared relay recipient failed', e); return null; }
});
}));
const sentRows = rows.filter(Boolean), seen = new Set();
result.sent = sentRows.filter(row => !seen.has(row.uid) && seen.add(row.uid));
result.sent.forEach(({ uid }) => db.bumpStreak(uid).then(() => {}, () => {}));
if (!result.sent.length) {
await sb.storage.from(SNAP_BUCKET).remove([id]);
await db.delRelayPayloads([id]);
}
} catch (e) {
console.error('[mayfly] shared relay failed', e);
if (payloadCreated) {
await sb.storage.from(SNAP_BUCKET).remove([id]);
await db.delRelayPayloads([id]);
}
}
return result;
};
// Post to My Story: a 24h broadcast to all friends. The full image stays in our
// browser (served P2P like a live snap); only the LQIP preview lands in the DB.
const postStory = async (shot, caption) => {
try {
const id = uuid();
const source = shot.rawBlob || await fetch(shot.full).then(r => r.blob());
const preview = await makeStoryPreview(source);
const { error } = await db.addStory({ id, preview, caption, w: shot.w, h: shot.h });
if (error) throw error;
await idb.set('story:' + id, shot.rawBlob || shot.full);
return true;
} catch (e) { console.error('[mayfly] story failed', e); return false; }
};
// ===================== stories =====================
// A horizontal bar of friends (and you) with an active story; tap a ring to play.
const renderStoriesBar = async (into) => {
if (!into) return;
const [{ data: stories }, { data: viewed }] = await Promise.all([db.activeStories(), db.myViewedStories()]);
const seen = new Set((viewed || []).map(v => v.story_id));
// group active stories by author, preserving chronological order within each
const byUser = new Map();
(stories || []).forEach(s => { if (!byUser.has(s.user_id)) byUser.set(s.user_id, []); byUser.get(s.user_id).push(s); });
const mine = byUser.get(state.me.id) || [];
byUser.delete(state.me.id);
const storyGroups = mine.length ? [{ items: mine, mine: true }] : [];
into.innerHTML = '';
// "Your Story" — either your ring, or a + to add
const meRing = el(`<button class="storyitem">
<span class="ring ${mine.length ? 'mine' : 'add'}">${mine.length ? avatarHTML(state.profile.username, state.profile.avatar) : '+'}</span>
<span class="sname">Your Story</span></button>`);
meRing.onclick = () => mine.length ? playStories(storyGroups, 0) : (location.hash = '#/camera');
into.appendChild(meRing);
for (const [uid, items] of byUser) {
const groupIndex = storyGroups.length;
storyGroups.push({ items, mine: false });
const a = items[0].author || {};
const allSeen = items.every(s => seen.has(s.id));
const ring = el(`<button class="storyitem"><span class="ring ${allSeen ? 'seen' : 'fresh'}">${avatarHTML(a.username, a.avatar)}</span><span class="sname">${esc(a.username || '')}</span></button>`);
ring.onclick = () => playStories(storyGroups, groupIndex);
into.appendChild(ring);
}
if (!mine.length && !byUser.size) into.innerHTML = `<div class="muted tiny" style="padding:10px 4px">No stories yet — tap ◉ and post to <b>My Story</b>.</div>`;
};
// Full-screen story player. It advances through each person's stories, then the next person.
const playStories = (groups, startGroup = 0) => {
if (!groups.length) return;
let i = 0, groupIndex = startGroup, timerId = null, advanceStarted = 0, advanceRemaining = 5000, items, mine, closed = false;
const ov = el(`<div class="player stories"><div class="segs"></div>
<img alt="story"><div class="pcap"></div><div class="pname"></div>
<div class="tapzones"><div class="tz left"></div><div class="tz right"></div></div>
<form class="storyreply" hidden><input maxlength="120" placeholder="Reply to Story…" aria-label="Reply to Story"><button type="submit">Send</button></form>
<button class="storymore" aria-label="Story options" title="Story options" hidden>⋮</button>
<div class="storymenu" hidden><button class="storydelete" type="button">Delete story</button></div>
<button class="storyclose" aria-label="Close stories" title="Close">×</button>
<button class="storyprev" aria-label="Previous story" title="Previous story">‹</button>
<button class="storynext" aria-label="Next story" title="Next story">›</button>
<div class="viewers"></div></div>`);
document.body.appendChild(ov);
const img = $('img', ov), segs = $('.segs', ov), more = $('.storymore', ov), menu = $('.storymenu', ov), reply = $('.storyreply', ov);
let ownStoryUrl = null;
const stopAdvance = () => { clearTimeout(timerId); timerId = null; };
const pauseAdvance = () => {
if (!timerId) return;
advanceRemaining = Math.max(0, advanceRemaining - (Date.now() - advanceStarted));
stopAdvance();
const bar = segs.querySelector(`i[data-seg="${i}"]`);
if (!bar) return;
const trackWidth = bar.parentElement?.getBoundingClientRect().width || 1;
const width = Math.min(trackWidth, parseFloat(getComputedStyle(bar).width) || 0);
bar.style.transition = 'none';
bar.style.width = `${(width / trackWidth) * 100}%`;
};
const resumeAdvance = () => {
if (timerId || closed) return;
if (advanceRemaining <= 0) return void show(i + 1);
const bar = segs.querySelector(`i[data-seg="${i}"]`);
if (bar) requestAnimationFrame(() => { bar.style.transition = `width ${advanceRemaining}ms linear`; bar.style.width = '100%'; });
advanceStarted = Date.now();
timerId = setTimeout(() => show(i + 1), advanceRemaining);
};
const close = () => { closed = true; stopAdvance(); if (ownStoryUrl) URL.revokeObjectURL(ownStoryUrl); document.removeEventListener('keydown', onKeydown); ov.remove(); };
const setGroup = (nextGroup, atEnd = false) => {
groupIndex = nextGroup;
({ items, mine } = groups[groupIndex]);
segs.innerHTML = items.map((_, k) => `<span><i data-seg="${k}"></i></span>`).join('');
show(atEnd ? items.length - 1 : 0);
};
const show = async (k) => {
stopAdvance();
if (ownStoryUrl) { URL.revokeObjectURL(ownStoryUrl); ownStoryUrl = null; }
if (k < 0) return groupIndex > 0 ? setGroup(groupIndex - 1, true) : show(0);
if (k >= items.length) return groupIndex < groups.length - 1 ? setGroup(groupIndex + 1) : close();
i = k;
more.hidden = !mine;
menu.hidden = true;
reply.hidden = mine;
segs.querySelectorAll('i').forEach((s, j) => { s.style.transition = 'none'; s.style.width = j < k ? '100%' : '0'; });
const s = items[k];
$('.pname', ov).textContent = (s.author?.username) || (mine ? 'You' : '');
$('.pcap', ov).textContent = s.caption || '';
// The database-backed Story preview is intentionally size-bounded, but it
// should remain readable when the author is offline.
img.style.filter = 'none'; img.src = safeMediaUrl(s.preview);
if (!mine) db.viewStory(s.id).then(({ error }) => {
if (!error) window.dispatchEvent(new Event('mf-story-viewed'));
}, () => {});
// pull the full image P2P (from our own IndexedDB if it's ours)
const shownGroup = groupIndex;
let full = mine ? await idb.get('story:' + s.id) : await fetchSnap(s.id, s.user_id);
if (closed || shownGroup !== groupIndex || i !== k) return;
if (mine && full instanceof Blob) { ownStoryUrl = URL.createObjectURL(full); full = ownStoryUrl; }
if (full) { img.src = safeMediaUrl(full); img.style.filter = 'none'; }
if (mine) showViewers(s.id);
// advance the current segment bar, then move on
advanceRemaining = 5000;
resumeAdvance();
};
// A tappable list of everyone who viewed this Story, each with quick actions:
// start a chat, or jump straight into their own Story if they have one live.
const openViewerList = async (viewers) => {
pauseAdvance();
const storyByUid = new Map(); // viewers with an UNWATCHED live Story → ring their avatar
try {
const [{ data: act }, { data: viewed }] = await Promise.all([db.activeStories(), db.myViewedStories()]);
const seen = new Set((viewed || []).map(v => v.story_id));
(act || []).forEach(st => { if (st.user_id !== state.me.id && !seen.has(st.id)) (storyByUid.get(st.user_id) || storyByUid.set(st.user_id, []).get(st.user_id)).push(st); });
} catch (e) {}
const m = el(`<div class="modal viewerlist"><div class="sheet"><div class="mhead">Viewed by · ${viewers.length}<button class="x icon" aria-label="Close">✕</button></div><div class="mbody"></div></div></div>`);
const listBody = $('.mbody', m);
if (!viewers.length) listBody.innerHTML = `<div class="empty">No views yet.</div>`;
viewers.forEach(v => {
const u = v.viewer; if (!u) return;
const theirStory = storyByUid.get(u.id);
// Viewers with a live Story get a ring on their avatar; tapping it plays it.
const avatar = theirStory
? `<button class="storyavatar" aria-label="View ${esc(u.username)}'s story">${avatarHTML(u.username, u.avatar, 'hasstory')}</button>`
: avatarHTML(u.username, u.avatar);
const row = el(`<div class="urow">${avatar}<div class="who"><b>${esc(u.username)}</b><div class="sub">${esc(ago(v.viewed_at))}</div></div><div class="acts"><button class="pill primary vchat">Chat</button></div></div>`);
$('.vchat', row).onclick = () => { m.remove(); close(); location.hash = '#/c/' + u.id; };
if (theirStory) $('.storyavatar', row).onclick = () => { m.remove(); close(); playStories([{ items: theirStory, mine: false }], 0); };
listBody.appendChild(row);
});
const dismiss = () => { m.remove(); resumeAdvance(); };
$('.x', m).onclick = dismiss;
m.onclick = (e) => { if (e.target === m) dismiss(); };
document.body.appendChild(m);
};
const showViewers = async (id) => {
const { data } = await db.storyViewers(id);
const viewers = data || [];
const box = $('.viewers', ov);
box.innerHTML = `👁 ${viewers.length}` + (viewers.length ? ' · ' + viewers.slice(0, 3).map(v => esc(v.viewer?.username || '')).filter(Boolean).join(', ') : '');
box.classList.toggle('tappable', viewers.length > 0);
box.onclick = (e) => { e.stopPropagation(); if (viewers.length) openViewerList(viewers); };
};
const onKeydown = (e) => {
if (e.key === 'Escape') close();
if (e.key === 'ArrowRight') show(i + 1);
if (e.key === 'ArrowLeft') show(i - 1);
};
$('.tz.right', ov).onclick = () => show(i + 1);
$('.tz.left', ov).onclick = () => show(i - 1);
$('.storyclose', ov).onclick = close;
reply.onsubmit = async (e) => {
e.preventDefault();
const text = $('input', reply).value.trim(), s = items[i];
if (!text || mine || !s?.user_id) return;
const send = $('button', reply); send.disabled = true;
const sent = await sendStoryReply(s.user_id, s.author?.username || 'Story author', text, s);
if (sent) { const input = $('input', reply); input.value = ''; input.blur(); toast('Story reply sent.'); }
send.disabled = false;
};
const replyInput = $('input', reply);
replyInput.onfocus = pauseAdvance;
replyInput.onblur = resumeAdvance;
more.onclick = (e) => { e.stopPropagation(); menu.hidden = !menu.hidden; };
$('.storydelete', ov).onclick = async (e) => {
e.stopPropagation();
const s = items[i];
if (!mine || !s || !confirm('Delete this Story?')) return;
const { error } = await db.delStory(s.id);
if (error) return void toast('Could not delete that Story.');
await idb.del('story:' + s.id);
close();
if ($('#storiesbar')) renderStoriesBar($('#storiesbar'));
};
$('.storyprev', ov).onclick = () => show(i - 1);
$('.storynext', ov).onclick = () => show(i + 1);
$('.viewers', ov).onclick = (e) => e.stopPropagation();
document.addEventListener('keydown', onKeydown);
setGroup(startGroup);
};
// ===================== chats (unified conversations: snaps + chat) =====================
// Responsive: two-pane (list + open thread) on wide screens; single-pane on mobile
// where opening a conversation swaps to the thread (the `showthread` class).
const viewChats = (activeUid, activeGroupId = null) => {
const open = activeUid || activeGroupId;
app.innerHTML = `<main class="chats ${open ? 'showthread' : ''}">
<aside class="convlist">
<div id="storiesbar" class="storiesbar"></div>
<div class="grouphead">Groups <button class="pill primary" id="newgroup" aria-label="New group">+</button></div>
<div id="grouplist"></div>
<div class="convhead">Chats <button class="icon allchatmore" aria-label="All chat options">${icon('more')}</button><div class="headmenu" hidden><button type="button" class="clearallchats">Clear all conversations on this device</button></div></div>
<div id="convs"><div class="spin">Loading…</div></div>
</aside>
<section class="threadpane" id="threadpane">${open ? '<div class="spin">…</div>' : '<div class="threadempty">Pick a conversation, or tap ◉ on someone to snap them.</div>'}</section>
</main>`;
$('#newgroup').onclick = () => createGroupFlow();
const allMenu = $('.convhead .headmenu'), allMore = $('.allchatmore');
allMore.onclick = () => { allMenu.hidden = !allMenu.hidden; allMore.setAttribute('aria-expanded', String(!allMenu.hidden)); };
$('.clearallchats').onclick = async () => {
if (!confirm('Clear all local conversations and opened media from this device? Active delivery data and your account will stay intact.')) return;
allMenu.hidden = true;
await clearAllLocalConversations(); clearAllGroupConversations();
location.hash = '#/chats'; toast('Local conversations cleared.');
};
renderStoriesBar($('#storiesbar'));
renderGroupList($('#grouplist'), activeGroupId);
renderConvs($('#convs'), activeUid);
if (activeUid) openConversation($('#threadpane'), activeUid);
else if (activeGroupId) openGroupById(activeGroupId, $('#threadpane'));
};
// ===================== friends =====================
const otherOf = (row) => row.requester_id === state.me.id ? row.addressee : row.requester;
let streakMap = {};
const pairKey = (x, y) => [x, y].sort().join('|');
const HIDDEN_KEY = 'mf_hidden';
const getHidden = () => { try { return new Set(JSON.parse(localStorage[HIDDEN_KEY] || '[]')); } catch { return new Set(); } };
const hideUser = (uid) => { const h = getHidden(); h.add(uid); localStorage[HIDDEN_KEY] = JSON.stringify([...h]); };
const viewFriends = () => {
const forcedPrivate = Boolean(state.profile?.profile_private && state.profile?.privacy_locked);
app.innerHTML = `<main>
<h3 class="vtitle">Friends</h3>
<input class="field searchbar" id="usearch" placeholder="Search people by username…" autocomplete="off">
<div id="reqs"></div>
<div id="friendlist"></div>
<div class="section-title">Add people</div>
<div id="discover"><div class="spin">Loading…</div></div>
</main>`;
const s = $('#usearch'); let t;
if (forcedPrivate) {
s.remove();
const discover = $('#discover');
discover.previousElementSibling?.remove();
discover.innerHTML = '<div class="empty">Your account is private by administrator setting. You cannot add people while this is enabled.</div>';
} else s.oninput = () => { clearTimeout(t); t = setTimeout(() => renderDiscover(s.value.trim()), 220); };
renderRequests();
renderFriends();
if (!forcedPrivate) renderDiscover('');
};
// Discover: up to 50 people you can add — Add sends a request, ✕ hides them for good.
// Already-friends / pending-either-way / hidden people are filtered out.
const renderDiscover = async (q) => {
const box = $('#discover'); if (!box) return;
const [{ data: profs }, { data: fr }, { data: out }, { data: inc }] = await Promise.all([
q ? db.searchProfiles(q) : db.allProfiles(),
db.friends(), db.outgoingRequests(), db.incomingRequests(),
]);
if (!$('#discover')) return;
const exclude = getHidden(); exclude.add(state.me.id);
(fr || []).forEach(f => exclude.add(f.requester_id === state.me.id ? f.addressee_id : f.requester_id));
(out || []).forEach(o => exclude.add(o.addressee_id));
(inc || []).forEach(i => exclude.add(i.requester_id));
const list = (profs || []).filter(p => !exclude.has(p.id));
box.innerHTML = '';
if (!list.length) return void (box.innerHTML = `<div class="empty">${q ? 'No one matched that.' : 'No new people to add right now.'}</div>`);
list.forEach(p => {
const row = el(`<div class="urow" data-uid="${p.id}">${avatarHTML(p.username, p.avatar)}
<div class="who"><button class="profilelink">${esc(p.username)}</button>${isOnline(p.id) ? '<div class="sub"><i class="dot"></i>online</div>' : ''}</div>
<div class="acts"><button class="pill primary addbtn">Add</button><button class="pill hidebtn" aria-label="Hide">✕</button></div></div>`);
$('.profilelink', row).onclick = () => { location.hash = '#/profile/' + p.id; };
$('.addbtn', row).onclick = async () => {
const b = $('.addbtn', row); b.disabled = true;
const { error } = await db.sendRequest(p.id);
if (error) { b.disabled = false; return toast('Could not send request.'); }
b.textContent = 'Requested'; b.classList.remove('primary');
};
$('.hidebtn', row).onclick = () => { hideUser(p.id); row.remove(); if (!$('#discover .urow')) box.innerHTML = `<div class="empty">No more people to add.</div>`; };
box.appendChild(row);
});
};
const renderRequests = async () => {
const box = $('#reqs'); if (!box) return;
const { data } = await db.incomingRequests();
box.innerHTML = '';
if (!data || !data.length) return;
box.appendChild(el(`<div class="section-title">Friend requests</div>`));
data.forEach(r => {
const u = r.requester || {};
const row = el(`<div class="urow">${avatarHTML(u.username, u.avatar)}
<div class="who"><b>${esc(u.username || 'someone')}</b><div class="sub">wants to be friends</div></div>
<div class="acts"><button class="pill primary ok">Accept</button><button class="pill no">✕</button></div></div>`);
$('.ok', row).onclick = async () => { await db.acceptRequest(r.requester_id); row.remove(); renderFriends(); };
$('.no', row).onclick = async () => { await db.removeFriend(r.requester_id); row.remove(); };
box.appendChild(row);
});
};
const renderFriends = async () => {
const box = $('#friendlist'); if (!box) return;
const [{ data: fr }, { data: stk }, { data: activity }] = await Promise.all([db.friends(), db.streaks(), db.friendActivity()]);
if (!$('#friendlist')) return;
streakMap = {};
(stk || []).forEach(s => streakMap[pairKey(s.user_a, s.user_b)] = s.count);
setFriendActivity(activity || []);
const friends = (fr || []).map(otherOf).filter(Boolean);
box.innerHTML = `<div class="section-title">Your friends</div>`;
if (!friends.length) return void box.appendChild(el(`<div class="empty">No friends yet — add someone from <b>Add people</b> below.</div>`));
friends.forEach(u => {
const streak = streakMap[pairKey(state.me.id, u.id)] || 0;
const row = el(`<div class="urow">${avatarHTML(u.username, u.avatar)}
<div class="who"><button class="profilelink">${esc(u.username)}</button>
<div class="sub">${isOnline(u.id) ? '<i class="dot"></i>' : ''}${activityText(u.id)}${streak ? ` · 🔥 ${streak}` : ''}</div></div>
<div class="acts"><button class="rowaction chatbtn" data-go="#/c/${u.id}" aria-label="Chat with ${esc(u.username)}" title="Chat">${icon('messageSquare', 20)}</button><button class="rowaction snapbtn" aria-label="Send a Snap to ${esc(u.username)}" title="Send Snap">${icon('camera', 20)}</button></div></div>`);
$('.profilelink', row).onclick = () => { location.hash = '#/profile/' + u.id; };
$('.snapbtn', row).onclick = () => { location.hash = '#/snap/' + u.id; };
box.appendChild(row);
});
};
// ===================== me / settings =====================
const viewMe = () => {
const p = state.profile;
const isAdmin = state.me.id === MAYFLY_ADMIN_ID;
app.innerHTML = `<main>
<div class="mehead">
<div class="avatar big" id="mav">${isMediaUrl(p.avatar) ? `<img src="${p.avatar}" alt="">` : initial(p.username)}</div>
<input id="mfile" type="file" accept="image/*" hidden>
<button class="pill" id="mpick">Change photo</button>
</div>
<label class="lbl">Username</label>
<input class="field" id="muser" value="${esc(p.username)}" autocomplete="off">
<label class="lbl">Public profile</label>
<textarea class="field profilebio" id="mbio" maxlength="200" placeholder="Tell people a little about yourself…">${esc(p.bio || '')}</textarea>
<div class="muted tiny" id="biocount">${String(p.bio || '').length}/200</div>
<div class="settingrow"><div><b>Private profile</b><div class="muted tiny">${p.privacy_locked ? 'Set by an administrator. Only an administrator can remove it.' : 'Hide your profile from Add People.'}</div></div><label class="switch"><input id="privacytoggle" type="checkbox" ${p.profile_private ? 'checked' : ''} ${p.privacy_locked ? 'disabled' : ''}><span></span></label></div>
<div class="err" id="merr"></div>
<button class="btn" id="msave">Save</button>
<button class="pill" id="mmemories">Memories</button>
<div class="settingrow"><div><b>Notifications</b><div class="muted tiny" id="pushstatus"></div></div><label class="switch"><input id="pushtoggle" type="checkbox"><span></span></label></div>
${isAdmin ? `<section class="adminpanel"><b>Admin tools</b><p class="muted tiny">Search a Mayfly user to manage profile privacy or remove their account.</p><input class="field" id="adminusersearch" placeholder="Find a user" autocomplete="off"><div id="adminuserresults" class="adminresults"></div></section>` : ''}
<button class="btn ghost" id="mout">Log out</button>
<p class="muted tiny">mayfly 🐛 — snaps vanish after they're opened. Full photos are never stored on our server: they stream peer-to-peer when your friend is online, or are end-to-end encrypted when they're not.</p>
</main>`;
let newAvatar = null;
$('#mmemories').onclick = () => { location.hash = '#/memories'; };
const bio = $('#mbio'), bioCount = $('#biocount');
bio.oninput = () => { bioCount.textContent = `${bio.value.length}/200`; };
const privacyToggle = $('#privacytoggle');
privacyToggle.onchange = async () => {
const profile_private = privacyToggle.checked;
privacyToggle.disabled = true;
const { error } = await db.updateProfile({ profile_private });
if (error) { privacyToggle.checked = !profile_private; toast('Could not update profile privacy.'); }
else { state.profile.profile_private = profile_private; toast(profile_private ? 'Profile is private' : 'Profile is public'); }
privacyToggle.disabled = Boolean(state.profile.privacy_locked);
};
$('#mpick').onclick = () => $('#mfile').click();
$('#mfile').onchange = async () => {
const f = $('#mfile').files[0]; if (!f) return;
try { newAvatar = await makeAvatar(f); $('#mav').innerHTML = `<img src="${safeMediaUrl(newAvatar)}" alt="">`; }
catch (e) { $('#merr').textContent = 'Could not read that image.'; }
};
$('#msave').onclick = async () => {
const username = $('#muser').value.trim();
if (!/^[a-z0-9_.]{3,20}$/i.test(username)) return void ($('#merr').textContent = 'Username: 3–20 letters, numbers, _ or .');
const profileBio = bio.value.trim();
if (profileBio.length > 200) return void ($('#merr').textContent = 'Public profile: up to 200 characters.');
const patch = { username, bio: profileBio }; if (newAvatar) patch.avatar = newAvatar;
const { error } = await db.updateProfile(patch);
if (error) return void ($('#merr').textContent = /duplicate|unique/i.test(error.message) ? 'That username is taken.' : error.message);
Object.assign(state.profile, patch); mountChrome(true); toast('Saved');
};
const pushToggle = $('#pushtoggle'), pushStatus = $('#pushstatus');
const refreshPushToggle = () => {
const supported = 'Notification' in window && 'serviceWorker' in navigator && 'PushManager' in window;
pushToggle.checked = supported && pushPreference() && Notification.permission === 'granted';
if (!supported) pushStatus.textContent = 'Notifications are not supported by this browser.';
else if (!pushPreference()) pushStatus.textContent = 'Off on this device.';
else if (Notification.permission === 'denied') pushStatus.textContent = 'Blocked in browser settings.';
else if (Notification.permission === 'granted') { pushStatus.textContent='Checking this device…';pushRegistrationActive().then(active=>{if($('#pushstatus'))pushStatus.textContent=active?'On for 1:1 messages and calls.':'Permission granted, but this device is not registered. Toggle off and on to repair.';}); }
else pushStatus.textContent = 'Tap the switch to enable notifications.';
};
refreshPushToggle();
pushToggle.onchange = async () => {
if (pushToggle.checked) {
const enabled = await enablePush();
if (!enabled) toast('Allow notifications in your browser settings to turn them on.');
} else await disablePush();
refreshPushToggle();
};
if (isAdmin) {
const search = $('#adminusersearch'), results = $('#adminuserresults');
let searchTimer = null;
const showUsers = async () => {
const q = search.value.trim();
if (q.length < 2) { results.innerHTML = `<div class="muted tiny">Type at least two characters.</div>`; return; }
results.innerHTML = `<div class="spin">Searching…</div>`;
const { data, error } = await db.adminSearchProfiles(q);
if (!$('#adminuserresults')) return;
if (error) { results.innerHTML = `<div class="err">Could not search users.</div>`; return; }
const users = (data || []).filter(u => u.id !== MAYFLY_ADMIN_ID);
if (!users.length) { results.innerHTML = `<div class="muted tiny">No matching users.</div>`; return; }
results.innerHTML = '';
users.forEach((u) => {
const row = el(`<div class="urow">${avatarHTML(u.username, u.avatar)}<div class="who"><b>${esc(u.username)}</b><div class="sub privacylabel">${u.privacy_locked ? 'Forced private' : (u.profile_private ? 'Private' : 'Public')}</div></div><select class="adminactions" aria-label="Manage ${esc(u.username)}"><option value="">Manage</option><option value="privacy">${u.privacy_locked ? 'Unlock privacy' : 'Force private'}</option><option value="remove">Remove account</option></select></div>`);
const actions = $('.adminactions', row);
actions.onchange = async () => {
const action = actions.value; actions.value = '';
if (!action) return;
if (action === 'privacy') {
const forcePrivate = !u.privacy_locked;
if (!confirm(forcePrivate ? `Force ${u.username}'s profile private and prevent them from adding people?` : `Remove the forced privacy lock for ${u.username}?`)) return;
actions.disabled = true;
const { error: privacyError } = await db.adminSetProfilePrivacy(u.id, forcePrivate);
actions.disabled = false;
if (privacyError) return toast('Could not update profile privacy.');
u.profile_private = forcePrivate; u.privacy_locked = forcePrivate;
actions.querySelector('option[value="privacy"]').textContent = forcePrivate ? 'Unlock privacy' : 'Force private';
$('.privacylabel', row).textContent = forcePrivate ? 'Forced private' : 'Public';
return toast(forcePrivate ? `${u.username} is now forced private.` : `Privacy lock removed for ${u.username}.`);
}
if (!confirm(`Remove ${u.username} from Mayfly and delete their server-side data? This cannot be undone.`)) return;
actions.disabled = true;
const { error: removeError } = await db.adminDeleteUser(u.id);
if (removeError) { actions.disabled = false; return toast('Could not remove that account.'); }
row.remove(); toast(`${u.username} was removed.`);
if (!results.children.length) results.innerHTML = `<div class="muted tiny">No matching users.</div>`;
};
results.appendChild(row);
});
};
search.oninput = () => { clearTimeout(searchTimer); searchTimer = setTimeout(showUsers, 220); };
results.innerHTML = `<div class="muted tiny">Type at least two characters.</div>`;
}
$('#mout').onclick = async () => { stopStream();await releasePushForCurrentUser();await sb.auth.signOut(); };
};