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

165 lines
4.4 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;
user.getMultipleUserFields([socket.uid, touid], ['username'], 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) {
});
});
}
Messaging.parse(msg, socket.uid, socket.uid, toUsername, function(parsed) {
Messaging.addMessage(socket.uid, touid, msg, function(err, message) {
2014-01-15 18:20:05 +01:00
var numSockets = 0,
x;
2014-01-16 20:14:09 -05:00
if (server.userSockets[touid]) {
numSockets = server.userSockets[touid].length;
2014-01-15 18:20:05 +01:00
for (x = 0; x < numSockets; ++x) {
2014-01-16 20:14:09 -05:00
server.userSockets[touid][x].emit('event:chats.receive', {
fromuid: socket.uid,
2014-01-15 18:20:05 +01:00
username: username,
// todo this isnt very nice, but can't think of a better way atm
message: parsed.replace("chat-user-you'>You", "'>" + username),
timestamp: Date.now()
});
}
}
2014-01-16 20:14:09 -05:00
if (server.userSockets[socket.uid]) {
2014-01-15 18:20:05 +01:00
2014-01-16 20:14:09 -05:00
numSockets = server.userSockets[socket.uid].length;
2014-01-15 18:20:05 +01:00
for (x = 0; x < numSockets; ++x) {
2014-01-16 20:14:09 -05:00
server.userSockets[socket.uid][x].emit('event:chats.receive', {
2014-01-15 18:20:05 +01:00
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;