From c22ca39e3b0b1f94876e99a67e39ee2d9ea23459 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Wed, 29 Mar 2023 15:18:15 +0200 Subject: [PATCH] confirmRoom --- src/api/acts/rooms.ts | 44 ++++++++++++++++++++++++++++++++++++++++++- src/api/user.ts | 13 ++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/api/acts/rooms.ts b/src/api/acts/rooms.ts index 9d1bd47..3651374 100644 --- a/src/api/acts/rooms.ts +++ b/src/api/acts/rooms.ts @@ -467,7 +467,49 @@ export const inviteUser: Act = { } }; - +export const confirmRoom: Act = { + state: STATE.client | STATE.remote, + right: 0, + data: { + room: "name-100", + server: "string", + }, + 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, "confirmRoom", data); + if (resp.state == "ok") { + try { + let query = await insert(remoteRooms.accID, remoteRooms.server, remoteRooms.room, remoteRooms.confirmed) + .add(client.accID, resp.server, data.room, true) + .query(db); + if (query.affectedRows == 0) throw new Error("insertion error"); + } catch (error) { + await update(remoteRooms) + .set(remoteRooms.confirmed, true) + .where(and( + eq(remoteRooms.server, resp.server), + eq(remoteRooms.room, data.room), + eq(remoteRooms.accID, client.accID) + )).query(db); + } + } + aws(resp.state, resp.data); + return; + } + let roomID = await client.isInRoom(data.room); + if (roomID == -1) return void aws("error", "existence"); + let req = await update(roomMembers) + .set(roomMembers.confimed, true) + .where(and( + eq(roomMembers.roomID, roomID), + eq(roomMembers.name, client.name), + eq(roomMembers.server, client.state == STATE.client ? "local" : client.server.tag) + )).query(db); + if (req.affectedRows > 0) aws("ok", ""); + else aws("error", "existence"); + } +} export const kickMember: Act = { state: STATE.client | STATE.remote, diff --git a/src/api/user.ts b/src/api/user.ts index 44b3fdc..8c3547c 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -52,11 +52,18 @@ export class Client { debug("Client", "passing act to remote:", server); try { let s = await outbagURLfromTag(server); - return await fetchRemoteAs(s, this.name, act, data); + let resp = await fetchRemoteAs(s, this.name, act, data) as { + state: string, + data: any, + server: string | null + }; + resp.server = s.tag; + return resp; } catch (error) { return { state: "error", - data: "remote" + data: "remote", + server: null } } @@ -132,7 +139,7 @@ export function checktype(data: any, type: string) { } else if (type.startsWith("array-") && Array.isArray(data)) { let ttype = type.substring(6); for (let i = 0; i < data.length; i++) { - if(!checktype(data[i], ttype)) return false; + if (!checktype(data[i], ttype)) return false; } return true; } else if (type == "any") {