contact is release!
This commit is contained in:
@@ -102,12 +102,19 @@ function initializeSocket(server) {
|
|||||||
// Contacts socket
|
// Contacts socket
|
||||||
socket.on('contacts', async (contactUsername) => {
|
socket.on('contacts', async (contactUsername) => {
|
||||||
const username = socket.user.username;
|
const username = socket.user.username;
|
||||||
|
|
||||||
// Update contact list in db
|
// Update contact list in db
|
||||||
try {
|
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]);
|
await db.query(query, [username, contactUsername]);
|
||||||
} catch (err) {
|
} 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)')
|
socket.emit('socket error', 'Failed to update contacts (server error)')
|
||||||
}
|
}
|
||||||
// Get contact list from db
|
// Get contact list from db
|
||||||
|
|||||||
@@ -103,9 +103,11 @@ async function initializeSocket() {
|
|||||||
// Contacts handler
|
// Contacts handler
|
||||||
socket.on('contacts', (contacts) => {
|
socket.on('contacts', (contacts) => {
|
||||||
console.log('Received contacts: ', contacts);
|
console.log('Received contacts: ', contacts);
|
||||||
for(const contact of 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
|
||||||
addContact(contact.contact, socket)
|
document.getElementById('contacts').innerHTML = "";
|
||||||
}
|
for(const contact of contacts) {
|
||||||
|
addContact(contact.contact, socket)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// If not previous messages found on backend
|
// If not previous messages found on backend
|
||||||
@@ -157,6 +159,7 @@ async function initializeSocket() {
|
|||||||
messages.scrollTop = messages.scrollHeight;
|
messages.scrollTop = messages.scrollHeight;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Create contact li and create remove contact button
|
||||||
function addContact(contactUsername, socket) {
|
function addContact(contactUsername, socket) {
|
||||||
const contact = document.createElement('li');
|
const contact = document.createElement('li');
|
||||||
contact.className = 'contact-item';
|
contact.className = 'contact-item';
|
||||||
@@ -197,7 +200,5 @@ async function initializeSocket() {
|
|||||||
addedContacts.delete(contactUsername);
|
addedContacts.delete(contactUsername);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
initializeSocket();
|
initializeSocket();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user