You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
3.6 KiB
141 lines
3.6 KiB
// auth.js
|
|
// Copyright (C) 2024 DTP Technologies, LLC
|
|
// All Rights Reserved
|
|
|
|
'use strict';
|
|
|
|
import express from 'express';
|
|
|
|
import { SiteController, SiteError } from '../../lib/site-lib.js';
|
|
|
|
export default class ChatController extends SiteController {
|
|
|
|
static get name ( ) { return 'ChatController'; }
|
|
static get slug ( ) { return 'chat'; }
|
|
|
|
constructor (dtp) {
|
|
super(dtp, ChatController);
|
|
}
|
|
|
|
async start ( ) {
|
|
const {
|
|
// csrfToken: csrfTokenService,
|
|
// limiter: limiterService,
|
|
session: sessionService,
|
|
} = this.dtp.services;
|
|
|
|
const authRequired = sessionService.authCheckMiddleware({ requireLogin: true });
|
|
const multer = this.createMulter(ChatController.slug);
|
|
|
|
const router = express.Router();
|
|
this.dtp.app.use('/chat', router);
|
|
|
|
router.use(
|
|
async (req, res, next) => {
|
|
res.locals.currentView = 'auth';
|
|
return next();
|
|
},
|
|
authRequired,
|
|
);
|
|
|
|
router.param('roomId', this.populateRoomId.bind(this));
|
|
|
|
router.post(
|
|
'/room/:roomId/message',
|
|
multer.none(),
|
|
this.postRoomMessage.bind(this),
|
|
);
|
|
|
|
router.post(
|
|
'/room',
|
|
// limiterService.create(limiterService.config.chat.postCreateRoom),
|
|
this.postCreateRoom.bind(this),
|
|
);
|
|
|
|
router.get(
|
|
'/room/create',
|
|
this.getRoomCreateView.bind(this),
|
|
);
|
|
|
|
router.get(
|
|
'/room/:roomId/join',
|
|
// limiterService.create(limiterService.config.chat.getRoomView),
|
|
this.getRoomJoin.bind(this),
|
|
);
|
|
|
|
router.get(
|
|
'/room/:roomId',
|
|
// limiterService.create(limiterService.config.chat.getRoomView),
|
|
this.getRoomView.bind(this),
|
|
);
|
|
|
|
return router;
|
|
}
|
|
|
|
async populateRoomId (req, res, next, roomId) {
|
|
const { chat: chatService } = this.dtp.services;
|
|
try {
|
|
res.locals.room = await chatService.getRoomById(roomId);
|
|
if (!res.locals.room) {
|
|
throw new SiteError(404, "The chat room doesn't exist.");
|
|
}
|
|
return next();
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async postRoomMessage (req, res) {
|
|
const { chat: chatService } = this.dtp.services;
|
|
try {
|
|
await chatService.sendRoomMessage(res.locals.room, req.user, req.body);
|
|
return res.status(200).json({ success: true });
|
|
} catch (error) {
|
|
this.log.error('failed to send chat room message', { error });
|
|
return res.status(error.statusCode || 500).json({
|
|
success: false,
|
|
message: error.message,
|
|
});
|
|
}
|
|
}
|
|
|
|
async postCreateRoom (req, res, next) {
|
|
const { chat: chatService } = this.dtp.services;
|
|
try {
|
|
res.locals.room = await chatService.createRoom(req.user, req.body);
|
|
res.redirect(`/chat/room/${res.locals.room._id}`);
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async getRoomCreateView (req, res) {
|
|
res.render('chat/room/create');
|
|
}
|
|
|
|
async getRoomJoin (req, res, next) {
|
|
const { chat: chatService } = this.dtp.services;
|
|
try {
|
|
await chatService.joinRoom(res.locals.room, req.user);
|
|
res.status(200).json({ success: true, room: res.locals.room });
|
|
} catch (error) {
|
|
this.log.error('failed to join chat room', { error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async getRoomView (req, res, next) {
|
|
const { chat: chatService } = this.dtp.services;
|
|
try {
|
|
res.locals.currentView = 'chat-room';
|
|
|
|
res.locals.pagination = this.getPaginationParameters(req, 20);
|
|
res.locals.messages = await chatService.getRoomMessages(res.locals.room, res.locals.pagination);
|
|
|
|
res.render('chat/room/view');
|
|
} catch (error) {
|
|
this.log.error('failed to present the chat room view', { error });
|
|
return next(error);
|
|
}
|
|
}
|
|
}
|