mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
Update commit checkpoints, add API route for stream viewers, and synchronize viewer count to database
This commit is contained in:
@@ -117,7 +117,7 @@ export function useStreamViewers(username: string): UseStreamViewersResult {
|
||||
const perSession = presenceFeed.perSession
|
||||
if (perSession) {
|
||||
for (const sessionId of Object.keys(perSession)) {
|
||||
const entry = (perSession as Record<string, { value?: { lastActive: number } }>)[sessionId]
|
||||
const entry = (presenceFeed.perSession as Record<string, { value?: { lastActive: number } }>)[sessionId]
|
||||
if (entry?.value) {
|
||||
const age = now - entry.value.lastActive
|
||||
if (age < PRESENCE_STALE_MS) {
|
||||
@@ -139,6 +139,24 @@ export function useStreamViewers(username: string): UseStreamViewersResult {
|
||||
return () => clearInterval(interval)
|
||||
}, [presenceFeed?.$isLoaded, presenceFeed])
|
||||
|
||||
// Sync viewer count to database for external access
|
||||
useEffect(() => {
|
||||
if (viewerCount === 0) return
|
||||
|
||||
// Debounce the API call
|
||||
const timeout = setTimeout(() => {
|
||||
fetch(`/api/streams/${username}/viewers`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ viewerCount }),
|
||||
}).catch((err) => {
|
||||
console.error("Failed to sync viewer count:", err)
|
||||
})
|
||||
}, 1000)
|
||||
|
||||
return () => clearTimeout(timeout)
|
||||
}, [viewerCount, username])
|
||||
|
||||
return {
|
||||
viewerCount,
|
||||
isConnected: me.$isLoaded,
|
||||
|
||||
Reference in New Issue
Block a user