2023-10-28 16:24:50 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
// AID
|
|
|
|
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ2の[ノイズ文字列]
|
2024-01-21 07:44:03 +01:00
|
|
|
import { customAlphabet } from 'nanoid';
|
|
|
|
|
2023-10-28 16:24:50 +02:00
|
|
|
export const aidRegExp = /^[0-9a-z]{10}$/;
|
|
|
|
|
2024-01-21 07:44:03 +01:00
|
|
|
const rand = customAlphabet('0123456789', 5);
|
|
|
|
|
2023-10-28 16:24:50 +02:00
|
|
|
const TIME2000 = 946684800000;
|
|
|
|
let counter: number;
|
|
|
|
|
2024-01-21 07:44:03 +01:00
|
|
|
counter = parseInt(rand(), 10);
|
2023-10-28 16:24:50 +02:00
|
|
|
|
|
|
|
function getTime(time: number): string {
|
|
|
|
time = time - TIME2000;
|
|
|
|
if (time < 0) time = 0;
|
|
|
|
|
|
|
|
return time.toString(36).padStart(8, '0');
|
|
|
|
}
|
|
|
|
|
2024-01-21 04:12:51 +01:00
|
|
|
function getNoise(ctr: number): string {
|
|
|
|
return ctr.toString(36).padStart(2, '0').slice(-2);
|
2023-10-28 16:24:50 +02:00
|
|
|
}
|
|
|
|
|
2024-01-21 04:12:51 +01:00
|
|
|
export function genAid(t: number, ctr: number | null = null): string {
|
2023-10-28 16:24:50 +02:00
|
|
|
if (isNaN(t)) throw new Error('Failed to create AID: Invalid Date');
|
2024-01-21 04:12:51 +01:00
|
|
|
if (!ctr) {
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
return getTime(t) + getNoise(ctr ?? counter);
|
2023-10-28 16:24:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function parseAid(id: string): { date: Date; } {
|
|
|
|
const time = parseInt(id.slice(0, 8), 36) + TIME2000;
|
|
|
|
return { date: new Date(time) };
|
|
|
|
}
|