Skip to content

Commit 4352dc9

Browse files
chrisbobbegnprice
authored andcommitted
getNearOperandFromLink [nfc]: Return null for none instead of zero
NFC because we always drop the return value of this value on the floor. Re: whether 0 can actually be the ID of a real message…see https://chat.zulip.org/#narrow/stream/19-documentation/topic/IDs.20being.20zero/near/1112947 where Tim says: > Yeah, @_**Anders Kaseorg** is correct. The server has an > invariant that it won't generate an ID of 0 but it's possible to > create such an entry via raw database manipulation.
1 parent 2a73e3d commit 4352dc9

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

src/message/messagesActions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const messageLinkPress =
3838
if (narrow) {
3939
// This call is OK: `narrow` is truthy, so isNarrowLink(…) was true.
4040
const nearOperand = getNearOperandFromLink(href, auth.realm);
41-
dispatch(doNarrow(narrow, nearOperand));
41+
dispatch(doNarrow(narrow, nearOperand ?? 0));
4242
} else if (!isUrlOnRealm(href, auth.realm)) {
4343
openLinkWithUserPreference(href, getGlobalSettings());
4444
} else {

src/utils/__tests__/internalLinks-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ describe('getNarrowFromLink', () => {
420420

421421
describe('getNearOperandFromLink', () => {
422422
test('not message link', () => {
423-
expect(getNearOperandFromLink('https://example.com/#narrow/is/private', realm)).toBe(0);
424-
expect(getNearOperandFromLink('https://example.com/#narrow/stream/jest', realm)).toBe(0);
423+
expect(getNearOperandFromLink('https://example.com/#narrow/is/private', realm)).toBe(null);
424+
expect(getNearOperandFromLink('https://example.com/#narrow/stream/jest', realm)).toBe(null);
425425
});
426426

427427
test('`near` is the only operator', () => {

src/utils/internalLinks.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,13 @@ export const getNarrowFromLink = (
227227

228228
/**
229229
* From a URL and realm with `isNarrowLink(url, realm) === true`, give
230-
* message_id if the URL has /near/{message_id}, otherwise give zero.
230+
* message_id if the URL has /near/{message_id}, otherwise give null.
231231
*
232232
* This performs a call to `new URL` and therefore may take a fraction of a
233233
* millisecond. Avoid using in a context where it might be called more than
234234
* 10 or 100 times per user action.
235235
*/
236-
export const getNearOperandFromLink = (url: string, realm: URL): number => {
236+
export const getNearOperandFromLink = (url: string, realm: URL): number | null => {
237237
// isNarrowLink(…) is true, by jsdoc, so this call is OK.
238238
const hashSegments = getHashSegmentsFromNarrowLink(url, realm);
239239

@@ -249,7 +249,7 @@ export const getNearOperandFromLink = (url: string, realm: URL): number => {
249249
&& str === 'near',
250250
);
251251
if (nearOperatorIndex < 0) {
252-
return 0;
252+
return null;
253253
}
254254

255255
// We expect an operand to directly follow its operator.
@@ -258,7 +258,7 @@ export const getNearOperandFromLink = (url: string, realm: URL): number => {
258258
const nearOperandStr = hashSegments[nearOperandIndex];
259259
// Must look like a message ID
260260
if (!/^[0-9]+$/.test(nearOperandStr)) {
261-
return 0;
261+
return null;
262262
}
263263

264264
return parseInt(nearOperandStr, 10);

0 commit comments

Comments
 (0)