Просмотр исходного кода

refactor: Separate chat router

gugdun 10 месяцев назад
Родитель
Сommit
d907153334
4 измененных файлов с 49 добавлено и 41 удалено
  1. 1 0
      public/css/main.css
  2. 2 0
      src/index.js
  3. 46 0
      src/routes/chat.js
  4. 0 41
      src/routes/index.js

+ 1 - 0
public/css/main.css

@@ -208,6 +208,7 @@ body {
     flex-direction: column-reverse;
     width: 100%;
     height: 100%;
+    overflow-y: auto;
 }
 
 .message {

+ 2 - 0
src/index.js

@@ -12,6 +12,7 @@ const pgSession = require("connect-pg-simple")(session);
 
 const indexRouter = require("./routes/index");
 const authRouter = require("./routes/auth");
+const chatRouter = require("./routes/chat");
 
 const PORT = process.env.PORT || 5000;
 
@@ -40,6 +41,7 @@ longpoll.create("/poll");
 
 app.use(indexRouter);
 app.use(authRouter);
+app.use(chatRouter);
 
 app.use(function (req, res, next) {
     next(createError(404));

+ 46 - 0
src/routes/chat.js

@@ -0,0 +1,46 @@
+// Copyright (c) 2025 gugdun
+// All rights reserved. Unauthorized use, copying, or distribution is strictly prohibited.
+
+const path = require("path");
+const express = require("express");
+const ejs = require("ejs");
+const db = require("../db");
+
+const views = path.join(__dirname, "..", "views");
+const router = express.Router();
+
+router.get("/chat/:chat_id", async (req, res) => {
+    if (req.user) {
+        try {
+            const user = await db.one("SELECT id FROM users WHERE username = $1", [ req.params.chat_id ]);
+            const chat = await db.one("SELECT id FROM chats WHERE (user1_id = $1 AND user2_id = $2) OR (user1_id = $2 AND user2_id = $1)", [ req.user.id, user?.id ]);
+            const messages = await db.any("SELECT username, attachment_id, text, timestamp FROM messages JOIN users ON users.id = user_id WHERE chat_id = $1 ORDER BY messages.timestamp DESC", [ chat?.id ]);
+            res.render("layout", {
+                child: await ejs.renderFile(path.join(views, "chat.ejs"), {
+                    title: req.params.chat_id,
+                    empty: messages.length < 1,
+                    messages: messages.map((message) => {
+                        return {
+                            username: message.username,
+                            text: message.text,
+                            datetime: message.timestamp
+                        }
+                    })
+                })
+            });
+        } catch (err) {
+            console.log(err);
+            res.render("layout", {
+                child: await ejs.renderFile(path.join(views, "chat.ejs"), {
+                    title: req.params.chat_id,
+                    empty: true,
+                    messages: []
+                })
+            });
+        }
+    } else {
+        res.redirect("/login");
+    }
+});
+
+module.exports = router;

+ 0 - 41
src/routes/index.js

@@ -36,47 +36,6 @@ router.get("/", async (req, res) => {
     }
 });
 
-router.get("/chat/:chat_id", async (req, res) => {
-    if (req.user) {
-        try {
-            const user = await db.one("SELECT id FROM users WHERE username = $1", [ req.params.chat_id ]);
-            const chat = await db.one("SELECT id FROM chats WHERE (user1_id = $1 AND user2_id = $2) OR (user1_id = $2 AND user2_id = $1)", [ req.user.id, user?.id ]);
-            const messages = await db.any("SELECT username, attachment_id, text, timestamp FROM messages JOIN users ON users.id = user_id WHERE chat_id = $1 ORDER BY messages.timestamp DESC", [ chat?.id ]);
-            res.render("layout", {
-                child: await ejs.renderFile(path.join(views, "chat.ejs"), {
-                    title: req.params.chat_id,
-                    empty: messages.length < 1,
-                    messages: messages.map((message) => {
-                        const date = new Date(message.timestamp);
-                        const day = date.getDate().toString().padStart(2, '0');
-                        const month = date.getMonth().toString().padStart(2, '0');
-                        const hours = date.getHours().toString().padStart(2, '0');
-                        const minutes = date.getMinutes().toString().padStart(2, '0');
-                        const seconds = date.getSeconds().toString().padStart(2, '0');
-                        const format = `${day}.${month}.${date.getFullYear()} ${hours}:${minutes}:${seconds}`;
-                        return {
-                            username: message.username,
-                            text: message.text,
-                            datetime: message.timestamp
-                        }
-                    })
-                })
-            });
-        } catch (err) {
-            console.log(err);
-            res.render("layout", {
-                child: await ejs.renderFile(path.join(views, "chat.ejs"), {
-                    title: req.params.chat_id,
-                    empty: true,
-                    messages: []
-                })
-            });
-        }
-    } else {
-        res.redirect("/login");
-    }
-});
-
 router.get("/login", async (req, res) => {
     if (req.user) {
         res.redirect("/");