diff --git a/src/api/acts/admin.ts b/src/api/acts/admin.ts index 06be2b9..3b0f40a 100644 --- a/src/api/acts/admin.ts +++ b/src/api/acts/admin.ts @@ -1,4 +1,4 @@ -import { eq, insert, le, or, remove, select, update } from "dblang"; +import { and, eq, geq, insert, le, leq, or, remove, select, update } from "dblang"; import { PERMISSIONS } from "../../server/permissions.js"; import { sha256 } from "../../sys/crypto.js"; import { accounts, db, signupOTA } from "../../sys/db.js"; @@ -116,7 +116,10 @@ export const getOTAs: Act = { await remove(signupOTA) .where(or( eq(signupOTA.usesLeft, 0), - le(signupOTA.expires, uts()) + and( + le(signupOTA.expires, uts()), + geq(signupOTA.expires, 0) + ) )) .query(db); let req = await select([ @@ -127,7 +130,7 @@ export const getOTAs: Act = { .query(db); aws("ok", req.map(d => ({ token: d[signupOTA.token], - expires: d[signupOTA.expires], + expires: Number(d[signupOTA.expires]), usesLeft: d[signupOTA.usesLeft] }))); } diff --git a/src/api/acts/roomContent.ts b/src/api/acts/roomContent.ts index a83e9de..c9e12a5 100644 --- a/src/api/acts/roomContent.ts +++ b/src/api/acts/roomContent.ts @@ -400,7 +400,7 @@ export const getItems: Act = { .where(eq(listItems.roomID, roomID)) .query(db); let out = req.map(d => { - let listItemID = d[listItems.listItemID]; + let listItemID = Number(d[listItems.listItemID]); let state = d[listItems.state]; let title = d[listItems.title]; let description = d[listItems.description]; diff --git a/src/api/acts/rooms.ts b/src/api/acts/rooms.ts index ff7fcd5..e896c6b 100644 --- a/src/api/acts/rooms.ts +++ b/src/api/acts/rooms.ts @@ -1,4 +1,4 @@ -import { alias, and, eq, exists, geq, innerJoinOn, innerJoinUsing, insert, le, minus, naturalJoin, not, or, remove, select, update } from "dblang"; +import { alias, and, eq, exists, ge, geq, innerJoinOn, innerJoinUsing, insert, le, minus, naturalJoin, not, or, remove, select, update } from "dblang"; import { checkSelfTag, outbagURLfromTag } from "../../server/outbagURL.js"; import { ROOM_RIGHTS } from "../../server/permissions.js"; import { accounts, db, remoteRooms, roomMembers, roomOTAs, rooms } from "../../sys/db.js"; @@ -243,9 +243,12 @@ export const joinRoom: Act = { if (await isRoomFull(roomID)) return void aws("error", "limit"); // TODO: Make Transaktion when possible await remove(roomOTAs) - .where(and( - le(roomOTAs.expires, uts()), - geq(roomOTAs.expires, 0) + .where(or( + eq(roomOTAs.usesLeft, 0), + and( + le(roomOTAs.expires, uts()), + geq(roomOTAs.expires, 0) + ) )) .query(db); let req = await update(roomOTAs) @@ -355,13 +358,22 @@ export const getRoomOTAs: Act = { } let roomID = await client.isRoomAdmin(data.room, ROOM_RIGHTS.OTA); if (roomID == -1) return void aws("error", "roomAdmin"); + await remove(roomOTAs) + .where(or( + eq(roomOTAs.usesLeft, 0), + and( + le(roomOTAs.expires, uts()), + geq(roomOTAs.expires, 0) + ) + )) + .query(db); let req = await select([roomOTAs.token, roomOTAs.name, roomOTAs.expires, roomOTAs.usesLeft], roomOTAs) .where(eq(roomOTAs.roomID, roomID)) .query(db); aws("ok", req.map(d => ({ token: d[roomOTAs.token], name: d[roomOTAs.name], - expires: d[roomOTAs.expires], + expires: Number(d[roomOTAs.expires]), usesLeft: d[roomOTAs.usesLeft], }))); } diff --git a/tests/tests/post.js b/tests/tests/post.js index 463f516..fca4628 100644 --- a/tests/tests/post.js +++ b/tests/tests/post.js @@ -1,5 +1,6 @@ import { generateSigningKey, sign } from '../../dist/sys/crypto.js'; import { PERMISSIONS } from '../../dist/server/permissions.js'; +import { uts, wait } from '../../dist/sys/tools.js'; let name1 = "testUser1"; let name2 = "testUser2"; @@ -151,7 +152,7 @@ const list = [ maxRooms: 2, maxRoomSize: 10, maxUsersPerRoom: 2 - }, { + }, { accID: null, rights: 3, name: "testUser2", @@ -160,7 +161,7 @@ const list = [ maxRooms: 2, maxRoomSize: 10, maxUsersPerRoom: 2 - } + } ]); await req({ "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` @@ -180,7 +181,7 @@ const list = [ maxRooms: 2, maxRoomSize: 10, maxUsersPerRoom: 2 - }, { + }, { accID: null, rights: 5, name: "testUser2", @@ -189,8 +190,45 @@ const list = [ maxRooms: 2, maxRoomSize: 10, maxUsersPerRoom: 2 - } + } ]); + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "addOTA", { + token: "12345678", + expires: uts() + 1, + usesLeft: -1 + }, "ok", ""); + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "getOTAs", {}, "ok", [ + { + token: "12345678", + expires: null, + usesLeft: -1 + } + ]); + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "addOTA", { + token: "abc", + expires: -1, + usesLeft: -1 + }, "ok", ""); + await wait(2000); + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "getOTAs", {}, "ok", [{ + token: "abc", + expires: -1, + usesLeft: -1 + }]); + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "deleteOTA", { token: "abc" }, "ok", ""); + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "getOTAs", {}, "ok", []); }], ["room Owner", async (req) => { await req({ "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}`