summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2019-04-14 01:40:29 +0900
committersyuilo <syuilotan@yahoo.co.jp>2019-04-14 01:40:29 +0900
commitaa3d2deeaab5a215178eaae7d05b17628b33eca3 (patch)
tree9b273acad69697000ce34ee2be2ad0c451342999
parentUpdate id generation methods (diff)
downloadsharkey-aa3d2deeaab5a215178eaae7d05b17628b33eca3.tar.gz
sharkey-aa3d2deeaab5a215178eaae7d05b17628b33eca3.tar.bz2
sharkey-aa3d2deeaab5a215178eaae7d05b17628b33eca3.zip
Add meid
-rw-r--r--.config/example.yml16
-rw-r--r--src/misc/gen-id.ts2
-rw-r--r--src/misc/id/meid.ts24
3 files changed, 30 insertions, 12 deletions
diff --git a/.config/example.yml b/.config/example.yml
index 32ad165623..48b1a0fd1c 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 d16910d015..5ee65e8177 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 0000000000..0a0935e536
--- /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();
+}