2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-12 11:37:45 +09:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { ulid } from 'ulid';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { Config } from '@/config.js';
|
2023-12-03 14:38:42 +09:00
|
|
|
import { genAid, isSafeAidT, parseAid } from '@/misc/id/aid.js';
|
|
|
|
import { genAidx, isSafeAidxT, parseAidx } from '@/misc/id/aidx.js';
|
|
|
|
import { genMeid, isSafeMeidT, parseMeid } from '@/misc/id/meid.js';
|
|
|
|
import { genMeidg, isSafeMeidgT, parseMeidg } from '@/misc/id/meidg.js';
|
|
|
|
import { genObjectId, isSafeObjectIdT, parseObjectId } from '@/misc/id/object-id.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-04-09 04:41:06 +09:00
|
|
|
import { parseUlid } from '@/misc/id/ulid.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class IdService {
|
2022-12-10 08:55:07 +09:00
|
|
|
private method: string;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
) {
|
2022-12-10 08:55:07 +09:00
|
|
|
this.method = config.id.toLowerCase();
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2023-12-03 14:38:42 +09:00
|
|
|
@bindThis
|
|
|
|
public isSafeT(t: number): boolean {
|
|
|
|
switch (this.method) {
|
|
|
|
case 'aid': return isSafeAidT(t);
|
|
|
|
case 'aidx': return isSafeAidxT(t);
|
|
|
|
case 'meid': return isSafeMeidT(t);
|
|
|
|
case 'meidg': return isSafeMeidgT(t);
|
|
|
|
case 'ulid': return t > 0;
|
|
|
|
case 'objectid': return isSafeObjectIdT(t);
|
|
|
|
default: throw new Error('unrecognized id generation method');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 10:45:22 +09:00
|
|
|
/**
|
|
|
|
* 時間を元にIDを生成します(省略時は現在日時)
|
|
|
|
* @param time 日時
|
|
|
|
*/
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-10-16 10:45:22 +09:00
|
|
|
public gen(time?: number): string {
|
|
|
|
const t = (!time || (time > Date.now())) ? Date.now() : time;
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-12-10 08:55:07 +09:00
|
|
|
switch (this.method) {
|
2023-10-16 10:45:22 +09:00
|
|
|
case 'aid': return genAid(t);
|
|
|
|
case 'aidx': return genAidx(t);
|
|
|
|
case 'meid': return genMeid(t);
|
|
|
|
case 'meidg': return genMeidg(t);
|
|
|
|
case 'ulid': return ulid(t);
|
|
|
|
case 'objectid': return genObjectId(t);
|
2022-09-18 03:27:08 +09:00
|
|
|
default: throw new Error('unrecognized id generation method');
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 11:49:58 +09:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public parse(id: string): { date: Date; } {
|
|
|
|
switch (this.method) {
|
|
|
|
case 'aid': return parseAid(id);
|
2023-09-06 18:33:51 +09:00
|
|
|
case 'aidx': return parseAidx(id);
|
2023-06-23 16:30:47 +09:00
|
|
|
case 'objectid': return parseObjectId(id);
|
2023-04-09 04:41:06 +09:00
|
|
|
case 'meid': return parseMeid(id);
|
|
|
|
case 'meidg': return parseMeidg(id);
|
|
|
|
case 'ulid': return parseUlid(id);
|
2023-04-03 11:49:58 +09:00
|
|
|
default: throw new Error('unrecognized id generation method');
|
|
|
|
}
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|