2015-09-25 13:11:11 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-12-15 19:05:32 +02:00
|
|
|
var async = require('async');
|
|
|
|
|
|
|
|
|
|
var messaging = require('../../messaging');
|
|
|
|
|
var meta = require('../../meta');
|
2016-09-20 14:26:26 +03:00
|
|
|
var user = require('../../user');
|
2017-12-18 15:43:57 -05:00
|
|
|
var privileges = require('../../privileges');
|
2015-12-15 19:05:32 +02:00
|
|
|
var helpers = require('../helpers');
|
2015-09-25 13:11:11 -04:00
|
|
|
|
2017-08-11 11:55:46 -04:00
|
|
|
var chatsController = module.exports;
|
2015-09-25 13:11:11 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
chatsController.get = function (req, res, callback) {
|
2018-10-21 16:47:51 -04:00
|
|
|
if (meta.config.disableChat) {
|
2015-09-25 13:11:11 -04:00
|
|
|
return callback();
|
|
|
|
|
}
|
2017-08-11 11:55:46 -04:00
|
|
|
|
2016-09-20 14:26:26 +03:00
|
|
|
var uid;
|
|
|
|
|
var recentChats;
|
2016-09-20 14:41:53 +03:00
|
|
|
|
2016-09-20 14:26:26 +03:00
|
|
|
async.waterfall([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2017-08-11 11:55:46 -04:00
|
|
|
user.getUidByUserslug(req.params.userslug, next);
|
2016-09-20 14:26:26 +03:00
|
|
|
},
|
2017-08-11 11:55:46 -04:00
|
|
|
function (_uid, next) {
|
|
|
|
|
uid = _uid;
|
2016-09-20 14:26:26 +03:00
|
|
|
if (!uid) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2018-01-03 13:27:30 -05:00
|
|
|
privileges.global.can('chat', req.uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (canChat, next) {
|
|
|
|
|
if (!canChat) {
|
|
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
2016-10-03 20:35:36 +03:00
|
|
|
messaging.getRecentChats(req.uid, uid, 0, 19, next);
|
2016-09-20 14:26:26 +03:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (_recentChats, next) {
|
2016-09-20 14:26:26 +03:00
|
|
|
recentChats = _recentChats;
|
2016-10-03 20:35:36 +03:00
|
|
|
if (!recentChats) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2016-09-20 14:26:26 +03:00
|
|
|
if (!req.params.roomid) {
|
|
|
|
|
return res.render('chats', {
|
|
|
|
|
rooms: recentChats.rooms,
|
2016-09-20 14:41:53 +03:00
|
|
|
uid: uid,
|
2016-09-20 14:26:26 +03:00
|
|
|
userslug: req.params.userslug,
|
|
|
|
|
nextStart: recentChats.nextStart,
|
|
|
|
|
allowed: true,
|
|
|
|
|
title: '[[pages:chats]]',
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-06-01 13:12:28 -04:00
|
|
|
messaging.loadRoom(req.uid, { uid: uid, roomId: req.params.roomid }, next);
|
2016-09-20 14:26:26 +03:00
|
|
|
},
|
2018-06-01 13:12:28 -04:00
|
|
|
function (room) {
|
|
|
|
|
if (!room) {
|
2016-09-20 14:26:26 +03:00
|
|
|
return callback();
|
|
|
|
|
}
|
2017-08-11 11:55:46 -04:00
|
|
|
room.rooms = recentChats.rooms;
|
|
|
|
|
room.nextStart = recentChats.nextStart;
|
|
|
|
|
room.title = room.roomName || room.usernames || '[[pages:chats]]';
|
2018-06-01 13:12:28 -04:00
|
|
|
room.uid = uid;
|
|
|
|
|
room.userslug = req.params.userslug;
|
2017-08-11 11:55:46 -04:00
|
|
|
res.render('chats', room);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2015-09-25 13:11:11 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
chatsController.redirectToChat = function (req, res, next) {
|
2016-09-26 22:01:47 +03:00
|
|
|
var roomid = parseInt(req.params.roomid, 10);
|
2018-01-31 15:20:17 -05:00
|
|
|
if (!req.loggedIn) {
|
2016-09-26 22:01:47 +03:00
|
|
|
return next();
|
|
|
|
|
}
|
2017-08-11 11:55:46 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
user.getUserField(req.uid, 'userslug', next);
|
|
|
|
|
},
|
|
|
|
|
function (userslug, next) {
|
|
|
|
|
if (!userslug) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
helpers.redirect(res, '/user/' + userslug + '/chats' + (roomid ? '/' + roomid : ''));
|
|
|
|
|
},
|
|
|
|
|
], next);
|
2016-09-26 22:01:47 +03:00
|
|
|
};
|