adjusted client site, refactored database to use user_id instead contact_user_id

This commit is contained in:
slawk0
2024-11-28 18:37:54 +01:00
parent e6b015b590
commit bb1fc02127
3 changed files with 20 additions and 22 deletions

View File

@@ -74,12 +74,9 @@ function ContactsList({
InitializeContact(contact.username);
updateContactStatus(contact, true);
}}
key={contact.id}
key={contact.conversation_id}
>
<span className="flex-shrink-0">
{/*contact id: {contact.id} contact_user_id: {contact.user_id}*/}
{contact.username}
</span>
<span className="flex-shrink-0">{contact.username}</span>
<div className="w-4 h-4 mx-2 flex items-center justify-center mr-auto">
<p className="text-center text-2xl text-red-200 leading-none">

View File

@@ -25,6 +25,7 @@ export type ContactsProps = {
username: string;
user_id: number;
id: number;
conversation_id: number;
};
function Chat() {

View File

@@ -105,13 +105,13 @@ async function createTables() {
CREATE TABLE IF NOT EXISTS Contacts (
contact_id SERIAL PRIMARY KEY,
user_id INT REFERENCES Accounts(user_id) ON DELETE CASCADE,
contact_user_id INT REFERENCES Accounts(user_id) ON DELETE CASCADE,
user_id INT REFERENCES Accounts(user_id) ON DELETE CASCADE,
read BOOLEAN NOT NULL DEFAULT FALSE,
last_active TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT unique_contact UNIQUE (user_id, contact_user_id)
CONSTRAINT unique_contact UNIQUE (user_id, user_id)
);
CREATE INDEX IF NOT EXISTS idx_contacts_user_id ON Contacts (user_id);
CREATE INDEX IF NOT EXISTS idx_contacts_contact_user_id ON Contacts (contact_user_id);
CREATE INDEX IF NOT EXISTS idx_contacts_user_id ON Contacts (user_id);
`);
console.log("Contacts table created successfully.");
} catch (e) {
@@ -454,7 +454,7 @@ async function changePassword(username, newPasswordHash) {
async function insertContact(userUsername, receiverUsername, read) {
const query = `
INSERT INTO Contacts (user_id, contact_user_id, read, last_active)
INSERT INTO Contacts (user_id, user_id, read, last_active)
VALUES
((SELECT user_id FROM Accounts WHERE username = $1),
(SELECT user_id FROM Accounts WHERE username = $2),
@@ -473,11 +473,11 @@ async function insertContact(userUsername, receiverUsername, read) {
// const query = `
// SELECT
// c.contact_id,
// c.contact_user_id,
// c.user_id,
// a.username AS contact_username,
// c.last_active
// FROM Contacts c
// JOIN Accounts a ON a.user_id = c.contact_user_id
// JOIN Accounts a ON a.user_id = c.user_id
// WHERE c.user_id = $1
// ORDER BY c.last_active DESC;
// `;
@@ -495,14 +495,14 @@ async function getContacts(user_id) {
const contactsQuery = `
SELECT
c.contact_id AS id,
c.contact_user_id AS contact_user_id,
c.user_id AS user_id,
a.username AS username,
c.last_active,
'direct' AS type,
COALESCE(m.conversation_id, NULL) AS conversation_id
FROM Contacts c
JOIN Accounts a ON a.user_id = c.contact_user_id
LEFT JOIN Memberships m ON m.user_id = c.contact_user_id AND m.conversation_id = (
JOIN Accounts a ON a.user_id = c.user_id
LEFT JOIN Memberships m ON m.user_id = c.user_id AND m.conversation_id = (
SELECT conv.conversation_id
FROM Conversations conv
JOIN Memberships mem ON conv.conversation_id = mem.conversation_id
@@ -512,7 +512,7 @@ async function getContacts(user_id) {
SELECT 1
FROM Memberships mem2
WHERE mem2.conversation_id = mem.conversation_id
AND mem2.user_id = c.contact_user_id
AND mem2.user_id = c.user_id
)
LIMIT 1
)
@@ -540,7 +540,7 @@ async function getContacts(user_id) {
const contacts = contactsResult.rows;
const groups = groupsResult.rows.map((group) => ({
...group,
user_id: group.conversation_id, // Ensure user_id is correctly mapped
contact_id: group.conversation_id, // Ensure user_id is correctly mapped
}));
// Combine contacts and groups
@@ -574,20 +574,20 @@ async function deleteContact(user_id, contact_id) {
}
}
async function updateContactStatus(userUsername, contact_user_id, read) {
async function updateContactStatus(userUsername, user_id, read) {
const query = `
WITH users AS (
SELECT
(SELECT user_id FROM Accounts WHERE username = $2) as user_id,
(SELECT user_id FROM Accounts WHERE username = $3) as contact_user_id
(SELECT user_id FROM Accounts WHERE username = $3) as user_id
)
UPDATE Contacts SET read = $1
WHERE user_id = (SELECT user_id FROM users)
AND contact_user_id = (SELECT contact_user_id FROM users);
AND user_id = (SELECT user_id FROM users);
`;
try {
await client.query(query, [read, userUsername, contact_user_id]);
await updateContactLastActive(userUsername, contact_user_id);
await client.query(query, [read, userUsername, user_id]);
await updateContactLastActive(userUsername, user_id);
console.log("Successfully updated contact status");
} catch (e) {
console.error("Failed to update contact status ", e);
@@ -600,7 +600,7 @@ async function updateContactLastActive(userUsername, receiverUsername) {
UPDATE Contacts
SET last_active = $1
WHERE user_id = (SELECT user_id FROM Accounts WHERE username = $2)
AND contact_user_id = (SELECT user_id FROM Accounts WHERE username = $3);
AND user_id = (SELECT user_id FROM Accounts WHERE username = $3);
`;
try {
await client.query(query, [timestamp, userUsername, receiverUsername]);