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.
154 lines
4.0 KiB
154 lines
4.0 KiB
// email.js
|
|
// Copyright (C) 2021 Digital Telepresence, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const express = require('express');
|
|
|
|
const { SiteController,/*, SiteError*/
|
|
SiteError} = require('../../lib/site-lib');
|
|
|
|
class ChatController extends SiteController {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, module.exports);
|
|
}
|
|
|
|
async start ( ) {
|
|
const {
|
|
chat: chatService,
|
|
limiter: limiterService,
|
|
session: sessionService,
|
|
} = this.dtp.services;
|
|
|
|
const router = express.Router();
|
|
this.dtp.app.use('/chat', router);
|
|
|
|
router.use(
|
|
sessionService.authCheckMiddleware({ requireLogin: true }),
|
|
chatService.middleware({ maxOwnedRooms: 25, maxJoinedRooms: 50 }),
|
|
async (req, res, next) => {
|
|
res.locals.currentView = 'chat';
|
|
return next();
|
|
},
|
|
);
|
|
|
|
router.param('roomId', this.populateRoomId.bind(this));
|
|
|
|
router.post(
|
|
'/room/:roomId',
|
|
limiterService.create(limiterService.config.chat.postRoomUpdate),
|
|
this.postRoomUpdate.bind(this),
|
|
);
|
|
|
|
router.post(
|
|
'/room',
|
|
limiterService.create(limiterService.config.chat.postRoomCreate),
|
|
this.postRoomCreate.bind(this),
|
|
);
|
|
|
|
router.get(
|
|
'/room/create',
|
|
this.getRoomEditor.bind(this),
|
|
);
|
|
|
|
router.get(
|
|
'/room/:roomId',
|
|
limiterService.create(limiterService.config.chat.getRoomView),
|
|
this.getRoomView.bind(this),
|
|
);
|
|
|
|
router.get(
|
|
'/room',
|
|
limiterService.create(limiterService.config.chat.getRoomView),
|
|
this.getRoomHome.bind(this),
|
|
);
|
|
|
|
router.get(
|
|
'/',
|
|
limiterService.create(limiterService.config.chat.getHome),
|
|
this.getHome.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, 'Room not found');
|
|
}
|
|
return next();
|
|
} catch (error) {
|
|
this.log.error('failed to populate roomId', { roomId, error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async postRoomUpdate (req, res, next) {
|
|
const { chat: chatService } = this.dtp.services;
|
|
try {
|
|
res.locals.room = await chatService.updateRoom(res.locals.room, req.body);
|
|
res.redirect(`/chat/${res.locals.room._id}`);
|
|
} catch (error) {
|
|
this.log.error('failed to update chat room', { roomId: res.locals.room._id, error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async postRoomCreate (req, res, next) {
|
|
const { chat: chatService } = this.dtp.services;
|
|
try {
|
|
res.locals.room = await chatService.createRoom(req.user, req.body);
|
|
res.redirect(`/chat/${res.locals.room._id}`);
|
|
} catch (error) {
|
|
this.log.error('failed to create chat room', { error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async getRoomEditor (req, res) {
|
|
res.render('chat/room/editor');
|
|
}
|
|
|
|
async getRoomView (req, res, next) {
|
|
const { chat: chatService } = this.dtp.services;
|
|
try {
|
|
res.locals.pageTitle = res.locals.room.name;
|
|
|
|
const pagination = { skip: 0, cpp: 20 };
|
|
res.locals.chatMessages = await chatService.getChannelHistory(res.locals.room, pagination);
|
|
|
|
res.render('chat/room/view');
|
|
} catch (error) {
|
|
this.log.error('failed to render chat room view', { roomId: req.params.roomId, error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async getRoomHome (req, res, next) {
|
|
const { chat: chatService } = this.dtp.services;
|
|
try {
|
|
res.locals.pagination = this.getPaginationParameters(req, 20);
|
|
res.locals.rooms = await chatService.getPublicRooms(req.user, res.locals.pagination);
|
|
res.render('chat/room/index');
|
|
} catch (error) {
|
|
this.log.error('failed to render room home', { error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async getHome (req, res) {
|
|
res.locals.pageTitle = 'Chat Home';
|
|
res.render('chat/index');
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
slug: 'chat',
|
|
name: 'chat',
|
|
create: async (dtp) => { return new ChatController(dtp); },
|
|
};
|