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sC,CACxD,UAAW,KAAKA,EAAS,CACxB,MAAMC,EAAIJ,EAAS,IAAI,EAAE,MAAM,EAC/B,GAAI,CAACI,EAAG,SACR,MAAMC,EAAO,EAAE,YAAY,QAAU,OAAO,YAE3C,EAAE,gBACF,EAAE,iBAAiB,QAAUT,EAAW,EAAE,mBAAmB,OAAQS,CAAI,EAAI,EAE7ED,EAAE,QAAU,WAAW,IAAM,CAC5BT,EAAM,IAAIS,EAAE,EAAE,EACdE,EAAQ,EAAE,MAAM,CACjB,EAAG,GAAO,EACAF,EAAE,QACZ,aAAaA,EAAE,KAAK,EACpBA,EAAE,MAAQ,KAEZ,CACD,CAEA,SAASE,EAAQC,EAAe,CAC/B,MAAMH,EAAIJ,EAAS,IAAIO,CAAI,EACtBH,IACDA,EAAE,OAAO,aAAaA,EAAE,KAAK,EACjCJ,EAAS,OAAOO,CAAI,EACpBN,GAAU,UAAUM,CAAI,EACzB,CAEA,SAASC,GAAQ,CACZP,IACJA,EAAW,IAAI,qBAAqBC,EAAW,CAAE,UAAWH,EAAY,EACxE,SAAS,iBAAiB,mBAAoB,IAAM,CAC/C,SAAS,kBAAoB,UAAUJ,EAAM,MAAA,CAClD,CAAC,EACD,OAAO,iBAAiB,WAAY,IAAMA,EAAM,OAAO,EACxD,CAGO,SAASc,EAAWf,EAAkB,CAC/BC,EAAM,IAAID,CAAE,CAC1B,CAGO,SAASgB,EAAchB,EAAqB,CAClD,OAAOC,EAAM,IAAID,CAAE,CACpB,CAKO,SAASiB,EAAgBjB,EAAqC,CACpE,OAAQa,GAAS,CAChB,GAAgB,SAAO,qBAAyB,KAChD,OAAAC,EAAA,EACAR,EAAS,IAAIO,EAAM,CAAE,GAAAb,EAAI,MAAO,KAAM,EACtCO,GAAU,QAAQM,CAAI,EACf,IAAM,CACZD,EAAQC,CAAI,EACZZ,EAAM,MAAA,CACP,CACD,CACD","names":["createSeenQueue","send","flushMs","maxBatch","reported","pending","timer","flush","ids","ok","id","queue","seenHeight","cardHeight","viewHeight","THRESHOLDS","watching","observer","onEntries","entries","w","view","unwatch","node","start","reportSeen","seenInThisTab","seenWhenVisible"],"sources":["../../../../../../src/lib/feed-seen.ts"],"sourcesContent":["import { browser } from '$app/environment';\nimport type { Attachment } from 'svelte/attachments';\n\n/**\n * RAT-679: which post cards a signed-in viewer has actually had in front of\n * them. For You and Following leave those out on their next load\n * (/api/feed/seen → feed_seen); nothing disappears from the list on screen.\n *\n * Seen = at least half of the card (or, for a card taller than the screen, half\n * of the screen) for one second — a card flung past doesn't count. Reported in\n * small batches, and at once when the tab hides or the cards go away (an in-app\n * navigation), so the next feed load already knows.\n */\n\nconst SEEN_MS = 1000;\nconst FLUSH_MS = 2000;\nconst MAX_BATCH = 40;\n\ntype Send = (ids: string[]) => Promise<boolean>;\n\nexport function createSeenQueue(send: Send, flushMs = FLUSH_MS, maxBatch = MAX_BATCH) {\n\tconst reported = new Set<string>();\n\tlet pending: string[] = [];\n\tlet timer: ReturnType<typeof setTimeout> | null = null;\n\n\tfunction flush() {\n\t\tif (timer) clearTimeout(timer);\n\t\ttimer = null;\n\t\twhile (pending.length > 0) {\n\t\t\tconst ids = pending.slice(0, maxBatch);\n\t\t\tpending = pending.slice(maxBatch);\n\t\t\t// A beacon that didn't land only means the post is shown once more;\n\t\t\t// forgetting it lets the next sighting report it again.\n\t\t\tsend(ids).then(\n\t\t\t\t(ok) => {\n\t\t\t\t\tif (!ok) for (const id of ids) reported.delete(id);\n\t\t\t\t},\n\t\t\t\t() => {\n\t\t\t\t\tfor (const id of ids) reported.delete(id);\n\t\t\t\t}\n\t\t\t);\n\t\t}\n\t}\n\n\treturn {\n\t\tadd(id: string) {\n\t\t\tif (reported.has(id)) return;\n\t\t\treported.add(id);\n\t\t\tpending.push(id);\n\t\t\tif (pending.length >= maxBatch) flush();\n\t\t\telse timer ??= setTimeout(flush, flushMs);\n\t\t},\n\t\thas: (id: string) => reported.has(id),\n\t\tflush\n\t};\n}\n\nconst queue = createSeenQueue(async (ids) => {\n\tconst res = await fetch('/api/feed/seen', {\n\t\tmethod: 'POST',\n\t\theaders: { 'content-type': 'application/json' },\n\t\tbody: JSON.stringify({ ids }),\n\t\t// the page may be going away\n\t\tkeepalive: true\n\t});\n\treturn res.ok;\n});\n\n/** How much of a card has to be showing, in px. */\nexport function seenHeight(cardHeight: number, viewHeight: number): number {\n\treturn 0.5 * Math.min(cardHeight, viewHeight);\n}\n\n// One observer for every card on the page. Steps of a tenth, so that a card\n// much taller than the screen still reports its way past half a screen.\nconst THRESHOLDS = [0, 0.1, 0.2, 0.3, 0.4, 0.5];\ntype Watch = { id: string; timer: ReturnType<typeof setTimeout> | null };\nconst watching = new Map<Element, Watch>();\nlet observer: IntersectionObserver | null = null;\n\nfunction onEntries(entries: IntersectionObserverEntry[]) {\n\tfor (const e of entries) {\n\t\tconst w = watching.get(e.target);\n\t\tif (!w) continue;\n\t\tconst view = e.rootBounds?.height ?? window.innerHeight;\n\t\tconst enough =\n\t\t\te.isIntersecting &&\n\t\t\te.intersectionRect.height >= seenHeight(e.boundingClientRect.height, view) - 1;\n\t\tif (enough) {\n\t\t\tw.timer ??= setTimeout(() => {\n\t\t\t\tqueue.add(w.id);\n\t\t\t\tunwatch(e.target);\n\t\t\t}, SEEN_MS);\n\t\t} else if (w.timer) {\n\t\t\tclearTimeout(w.timer);\n\t\t\tw.timer = null;\n\t\t}\n\t}\n}\n\nfunction unwatch(node: Element) {\n\tconst w = watching.get(node);\n\tif (!w) return;\n\tif (w.timer) clearTimeout(w.timer);\n\twatching.delete(node);\n\tobserver?.unobserve(node);\n}\n\nfunction start() {\n\tif (observer) return;\n\tobserver = new IntersectionObserver(onEntries, { threshold: THRESHOLDS });\n\tdocument.addEventListener('visibilitychange', () => {\n\t\tif (document.visibilityState === 'hidden') queue.flush();\n\t});\n\twindow.addEventListener('pagehide', () => queue.flush());\n}\n\n/** For a place that shows a post without a card — the wide post page. */\nexport function reportSeen(id: string): void {\n\tif (browser) queue.add(id);\n}\n\n/** Whether this tab has reported the post seen (the server may not know yet). */\nexport function seenInThisTab(id: string): boolean {\n\treturn queue.has(id);\n}\n\n/** `{@attach seenWhenVisible(post.id)}` on a card. Only for signed-in viewers —\n *  and never on a card whose media is hidden from them (an age-gated stand-in):\n *  they haven't seen the post. */\nexport function seenWhenVisible(id: string): Attachment<HTMLElement> {\n\treturn (node) => {\n\t\tif (!browser || typeof IntersectionObserver === 'undefined') return;\n\t\tstart();\n\t\twatching.set(node, { id, timer: null });\n\t\tobserver?.observe(node);\n\t\treturn () => {\n\t\t\tunwatch(node);\n\t\t\tqueue.flush();\n\t\t};\n\t};\n}\n"],"version":3}