diff --git a/src/api/acts/client.ts b/src/api/acts/client.ts index ff2e6fd..19c62c6 100644 --- a/src/api/acts/client.ts +++ b/src/api/acts/client.ts @@ -1,11 +1,12 @@ -import { eq, select, update } from "dblang"; +import { and, eq, insert, remove, select, update } from "dblang"; import { PERMISSIONS } from "../../server/permissions.js"; import { sha256, sign } from "../../sys/crypto.js"; -import { accounts, db } from "../../sys/db.js"; +import { accounts, db, roomMembers, rooms } from "../../sys/db.js"; import { selfTag } from "../../sys/selfTag.js"; import { getSettings, SETTINGS } from "../../sys/settings.js"; import { get64 } from "../../sys/tools.js"; import { Client, STATE } from "../user.js"; +import { canCreateRoom } from "../helper.js" export const deleteAccount = { @@ -88,4 +89,66 @@ export const changePassword = { aws("error", "existence"); } } -}; \ No newline at end of file +}; + +export const createRoom = { + state: STATE.client, + right: PERMISSIONS.CAN_USE_API, + data: { + room: "name-100", + server: "string", // Unused/Ignored at the Moment + title: "string-255", + description: "string-255", + isPublic: "number", + icon: "string-255" + }, + func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { + if (!await canCreateRoom(client.accID)) + return void aws("error", "limit"); + let req = await insert( + rooms.name, + rooms.owner, + rooms.public, + rooms.title, + rooms.description, + rooms.icon + ).add( + data.room, + client.accID, + data.isPublic, + data.title, + data.description, + data.icon + ).query(db); + + if (req.affectedRows > 0) { + await insert(roomMembers.roomID, roomMembers.name, roomMembers.server, roomMembers.admin) + .add(req.insertId, client.name, "local", true) + .query(db); + aws("ok", ""); + } else { + aws("error", "existence"); + } + } +}; + +export const deleteRoom = { + state: STATE.client, + right: PERMISSIONS.CAN_USE_API, + data: { + room: "string", + server: "string", // Unused/Ignored at the Moment + }, + func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { + let req = await remove(rooms) + .where(and( + eq(rooms.name, data.room), + eq(rooms.owner, client.accID) + )).query(db); + if (req.affectedRows > 0) { + aws("ok", ""); + } else { + aws("error", "existence"); + } + } +} \ No newline at end of file diff --git a/src/api/helper.ts b/src/api/helper.ts index fb29bda..8cda6d9 100644 --- a/src/api/helper.ts +++ b/src/api/helper.ts @@ -15,5 +15,23 @@ export const isRoomFull = async (roomID: number) => { ) as any; let req = await select([currentCount, maxCount], null) .query(db); + if(maxCount == -1) return false; return req[0][currentCount] >= req[0][maxCount]; -}; \ No newline at end of file +}; + +export const canCreateRoom = async (accID: number) => { + let currentCount = alias( + select([count(rooms.roomID)], rooms) + .where(eq(rooms.owner, accID)), + "currentCount" + ) as any; + let maxCount = alias( + select([accounts.maxRooms],accounts) + .where(eq(accounts.accID, accID)), + "maxCount" + ) as any; + let req = await select([currentCount, maxCount], null) + .query(db); + if(maxCount == -1) return true; + return req[0][currentCount] < req[0][maxCount]; +} \ No newline at end of file