create/delete Rooms (untested)

This commit is contained in:
jusax23 2023-03-18 11:32:58 +01:00
parent 60b4c819e7
commit 10bbf16915
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
2 changed files with 85 additions and 4 deletions

View file

@ -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");
}
}
};
};
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");
}
}
}

View file

@ -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];
};
};
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];
}