mirror of
https://github.com/spacebarchat/server.git
synced 2026-04-08 06:05:40 +00:00
snowflake-based invite generation
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user