diff --git a/lib/shared/thread-utils.js b/lib/shared/thread-utils.js --- a/lib/shared/thread-utils.js +++ b/lib/shared/thread-utils.js @@ -101,6 +101,7 @@ import { useSelector } from '../utils/redux-utils.js'; import { firstLine } from '../utils/string-utils.js'; import { trimText } from '../utils/text-utils.js'; +import { idSchemaRegex } from '../utils/validation-utils.js'; const chatNameMaxLength = 191; const chatNameMinLength = 0; @@ -272,8 +273,7 @@ return `pending/${pendingThreadTypeString}${pendingThreadKey}`; } -const pendingThreadIDRegex = - 'pending/(type[0-9]+/[0-9]+(\\+[0-9]+)*|sidebar/[0-9]+)'; +const pendingThreadIDRegex = `pending/(type[0-9]+/[0-9]+(\\+[0-9]+)*|sidebar/${idSchemaRegex})`; type PendingThreadIDContents = { +threadType: ThreadType, diff --git a/lib/shared/thread-utils.test.js b/lib/shared/thread-utils.test.js --- a/lib/shared/thread-utils.test.js +++ b/lib/shared/thread-utils.test.js @@ -13,6 +13,15 @@ expect(parsePendingThreadID('pending/sidebar/12345')).toStrictEqual( sidebarResult, ); + + const sidebarResultWithNewSchema = { + threadType: threadTypes.SIDEBAR, + memberIDs: [], + sourceMessageID: '789|12345', + }; + expect(parsePendingThreadID('pending/sidebar/789|12345')).toStrictEqual( + sidebarResultWithNewSchema, + ); }); it('should return correct data for real pending sidebar ID', () => { diff --git a/lib/utils/url-utils.js b/lib/utils/url-utils.js --- a/lib/utils/url-utils.js +++ b/lib/utils/url-utils.js @@ -1,5 +1,6 @@ // @flow +import { idSchemaRegex } from './validation-utils.js'; import { pendingThreadIDRegex } from '../shared/thread-utils.js'; export type URLInfo = { @@ -20,7 +21,7 @@ // to regexes must be reflected in infoFromURL. const yearRegex = new RegExp('(/|^)year/([0-9]+)(/|$)', 'i'); const monthRegex = new RegExp('(/|^)month/([0-9]+)(/|$)', 'i'); -const threadRegex = new RegExp('(/|^)thread/([0-9]+)(/|$)', 'i'); +const threadRegex = new RegExp(`(/|^)thread/(${idSchemaRegex})(/|$)`, 'i'); const verifyRegex = new RegExp('(/|^)verify/([a-f0-9]+)(/|$)', 'i'); const calendarRegex = new RegExp('(/|^)calendar(/|$)', 'i'); const chatRegex = new RegExp('(/|^)chat(/|$)', 'i'); diff --git a/lib/utils/validation-utils.js b/lib/utils/validation-utils.js --- a/lib/utils/validation-utils.js +++ b/lib/utils/validation-utils.js @@ -219,6 +219,8 @@ return input; } +const idSchemaRegex = '(?:[0-9]+\\|)?[0-9]+'; + export { tBool, tString, @@ -245,4 +247,5 @@ convertClientIDsToServerIDs, convertServerIDsToClientIDs, convertObject, + idSchemaRegex, }; diff --git a/lib/utils/validation-utils.test.js b/lib/utils/validation-utils.test.js --- a/lib/utils/validation-utils.test.js +++ b/lib/utils/validation-utils.test.js @@ -1,5 +1,6 @@ // @flow +import invariant from 'invariant'; import t from 'tcomb'; import { @@ -10,6 +11,7 @@ tID, convertServerIDsToClientIDs, convertClientIDsToServerIDs, + idSchemaRegex, } from './validation-utils.js'; import { threadTypes } from '../types/thread-types-enum.js'; import { values } from '../utils/objects.js'; @@ -189,3 +191,19 @@ ).toStrictEqual(serverData); }); }); + +describe('idSchemaRegex tests', () => { + it('should capture ids', () => { + const regex = new RegExp(`^(${idSchemaRegex})$`); + const ids = ['123|123', '0|0', '123', '0']; + + for (const id of ids) { + const result = regex.exec(id); + expect(result).not.toBeNull(); + invariant(result, 'result is not null'); + const matches = [...result]; + expect(matches).toHaveLength(2); + expect(matches[1]).toBe(id); + } + }); +});