From aa3d2deeaab5a215178eaae7d05b17628b33eca3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 14 Apr 2019 01:40:29 +0900 Subject: [PATCH] Add meid --- .config/example.yml | 16 ++++------------ src/misc/gen-id.ts | 2 ++ src/misc/id/meid.ts | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 src/misc/id/meid.ts diff --git a/.config/example.yml b/.config/example.yml index 32ad16562..48b1a0fd1 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -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' diff --git a/src/misc/gen-id.ts b/src/misc/gen-id.ts index d16910d01..5ee65e817 100644 --- a/src/misc/gen-id.ts +++ b/src/misc/gen-id.ts @@ -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'; diff --git a/src/misc/id/meid.ts b/src/misc/id/meid.ts new file mode 100644 index 000000000..0a0935e53 --- /dev/null +++ b/src/misc/id/meid.ts @@ -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(); +}