diff --git a/src/api/acts/rooms.ts b/src/api/acts/rooms.ts index 50322f6..4dda90b 100644 --- a/src/api/acts/rooms.ts +++ b/src/api/acts/rooms.ts @@ -1,4 +1,4 @@ -import { alias, and, eq, exists, innerJoinOn, innerJoinUsing, insert, le, minus, naturalJoin, not, or, remove, select, update } from "dblang"; +import { alias, and, eq, exists, geq, innerJoinOn, innerJoinUsing, insert, le, minus, naturalJoin, not, or, remove, select, update } from "dblang"; import { checkSelfTag } from "../../server/outbagURL.js"; import { ROOM_RIGHTS } from "../../server/permissions.js"; import { accounts, db, remoteRooms, roomMembers, roomOTAs, rooms } from "../../sys/db.js"; @@ -75,6 +75,7 @@ export const getRoomMembers: Act = { }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.server, "getRoomMembers", data); aws(resp.state, resp.data); return; @@ -117,9 +118,14 @@ export const joinRoom: Act = { }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.server, "joinRoom", data); if (resp.state == "ok") { - //TODO: add server to personal list + try { + await insert(remoteRooms.accID, remoteRooms.server) + .add(client.accID, data.server) + .query(db); + } catch (error) { } } else if (resp.data == "ota") { client.suspect(); client.suspect(); @@ -138,7 +144,10 @@ export const joinRoom: Act = { if (await isRoomFull(roomID)) return void aws("error", "limit"); // TODO: Make Transaktion when possible await remove(roomOTAs) - .where(le(roomOTAs.expires, uts())) + .where(and( + le(roomOTAs.expires, uts()), + geq(roomOTAs.expires, 0) + )) .query(db); let req = await update(roomOTAs) .set(roomOTAs.usesLeft, minus(roomOTAs.usesLeft, 1)) @@ -180,9 +189,14 @@ export const joinPublicRoom: Act = { }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.server, "joinPublicRoom", data); if (resp.state == "ok") { - //TODO: add server to personal list + try { + await insert(remoteRooms.accID, remoteRooms.server) + .add(client.accID, data.server) + .query(db); + } catch (error) { } } aws(resp.state, resp.data); return; @@ -223,6 +237,7 @@ export const getRoomOTAs: Act = { }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.server, "getRoomOTAs", data); aws(resp.state, resp.data); return; @@ -252,6 +267,7 @@ export const addRoomOTA: Act = { }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.server, "addRoomOTA", data); aws(resp.state, resp.data); return; @@ -285,6 +301,7 @@ export const deleteRoomOTA: Act = { }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.server, "deleteRoomOTA", data); aws(resp.state, resp.data); return; @@ -311,6 +328,7 @@ export const kickMember: Act = { }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.server, "kickMember", data); aws(resp.state, resp.data); return; @@ -353,6 +371,7 @@ export const setAdminStatus: Act = { }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.roomServer, "setAdminStatus", data); aws(resp.state, resp.data); return; @@ -383,11 +402,14 @@ export const leaveRoom: Act = { }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.server, "leaveRoom", data); - if (resp.state == "ok") { - //TODO: remove server from personal list - } else if (resp.data == "ota") { - client.suspect(); + if (resp.state == "ok" || resp.data == "existence") { + await remove(remoteRooms) + .where(and( + eq(client.accID, remoteRooms.accID), + eq(data.server, remoteRooms.server) + )).query(db); } aws(resp.state, resp.data); return; @@ -429,6 +451,7 @@ export const roomSettings: Act = { }, func: async (client, data, aws) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.server, "roomSettings", data); aws(resp.state, resp.data); return; @@ -456,6 +479,7 @@ export const roomMeta: Act = { }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", "right"); let resp = await client.pass(data.server, "roomMeta", data); aws(resp.state, resp.data); return;