From 85c8f674b4926d03ac8edfbf3622ea76c9289850 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 22 Jul 2023 14:55:21 -0500 Subject: [PATCH] Streaming: allow connecting if not logged in, gate certain topics --- .../api/hooks/streaming/useDirectStream.ts | 12 +++++++++++- app/soapbox/api/hooks/streaming/useListStream.ts | 7 +++++++ app/soapbox/api/hooks/streaming/useNostrStream.ts | 15 ++++++++++++--- .../api/hooks/streaming/useTimelineStream.ts | 2 +- app/soapbox/api/hooks/streaming/useUserStream.ts | 11 ++++++++++- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/app/soapbox/api/hooks/streaming/useDirectStream.ts b/app/soapbox/api/hooks/streaming/useDirectStream.ts index e97fc88609..9d3b47853d 100644 --- a/app/soapbox/api/hooks/streaming/useDirectStream.ts +++ b/app/soapbox/api/hooks/streaming/useDirectStream.ts @@ -1,7 +1,17 @@ +import { useLoggedIn } from 'soapbox/hooks'; + import { useTimelineStream } from './useTimelineStream'; function useDirectStream() { - return useTimelineStream('direct', 'direct'); + const { isLoggedIn } = useLoggedIn(); + + return useTimelineStream( + 'direct', + 'direct', + null, + null, + { enabled: isLoggedIn }, + ); } export { useDirectStream }; \ No newline at end of file diff --git a/app/soapbox/api/hooks/streaming/useListStream.ts b/app/soapbox/api/hooks/streaming/useListStream.ts index f38b0209c1..661bdce4f1 100644 --- a/app/soapbox/api/hooks/streaming/useListStream.ts +++ b/app/soapbox/api/hooks/streaming/useListStream.ts @@ -1,9 +1,16 @@ +import { useLoggedIn } from 'soapbox/hooks'; + import { useTimelineStream } from './useTimelineStream'; function useListStream(listId: string) { + const { isLoggedIn } = useLoggedIn(); + return useTimelineStream( `list:${listId}`, `list&list=${listId}`, + null, + null, + { enabled: isLoggedIn }, ); } diff --git a/app/soapbox/api/hooks/streaming/useNostrStream.ts b/app/soapbox/api/hooks/streaming/useNostrStream.ts index 3e6ef707f2..6748f95ea7 100644 --- a/app/soapbox/api/hooks/streaming/useNostrStream.ts +++ b/app/soapbox/api/hooks/streaming/useNostrStream.ts @@ -1,11 +1,20 @@ -import { useFeatures } from 'soapbox/hooks'; +import { useFeatures, useLoggedIn } from 'soapbox/hooks'; import { useTimelineStream } from './useTimelineStream'; function useNostrStream() { const features = useFeatures(); - const enabled = features.nostrSign && Boolean(window.nostr); - return useTimelineStream('nostr', 'nostr', null, null, { enabled }); + const { isLoggedIn } = useLoggedIn(); + + return useTimelineStream( + 'nostr', + 'nostr', + null, + null, + { + enabled: isLoggedIn && features.nostrSign && Boolean(window.nostr), + }, + ); } export { useNostrStream }; \ No newline at end of file diff --git a/app/soapbox/api/hooks/streaming/useTimelineStream.ts b/app/soapbox/api/hooks/streaming/useTimelineStream.ts index a0dfb2ded0..28998e0903 100644 --- a/app/soapbox/api/hooks/streaming/useTimelineStream.ts +++ b/app/soapbox/api/hooks/streaming/useTimelineStream.ts @@ -17,7 +17,7 @@ function useTimelineStream(...args: Parameters) { const streamingUrl = instance.urls.get('streaming_api'); const connect = () => { - if (enabled && accessToken && streamingUrl && !stream.current) { + if (enabled && streamingUrl && !stream.current) { stream.current = dispatch(connectTimelineStream(...args)); } }; diff --git a/app/soapbox/api/hooks/streaming/useUserStream.ts b/app/soapbox/api/hooks/streaming/useUserStream.ts index 5e0bb9aed4..cededf2aad 100644 --- a/app/soapbox/api/hooks/streaming/useUserStream.ts +++ b/app/soapbox/api/hooks/streaming/useUserStream.ts @@ -2,14 +2,23 @@ import { fetchAnnouncements } from 'soapbox/actions/announcements'; import { expandNotifications } from 'soapbox/actions/notifications'; import { expandHomeTimeline } from 'soapbox/actions/timelines'; import { useStatContext } from 'soapbox/contexts/stat-context'; +import { useLoggedIn } from 'soapbox/hooks'; import { useTimelineStream } from './useTimelineStream'; import type { AppDispatch } from 'soapbox/store'; function useUserStream() { + const { isLoggedIn } = useLoggedIn(); const statContext = useStatContext(); - return useTimelineStream('home', 'user', refresh, null, { statContext }); + + return useTimelineStream( + 'home', + 'user', + refresh, + null, + { statContext, enabled: isLoggedIn }, + ); } /** Refresh home timeline and notifications. */