|
|
@@ -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("/");
|