all files / src/controllers/accounts/ chats.js

20.59% Statements 7/34
0% Branches 0/19
0% Functions 0/6
20.59% Lines 7/34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                                                                                                                           
'use strict';
 
var async = require('async');
 
var messaging = require('../../messaging');
var meta = require('../../meta');
var helpers = require('../helpers');
 
 
var chatsController = {};
 
chatsController.get = function(req, res, callback) {
	if (parseInt(meta.config.disableChat, 10) === 1) {
		return callback();
	}
 
	messaging.getRecentChats(req.uid, 0, 19, function(err, recentChats) {
		if (err) {
			return callback(err);
		}
 
		if (!req.params.roomid) {
			return res.render('chats', {
				rooms: recentChats.rooms,
				nextStart: recentChats.nextStart,
				allowed: true,
				title: '[[pages:chats]]',
				breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:chats]]'}])
			});
		}
 
		async.waterfall([
			function (next) {
				messaging.isUserInRoom(req.uid, req.params.roomid, next);
			},
			function (inRoom, next) {
				if (!inRoom) {
					return callback();
				}
 
				async.parallel({
					users: async.apply(messaging.getUsersInRoom, req.params.roomid, 0, -1),
					messages: async.apply(messaging.getMessages, {
						uid: req.uid,
						roomId: req.params.roomid,
						since: 'recent',
						isNew: false
					}),
					room: async.apply(messaging.getRoomData, req.params.roomid)
				}, next);
			}
		], function(err, data) {
			if (err) {
				return callback(err);
			}
			var room = data.room;
			room.messages = data.messages;
 
			room.isOwner = parseInt(room.owner, 10) === parseInt(req.uid, 10);
			room.users = data.users.filter(function(user) {
				return user && parseInt(user.uid, 10) && parseInt(user.uid, 10) !== req.uid;
			});
 
			room.rooms = recentChats.rooms;
			room.nextStart = recentChats.nextStart;
			room.title = room.roomName;
			room.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[pages:chats]]', url: '/chats'}, {text: room.roomName}]);
			room.maximumUsersInChatRoom = parseInt(meta.config.maximumUsersInChatRoom, 10) || 0;
			room.maximumChatMessageLength = parseInt(meta.config.maximumChatMessageLength, 10) || 1000;
			room.showUserInput = !room.maximumUsersInChatRoom || room.maximumUsersInChatRoom > 2;
 
			res.render('chats', room);
		});
	});
};
 
module.exports = chatsController;