From f318c35544487654022605f575c48d0cd3822b53 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 20 May 2023 21:15:53 -0500 Subject: [PATCH] Simplify event signing --- app/soapbox/actions/streaming.ts | 16 ++++++++-------- app/soapbox/features/ui/index.tsx | 24 +++++++++++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/soapbox/actions/streaming.ts b/app/soapbox/actions/streaming.ts index a0dddda83..65752c223 100644 --- a/app/soapbox/actions/streaming.ts +++ b/app/soapbox/actions/streaming.ts @@ -182,13 +182,9 @@ const connectTimelineStream = ( dispatch({ type: MARKER_FETCH_SUCCESS, marker: JSON.parse(data.payload) }); break; case 'nostr.sign': - (async () => { - const event = await window.nostr?.signEvent(JSON.parse(data.payload)).catch(() => undefined); - - if (event) { - websocket.send(JSON.stringify({ event: 'nostr.sign', payload: JSON.stringify(event) })); - } - })(); + window.nostr?.signEvent(JSON.parse(data.payload)) + .then((data) => websocket.send(JSON.stringify({ type: 'nostr.sign', data }))) + .catch(() => console.warn('Failed to sign Nostr event.')); break; } }, @@ -201,7 +197,7 @@ const refreshHomeTimelineAndNotification = (dispatch: AppDispatch, done?: () => dispatch(fetchAnnouncements(done)))))); const connectUserStream = (opts?: StreamOpts) => - connectTimelineStream('home', `user${'nostr' in window ? '&nostr=true' : ''}`, refreshHomeTimelineAndNotification, null, opts); + connectTimelineStream('home', 'user', refreshHomeTimelineAndNotification, null, opts); const connectCommunityStream = ({ onlyMedia }: Record = {}) => connectTimelineStream(`community${onlyMedia ? ':media' : ''}`, `public:local${onlyMedia ? ':media' : ''}`); @@ -224,6 +220,9 @@ const connectListStream = (id: string) => const connectGroupStream = (id: string) => connectTimelineStream(`group:${id}`, `group&group=${id}`); +const connectNostrStream = () => + connectTimelineStream('nostr', 'nostr'); + export { STREAMING_CHAT_UPDATE, STREAMING_FOLLOW_RELATIONSHIPS_UPDATE, @@ -236,4 +235,5 @@ export { connectDirectStream, connectListStream, connectGroupStream, + connectNostrStream, }; diff --git a/app/soapbox/features/ui/index.tsx b/app/soapbox/features/ui/index.tsx index 298e3314c..4d487dfea 100644 --- a/app/soapbox/features/ui/index.tsx +++ b/app/soapbox/features/ui/index.tsx @@ -16,7 +16,7 @@ import { openModal } from 'soapbox/actions/modals'; import { expandNotifications } from 'soapbox/actions/notifications'; import { register as registerPushNotifications } from 'soapbox/actions/push-notifications'; import { fetchScheduledStatuses } from 'soapbox/actions/scheduled-statuses'; -import { connectUserStream } from 'soapbox/actions/streaming'; +import { connectNostrStream, connectUserStream } from 'soapbox/actions/streaming'; import { fetchSuggestionsForTimeline } from 'soapbox/actions/suggestions'; import { expandHomeTimeline } from 'soapbox/actions/timelines'; import GroupLookupHoc from 'soapbox/components/hoc/group-lookup-hoc'; @@ -391,7 +391,8 @@ const UI: React.FC = ({ children }) => { const instance = useInstance(); const statContext = useStatContext(); - const disconnect = useRef(null); + const userStream = useRef(null); + const nostrStream = useRef(null); const node = useRef(null); const hotkeys = useRef(null); @@ -416,15 +417,24 @@ const UI: React.FC = ({ children }) => { }; const connectStreaming = () => { - if (!disconnect.current && accessToken && streamingUrl) { - disconnect.current = dispatch(connectUserStream({ statContext })); + if (accessToken && streamingUrl) { + if (!userStream.current) { + userStream.current = dispatch(connectUserStream({ statContext })); + } + if (!nostrStream.current && window.nostr) { + nostrStream.current = dispatch(connectNostrStream()); + } } }; const disconnectStreaming = () => { - if (disconnect.current) { - disconnect.current(); - disconnect.current = null; + if (userStream.current) { + userStream.current(); + userStream.current = null; + } + if (nostrStream.current) { + nostrStream.current(); + nostrStream.current = null; } };