From ee10e75464a2087a981fe85810aed2517658be02 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Mon, 27 Mar 2023 19:17:04 +0200 Subject: [PATCH] completed invitation and changed otas --- src/api/acts/client.ts | 34 +++++++++++++++++++++++++++++++--- src/api/acts/rooms.ts | 26 ++++++++++++++++++-------- src/sys/db.ts | 10 +++++++--- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/api/acts/client.ts b/src/api/acts/client.ts index 33c2a6b..42ae3f5 100644 --- a/src/api/acts/client.ts +++ b/src/api/acts/client.ts @@ -212,19 +212,47 @@ export const getInvitations: Act = { await remove(invitations) .where(leq(invitations.expires, uts())) .query(db); - let req = await select([invitations.room, invitations.server, invitations.ota, invitations.expires], invitations) + let req = await select([ + invitations.invitationID, + invitations.room, + invitations.server, + invitations.ota, + invitations.expires, + invitations.fromName, + invitations.fromServer, + ], invitations) .where(eq(invitations.accID, client.accID)) .query(db); let out = req.map(d => { + let invitationID = d[invitations.invitationID]; let room = d[invitations.room]; let server = d[invitations.server]; let ota = d[invitations.ota]; let expires = d[invitations.expires]; - if (room != null && server != null && ota != null && expires != null) { - return { room, server, ota, expires }; + let fromName = d[invitations.fromName]; + let fromServer = d[invitations.fromServer]; + if (invitationID != null && room != null && server != null && ota != null && expires != null && fromName != null && fromServer != null) { + return { invitationID, room, server, ota, expires, fromName, fromServer }; } return null; }); aws("ok", out.filter(d => d != null)); } +}; + +export const deleteInvitation: Act = { + state: STATE.client, + right: PERMISSIONS.CAN_USE_API, + data: { + invitationID: "number", + }, + func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { + let req = await remove(invitations) + .where(and( + eq(invitations.accID, client.accID), + eq(invitations.invitationID, data.invitationID), + )).query(db); + if (req.affectedRows > 0) aws("ok", ""); + else aws("error", "existence"); + } }; \ No newline at end of file diff --git a/src/api/acts/rooms.ts b/src/api/acts/rooms.ts index 168a3c7..6911dc2 100644 --- a/src/api/acts/rooms.ts +++ b/src/api/acts/rooms.ts @@ -303,26 +303,30 @@ export const getRoomOTAs: Act = { } let roomID = await client.isRoomAdmin(data.room, ROOM_RIGHTS.OTA); if (roomID == -1) return void aws("error", "roomAdmin"); - let req = await select([roomOTAs.token, roomOTAs.expires, roomOTAs.usesLeft], roomOTAs) + let req = await select([roomOTAs.token, roomOTAs.name, roomOTAs.expires, roomOTAs.usesLeft, roomOTAs.isInvitation], 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], - usesLeft: d[roomOTAs.usesLeft] + usesLeft: d[roomOTAs.usesLeft], + isInvitation: d[roomOTAs.isInvitation], }))); } }; -export const addRoomOTA: Act = { +export const addRoomOTA: Act = { // or change it, primary key is room and token state: STATE.client | STATE.remote, right: 0, data: { room: "string", server: "string", - token: "string-255", + token: "string-256", + name: "string-256", expires: "number", usesLeft: "number", + isInvitation: "boolean" }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!checkSelfTag(data.server)) { @@ -334,13 +338,15 @@ export const addRoomOTA: Act = { let roomID = await client.isRoomAdmin(data.room, ROOM_RIGHTS.OTA); if (roomID == -1) return void aws("error", "roomAdmin"); try { - await insert(roomOTAs.roomID, roomOTAs.token, roomOTAs.expires, roomOTAs.usesLeft) - .add(roomID, data.token, data.expires, data.usesLeft) + await insert(roomOTAs.roomID, roomOTAs.token, roomOTAs.name, roomOTAs.expires, roomOTAs.usesLeft, roomOTAs.isInvitation) + .add(roomID, data.token, data.name, data.expires, data.usesLeft, data.isInvitation) .query(db); } catch (error) { await update(roomOTAs) .set(roomOTAs.expires, data.expires) .set(roomOTAs.usesLeft, data.usesLeft) + .set(roomOTAs.name, data.name) + .set(roomOTAs.isInvitation, data.isInvitation) .where(and( eq(roomOTAs.token, data.token), eq(roomOTAs.roomID, roomID) @@ -409,13 +415,17 @@ export const invite: Act = { invitations.room, invitations.server, invitations.ota, - invitations.expires + invitations.expires, + invitations.fromName, + invitations.fromServer ).add( resp[0].accID, data.room, roomServer.tag, data.token, - Math.min(data.expires, uts() + 60 * 60 * 24 * 10) + Math.min(data.expires, uts() + 60 * 60 * 24 * 10), + client.name, + client.server.tag, ).query(db); } catch (error) { diff --git a/src/sys/db.ts b/src/sys/db.ts index 5b437c7..d31ed3f 100644 --- a/src/sys/db.ts +++ b/src/sys/db.ts @@ -96,6 +96,8 @@ invitations.addAttributes({ server: { type: VARCHAR(256) }, ota: { type: VARCHAR(128) }, expires: { type: BIGINT }, + fromName: { type: VARCHAR(256) }, + fromServer: { type: VARCHAR(256) }, }); export const settings = db.newTable("settings"); @@ -168,12 +170,14 @@ roomOTAs.addAttributes({ onUpdate: onAction.cascade } }, + name: { type: VARCHAR(256) }, token: { - type: VARCHAR(128), + type: VARCHAR(256), primaryKey: true }, expires: { type: BIGINT }, - usesLeft: { type: INT } + usesLeft: { type: INT }, + isInvitation: { type: BOOL }, }); export const listCategories = db.newTable("listCategories"); @@ -250,7 +254,7 @@ listItems.addAttributes({ unit: { type: SMALLINT }, value: { type: TEXT }, link: { - type:INT, + type: INT, foreignKey: { link: listProducts.listProdID, onDelete: onAction.setNull,