2015-09-25 13:11:11 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2023-07-12 13:03:54 -04:00
|
|
|
const db = require('../../database');
|
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');
|
2015-12-15 19:05:32 +02:00
|
|
|
|
2019-08-12 14:49:40 -04:00
|
|
|
const chatsController = module.exports;
|
2015-09-25 13:11:11 -04:00
|
|
|
|
2019-08-12 14:49:40 -04:00
|
|
|
chatsController.get = async function (req, res, next) {
|
2018-10-21 16:47:51 -04:00
|
|
|
if (meta.config.disableChat) {
|
2019-08-12 14:49:40 -04:00
|
|
|
return next();
|
2015-09-25 13:11:11 -04:00
|
|
|
}
|
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]]'));
|
|
|
|
|
}
|
2023-07-12 13:03:54 -04:00
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
title: '[[pages:chats]]',
|
|
|
|
|
uid: uid,
|
|
|
|
|
userslug: req.params.userslug,
|
|
|
|
|
};
|
|
|
|
|
const isSwitch = res.locals.isAPI && parseInt(req.query.switch, 10) === 1;
|
|
|
|
|
if (!isSwitch) {
|
|
|
|
|
const [recentChats, publicRooms, privateRoomCount] = await Promise.all([
|
|
|
|
|
messaging.getRecentChats(req.uid, uid, 0, 29),
|
|
|
|
|
messaging.getPublicRooms(req.uid, uid),
|
|
|
|
|
db.sortedSetCard(`uid:${uid}:chat:rooms`),
|
|
|
|
|
]);
|
|
|
|
|
if (!recentChats) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
payload.rooms = recentChats.rooms;
|
|
|
|
|
payload.nextStart = recentChats.nextStart;
|
|
|
|
|
payload.publicRooms = publicRooms;
|
|
|
|
|
payload.privateRoomCount = privateRoomCount;
|
2019-08-12 14:49:40 -04:00
|
|
|
}
|
2016-09-20 14:41:53 +03:00
|
|
|
|
2019-08-12 14:49:40 -04:00
|
|
|
if (!req.params.roomid) {
|
2023-07-12 13:03:54 -04:00
|
|
|
return res.render('chats', payload);
|
2019-08-12 14:49:40 -04:00
|
|
|
}
|
2023-07-12 13:03:54 -04:00
|
|
|
|
2019-08-12 14:49:40 -04:00
|
|
|
const room = await messaging.loadRoom(req.uid, { uid: uid, roomId: req.params.roomid });
|
|
|
|
|
if (!room) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
room.title = room.roomName || room.usernames || '[[pages:chats]]';
|
2023-07-12 13:03:54 -04:00
|
|
|
room.bodyClasses = ['chat-loaded'];
|
2019-09-17 18:02:52 +00:00
|
|
|
room.canViewInfo = await privileges.global.can('view:users:info', uid);
|
|
|
|
|
|
2023-07-12 13:03:54 -04:00
|
|
|
res.render('chats', {
|
|
|
|
|
...payload,
|
|
|
|
|
...room,
|
|
|
|
|
});
|
2015-09-25 13:11:11 -04:00
|
|
|
};
|
|
|
|
|
|
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);
|
2021-02-03 23:59:08 -07:00
|
|
|
helpers.redirect(res, `/user/${userslug}/chats${roomid ? `/${roomid}` : ''}`);
|
2016-09-26 22:01:47 +03:00
|
|
|
};
|