Simplify event signing

This commit is contained in:
Alex Gleason 2023-05-20 21:15:53 -05:00
parent e5837ebefb
commit f318c35544
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 25 additions and 15 deletions

View file

@ -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<string, any> = {}) =>
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,
};

View file

@ -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<IUI> = ({ children }) => {
const instance = useInstance();
const statContext = useStatContext();
const disconnect = useRef<any>(null);
const userStream = useRef<any>(null);
const nostrStream = useRef<any>(null);
const node = useRef<HTMLDivElement | null>(null);
const hotkeys = useRef<HTMLDivElement | null>(null);
@ -416,15 +417,24 @@ const UI: React.FC<IUI> = ({ 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;
}
};