You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.6 KiB
JavaScript

"use strict";
var meta = require('../meta'),
Messaging = require('../messaging'),
utils = require('../../public/src/utils'),
10 years ago
async = require('async'),
server = require('./'),
10 years ago
rooms = require('./rooms'),
SocketModules = {
chats: {},
sounds: {},
settings: {}
};
/* Chat */
SocketModules.chats.get = function(socket, data, callback) {
11 years ago
if(!data) {
return callback(new Error('[[error:invalid-data]]'));
11 years ago
}
11 years ago
Messaging.getMessages(socket.uid, data.touid, data.since, false, callback);
};
11 years ago
SocketModules.chats.send = function(socket, data, callback) {
10 years ago
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
11 years ago
}
var now = Date.now(),
touid = parseInt(data.touid, 10);
10 years ago
// Websocket rate limiting
11 years ago
socket.lastChatMessageTime = socket.lastChatMessageTime || 0;
if (now - socket.lastChatMessageTime < 200) {
return callback(new Error('[[error:too-many-messages]]'));
} else {
socket.lastChatMessageTime = now;
11 years ago
}
Messaging.canMessage(socket.uid, touid, function(err, allowed) {
if (err || !allowed) {
return callback(err || new Error('[[error:chat-restricted]]'));
10 years ago
}
Messaging.addMessage(socket.uid, touid, data.message, function(err, message) {
if (err) {
return callback(err);
11 years ago
}
10 years ago
Messaging.notifyUser(socket.uid, touid, message);
10 years ago
callback();
10 years ago
});
10 years ago
});
};
11 years ago
10 years ago
SocketModules.chats.canMessage = function(socket, toUid, callback) {
Messaging.canMessage(socket.uid, toUid, function(err, allowed) {
callback(!allowed ? new Error('[[error:chat-restricted]]') : undefined);
});
};
SocketModules.chats.markRead = function(socket, touid, callback) {
Messaging.markRead(socket.uid, touid, function(err) {
if (!err) {
Messaging.pushUnreadCount(socket.uid);
}
});
};
11 years ago
SocketModules.chats.userStartTyping = function(socket, data, callback) {
sendTypingNotification('event:chats.userStartTyping', socket, data, callback);
};
SocketModules.chats.userStopTyping = function(socket, data, callback) {
sendTypingNotification('event:chats.userStopTyping', socket, data, callback);
};
function sendTypingNotification(event, socket, data, callback) {
if (!socket.uid || !data) {
return;
}
server.in('uid_' + data.touid).emit(event, data.fromUid);
11 years ago
}
11 years ago
SocketModules.chats.getRecentChats = function(socket, data, callback) {
if (!data || !utils.isNumber(data.after)) {
return callback(new Error('[[error:invalid-data]]'));
}
var start = parseInt(data.after, 10),
stop = start + 9;
11 years ago
Messaging.getRecentChats(socket.uid, start, stop, callback);
};
10 years ago
SocketModules.chats.sync = function(socket, data, callback) {
var chats = [],
uids = [],
socketIds = rooms.clients('uid_' + socket.uid);
rooms.broadcast(socket, 'uid_' + socket.uid, 'query:chats.sync', {}, function(err, sessionData) {
sessionData.forEach(function(data) {
data.forEach(function(chat) {
if (uids.indexOf(chat.uid) === -1) {
chats.push(chat);
uids.push(chat.uid);
}
});
});
callback(err, chats);
});
};
SocketModules.chats.open = function(socket, data, callback) {
rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.open', data);
};
SocketModules.chats.close = function(socket, data, callback) {
rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.close', data);
};
SocketModules.chats.toggleNew = function(socket, data, callback) {
rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.toggleNew', data);
};
/* Sounds */
SocketModules.sounds.getSounds = function(socket, data, callback) {
// Read sounds from local directory
meta.sounds.getFiles(callback);
};
SocketModules.sounds.getMapping = function(socket, data, callback) {
meta.sounds.getMapping(callback);
};
module.exports = SocketModules;