diff --git a/docker-compose.yml b/docker-compose.yml index c6549f6..5c3afb8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,12 @@ +version: '3.8' + services: relay-server: build: context: ./server dockerfile: Dockerfile + env_file: + - server/.env environment: SERVER_PORT: ${SERVER_PORT} JWT_SECRET: ${JWT_SECRET} @@ -11,6 +15,8 @@ services: PG_PASSWORD: ${PG_PASSWORD} PG_DATABASE: ${PG_DATABASE} PG_HOST: db + volumes: + - attachments:/usr/src/app/attachments ports: - "3000" depends_on: @@ -36,6 +42,8 @@ services: image: postgres:latest restart: always user: postgres + env_file: + - server/.env volumes: - postgres-data:/var/lib/postgresql/data environment: @@ -57,4 +65,5 @@ networks: driver: bridge volumes: - postgres-data: \ No newline at end of file + postgres-data: + attachments: \ No newline at end of file diff --git a/server/Dockerfile b/server/Dockerfile index c8b2f6d..9f165c1 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,21 +1,24 @@ - ARG NODE_VERSION=20.17.0 FROM node:${NODE_VERSION}-alpine WORKDIR /usr/src/app +# Create the attachments directory and set permissions RUN mkdir -p attachments && chown -R node:node attachments +# Install dependencies RUN --mount=type=bind,source=package.json,target=package.json \ --mount=type=bind,source=package-lock.json,target=package-lock.json \ --mount=type=cache,target=/root/.npm \ npm ci --omit=dev +# Switch to the node user USER node -COPY . . +# Copy the application code +COPY --chown=node:node . . EXPOSE 3000 -CMD node server.js +CMD node server.js \ No newline at end of file diff --git a/server/db/db.js b/server/db/db.js index bb8afff..b087123 100644 --- a/server/db/db.js +++ b/server/db/db.js @@ -529,7 +529,7 @@ async function insertContact(initiatorId, receiverId, contactUsername, read) { SELECT COUNT(DISTINCT user_id) FROM Memberships WHERE conversation_id = c.conversation_id - ) = 1 -- Allow a conversation with a single user (self) + ) = 1 LIMIT 1 `; const conversationResult = await client.query(findConversationQuery, [ @@ -540,16 +540,20 @@ async function insertContact(initiatorId, receiverId, contactUsername, read) { // Create new conversation if none exists if (!conversation_id) { - console.error("CONVERSATION DON'T EXIST: ", conversation_id); const createConversationQuery = ` INSERT INTO Conversations (conversation_type) VALUES ('direct') RETURNING conversation_id `; const newConversationResult = await client.query(createConversationQuery); + + if (!newConversationResult) { + console.error("Failed to create new conversation"); + return null; + } + conversation_id = newConversationResult.rows[0].conversation_id; - console.error("NEW CONVERSATION ID: ", conversation_id); - // Create memberships for both users (or single user if they are the same) + // Create memberships for users const createMembershipsQuery = ` INSERT INTO Memberships (conversation_id, user_id) VALUES ($1, $2), ($1, $3)