Page MenuHomePhabricator

D10992.id36844.diff
No OneTemporary

D10992.id36844.diff

diff --git a/lib/hooks/invite-links.js b/lib/hooks/invite-links.js
--- a/lib/hooks/invite-links.js
+++ b/lib/hooks/invite-links.js
@@ -16,8 +16,10 @@
} from '../actions/thread-actions.js';
import { extractKeyserverIDFromID } from '../keyserver-conn/keyserver-call-utils.js';
import { createLoadingStatusSelector } from '../selectors/loading-selectors.js';
+import { isLoggedInToKeyserver } from '../selectors/user-selectors.js';
import type { KeyserverOverride } from '../shared/invite-links.js';
import { useIsKeyserverURLValid } from '../shared/keyserver-utils.js';
+import { callSingleKeyserverEndpointTimeout } from '../shared/timeouts.js';
import type { CalendarQuery } from '../types/entry-types.js';
import type { SetState } from '../types/hook-types.js';
import { defaultKeyserverInfo } from '../types/keyserver-types.js';
@@ -26,6 +28,7 @@
InviteLinkVerificationResponse,
} from '../types/link-types.js';
import type { LoadingStatus } from '../types/loading-types.js';
+import type { ThreadJoinPayload } from '../types/thread-types.js';
import { useDispatchActionPromise } from '../utils/redux-promise-utils.js';
import { useDispatch, useSelector } from '../utils/redux-utils.js';
@@ -157,60 +160,93 @@
const isKeyserverKnown = useSelector(state =>
keyserverID ? !!state.keyserverStore.keyserverInfos[keyserverID] : false,
);
+ const isAuthenticated = useSelector(isLoggedInToKeyserver(keyserverID));
const keyserverURL = keyserverOverride?.keyserverURL;
const isKeyserverURLValid = useIsKeyserverURLValid(keyserverURL);
- const createJoinCommunityAction = React.useCallback(async () => {
- invariant(
- communityID,
- 'CommunityID should be present while calling this function',
- );
- if (!isKeyserverKnown) {
- const isValid = await isKeyserverURLValid();
- if (!isValid || !keyserverURL) {
+ const [joinCommunityPromiseFunctions, setJoinCommunityPromiseFunctions] =
+ React.useState<?{
+ +resolve: ThreadJoinPayload => mixed,
+ +reject: () => mixed,
+ }>(null);
+ const createJoinCommunityAction = React.useCallback(() => {
+ return new Promise<ThreadJoinPayload>((resolve, reject) => {
+ setJoinCommunityPromiseFunctions({
+ resolve,
+ reject,
+ });
+ setTimeout(() => {
+ reject();
onInvalidLinkDetected();
- throw new Error(`Invalid keyserver url ${keyserverURL ?? ''}`);
+ }, callSingleKeyserverEndpointTimeout);
+ });
+ }, [onInvalidLinkDetected]);
+
+ React.useEffect(() => {
+ void (async () => {
+ if (joinCommunityPromiseFunctions && !isKeyserverKnown) {
+ const isValid = await isKeyserverURLValid();
+ if (!isValid || !keyserverURL) {
+ onInvalidLinkDetected();
+ joinCommunityPromiseFunctions.reject();
+ setJoinCommunityPromiseFunctions(null);
+ return;
+ }
+ dispatch({
+ type: addKeyserverActionType,
+ payload: {
+ keyserverAdminUserID: keyserverID,
+ newKeyserverInfo: defaultKeyserverInfo(keyserverURL),
+ },
+ });
}
- dispatch({
- type: addKeyserverActionType,
- payload: {
- keyserverAdminUserID: keyserverID,
- newKeyserverInfo: defaultKeyserverInfo(keyserverURL),
- },
- });
- // TODO wait for a keyserver auth
- }
- const query = calendarQuery();
- try {
- const result = await callJoinThread({
- threadID: communityID,
- calendarQuery: {
- startDate: query.startDate,
- endDate: query.endDate,
- filters: [
- ...query.filters,
- { type: 'threads', threadIDs: [communityID] },
- ],
- },
- inviteLinkSecret: inviteSecret,
- });
- onFinish();
- return result;
- } catch (e) {
- onInvalidLinkDetected();
- throw e;
- }
+ })();
}, [
- calendarQuery,
- callJoinThread,
- communityID,
dispatch,
- inviteSecret,
isKeyserverKnown,
isKeyserverURLValid,
+ joinCommunityPromiseFunctions,
keyserverID,
keyserverURL,
+ onInvalidLinkDetected,
+ ]);
+
+ React.useEffect(() => {
+ void (async () => {
+ if (joinCommunityPromiseFunctions && isAuthenticated) {
+ invariant(communityID, 'CommunityID should be set');
+ const query = calendarQuery();
+ try {
+ const result = await callJoinThread({
+ threadID: communityID,
+ calendarQuery: {
+ startDate: query.startDate,
+ endDate: query.endDate,
+ filters: [
+ ...query.filters,
+ { type: 'threads', threadIDs: [communityID] },
+ ],
+ },
+ inviteLinkSecret: inviteSecret,
+ });
+ onFinish();
+ joinCommunityPromiseFunctions.resolve(result);
+ } catch (e) {
+ onInvalidLinkDetected();
+ joinCommunityPromiseFunctions.reject();
+ } finally {
+ setJoinCommunityPromiseFunctions(null);
+ }
+ }
+ })();
+ }, [
+ calendarQuery,
+ callJoinThread,
+ communityID,
+ inviteSecret,
+ isAuthenticated,
+ joinCommunityPromiseFunctions,
onFinish,
onInvalidLinkDetected,
]);
diff --git a/lib/selectors/user-selectors.js b/lib/selectors/user-selectors.js
--- a/lib/selectors/user-selectors.js
+++ b/lib/selectors/user-selectors.js
@@ -143,9 +143,12 @@
);
const isLoggedInToKeyserver: (
- keyserverID: string,
+ keyserverID: ?string,
) => (state: BaseAppState<>) => boolean = _memoize(
- (keyserverID: string) => (state: BaseAppState<>) => {
+ (keyserverID: ?string) => (state: BaseAppState<>) => {
+ if (!keyserverID) {
+ return false;
+ }
const cookie = state.keyserverStore.keyserverInfos[keyserverID]?.cookie;
return !!cookie && cookie.startsWith('user=');
},

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 24, 5:39 AM (18 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2698543
Default Alt Text
D10992.id36844.diff (5 KB)

Event Timeline