refactor db up check in functions
This commit is contained in:
parent
16b6b4edf9
commit
512b827b40
263
src/db.js
263
src/db.js
@ -92,189 +92,124 @@ const database = {
|
|||||||
console.error("Pubkey is required");
|
console.error("Pubkey is required");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const maxRetries = 5;
|
await retryWithDelay(database.checkIfDatabaseIsUp, 5);
|
||||||
let retries = 0;
|
try {
|
||||||
while (retries < maxRetries) {
|
await pool.query(
|
||||||
const dbUp = await database.checkIfDatabaseIsUp();
|
'INSERT INTO "users" (uuid, pubkey) VALUES (DEFAULT, $1)',
|
||||||
if (dbUp) {
|
[pubkey]
|
||||||
try {
|
);
|
||||||
await pool.query(
|
console.log(`Added user with the public key ${pubkey}.`);
|
||||||
'INSERT INTO "users" (uuid, pubkey) VALUES (DEFAULT, $1)',
|
return;
|
||||||
[pubkey]
|
} catch (err) {
|
||||||
);
|
console.error('Error adding user:', err);
|
||||||
console.log(`Added user with the public key ${pubkey}.`);
|
throw err;
|
||||||
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) => {
|
createRoom: async (pubkey1, pubkey2) => {
|
||||||
const maxRetries = 5;
|
await retryWithDelay(database.checkIfDatabaseIsUp, 5);
|
||||||
let retries = 0;
|
try {
|
||||||
while (retries < maxRetries) {
|
const userQuery = 'SELECT uuid FROM users WHERE pubkey = $1';
|
||||||
const dbUp = await database.checkIfDatabaseIsUp();
|
const uuidRes1 = await pool.query(userQuery, [pubkey1]);
|
||||||
if (dbUp) {
|
const uuidRes2 = await pool.query(userQuery, [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]);
|
|
||||||
|
|
||||||
if (!uuidRes1.rows[0] || !uuidRes2.rows[0]) {
|
if (!uuidRes1.rows[0] || !uuidRes2.rows[0]) {
|
||||||
throw new Error('One or both users not found');
|
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;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getRooms: async (pubkey) => {
|
getRooms: async (pubkey) => {
|
||||||
const maxRetries = 5;
|
await retryWithDelay(database.checkIfDatabaseIsUp, 5);
|
||||||
let retries = 0;
|
try {
|
||||||
while (retries < maxRetries) {
|
const roomsRes = await pool.query(
|
||||||
const dbUp = await database.checkIfDatabaseIsUp();
|
'SELECT m.room_uuid FROM room_members m, users u WHERE m.user_uuid = u.uuid AND u.pubkey = $1',
|
||||||
if (dbUp) {
|
[pubkey]
|
||||||
try {
|
);
|
||||||
const roomsRes = await pool.query(
|
return roomsRes.rows.map(row => row.room_uuid);
|
||||||
'SELECT m.room_uuid FROM room_members m, users u WHERE m.user_uuid = u.uuid AND u.pubkey = $1',
|
} catch (err) {
|
||||||
[pubkey]
|
console.error('Error retrieving rooms:', err);
|
||||||
);
|
throw err;
|
||||||
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) => {
|
getRoomMembers: async (roomid) => {
|
||||||
const maxRetries = 5;
|
await retryWithDelay(database.checkIfDatabaseIsUp, 5);
|
||||||
let retries = 0;
|
try {
|
||||||
while (retries < maxRetries) {
|
const members = await pool.query(
|
||||||
const dbUp = await database.checkIfDatabaseIsUp();
|
'SELECT u.pubkey FROM room_members r, users u WHERE r.user_uuid = u.uuid AND r.room_uuid = $1',
|
||||||
if (dbUp) {
|
[roomid]
|
||||||
try {
|
);
|
||||||
const members = await pool.query(
|
return members.rows.map(row => row.pubkey);
|
||||||
'SELECT u.pubkey FROM room_members r, users u WHERE r.user_uuid = u.uuid AND r.room_uuid = $1',
|
} catch (err) {
|
||||||
[roomid]
|
console.error('Error retrieving rooms:', err);
|
||||||
);
|
throw err;
|
||||||
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) => {
|
getPeers: async (pubkey) => {
|
||||||
const maxRetries = 5;
|
await retryWithDelay(database.checkIfDatabaseIsUp, 5);
|
||||||
let retries = 0;
|
try {
|
||||||
while (retries < maxRetries) {
|
const peers = await pool.query(
|
||||||
const dbUp = await database.checkIfDatabaseIsUp();
|
`SELECT u1.pubkey
|
||||||
if (dbUp) {
|
FROM room_members r1, room_members r2, users u1, users u2
|
||||||
try {
|
WHERE r1.user_uuid = u1.uuid
|
||||||
const peers = await pool.query(
|
AND r2.user_uuid = u2.uuid
|
||||||
`SELECT u1.pubkey
|
AND r1.room_uuid = r2.room_uuid
|
||||||
FROM room_members r1, room_members r2, users u1, users u2
|
AND u2.pubkey != u1.pubkey
|
||||||
WHERE r1.user_uuid = u1.uuid
|
AND u2.pubkey = $1`,
|
||||||
AND r2.user_uuid = u2.uuid
|
[pubkey]
|
||||||
AND r1.room_uuid = r2.room_uuid
|
);
|
||||||
AND u2.pubkey != u1.pubkey
|
return peers.rows.map(row => row.pubkey);
|
||||||
AND u2.pubkey = $1`,
|
} catch (err) {
|
||||||
[pubkey]
|
console.error('Error retrieving peers:', err);
|
||||||
);
|
throw err;
|
||||||
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 () => {
|
getPublicKeys: async () => {
|
||||||
const maxRetries = 5;
|
await retryWithDelay(database.checkIfDatabaseIsUp, 5);
|
||||||
let retries = 0;
|
try {
|
||||||
while (retries < maxRetries) {
|
const result = await pool.query('SELECT pubkey FROM users');
|
||||||
const dbUp = await database.checkIfDatabaseIsUp();
|
const publicKeys = result.rows.map(row => row.pubkey);
|
||||||
if (dbUp) {
|
return publicKeys;
|
||||||
try {
|
} catch (err) {
|
||||||
const result = await pool.query('SELECT pubkey FROM users');
|
console.error('Error retrieving public keys:', err);
|
||||||
const publicKeys = result.rows.map(row => row.pubkey);
|
throw new Error('Error retrieving public keys');
|
||||||
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.`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const retryWithDelay = (operation, retries = 5, delay = 5000) => new Promise((resolve, reject) => {
|
||||||
|
return operation()
|
||||||
|
.then(resolve)
|
||||||
|
.catch((err) => {
|
||||||
|
if (retries > 0) {
|
||||||
|
return wait(delay)
|
||||||
|
.then(retryWithDelay.bind(null, operation, retries - 1, delay))
|
||||||
|
.then(resolve)
|
||||||
|
.catch(reject);
|
||||||
|
}
|
||||||
|
|
||||||
|
return reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const wait = (time_in_ms) => new Promise((resolve) => {
|
||||||
|
setTimeout(() => resolve(), time_in_ms);
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = database;
|
module.exports = database;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user