code refactor

This commit is contained in:
slawk0
2024-12-13 19:54:19 +01:00
parent c5aac7ad2f
commit 76c49c3709
5 changed files with 32 additions and 44 deletions

View File

@@ -131,7 +131,7 @@ const MessageForm = ({ contact }: MessageFormProps) => {
const response = await uploadFiles(files.map((f) => f.file));
console.log(response);
attachmentUrls = response;
} catch (e) {
} catch {
console.error('Failed to upload attachments');
}
}

View File

@@ -80,8 +80,7 @@ function MessagesArea({
conversation_id: currentContact?.conversation_id,
},
(response: { status: string; message: string }) => {
if (response.status == 'ok') {
} else {
if (response.status !== 'ok') {
console.error('Failed to delete message: ', response.message);
}
},

View File

@@ -20,47 +20,10 @@ function initializeSocket(token: string): Socket | null {
return socket;
}
function sendMessage(message: string, recipient: string, tempId: string) {
if (!socket) {
console.error('Socket not initialized');
return;
}
socket.emit(
'chat message',
{
message: message,
recipient: recipient,
},
(response: { status: string; tempId: string }) => {
console.log(response.status, response.tempId);
},
);
console.log('sent message: ', {
message: message,
recipient: recipient,
});
}
function joinRoom(conversation_id: number) {
if (!socket) return;
socket.emit('join room', conversation_id);
console.log('sent on join room: ', conversation_id);
}
async function createRoom(
roomName: string,
): Promise<{ status: string } | undefined> {
return new Promise((resolve, reject) => {
if (!socket) {
reject('Socket not initialized');
return;
}
socket.emit('create room', roomName, (response: { status: string }) => {
resolve(response);
});
});
}
export { initializeSocket, sendMessage, createRoom, joinRoom, socket };
export { initializeSocket, joinRoom, socket };

View File

@@ -12,7 +12,7 @@ services:
PG_DATABASE: ${PG_DATABASE}
PG_HOST: db
ports:
- 3000
- "3000"
depends_on:
db:
condition: service_healthy

View File

@@ -89,7 +89,7 @@ async function createTables() {
conversation_id INT NOT NULL REFERENCES Conversations(conversation_id) ON DELETE CASCADE,
user_id INT REFERENCES Accounts(user_id) ON DELETE CASCADE,
joined_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (conversation_id, user_id)
PRIMARY KEY (conversation_id, user_id) -- This composite primary key ensures uniqueness, but make sure that these fields are frequently queried together to avoid unnecessary performance overhead.
);
CREATE INDEX IF NOT EXISTS idx_memberships_conversation_id ON Memberships (conversation_id);
CREATE INDEX IF NOT EXISTS idx_memberships_user_id ON Memberships (user_id);
@@ -100,6 +100,7 @@ async function createTables() {
}
try {
// Create Contacts Table
await client.query(`
CREATE TABLE IF NOT EXISTS Contacts (
contact_id SERIAL PRIMARY KEY,
@@ -113,10 +114,35 @@ async function createTables() {
CREATE INDEX IF NOT EXISTS idx_contacts_conversation_id ON Contacts (conversation_id);
CREATE INDEX IF NOT EXISTS idx_contacts_contact_id ON Contacts (contact_id);
`);
console.log("Contacts table created successfully.");
} catch (e) {
console.error("Failed to create Contacts table: ", e);
}
try {
// Create GroupAdmins Table
await client.query(`
CREATE TABLE IF NOT EXISTS GroupAdmins (
conversation_id INT NOT NULL REFERENCES Conversations(conversation_id) ON DELETE CASCADE,
user_id INT NOT NULL REFERENCES Accounts(user_id) ON DELETE CASCADE,
granted_by INT NOT NULL REFERENCES Accounts(user_id) ON DELETE CASCADE,
granted_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (conversation_id, user_id),
CONSTRAINT group_conversation_only CHECK (conversation_type = 'group'),
CONSTRAINT admin_grants_admin CHECK (
granted_by = user_id OR
EXISTS (
SELECT 1 FROM GroupAdmins ga
WHERE ga.conversation_id = GroupAdmins.conversation_id
AND ga.user_id = GroupAdmins.granted_by
)
)
);
CREATE INDEX IF NOT EXISTS idx_group_admins_conversation_id ON GroupAdmins (conversation_id);
CREATE INDEX IF NOT EXISTS idx_group_admins_user_id ON GroupAdmins (user_id);
`);
} catch (e) {
console.error("Failed to create GroupAdmins table: ", e);
}
}
async function insertUser(username, passwordHash) {