diff --git a/src/db.js b/src/db.js index 34d7ecb..d05bc33 100644 --- a/src/db.js +++ b/src/db.js @@ -92,95 +92,187 @@ const database = { console.error("Pubkey is required"); return; } - try { - await pool.query( - 'INSERT INTO "users" (uuid, pubkey) VALUES (DEFAULT, $1)', - [pubkey,] - ); - console.log(`Added user with the public key ${pubkey} .`); - } catch (err) { - console.error('Error adding user:', err); + const maxRetries = 5; + let retries = 0; + while (retries < maxRetries) { + const dbUp = await database.checkIfDatabaseIsUp(); + if (dbUp) { + try { + await pool.query( + 'INSERT INTO "users" (uuid, pubkey) VALUES (DEFAULT, $1)', + [pubkey] + ); + console.log(`Added user with the public key ${pubkey}.`); + return; + } catch (err) { + console.error('Error adding user:', err); + throw err; + } + } else { + console.log('Waiting for the database to start...'); + retries += 1; + if (retries < maxRetries) { + await new Promise(resolve => setTimeout(resolve, 10000)); + } else { + console.error(`Failed to connect to the database after ${maxRetries} attempts.`); + } + } } }, createRoom: async (pubkey1, pubkey2) => { - try { - const userQuery = 'SELECT uuid FROM users WHERE pubkey = $1'; - const uuidRes1 = await pool.query(userQuery, [pubkey1]); - const uuidRes2 = await pool.query(userQuery, [pubkey2]); + const maxRetries = 5; + let retries = 0; + while (retries < maxRetries) { + const dbUp = await database.checkIfDatabaseIsUp(); + if (dbUp) { + try { + const userQuery = 'SELECT uuid FROM users WHERE pubkey = $1'; + const uuidRes1 = await pool.query(userQuery, [pubkey1]); + const uuidRes2 = await pool.query(userQuery, [pubkey2]); - if (!uuidRes1.rows[0] || !uuidRes2.rows[0]) { - throw new Error('One or both users not found'); + if (!uuidRes1.rows[0] || !uuidRes2.rows[0]) { + throw new Error('One or both users not found'); + } + + const uuid1 = uuidRes1.rows[0].uuid; + const uuid2 = uuidRes2.rows[0].uuid; + + const roomRes = await pool.query( + 'INSERT INTO room (uuid) VALUES (DEFAULT) RETURNING uuid' + ); + const roomid = roomRes.rows[0].uuid; + + await pool.query( + `INSERT INTO room_members (room_uuid, user_uuid) + VALUES ($1, $2), ($1, $3)`, + [roomid, uuid1, uuid2] + ); + return roomid; + } catch (err) { + console.error('Error creating the room:', err); + throw err; + } + } else { + console.log('Waiting for the database to start...'); + retries += 1; + if (retries < maxRetries) { + await new Promise(resolve => setTimeout(resolve, 10000)); + } else { + console.error(`Failed to connect to the database after ${maxRetries} attempts.`); + } } - - const uuid1 = uuidRes1.rows[0].uuid; - const uuid2 = uuidRes2.rows[0].uuid; - - const roomRes = await pool.query( - 'INSERT INTO room (uuid) VALUES (DEFAULT) RETURNING uuid' - ); - const roomid = roomRes.rows[0].uuid; - - await pool.query( - `INSERT INTO room_members (room_uuid, user_uuid) - VALUES ($1, $2), ($1, $3)`, - [roomid, uuid1, uuid2] - ); - return roomid; - } catch (err) { - console.error('Error creating the room:', err); - throw err; // Re-throw to handle in calling code } }, getRooms: async (pubkey) => { - try { - const roomsRes = await pool.query( - 'SELECT m.room_uuid FROM room_members m, users u WHERE m.user_uuid = u.uuid AND u.pubkey = $1', - [pubkey] - ); - return roomsRes.rows.map(row => row.room_uuid); - } catch (err) { - console.error('Error retrieving rooms:', err); - throw err; + const maxRetries = 5; + let retries = 0; + while (retries < maxRetries) { + const dbUp = await database.checkIfDatabaseIsUp(); + if (dbUp) { + try { + const roomsRes = await pool.query( + 'SELECT m.room_uuid FROM room_members m, users u WHERE m.user_uuid = u.uuid AND u.pubkey = $1', + [pubkey] + ); + return roomsRes.rows.map(row => row.room_uuid); + } catch (err) { + console.error('Error retrieving rooms:', err); + throw err; + } + } else { + console.log('Waiting for the database to start...'); + retries += 1; + if (retries < maxRetries) { + await new Promise(resolve => setTimeout(resolve, 10000)); + } else { + console.error(`Failed to connect to the database after ${maxRetries} attempts.`); + } + } } }, getRoomMembers: async (roomid) => { - try { - const members = await pool.query( - 'SELECT u.pubkey FROM room_members r, users u WHERE r.user_uuid = u.uuid AND r.room_uuid = $1', - [roomid] - ); - return members.rows.map(row => row.pubkey); - } catch (err) { - console.error('Error retrieving rooms:', err); - throw err; + const maxRetries = 5; + let retries = 0; + while (retries < maxRetries) { + const dbUp = await database.checkIfDatabaseIsUp(); + if (dbUp) { + try { + const members = await pool.query( + 'SELECT u.pubkey FROM room_members r, users u WHERE r.user_uuid = u.uuid AND r.room_uuid = $1', + [roomid] + ); + return members.rows.map(row => row.pubkey); + } catch (err) { + console.error('Error retrieving rooms:', err); + throw err; + } + } else { + console.log('Waiting for the database to start...'); + retries += 1; + if (retries < maxRetries) { + await new Promise(resolve => setTimeout(resolve, 10000)); + } else { + console.error(`Failed to connect to the database after ${maxRetries} attempts.`); + } + } } }, getPeers: async (pubkey) => { - try { - const peers = await pool.query( - `SELECT u1.pubkey - FROM room_members r1, room_members r2, users u1, users u2 - WHERE r1.user_uuid = u1.uuid - AND r2.user_uuid = u2.uuid - AND r1.room_uuid = r2.room_uuid - AND u2.pubkey != u1.pubkey - AND u2.pubkey = $1`, - [pubkey] - ); - return peers.rows.map(row => row.pubkey); - } catch (err) { - console.error('Error retrieving peers:', err); - throw err; + const maxRetries = 5; + let retries = 0; + while (retries < maxRetries) { + const dbUp = await database.checkIfDatabaseIsUp(); + if (dbUp) { + try { + const peers = await pool.query( + `SELECT u1.pubkey + FROM room_members r1, room_members r2, users u1, users u2 + WHERE r1.user_uuid = u1.uuid + AND r2.user_uuid = u2.uuid + AND r1.room_uuid = r2.room_uuid + AND u2.pubkey != u1.pubkey + AND u2.pubkey = $1`, + [pubkey] + ); + return peers.rows.map(row => row.pubkey); + } catch (err) { + console.error('Error retrieving peers:', err); + throw err; + } + } else { + console.log('Waiting for the database to start...'); + retries += 1; + if (retries < maxRetries) { + await new Promise(resolve => setTimeout(resolve, 10000)); + } else { + console.error(`Failed to connect to the database after ${maxRetries} attempts.`); + } + } } }, getPublicKeys: async () => { - try { - const result = await pool.query('SELECT pubkey FROM users'); - const publicKeys = result.rows.map(row => row.pubkey); - return publicKeys; - } catch (err) { - console.error('Error retrieving public keys:', err); - throw new Error('Error retrieving public keys'); + const maxRetries = 5; + let retries = 0; + while (retries < maxRetries) { + const dbUp = await database.checkIfDatabaseIsUp(); + if (dbUp) { + try { + const result = await pool.query('SELECT pubkey FROM users'); + const publicKeys = result.rows.map(row => row.pubkey); + return publicKeys; + } catch (err) { + console.error('Error retrieving public keys:', err); + throw new Error('Error retrieving public keys'); + } + } else { + console.log('Waiting for the database to start...'); + retries += 1; + if (retries < maxRetries) { + await new Promise(resolve => setTimeout(resolve, 10000)); + } else { + console.error(`Failed to connect to the database after ${maxRetries} attempts.`); + } + } } } };