1
0
forked from mirror/misskey
This commit is contained in:
syuilo 2019-04-14 01:40:29 +09:00
parent e64912545a
commit aa3d2deeaa
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
3 changed files with 30 additions and 12 deletions

View File

@ -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'

View File

@ -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
View 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();
}