2024-01-09 11:54:30 +00:00
|
|
|
/**
|
|
|
|
* Generates a random nickname.
|
|
|
|
* @param base Nickname prefix
|
|
|
|
* @returns A nickname like "Given Base 12345"
|
|
|
|
*/
|
2023-05-04 17:14:23 +00:00
|
|
|
function randomNick (base: string): string {
|
|
|
|
// using a 6 digit random number to generate a nickname with low colision risk
|
|
|
|
const n = 100000 + Math.floor(Math.random() * 900000)
|
|
|
|
return base + ' ' + n.toString()
|
|
|
|
}
|
|
|
|
|
2024-01-09 11:54:30 +00:00
|
|
|
/**
|
2024-01-09 16:56:33 +00:00
|
|
|
* Get the previous anonymous nickname (stored in localStorage).
|
|
|
|
* If it was set more than 12 hours ago, it will be ignored (considering the nickname should not remain forever).
|
2024-01-09 11:54:30 +00:00
|
|
|
* @returns previous nickname or null
|
|
|
|
*/
|
|
|
|
function getPreviousAnonymousNick (): string | null {
|
|
|
|
try {
|
2024-01-09 16:56:33 +00:00
|
|
|
const lastAccess = parseInt(localStorage.getItem('livechat-previous-anonymous-nickname-last-access') ?? '0')
|
|
|
|
const now = Date.now()
|
|
|
|
if (lastAccess && !isNaN(lastAccess)) {
|
|
|
|
const expires = lastAccess + 1000 * 60 * 60 * 12 // nickname expires after 12 hours
|
|
|
|
if (now > expires) {
|
|
|
|
console.log('Anonymous nickname has expired.')
|
|
|
|
localStorage.removeItem('livechat-previous-anonymous-nickname')
|
|
|
|
localStorage.removeItem('livechat-previous-anonymous-nickname-last-access')
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const nick = localStorage.getItem('livechat-previous-anonymous-nickname')
|
|
|
|
if (nick !== null) {
|
|
|
|
localStorage.setItem('livechat-previous-anonymous-nickname-last-access', now.toString())
|
|
|
|
}
|
|
|
|
return nick
|
2024-01-09 11:54:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-09 16:56:33 +00:00
|
|
|
* Stores the chosen nickname in localStorage.
|
2024-01-09 11:54:30 +00:00
|
|
|
*/
|
|
|
|
function setPreviousAnonymousNick (nick: string): void {
|
|
|
|
try {
|
|
|
|
console.log('Storing anonymous nickname', nick)
|
2024-01-09 16:56:33 +00:00
|
|
|
localStorage.setItem('livechat-previous-anonymous-nickname', nick)
|
|
|
|
localStorage.setItem('livechat-previous-anonymous-nickname-last-access', Date.now().toString())
|
2024-01-09 11:54:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 17:14:23 +00:00
|
|
|
export {
|
2024-01-09 11:54:30 +00:00
|
|
|
randomNick,
|
|
|
|
getPreviousAnonymousNick,
|
|
|
|
setPreviousAnonymousNick
|
2023-05-04 17:14:23 +00:00
|
|
|
}
|