Skip to content

Commit 71159f3

Browse files
committed
Changes for GUEST
This is for the users who is logged in via code
1 parent b38e15c commit 71159f3

7 files changed

Lines changed: 419 additions & 23 deletions

File tree

index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,10 @@ <h1 class="headline">Private Garden</h1>
500500
</main>
501501

502502
<script>
503-
const VALID_KEY = "CIVET-908N-227O";
503+
const VALID_KEY = "CIVET-908O-227N";
504504
const ACCESS_SESSION_KEY = "noctive_site_access";
505+
const ACCESS_MODE_SESSION_KEY = "noctive_access_mode";
506+
const GUEST_PREVIEW_MODE = "guest";
505507

506508
const pill = document.getElementById("pill");
507509
const status = document.getElementById("status");
@@ -581,6 +583,7 @@ <h1 class="headline">Private Garden</h1>
581583
if (normalize(entered) === normalize(VALID_KEY)) {
582584
setStatus("Access granted.", "good");
583585
sessionStorage.setItem(ACCESS_SESSION_KEY, "granted");
586+
sessionStorage.setItem(ACCESS_MODE_SESSION_KEY, GUEST_PREVIEW_MODE);
584587
setTimeout(() => {
585588
window.location.replace("./pages/home.html");
586589
}, 900);

js/main.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
collection,
55
deleteDoc,
66
doc,
7-
getDocs,
87
increment,
98
onSnapshot,
109
orderBy,
@@ -26,6 +25,8 @@ let currentViewerIsAdmin = false;
2625
const PUBLIC_PROFILE_DIRECTORY_KEY = "noctive_public_profile_directory";
2726
const POST_VOTE_STORAGE_PREFIX = "noctive_post_votes_";
2827
const BIO_MAX_LENGTH = 180;
28+
const ACCESS_MODE_SESSION_KEY = "noctive_access_mode";
29+
const GUEST_PREVIEW_MODE = "guest";
2930

3031
const ADMIN_CONFIG = {
3132
uids: [
@@ -40,6 +41,15 @@ const ADMIN_CONFIG = {
4041
]
4142
};
4243

44+
function isGuestPreviewSession() {
45+
try {
46+
return window.sessionStorage.getItem(ACCESS_MODE_SESSION_KEY) === GUEST_PREVIEW_MODE;
47+
} catch (error) {
48+
console.warn("Could not read guest preview session:", error);
49+
return false;
50+
}
51+
}
52+
4353
function normalizeAdminValue(value) {
4454
return String(value || "").trim().toLowerCase();
4555
}
@@ -442,13 +452,15 @@ function renderPosts(snapshot) {
442452

443453
if (snapshot.empty) {
444454
container.innerHTML = "<p>No posts yet.</p>";
455+
window.noctiveFeedBridge?.renderGuestPreviewPosts?.(container);
445456
return;
446457
}
447458

448459
snapshot.forEach((doc) => {
449460
container.appendChild(buildPostCard(doc));
450461
});
451462

463+
window.noctiveFeedBridge?.renderGuestPreviewPosts?.(container);
452464
window.noctiveFeedBridge?.applyGeneratedAvatars?.();
453465
}
454466

@@ -461,7 +473,7 @@ function renderPostsError(error) {
461473
container.innerHTML = "<p>Could not load posts.</p>";
462474
}
463475

464-
function subscribeToPosts(postsSource) {
476+
function subscribeToPosts(postsSource, fallbackSource = null) {
465477
if (stopPostsSubscription) {
466478
stopPostsSubscription();
467479
}
@@ -472,12 +484,18 @@ function subscribeToPosts(postsSource) {
472484
renderPosts(snapshot);
473485
},
474486
(error) => {
487+
if (fallbackSource && fallbackSource !== postsSource) {
488+
console.warn("Falling back to alternate posts source:", error);
489+
subscribeToPosts(fallbackSource, null);
490+
return;
491+
}
492+
475493
renderPostsError(error);
476494
}
477495
);
478496
}
479497

480-
async function loadPosts() {
498+
function loadPosts() {
481499
const container = getFeedContainer();
482500

483501
if (!container) return;
@@ -486,21 +504,8 @@ async function loadPosts() {
486504
container.innerHTML = "<p>Loading posts...</p>";
487505

488506
const postsCollection = collection(db, "Posts");
489-
490-
try {
491-
const orderedPostsQuery = query(postsCollection, orderBy("createdAt", "desc"));
492-
await getDocs(orderedPostsQuery);
493-
subscribeToPosts(orderedPostsQuery);
494-
} catch (orderError) {
495-
console.warn("Falling back to unordered posts:", orderError);
496-
497-
try {
498-
await getDocs(postsCollection);
499-
subscribeToPosts(postsCollection);
500-
} catch (error) {
501-
renderPostsError(error);
502-
}
503-
}
507+
const orderedPostsQuery = query(postsCollection, orderBy("createdAt", "desc"));
508+
subscribeToPosts(orderedPostsQuery, postsCollection);
504509
}
505510

506511
async function createPost(text) {
@@ -512,8 +517,8 @@ async function createPost(text) {
512517

513518
const uid = window.noctiveViewerKey;
514519

515-
if (!uid || uid === "guest") {
516-
throw new Error("Sign in before posting.");
520+
if (isGuestPreviewSession() || !uid || uid === "guest") {
521+
throw new Error("Guest preview posts do not publish. Sign up to post for real.");
517522
}
518523

519524
const displayName =
@@ -538,7 +543,7 @@ window.noctiveCreatePost = createPost;
538543
async function voteOnPost(postId, direction) {
539544
const normalizedDirection = direction === "down" ? "down" : "up";
540545

541-
if (!currentViewer?.uid) {
546+
if (isGuestPreviewSession() || !currentViewer?.uid) {
542547
throw new Error("Sign in before voting.");
543548
}
544549

@@ -585,6 +590,16 @@ async function voteOnPost(postId, direction) {
585590
window.noctiveVotePost = voteOnPost;
586591

587592
onAuthStateChanged(getAuth(), (user) => {
593+
if (isGuestPreviewSession()) {
594+
currentViewer = null;
595+
currentViewerIsAdmin = false;
596+
597+
if (latestSnapshot) {
598+
renderPosts(latestSnapshot);
599+
}
600+
return;
601+
}
602+
588603
currentViewer = user;
589604
currentViewerIsAdmin = isAdminIdentity(getViewerAdminIdentity(user));
590605

0 commit comments

Comments
 (0)