contact is release!
This commit is contained in:
@@ -102,12 +102,19 @@ function initializeSocket(server) {
|
||||
// Contacts socket
|
||||
socket.on('contacts', async (contactUsername) => {
|
||||
const username = socket.user.username;
|
||||
|
||||
// Update contact list in db
|
||||
try {
|
||||
const query = ('INSERT INTO contacts (username, contact) VALUES ($1, $2)');
|
||||
const query = `
|
||||
INSERT INTO contacts (username, contact)
|
||||
SELECT $1, $2
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM contacts WHERE username = $1 AND contact = $2
|
||||
)
|
||||
`;
|
||||
await db.query(query, [username, contactUsername]);
|
||||
} catch (err) {
|
||||
console.error('Failed to update contacts')
|
||||
console.error('Failed to update contacts ', err)
|
||||
socket.emit('socket error', 'Failed to update contacts (server error)')
|
||||
}
|
||||
// Get contact list from db
|
||||
|
||||
@@ -103,6 +103,8 @@ async function initializeSocket() {
|
||||
// Contacts handler
|
||||
socket.on('contacts', (contacts) => {
|
||||
console.log('Received contacts: ', contacts);
|
||||
// Clear contact to avoid duplicates because backend send all user contacts and add to existing so it just clear previous and display data from db
|
||||
document.getElementById('contacts').innerHTML = "";
|
||||
for(const contact of contacts) {
|
||||
addContact(contact.contact, socket)
|
||||
}
|
||||
@@ -157,6 +159,7 @@ async function initializeSocket() {
|
||||
messages.scrollTop = messages.scrollHeight;
|
||||
});
|
||||
|
||||
// Create contact li and create remove contact button
|
||||
function addContact(contactUsername, socket) {
|
||||
const contact = document.createElement('li');
|
||||
contact.className = 'contact-item';
|
||||
@@ -197,7 +200,5 @@ async function initializeSocket() {
|
||||
addedContacts.delete(contactUsername);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
initializeSocket();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user