diff --git a/lib/utils/action-utils.js b/lib/utils/action-utils.js --- a/lib/utils/action-utils.js +++ b/lib/utils/action-utils.js @@ -12,6 +12,7 @@ CallServerEndpointOptions, } from './call-server-endpoint.js'; import { getConfig } from './config.js'; +import { idWithKeyserverInfoRegex } from './validation-utils.js'; import { serverCallStateSelector } from '../selectors/server-calls.js'; import { logInActionSources, @@ -35,6 +36,17 @@ import type { ConnectionStatus } from '../types/socket-types.js'; import type { CurrentUserInfo } from '../types/user-types.js'; +const keyserverIDfromIDRegex = '[0-9]+(?=\\|)'; + +function extractKeyserverIDFromID(id: string): ?string { + if (!id.match(idWithKeyserverInfoRegex)) { + return null; + } + + const result = id.match(keyserverIDfromIDRegex); + return result ? result[0] : null; +} + let nextPromiseIndex = 0; export type ActionTypes< @@ -460,4 +472,5 @@ registerActiveSocket, useServerCall, bindCookieAndUtilsIntoCallServerEndpoint, + extractKeyserverIDFromID, }; diff --git a/lib/utils/action-utils.test.js b/lib/utils/action-utils.test.js new file mode 100644 --- /dev/null +++ b/lib/utils/action-utils.test.js @@ -0,0 +1,33 @@ +// @flow + +import { extractKeyserverIDFromID } from './action-utils.js'; + +describe('extractKeyserverIDFromID', () => { + it('should return for |', () => { + const keyserverID = '404'; + const id = keyserverID + '|1234'; + expect(extractKeyserverIDFromID(id)).toBe(keyserverID); + }); + it('should return null if the id contains nonnumerical characters', () => { + const id1 = 'a256|1234'; + expect(extractKeyserverIDFromID(id1)).toBe(null); + const id2 = '256|1234b'; + expect(extractKeyserverIDFromID(id2)).toBe(null); + const id3 = '256|c1234'; + expect(extractKeyserverIDFromID(id3)).toBe(null); + }); + it("should return null if the id doesn't contain |", () => { + const id = '2561234'; + expect(extractKeyserverIDFromID(id)).toBe(null); + }); + it('should return null if the | is not between two numbers', () => { + const id1 = '2561234|'; + expect(extractKeyserverIDFromID(id1)).toBe(null); + const id2 = '|2561234'; + expect(extractKeyserverIDFromID(id2)).toBe(null); + const id3 = '2561234|a'; + expect(extractKeyserverIDFromID(id3)).toBe(null); + const id4 = 'a|2561234'; + expect(extractKeyserverIDFromID(id4)).toBe(null); + }); +}); 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 @@ -104,6 +104,7 @@ const ashoatKeyserverID = '256'; const idSchemaRegex = '(?:[0-9]+\\|)?[0-9]+'; +const idWithKeyserverInfoRegex = '^[0-9]+\\|[0-9]+$'; const pendingThreadIDRegex = `pending/(type[0-9]+/[0-9]+(\\+[0-9]+)*|sidebar/${idSchemaRegex})`; @@ -131,5 +132,6 @@ assertWithValidator, ashoatKeyserverID, idSchemaRegex, + idWithKeyserverInfoRegex, pendingThreadIDRegex, };