mirror of
https://github.com/misskey-dev/misskey.git
synced 2024-12-21 08:55:08 +01:00
48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import { Inject, Injectable } from '@nestjs/common';
|
|
import { ulid } from 'ulid';
|
|
import { DI } from '@/di-symbols.js';
|
|
import type { Config } from '@/config.js';
|
|
import { genAid, parseAid } from '@/misc/id/aid.js';
|
|
import { genMeid } from '@/misc/id/meid.js';
|
|
import { genMeidg } from '@/misc/id/meidg.js';
|
|
import { genObjectId } from '@/misc/id/object-id.js';
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
@Injectable()
|
|
export class IdService {
|
|
private method: string;
|
|
|
|
constructor(
|
|
@Inject(DI.config)
|
|
private config: Config,
|
|
) {
|
|
this.method = config.id.toLowerCase();
|
|
}
|
|
|
|
@bindThis
|
|
public genId(date?: Date): string {
|
|
if (!date || (date > new Date())) date = new Date();
|
|
|
|
switch (this.method) {
|
|
case 'aid': return genAid(date);
|
|
case 'meid': return genMeid(date);
|
|
case 'meidg': return genMeidg(date);
|
|
case 'ulid': return ulid(date.getTime());
|
|
case 'objectid': return genObjectId(date);
|
|
default: throw new Error('unrecognized id generation method');
|
|
}
|
|
}
|
|
|
|
@bindThis
|
|
public parse(id: string): { date: Date; } {
|
|
switch (this.method) {
|
|
case 'aid': return parseAid(id);
|
|
// TODO
|
|
//case 'meid':
|
|
//case 'meidg':
|
|
//case 'ulid':
|
|
//case 'objectid':
|
|
default: throw new Error('unrecognized id generation method');
|
|
}
|
|
}
|
|
}
|