create/delete Rooms (untested)
This commit is contained in:
parent
60b4c819e7
commit
10bbf16915
2 changed files with 85 additions and 4 deletions
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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];
|
||||
}
|
Loading…
Reference in a new issue