Skip to content

Commit 4925441

Browse files
committed
msglist: Give error alert when tapped link has URL that didn't parse
Just before this commit, if an `href` fails to parse, it would do so in the `new URL` call that's meant to pass a URL object to `getNarrowFromLink` (the line with the comment "TODO: Use parsedUrl, below"). The error would propagate up to messageLinkPress's caller, in handleWebViewOutboundEvent, and from there to a React event handler, with the result (I expect) that the app wouldn't crash, but it also wouldn't give any useful feedback to the user. So, give some useful feedback to the user. The issue is filed as "Fail early (and tell user) on a message-link tap when the URL doesn't parse" (#5518) . I guess we have been failing early, we just haven't been telling the user. Fixes: #5518
1 parent c907c73 commit 4925441

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/message/messagesActions.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow strict-local */
22
import * as logging from '../utils/logging';
33
import * as NavigationService from '../nav/NavigationService';
4-
import type { Message, Narrow, ThunkAction } from '../types';
4+
import type { GetText, Message, Narrow, ThunkAction } from '../types';
55
import { getAuth, getRealm, getMessages, getZulipFeatureLevel } from '../selectors';
66
import { getNearOperandFromLink, getNarrowFromLink } from '../utils/internalLinks';
77
import { openLinkWithUserPreference } from '../utils/openLink';
@@ -21,6 +21,7 @@ import {
2121
caseNarrowDefault,
2222
} from '../utils/narrow';
2323
import { hasMessageEverBeenInStream, hasMessageEverHadTopic } from './messageSelectors';
24+
import { showErrorAlert } from '../utils/info';
2425

2526
/**
2627
* Navigate to the given narrow.
@@ -215,24 +216,23 @@ const doNarrowNearLink =
215216
* Handle a link tap that isn't an image we want to show in the lightbox.
216217
*/
217218
export const messageLinkPress =
218-
(href: string): ThunkAction<Promise<void>> =>
219+
(href: string, _: GetText): ThunkAction<Promise<void>> =>
219220
async (dispatch, getState, { getGlobalSettings }) => {
220221
const state = getState();
221222
const auth = getAuth(state);
222223
const streamsById = getStreamsById(state);
223224
const streamsByName = getStreamsByName(state);
224225
const ownUserId = getOwnUserId(state);
225-
const narrow = getNarrowFromLink(
226-
new URL(href, auth.realm), // TODO: Use parsedUrl, below.
227-
auth.realm,
228-
streamsById,
229-
streamsByName,
230-
ownUserId,
231-
);
232226

233227
const parsedUrl = tryParseUrl(href, auth.realm);
228+
if (!parsedUrl) {
229+
showErrorAlert(_('Cannot open link'), _('Invalid URL.'));
230+
return;
231+
}
234232
// TODO: Replace all uses of `href` below with `parsedUrl`.
235233

234+
const narrow = getNarrowFromLink(parsedUrl, auth.realm, streamsById, streamsByName, ownUserId);
235+
236236
// TODO: In some cases getNarrowFromLink successfully parses the link, but
237237
// finds it points somewhere we can't see: in particular, to a stream
238238
// that's hidden from our user (perhaps doesn't exist.) For those,
@@ -247,7 +247,7 @@ export const messageLinkPress =
247247
}
248248

249249
await dispatch(doNarrowNearLink(narrow, nearOperand));
250-
} else if (!parsedUrl || !isUrlOnRealm(parsedUrl, auth.realm)) {
250+
} else if (!isUrlOnRealm(parsedUrl, auth.realm)) {
251251
openLinkWithUserPreference(href, getGlobalSettings());
252252
} else {
253253
const url = (await api.tryGetFileTemporaryUrl(parsedUrl, auth)) ?? parsedUrl;

src/webview/handleOutboundEvents.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ export const handleWebViewOutboundEvent = (
313313
if (isUrlAnImage(event.href)) {
314314
handleImage(props, navigation, event.href, event.messageId);
315315
} else {
316-
props.dispatch(messageLinkPress(event.href));
316+
props.dispatch(messageLinkPress(event.href, props._));
317317
}
318318
break;
319319

static/translations/messages_en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"Notify everyone": "Notify everyone",
1313
"Cannot open image": "Cannot open image",
1414
"Invalid image URL.": "Invalid image URL.",
15+
"Cannot open link": "Cannot open link",
16+
"Invalid URL.": "Invalid URL.",
1517
"Invisible mode": "Invisible mode",
1618
"{num_of_people, plural,\n one {This message has been <z-link>read</z-link> by {num_of_people} person:}\n other {This message has been <z-link>read</z-link> by {num_of_people} people:}}": "{num_of_people, plural,\n one {This message has been <z-link>read</z-link> by {num_of_people} person:}\n other {This message has been <z-link>read</z-link> by {num_of_people} people:}}",
1719
"View read receipts": "View read receipts",

0 commit comments

Comments
 (0)