forked from mirror/misskey
Add meid
This commit is contained in:
parent
e64912545a
commit
aa3d2deeaa
@ -127,18 +127,10 @@ drive:
|
||||
# change it according to your preferences.
|
||||
|
||||
# Available methods:
|
||||
# aid ... Use AID for ID generation
|
||||
# ulid ... Use ulid for ID generation
|
||||
# objectid ... This is left for backward compatibility.
|
||||
|
||||
# AID is the original ID generation method.
|
||||
|
||||
# ULID: Universally Unique Lexicographically Sortable Identifier.
|
||||
# for more details: https://github.com/ulid/spec
|
||||
# * Normally, AID should be sufficient.
|
||||
|
||||
# ObjectID is the method used in previous versions of Misskey.
|
||||
# * Choose this if you are migrating from a previous Misskey.
|
||||
# aid ... Short, Millisecond accuracy
|
||||
# meid ... Similar to ObjectID, Millisecond accuracy
|
||||
# ulid ... Millisecond accuracy
|
||||
# objectid ... This is left for backward compatibility
|
||||
|
||||
id: 'aid'
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { ulid } from 'ulid';
|
||||
import { genAid } from './id/aid';
|
||||
import { genMeid } from './id/meid';
|
||||
import { genObjectId } from './id/object-id';
|
||||
import config from '../config';
|
||||
|
||||
@ -10,6 +11,7 @@ export function genId(date?: Date): string {
|
||||
|
||||
switch (metohd) {
|
||||
case 'aid': return genAid(date);
|
||||
case 'meid': return genMeid(date);
|
||||
case 'ulid': return ulid(date.getTime());
|
||||
case 'objectid': return genObjectId(date);
|
||||
default: throw 'unknown id generation method';
|
||||
|
24
src/misc/id/meid.ts
Normal file
24
src/misc/id/meid.ts
Normal file
@ -0,0 +1,24 @@
|
||||
const CHARS = '0123456789abcdef';
|
||||
|
||||
function getTime(time: number) {
|
||||
if (time < 0) time = 0;
|
||||
if (time === 0) {
|
||||
return CHARS[0];
|
||||
}
|
||||
|
||||
return time.toString(16).padStart(16, CHARS[0]);
|
||||
}
|
||||
|
||||
function getRandom() {
|
||||
let str = '';
|
||||
|
||||
for (let i = 0; i < 7; i++) {
|
||||
str += CHARS[Math.floor(Math.random() * CHARS.length)];
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
export function genMeid(date: Date): string {
|
||||
return 'f' + getTime(date.getTime()) + getRandom();
|
||||
}
|
Loading…
Reference in New Issue
Block a user