From 8caad60a43399c7b1ff37bd25d3d98c37fcbf898 Mon Sep 17 00:00:00 2001 From: knotteye Date: Tue, 13 Oct 2020 15:29:47 -0500 Subject: [PATCH] Add functions for generating and using invite codes --- src/api.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/api.ts b/src/api.ts index a803d63..abf2e94 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,4 +1,5 @@ import * as db from "./database"; +import * as base64id from "base64id"; import { config } from "./config"; import {unlink} from "fs"; @@ -97,4 +98,18 @@ async function getConfig(username: string, all?: boolean): Promise{ return t; } -export { register, update, changepwd, changesk, login, updateChat, deleteVODs, getConfig }; \ No newline at end of file +async function genInvite(user: string): Promise{ + var invitecode: string = base64id.generateId(); + await db.query('INSERT INTO invites (code) VALUES ('+invitecode+')'); + return invitecode; +} + +async function useInvite(code: string): Promise{ + if(typeof(code) !== "string" || code === "") return false; + var result = await db.query('SELECT code FROM invites WHERE code='+db.raw.escape(code)); + if(!result[0] || result[0]['code'] !== code) return false; + await db.query('DELETE FROM invites WHERE code='+db.raw.escape(code)); + return true; +} + +export { register, update, changepwd, changesk, login, updateChat, deleteVODs, getConfig, genInvite, useInvite }; \ No newline at end of file