2014-01-10 16:00:03 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
var posts = require('../posts'),
|
|
|
|
|
postTools = require('../postTools'),
|
|
|
|
|
topics = require('../topics'),
|
|
|
|
|
meta = require('../meta'),
|
|
|
|
|
Messaging = require('../messaging'),
|
|
|
|
|
user = require('../user'),
|
|
|
|
|
notifications = require('../notifications'),
|
2014-01-18 15:35:51 -05:00
|
|
|
plugins = require('../plugins'),
|
2014-07-08 11:55:55 -04:00
|
|
|
utils = require('../../public/src/utils'),
|
2014-01-10 16:00:03 -05:00
|
|
|
|
|
|
|
|
async = require('async'),
|
|
|
|
|
S = require('string'),
|
|
|
|
|
winston = require('winston'),
|
2014-03-01 21:31:50 -05:00
|
|
|
_ = require('underscore'),
|
2014-01-16 20:14:09 -05:00
|
|
|
server = require('./'),
|
2014-07-08 21:53:18 -04:00
|
|
|
nconf = require('nconf'),
|
2014-01-10 16:00:03 -05:00
|
|
|
|
2014-03-14 00:34:51 -04:00
|
|
|
SocketModules = {
|
2014-10-25 19:19:47 -04:00
|
|
|
composer: {},
|
2014-03-14 00:34:51 -04:00
|
|
|
chats: {},
|
|
|
|
|
notifications: {},
|
|
|
|
|
sounds: {},
|
|
|
|
|
settings: {}
|
|
|
|
|
};
|
2014-01-10 16:00:03 -05:00
|
|
|
|
|
|
|
|
/* Posts Composer */
|
|
|
|
|
|
2014-01-16 22:06:23 -05:00
|
|
|
SocketModules.composer.push = function(socket, pid, callback) {
|
2014-05-21 16:13:46 -04:00
|
|
|
posts.getPostFields(pid, ['content', 'tid'], function(err, postData) {
|
2014-04-09 21:36:57 -04:00
|
|
|
if(err || (!postData && !postData.content)) {
|
|
|
|
|
return callback(err || new Error('[[error:invalid-pid]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
topic: function(next) {
|
|
|
|
|
topics.getTopicDataByPid(pid, next);
|
|
|
|
|
},
|
2014-05-21 16:13:46 -04:00
|
|
|
tags: function(next) {
|
|
|
|
|
topics.getTopicTags(postData.tid, next);
|
|
|
|
|
},
|
2014-06-13 15:33:22 -04:00
|
|
|
isMain: function(next) {
|
2014-06-23 18:06:59 -04:00
|
|
|
posts.isMain(pid, next);
|
2014-04-09 21:36:57 -04:00
|
|
|
}
|
|
|
|
|
}, function(err, results) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-01 17:24:18 -04:00
|
|
|
if (!results.topic) {
|
|
|
|
|
return callback(new Error('[[error:no-topic]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-09 21:36:57 -04:00
|
|
|
callback(null, {
|
|
|
|
|
pid: pid,
|
|
|
|
|
body: postData.content,
|
|
|
|
|
title: results.topic.title,
|
|
|
|
|
topic_thumb: results.topic.thumb,
|
2014-05-21 16:13:46 -04:00
|
|
|
tags: results.tags,
|
2014-06-13 15:33:22 -04:00
|
|
|
isMain: results.isMain
|
2014-04-09 21:36:57 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
2014-01-16 15:28:21 -05:00
|
|
|
SocketModules.composer.editCheck = function(socket, pid, callback) {
|
2014-06-23 18:06:59 -04:00
|
|
|
posts.isMain(pid, function(err, isMain) {
|
|
|
|
|
callback(err, {
|
|
|
|
|
titleEditable: isMain
|
2014-01-10 16:00:03 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-17 16:16:00 -05:00
|
|
|
SocketModules.composer.renderPreview = function(socket, content, callback) {
|
2014-11-05 18:59:20 -05:00
|
|
|
plugins.fireHook('filter:parse.raw', content, callback);
|
2014-02-27 14:05:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SocketModules.composer.renderHelp = function(socket, data, callback) {
|
2014-05-10 23:43:15 -04:00
|
|
|
var helpText = meta.config['composer:customHelpText'] || '';
|
|
|
|
|
|
|
|
|
|
if (meta.config['composer:showHelpTab'] === '0') {
|
|
|
|
|
return callback(new Error('help-hidden'));
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-05 18:59:20 -05:00
|
|
|
plugins.fireHook('filter:parse.raw', helpText, function(err, helpText) {
|
2014-05-10 23:43:15 -04:00
|
|
|
if (!meta.config['composer:allowPluginHelp'] || meta.config['composer:allowPluginHelp'] === '1') {
|
|
|
|
|
plugins.fireHook('filter:composer.help', helpText, callback);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, helpText);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-02-27 14:05:31 -05:00
|
|
|
};
|
2014-01-17 16:16:00 -05:00
|
|
|
|
2014-10-25 19:19:47 -04:00
|
|
|
SocketModules.composer.notifyTyping = function(socket, data) {
|
2014-10-29 00:10:57 -04:00
|
|
|
if (!socket.uid || !parseInt(data.tid, 10)) {
|
2014-10-25 19:22:13 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2014-10-25 19:19:47 -04:00
|
|
|
server.in('topic_' + data.tid).emit('event:topic.notifyTyping', data);
|
2014-03-01 15:46:13 -05:00
|
|
|
};
|
|
|
|
|
|
2014-10-25 19:19:47 -04:00
|
|
|
SocketModules.composer.stopNotifyTyping = function(socket, data) {
|
2014-10-29 00:10:57 -04:00
|
|
|
if (!socket.uid || !parseInt(data.tid, 10)) {
|
2014-10-25 19:22:13 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2014-10-25 19:19:47 -04:00
|
|
|
server.in('topic_' + data.tid).emit('event:topic.stopNotifyTyping', data);
|
2014-03-28 12:13:01 -04:00
|
|
|
};
|
2014-03-01 21:31:50 -05:00
|
|
|
|
2014-01-10 16:00:03 -05:00
|
|
|
/* Chat */
|
|
|
|
|
|
2014-01-16 15:28:21 -05:00
|
|
|
SocketModules.chats.get = function(socket, data, callback) {
|
2014-01-17 12:55:38 -05:00
|
|
|
if(!data) {
|
2014-04-09 21:36:57 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
2014-01-17 12:55:38 -05:00
|
|
|
}
|
|
|
|
|
|
2014-09-17 16:29:03 -04:00
|
|
|
Messaging.getMessages(socket.uid, data.touid, data.since, false, callback);
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
2014-03-28 12:13:01 -04:00
|
|
|
SocketModules.chats.send = function(socket, data, callback) {
|
2014-01-17 12:55:38 -05:00
|
|
|
if(!data) {
|
2014-04-09 21:36:57 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
2014-01-17 12:55:38 -05:00
|
|
|
}
|
2014-01-10 16:00:03 -05:00
|
|
|
|
2014-04-23 21:47:51 -04:00
|
|
|
var touid = parseInt(data.touid, 10);
|
2014-01-16 15:28:21 -05:00
|
|
|
if (touid === socket.uid || socket.uid === 0) {
|
2014-01-10 16:00:03 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-15 14:49:11 -04:00
|
|
|
var msg = S(data.message).stripTags().s;
|
2014-07-23 18:23:03 -04:00
|
|
|
|
2014-10-15 14:49:11 -04:00
|
|
|
var now = Date.now();
|
|
|
|
|
socket.lastChatMessageTime = socket.lastChatMessageTime || 0;
|
2014-07-23 18:23:03 -04:00
|
|
|
|
2014-10-15 14:49:11 -04:00
|
|
|
if (now - socket.lastChatMessageTime < 200) {
|
|
|
|
|
return callback(new Error('[[error:too-many-messages]]'));
|
|
|
|
|
}
|
2014-07-23 18:23:03 -04:00
|
|
|
|
2014-10-15 14:49:11 -04:00
|
|
|
socket.lastChatMessageTime = now;
|
2014-01-10 16:00:03 -05:00
|
|
|
|
2014-10-03 15:28:46 -04:00
|
|
|
user.getUserField(socket.uid, 'banned', function(err, banned) {
|
2014-03-28 12:13:01 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
2014-01-10 16:00:03 -05:00
|
|
|
}
|
|
|
|
|
|
2014-10-03 15:28:46 -04:00
|
|
|
if (parseInt(banned, 10) === 1) {
|
|
|
|
|
return callback(new Error('[[error:user-banned]]'));
|
|
|
|
|
}
|
2014-01-10 16:00:03 -05:00
|
|
|
|
2014-10-30 17:50:07 -04:00
|
|
|
Messaging.canMessage(socket.uid, touid, function(err, allowed) {
|
|
|
|
|
if (allowed) {
|
|
|
|
|
Messaging.addMessage(socket.uid, touid, msg, function(err, message) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Messaging.notifyUser(socket.uid, touid, message);
|
|
|
|
|
|
|
|
|
|
// Recipient
|
|
|
|
|
SocketModules.chats.pushUnreadCount(touid);
|
|
|
|
|
server.in('uid_' + touid).emit('event:chats.receive', {
|
|
|
|
|
withUid: socket.uid,
|
|
|
|
|
message: message,
|
|
|
|
|
self: 0
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Sender
|
|
|
|
|
SocketModules.chats.pushUnreadCount(socket.uid);
|
|
|
|
|
server.in('uid_' + socket.uid).emit('event:chats.receive', {
|
|
|
|
|
withUid: touid,
|
|
|
|
|
message: message,
|
|
|
|
|
self: 1
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
callback(new Error('[[error:chat-restricted]]'))
|
2014-10-03 15:28:46 -04:00
|
|
|
}
|
2014-10-30 17:50:07 -04:00
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
};
|
2014-10-03 15:28:46 -04:00
|
|
|
|
2014-10-30 17:50:07 -04:00
|
|
|
SocketModules.chats.canMessage = function(socket, toUid, callback) {
|
|
|
|
|
Messaging.canMessage(socket.uid, toUid, function(err, allowed) {
|
|
|
|
|
callback(!allowed ? new Error('[[error:chat-restricted]]') : undefined);
|
2014-01-10 16:00:03 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-19 10:33:27 -04:00
|
|
|
SocketModules.chats.pushUnreadCount = function(uid) {
|
|
|
|
|
Messaging.getUnreadCount(uid, function(err, unreadCount) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-09-03 17:22:29 -04:00
|
|
|
server.in('uid_' + uid).emit('event:unread.updateChatCount', null, unreadCount);
|
2014-07-19 10:33:27 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SocketModules.chats.markRead = function(socket, touid, callback) {
|
|
|
|
|
Messaging.markRead(socket.uid, touid, function(err) {
|
|
|
|
|
if (!err) {
|
|
|
|
|
SocketModules.chats.pushUnreadCount(socket.uid);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-15 12:48:28 -04:00
|
|
|
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;
|
|
|
|
|
}
|
2014-09-03 18:07:56 -04:00
|
|
|
server.in('uid_' + data.touid).emit(event, data.fromUid);
|
2014-04-15 12:48:28 -04:00
|
|
|
}
|
|
|
|
|
|
2014-09-15 16:26:25 -04:00
|
|
|
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),
|
|
|
|
|
end = start + 9;
|
|
|
|
|
|
|
|
|
|
Messaging.getRecentChats(socket.uid, start, end, callback);
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Notifications */
|
2014-07-28 15:52:33 -04:00
|
|
|
SocketModules.notifications.markRead = function(socket, nid) {
|
|
|
|
|
notifications.markRead(nid, socket.uid);
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
2014-07-28 15:52:33 -04:00
|
|
|
SocketModules.notifications.markAllRead = function(socket, data, callback) {
|
|
|
|
|
notifications.markAllRead(socket.uid, callback);
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
2014-03-13 17:15:07 -04:00
|
|
|
/* Sounds */
|
2014-03-14 00:34:51 -04:00
|
|
|
SocketModules.sounds.getSounds = function(socket, data, callback) {
|
2014-03-13 17:15:07 -04:00
|
|
|
// Read sounds from local directory
|
2014-04-12 18:33:52 -04:00
|
|
|
meta.sounds.getFiles(callback);
|
2014-03-13 17:15:07 -04:00
|
|
|
};
|
|
|
|
|
|
2014-03-14 00:34:51 -04:00
|
|
|
SocketModules.sounds.getMapping = function(socket, data, callback) {
|
2014-03-13 17:15:07 -04:00
|
|
|
meta.sounds.getMapping(callback);
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
module.exports = SocketModules;
|