Files
NodeBB/src/controllers/accounts/chats.js

66 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
2019-08-12 14:49:40 -04:00
const messaging = require('../../messaging');
const meta = require('../../meta');
const user = require('../../user');
const privileges = require('../../privileges');
const helpers = require('../helpers');
2019-08-12 14:49:40 -04:00
const chatsController = module.exports;
2019-08-12 14:49:40 -04:00
chatsController.get = async function (req, res, next) {
if (meta.config.disableChat) {
2019-08-12 14:49:40 -04:00
return next();
}
2017-08-11 11:55:46 -04:00
2019-08-12 14:49:40 -04:00
const uid = await user.getUidByUserslug(req.params.userslug);
if (!uid) {
return next();
}
const canChat = await privileges.global.can('chat', req.uid);
if (!canChat) {
return next(new Error('[[error:no-privileges]]'));
}
const recentChats = await messaging.getRecentChats(req.uid, uid, 0, 19);
if (!recentChats) {
return next();
}
2016-09-20 14:41:53 +03:00
2019-08-12 14:49:40 -04:00
if (!req.params.roomid) {
return res.render('chats', {
rooms: recentChats.rooms,
uid: uid,
userslug: req.params.userslug,
nextStart: recentChats.nextStart,
allowed: true,
title: '[[pages:chats]]',
});
}
const room = await messaging.loadRoom(req.uid, { uid: uid, roomId: req.params.roomid });
if (!room) {
return next();
}
room.rooms = recentChats.rooms;
room.nextStart = recentChats.nextStart;
room.title = room.roomName || room.usernames || '[[pages:chats]]';
room.uid = uid;
room.userslug = req.params.userslug;
room.canViewInfo = await privileges.global.can('view:users:info', uid);
2019-08-12 14:49:40 -04:00
res.render('chats', room);
};
2019-08-12 14:49:40 -04:00
chatsController.redirectToChat = async function (req, res, next) {
2018-01-31 15:20:17 -05:00
if (!req.loggedIn) {
2016-09-26 22:01:47 +03:00
return next();
}
2019-08-12 14:49:40 -04:00
const userslug = await user.getUserField(req.uid, 'userslug');
if (!userslug) {
return next();
}
const roomid = parseInt(req.params.roomid, 10);
helpers.redirect(res, '/user/' + userslug + '/chats' + (roomid ? '/' + roomid : ''));
2016-09-26 22:01:47 +03:00
};