Files
NodeBB/src/socket.io/modules.js

158 lines
3.6 KiB
JavaScript
Raw Normal View History

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-01-10 16:00:03 -05:00
async = require('async'),
S = require('string'),
winston = require('winston'),
2014-01-16 20:14:09 -05:00
server = require('./'),
2014-01-10 16:00:03 -05:00
SocketModules = {};
/* Posts Composer */
SocketModules.composer = {};
2014-01-16 22:06:23 -05:00
SocketModules.composer.push = function(socket, pid, callback) {
if (socket.uid || parseInt(meta.config.allowGuestPosting, 10)) {
if (parseInt(pid, 10) > 0) {
2014-01-10 16:00:03 -05:00
async.parallel([
function(next) {
2014-01-16 22:06:23 -05:00
posts.getPostFields(pid, ['content'], next);
2014-01-10 16:00:03 -05:00
},
function(next) {
2014-01-16 22:06:23 -05:00
topics.getTitleByPid(pid, function(title) {
2014-01-10 16:00:03 -05:00
next(null, title);
});
}
], function(err, results) {
2014-01-17 12:55:38 -05:00
if(err) {
return callback(err);
}
callback(null, {
2014-01-10 16:00:03 -05:00
title: results[1],
2014-01-16 22:06:23 -05:00
pid: pid,
2014-01-10 16:00:03 -05:00
body: results[0].content
});
});
}
} else {
2014-01-16 22:06:23 -05:00
callback(new Error('no-uid'));
2014-01-10 16:00:03 -05:00
}
};
SocketModules.composer.editCheck = function(socket, pid, callback) {
2014-01-10 16:00:03 -05:00
posts.getPostField(pid, 'tid', function(err, tid) {
2014-01-16 20:14:09 -05:00
if (err) {
return callback(err);
}
2014-01-10 16:00:03 -05:00
postTools.isMain(pid, tid, function(err, isMain) {
2014-01-16 20:14:09 -05:00
callback(err, {
2014-01-10 16:00:03 -05:00
titleEditable: isMain
});
});
});
};
2014-01-17 16:16:00 -05:00
SocketModules.composer.renderPreview = function(socket, content, callback) {
2014-01-18 15:35:51 -05:00
plugins.fireHook('filter:post.parse', content, callback);
2014-01-17 16:16:00 -05:00
}
2014-01-10 16:00:03 -05:00
/* Chat */
SocketModules.chats = {};
SocketModules.chats.get = function(socket, data, callback) {
2014-01-17 12:55:38 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
Messaging.getMessages(socket.uid, data.touid, callback);
2014-01-10 16:00:03 -05:00
};
SocketModules.chats.send = function(socket, data) {
2014-01-17 12:55:38 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
2014-01-10 16:00:03 -05:00
var touid = data.touid;
if (touid === socket.uid || socket.uid === 0) {
2014-01-10 16:00:03 -05:00
return;
}
var msg = S(data.message).stripTags().s;
2014-01-20 12:56:09 -05:00
user.getMultipleUserFields([socket.uid, touid], ['username', 'picture'], function(err, usersData) {
2014-01-10 16:00:03 -05:00
if(err) {
return;
}
var username = usersData[0].username,
toUsername = usersData[1].username,
finalMessage = username + ' : ' + msg,
notifText = 'New message from <strong>' + username + '</strong>';
if (!module.parent.exports.isUserOnline(touid)) {
notifications.create(notifText, 'javascript:app.openChat(&apos;' + username + '&apos;, ' + socket.uid + ');', 'notification_' + socket.uid + '_' + touid, function(nid) {
2014-01-10 16:00:03 -05:00
notifications.push(nid, [touid], function(success) {
});
});
}
usersData[0].uid = socket.uid;
usersData[1].uid = touid;
Messaging.parse(msg, socket.uid, socket.uid, usersData[1], usersData[0], true, function(parsed) {
Messaging.addMessage(socket.uid, touid, msg, function(err, message) {
server.getUserSockets(touid).forEach(function(s) {
s.emit('event:chats.receive', {
fromuid: socket.uid,
username: username,
message: parsed,
timestamp: Date.now()
});
});
server.getUserSockets(socket.uid).forEach(function(s) {
s.emit('event:chats.receive', {
fromuid: touid,
username: toUsername,
message: parsed,
timestamp: Date.now()
});
});
});
});
2014-01-10 16:00:03 -05:00
});
};
SocketModules.chats.list = function(socket, data, callback) {
2014-01-16 20:14:09 -05:00
Messaging.getRecentChats(socket.uid, callback);
2014-01-10 16:00:03 -05:00
};
/* Notifications */
SocketModules.notifications = {};
SocketModules.notifications.mark_read = function(socket, nid) {
notifications.mark_read(nid, socket.uid);
2014-01-10 16:00:03 -05:00
};
SocketModules.notifications.mark_all_read = function(socket, data, callback) {
2014-01-16 20:14:09 -05:00
notifications.mark_all_read(socket.uid, callback);
2014-01-10 16:00:03 -05:00
};
module.exports = SocketModules;