Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/url-utils.js
// @flow | // @flow | ||||
import { pendingThreadIDRegex } from '../shared/thread-utils.js'; | import { pendingThreadIDRegex } from '../shared/thread-utils.js'; | ||||
export type URLInfo = { | export type URLInfo = { | ||||
+year?: number, | +year?: number, | ||||
+month?: number, // 1-indexed | +month?: number, // 1-indexed | ||||
+verify?: string, | +verify?: string, | ||||
+calendar?: boolean, | +calendar?: boolean, | ||||
+chat?: boolean, | +chat?: boolean, | ||||
+thread?: string, | +thread?: string, | ||||
+settings?: 'account' | 'danger-zone', | +settings?: 'account' | 'danger-zone', | ||||
+threadCreation?: boolean, | +threadCreation?: boolean, | ||||
+selectedUserList?: $ReadOnlyArray<string>, | +selectedUserList?: $ReadOnlyArray<string>, | ||||
+inviteSecret?: string, | |||||
... | ... | ||||
}; | }; | ||||
// We use groups to capture parts of the URL and any changes | // We use groups to capture parts of the URL and any changes | ||||
// to regexes must be reflected in infoFromURL. | // to regexes must be reflected in infoFromURL. | ||||
const yearRegex = new RegExp('(/|^)year/([0-9]+)(/|$)', 'i'); | const yearRegex = new RegExp('(/|^)year/([0-9]+)(/|$)', 'i'); | ||||
const monthRegex = new RegExp('(/|^)month/([0-9]+)(/|$)', 'i'); | const monthRegex = new RegExp('(/|^)month/([0-9]+)(/|$)', 'i'); | ||||
const threadRegex = new RegExp('(/|^)thread/([0-9]+)(/|$)', 'i'); | const threadRegex = new RegExp('(/|^)thread/([0-9]+)(/|$)', 'i'); | ||||
const verifyRegex = new RegExp('(/|^)verify/([a-f0-9]+)(/|$)', 'i'); | const verifyRegex = new RegExp('(/|^)verify/([a-f0-9]+)(/|$)', 'i'); | ||||
const calendarRegex = new RegExp('(/|^)calendar(/|$)', 'i'); | const calendarRegex = new RegExp('(/|^)calendar(/|$)', 'i'); | ||||
const chatRegex = new RegExp('(/|^)chat(/|$)', 'i'); | const chatRegex = new RegExp('(/|^)chat(/|$)', 'i'); | ||||
const accountSettingsRegex = new RegExp('(/|^)settings/account(/|$)', 'i'); | const accountSettingsRegex = new RegExp('(/|^)settings/account(/|$)', 'i'); | ||||
const dangerZoneRegex = new RegExp('(/|^)settings/danger-zone(/|$)', 'i'); | const dangerZoneRegex = new RegExp('(/|^)settings/danger-zone(/|$)', 'i'); | ||||
const threadPendingRegex = new RegExp( | const threadPendingRegex = new RegExp( | ||||
`(/|^)thread/(${pendingThreadIDRegex})(/|$)`, | `(/|^)thread/(${pendingThreadIDRegex})(/|$)`, | ||||
'i', | 'i', | ||||
); | ); | ||||
const threadCreationRegex = new RegExp( | const threadCreationRegex = new RegExp( | ||||
'(/|^)thread/new(/([0-9]+([+][0-9]+)*))?(/|$)', | '(/|^)thread/new(/([0-9]+([+][0-9]+)*))?(/|$)', | ||||
'i', | 'i', | ||||
); | ); | ||||
const inviteLinkRegex = new RegExp( | |||||
'(/|^)handle/invite/([a-zA-Z0-9]+)(/|$)', | |||||
'i', | |||||
); | |||||
function infoFromURL(url: string): URLInfo { | function infoFromURL(url: string): URLInfo { | ||||
const yearMatches = yearRegex.exec(url); | const yearMatches = yearRegex.exec(url); | ||||
const monthMatches = monthRegex.exec(url); | const monthMatches = monthRegex.exec(url); | ||||
const threadMatches = threadRegex.exec(url); | const threadMatches = threadRegex.exec(url); | ||||
const verifyMatches = verifyRegex.exec(url); | const verifyMatches = verifyRegex.exec(url); | ||||
const calendarTest = calendarRegex.test(url); | const calendarTest = calendarRegex.test(url); | ||||
const chatTest = chatRegex.test(url); | const chatTest = chatRegex.test(url); | ||||
const accountSettingsTest = accountSettingsRegex.test(url); | const accountSettingsTest = accountSettingsRegex.test(url); | ||||
const dangerZoneTest = dangerZoneRegex.test(url); | const dangerZoneTest = dangerZoneRegex.test(url); | ||||
const threadPendingMatches = threadPendingRegex.exec(url); | const threadPendingMatches = threadPendingRegex.exec(url); | ||||
const threadCreateMatches = threadCreationRegex.exec(url); | const threadCreateMatches = threadCreationRegex.exec(url); | ||||
const inviteLinkMatches = inviteLinkRegex.exec(url); | |||||
const returnObj = {}; | const returnObj = {}; | ||||
if (yearMatches) { | if (yearMatches) { | ||||
returnObj.year = parseInt(yearMatches[2], 10); | returnObj.year = parseInt(yearMatches[2], 10); | ||||
} | } | ||||
if (monthMatches) { | if (monthMatches) { | ||||
const month = parseInt(monthMatches[2], 10); | const month = parseInt(monthMatches[2], 10); | ||||
if (month < 1 || month > 12) { | if (month < 1 || month > 12) { | ||||
Show All 9 Lines | function infoFromURL(url: string): URLInfo { | ||||
} | } | ||||
if (threadCreateMatches) { | if (threadCreateMatches) { | ||||
returnObj.threadCreation = true; | returnObj.threadCreation = true; | ||||
returnObj.selectedUserList = threadCreateMatches[3]?.split('+') ?? []; | returnObj.selectedUserList = threadCreateMatches[3]?.split('+') ?? []; | ||||
} | } | ||||
if (verifyMatches) { | if (verifyMatches) { | ||||
returnObj.verify = verifyMatches[2]; | returnObj.verify = verifyMatches[2]; | ||||
} | } | ||||
if (inviteLinkMatches) { | |||||
returnObj.inviteSecret = inviteLinkMatches[2]; | |||||
} | |||||
if (calendarTest) { | if (calendarTest) { | ||||
returnObj.calendar = true; | returnObj.calendar = true; | ||||
} else if (chatTest) { | } else if (chatTest) { | ||||
returnObj.chat = true; | returnObj.chat = true; | ||||
} else if (accountSettingsTest) { | } else if (accountSettingsTest) { | ||||
returnObj.settings = 'account'; | returnObj.settings = 'account'; | ||||
} else if (dangerZoneTest) { | } else if (dangerZoneTest) { | ||||
returnObj.settings = 'danger-zone'; | returnObj.settings = 'danger-zone'; | ||||
} | } | ||||
return returnObj; | return returnObj; | ||||
} | } | ||||
const setURLPrefix = 'SET_URL_PREFIX'; | const setURLPrefix = 'SET_URL_PREFIX'; | ||||
export { infoFromURL, setURLPrefix }; | export { infoFromURL, setURLPrefix }; |