diff options
author | Erkin Alp Güney <erkinalp9035@gmail.com> | 2021-11-28 11:25:41 +0300 |
---|---|---|
committer | Erkin Alp Güney <erkinalp9035@gmail.com> | 2021-11-28 11:25:41 +0300 |
commit | 6d2ae89a3beb5eb55a2ed1006af461bc849771ce (patch) | |
tree | d294861f1d8bb87da0984886cf3ec8062e3c06c4 /api | |
parent | Merge pull request #515 from Featyre/master (diff) | |
download | server-6d2ae89a3beb5eb55a2ed1006af461bc849771ce.tar.xz |
snowflake-based invite generation
Diffstat (limited to 'api')
-rw-r--r-- | api/src/util/RandomInviteID.ts | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/api/src/util/RandomInviteID.ts b/api/src/util/RandomInviteID.ts index b37994d8..45450b0d 100644 --- a/api/src/util/RandomInviteID.ts +++ b/api/src/util/RandomInviteID.ts @@ -1,3 +1,5 @@ +import { Snowflake } from "@fosscord/util"; + export function random(length = 6) { // Declare all characters let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; @@ -10,3 +12,20 @@ export function random(length = 6) { return str; } + +export function snowflakeBasedInvite() { + // Declare all characters + let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + let snowflake = Snowflake.generateWorkerProcess(); + + // snowflakes hold ~10.75 characters worth of entropy; + // safe to generate a 8-char invite out of them + let str = ""; + for (let i=0; i < 10; i++) { + str += chars.charAt((snowflake % chars.length)); + snowflake /= chars.length; + } + + return str.substr(3,8).reverse(); // little-endianise for entropy +} |