Skip to content

Node.js based forum software built for the modern web

so far so good... user, meta, notifications, categories

Julian Lam8dd8412ae3c6
diff --git a/public/src/app.js b/public/src/app.js
index 03d6554aa6..a00482a59a 100644
--- a/public/src/app.js
+++ b/public/src/app.js
@@ -36,9 +36,9 @@ var socket,
app.uid = data.uid;
app.showLoginMessage();
- socket.emit('api:updateHeader', {
+ socket.emit('api:meta.updateHeader', {
fields: ['username', 'picture', 'userslug']
- });
+ }, app.updateHeader);
});
socket.on('event:alert', function (data) {
@@ -84,9 +84,9 @@ var socket,
}, 3000);
}
- socket.emit('api:updateHeader', {
+ socket.emit('api:meta.updateHeader', {
fields: ['username', 'picture', 'userslug']
- });
+ }, app.updateHeader);
});
socket.on('event:disconnect', function() {
@@ -451,6 +451,69 @@ var socket,
});
};
+ app.updateHeader = function(data) {
+ $('#search-button').on('click', function() {
+ $('#search-fields').removeClass('hide').show();
+ $(this).hide();
+ $('#search-fields input').focus();
+
+ $('#search-form').on('submit', function() {
+ $('#search-fields').hide();
+ $('#search-button').show();
+ });
+
+ $('#search-fields input').on('blur', function() {
+ $('#search-fields').hide();
+ $('#search-button').show();
+ });
+ });
+
+ var loggedInMenu = $('#logged-in-menu'),
+ isLoggedIn = data.uid > 0,
+ allowGuestSearching = (data.config || {}).allowGuestSearching === '1';
+
+ if (isLoggedIn) {
+ $('.nodebb-loggedin').show();
+ $('.nodebb-loggedout').hide();
+
+ $('#logged-out-menu').addClass('hide');
+ $('#logged-in-menu').removeClass('hide');
+
+ $('#search-button').removeClass("hide").show();
+
+ var userLabel = loggedInMenu.find('#user_label');
+
+ if (userLabel.length) {
+ if (data['userslug'])
+ userLabel.find('#user-profile-link').attr('href', RELATIVE_PATH + '/user/' + data['userslug']);
+ if (data['picture'])
+ userLabel.find('img').attr('src', data['picture']);
+ if (data['username'])
+ userLabel.find('span').html(data['username']);
+
+ $('#logout-link').on('click', app.logout);
+ }
+ } else {
+ if (allowGuestSearching) {
+ $('#search-button').removeClass("hide").show();
+ } else {
+ $('#search-button').addClass("hide").hide();
+ }
+
+ $('.nodebb-loggedin').hide();
+ $('.nodebb-loggedout').show();
+
+ $('#logged-out-menu').removeClass('hide');
+ $('#logged-in-menu').addClass('hide');
+
+ }
+
+ $('#main-nav a,#user-control-list a,#logged-out-menu .dropdown-menu a').off('click').on('click', function() {
+ if($('.navbar .navbar-collapse').hasClass('in'))
+ $('.navbar-header button').click();
+ });
+ };
+
jQuery('document').ready(function () {
$('#search-form').on('submit', function () {
var input = $(this).find('input');
diff --git a/public/src/forum/accountedit.js b/public/src/forum/accountedit.js
index e86d785945..8b213ca868 100644
--- a/public/src/forum/accountedit.js
+++ b/public/src/forum/accountedit.js
@@ -108,9 +108,9 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
uploadedPicture = imageUrlOnServer;
- socket.emit('api:updateHeader', {
+ socket.emit('api:meta.updateHeader', {
fields: ['username', 'picture', 'userslug']
- });
+ }, app.updateHeader);
});
diff --git a/public/src/forum/category.js b/public/src/forum/category.js
index f084ef17ab..6eca83187a 100644
--- a/public/src/forum/category.js
+++ b/public/src/forum/category.js
@@ -36,38 +36,7 @@ define(['composer'], function(composer) {
socket.on('event:new_topic', Category.onNewTopic);
- socket.emit('api:categories.getRecentReplies', cid);
- socket.on('api:categories.getRecentReplies', function (posts) {
- if (!posts || posts.length === 0) {
- return;
- }
-
- var recent_replies = document.getElementById('category_recent_replies');
-
- recent_replies.innerHTML = '';
-
- var frag = document.createDocumentFragment(),
- li = document.createElement('li');
- for (var i = 0, numPosts = posts.length; i < numPosts; i++) {
-
- li.setAttribute('data-pid', posts[i].pid);
-
-
- li.innerHTML = '<a href="' + RELATIVE_PATH + '/user/' + posts[i].userslug + '"><img title="' + posts[i].username + '" class="img-rounded user-img" src="' + posts[i].picture + '"/></a>' +
- '<a href="' + RELATIVE_PATH + '/topic/' + posts[i].topicSlug + '#' + posts[i].pid + '">' +
- '<strong><span>'+ posts[i].username + '</span></strong>' +
- '<p>' +
- posts[i].content +
- '</p>' +
- '</a>' +
- '<span class="timeago pull-right" title="' + posts[i].relativeTime + '"></span>';
-
- frag.appendChild(li.cloneNode(true));
- recent_replies.appendChild(frag);
- }
- $('#category_recent_replies span.timeago').timeago();
- app.createUserTooltips();
- });
+ socket.emit('api:categories.getRecentReplies', cid, renderRecentReplies);
$(window).off('scroll').on('scroll', function (ev) {
var bottom = ($(document).height() - $(window).height()) * 0.9;
@@ -108,7 +77,7 @@ define(['composer'], function(composer) {
}
topic.hide().fadeIn('slow');
- socket.emit('api:categories.getRecentReplies', templates.get('category_id'));
+ socket.emit('api:categories.getRecentReplies', templates.get('category_id'), renderRecentReplies);
addActiveUser(data);
@@ -156,7 +125,7 @@ define(['composer'], function(composer) {
}
loadingMoreTopics = true;
- socket.emit('api:category.loadMore', {
+ socket.emit('api:categories.loadMore', {
cid: cid,
after: $('#topics-container').children('.category-item').length
}, function (data) {
@@ -167,5 +136,37 @@ define(['composer'], function(composer) {
});
}
+ function renderRecentReplies(posts) {
+ if (!posts || posts.length === 0) {
+ return;
+ }
+
+ var recent_replies = document.getElementById('category_recent_replies');
+
+ recent_replies.innerHTML = '';
+
+ var frag = document.createDocumentFragment(),
+ li = document.createElement('li');
+ for (var i = 0, numPosts = posts.length; i < numPosts; i++) {
+
+ li.setAttribute('data-pid', posts[i].pid);
+
+
+ li.innerHTML = '<a href="' + RELATIVE_PATH + '/user/' + posts[i].userslug + '"><img title="' + posts[i].username + '" class="img-rounded user-img" src="' + posts[i].picture + '"/></a>' +
+ '<a href="' + RELATIVE_PATH + '/topic/' + posts[i].topicSlug + '#' + posts[i].pid + '">' +
+ '<strong><span>'+ posts[i].username + '</span></strong>' +
+ '<p>' +
+ posts[i].content +
+ '</p>' +
+ '</a>' +
+ '<span class="timeago pull-right" title="' + posts[i].relativeTime + '"></span>';
+
+ frag.appendChild(li.cloneNode(true));
+ recent_replies.appendChild(frag);
+ }
+ $('#category_recent_replies span.timeago').timeago();
+ app.createUserTooltips();
+ };
+
return Category;
});
\ No newline at end of file
diff --git a/public/src/forum/footer.js b/public/src/forum/footer.js
index 5c1d57b51e..71e8c54d1c 100644
--- a/public/src/forum/footer.js
+++ b/public/src/forum/footer.js
@@ -1,72 +1,8 @@
(function() {
- socket.emit('api:updateHeader', {
+ socket.emit('api:meta.updateHeader', {
fields: ['username', 'picture', 'userslug']
- });
-
- socket.on('api:updateHeader', function(data) {
-
- $('#search-button').on('click', function() {
- $('#search-fields').removeClass('hide').show();
- $(this).hide();
- $('#search-fields input').focus();
-
- $('#search-form').on('submit', function() {
- $('#search-fields').hide();
- $('#search-button').show();
- });
-
- $('#search-fields input').on('blur', function() {
- $('#search-fields').hide();
- $('#search-button').show();
- });
- });
-
- var loggedInMenu = $('#logged-in-menu'),
- isLoggedIn = data.uid > 0,
- allowGuestSearching = (data.config || {}).allowGuestSearching === '1';
-
- if (isLoggedIn) {
- $('.nodebb-loggedin').show();
- $('.nodebb-loggedout').hide();
-
- $('#logged-out-menu').addClass('hide');
- $('#logged-in-menu').removeClass('hide');
-
- $('#search-button').removeClass("hide").show();
-
- var userLabel = loggedInMenu.find('#user_label');
-
- if (userLabel.length) {
- if (data['userslug'])
- userLabel.find('#user-profile-link').attr('href', RELATIVE_PATH + '/user/' + data['userslug']);
- if (data['picture'])
- userLabel.find('img').attr('src', data['picture']);
- if (data['username'])
- userLabel.find('span').html(data['username']);
-
- $('#logout-link').on('click', app.logout);
- }
- } else {
- if (allowGuestSearching) {
- $('#search-button').removeClass("hide").show();
- } else {
- $('#search-button').addClass("hide").hide();
- }
-
- $('.nodebb-loggedin').hide();
- $('.nodebb-loggedout').show();
-
- $('#logged-out-menu').removeClass('hide');
- $('#logged-in-menu').addClass('hide');
-
- }
-
- $('#main-nav a,#user-control-list a,#logged-out-menu .dropdown-menu a').off('click').on('click', function() {
- if($('.navbar .navbar-collapse').hasClass('in'))
- $('.navbar-header button').click();
- });
- });
+ }, app.updateHeader);
// Notifications dropdown
var notifContainer = document.getElementsByClassName('notifications')[0],
@@ -256,6 +192,6 @@
}
socket.on('event:unread.updateCount', updateUnreadCount);
- socket.emit('api:unread.count', updateUnreadCount);
+ socket.emit('api:user.getUnreadCount', updateUnreadCount);
}());
diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js
new file mode 100644
index 0000000000..e00e61c1e5
--- /dev/null
+++ b/src/socket.io/categories.js
@@ -0,0 +1,30 @@
+var categories = require('../categories'),
+
+ SocketCategories = {};
+
+SocketCategories.getRecentReplies = function(tid, callback, sessionData) {
+ categories.getRecentReplies(tid, sessionData.uid, 4, function(err, replies) {
+ callback(replies);
+ });
+};
+
+SocketCategories.get = function(callback) {
+ categories.getAllCategories(0, function(err, categories) {
+ if(callback) {
+ callback(categories);
+ }
+ });
+};
+
+SocketCategories.loadMore = function(data, callback, sessionData) {
+ var start = data.after,
+ end = start + 9;
+
+ categories.getCategoryTopics(data.cid, start, end, sessionData.uid, function(topics) {
+ callback({
+ topics: topics
+ });
+ });
+};
+
+module.exports = SocketCategories;
\ No newline at end of file
diff --git a/src/socket.io/index.js b/src/socket.io/index.js
index 3b676fef59..fef0a6d39c 100644
--- a/src/socket.io/index.js
+++ b/src/socket.io/index.js
@@ -145,11 +145,16 @@ Sockets.init = function() {
command = parts[1],
subcommand = parts[2], // MUST ADD RECURSION (:P)
executeHandler = function(args) {
+ // Session data
+ var sessionData = {
+ uid: uid
+ };
+
winston.info('[socket.io] Executing: ' + payload.name);
if (!subcommand) {
- Namespaces[namespace][command].call(Namespaces[namespace], args.length ? args : callback ? callback : undefined, args.length ? callback : undefined);
+ Namespaces[namespace][command].call(Namespaces[namespace], args.length ? args[0] : callback ? callback : sessionData, args.length ? callback : sessionData, args.length && callback ? sessionData : undefined);
} else {
- Namespaces[namespace][command][subcommand].call(Namespaces[namespace][command], args, callback);
+ Namespaces[namespace][command][subcommand].call(Namespaces[namespace][command], args.length ? args[0] : callback ? callback : sessionData, args.length ? callback : sessionData, args.length && callback ? sessionData : undefined);
}
};
diff --git a/src/socket.io/meta.js b/src/socket.io/meta.js
new file mode 100644
index 0000000000..64f248168e
--- /dev/null
+++ b/src/socket.io/meta.js
@@ -0,0 +1,38 @@
+var meta = require('../meta'),
+ user = require('../user'),
+
+ SocketMeta = {};
+
+SocketMeta.buildTitle = function(text, callback) {
+ meta.title.build(text, function(err, title) {
+ callback(title);
+ });
+};
+
+SocketMeta.updateHeader = function(data, callback, sessionData) {
+ console.log('HERE', data);
+ if (sessionData.uid) {
+ user.getUserFields(sessionData.uid, data.fields, function(err, fields) {
+ if (!err && fields) {
+ fields.uid = sessionData.uid;
+ callback(fields);
+ }
+ });
+ } else {
+ callback({
+ uid: 0,
+ username: "Anonymous User",
+ email: '',
+ picture: gravatar.url('', {
+ s: '24'
+ }, nconf.get('https')),
+ config: {
+ allowGuestSearching: meta.config.allowGuestSearching
+ }
+ });
+ }
+};
+
+/* Exports */
+
+module.exports = SocketMeta;
\ No newline at end of file
diff --git a/src/socket.io/notifications.js b/src/socket.io/notifications.js
new file mode 100644
index 0000000000..6f8085311b
--- /dev/null
+++ b/src/socket.io/notifications.js
@@ -0,0 +1,17 @@
+var user = require('../user'),
+
+ SocketNotifs = {};
+
+SocketNotifs.get = function(data, callback, sessionData) {
+ user.notifications.get(sessionData.uid, function(notifs) {
+ callback(notifs);
+ });
+};
+
+SocketNotifs.getCount = function(callback, sessionData) {
+ user.notifications.getUnreadCount(sessionData.uid, function(err, count) {
+ callback(err ? err.message : null, count);
+ });
+};
+
+module.exports = SocketNotifs;
\ No newline at end of file
diff --git a/src/socket.io/user.js b/src/socket.io/user.js
new file mode 100644
index 0000000000..ef1475a061
--- /dev/null
+++ b/src/socket.io/user.js
@@ -0,0 +1,138 @@
+var user = require('../user'),
+ topics = require('../topics'),
+
+ SocketUser = {};
+
+SocketUser.exists = function(data) {
+ if (data.username) {
+ user.exists(utils.slugify(data.username), function(exists) {
+ socket.emit('user.exists', {
+ exists: exists
+ });
+ });
+ }
+};
+
+SocketUser.count = function(callback) {
+ user.count(callback);
+};
+
+SocketUser.emailExists = function(data) {
+ user.email.exists(socket, data.email);
+};
+
+// Password Reset
+SocketUser.reset = {};
+
+SocketUser.reset.send = function(data) {
+ user.reset.send(socket, data.email);
+};
+
+SocketUser.reset.valid = function(data) {
+ user.reset.validate(socket, data.code);
+};
+
+SocketUser.reset.commit = function(data) {
+ user.reset.commit(socket, data.code, data.password);
+};
+
+SocketUser.isOnline = function(uid, callback) {
+ callback({
+ online: module.parent.exports.isUserOnline(uid),
+ uid: uid,
+ timestamp: Date.now()
+ });
+};
+
+SocketUser.changePassword = function(data, callback) {
+ user.changePassword(uid, data, callback);
+};
+
+SocketUser.updateProfile = function(data, callback) {
+ user.updateProfile(uid, data, callback);
+};
+
+SocketUser.changePicture = function(data, callback) {
+
+ var type = data.type;
+
+ function updateHeader() {
+ user.getUserFields(uid, ['picture'], function(err, fields) {
+ if (!err && fields) {
+ fields.uid = uid;
+ socket.emit('api:updateHeader', fields);
+ callback(true);
+ } else {
+ callback(false);
+ }
+ });
+ }
+
+ if (type === 'gravatar') {
+ user.getUserField(uid, 'gravatarpicture', function(err, gravatar) {
+ user.setUserField(uid, 'picture', gravatar);
+ updateHeader();
+ });
+ } else if (type === 'uploaded') {
+ user.getUserField(uid, 'uploadedpicture', function(err, uploadedpicture) {
+ user.setUserField(uid, 'picture', uploadedpicture);
+ updateHeader();
+ });
+ } else {
+ callback(false);
+ }
+};
+
+SocketUser.follow = function(data, callback) {
+ if (uid) {
+ user.follow(uid, data.uid, callback);
+ }
+};
+
+SocketUser.unfollow = function(data, callback) {
+ if (uid) {
+ user.unfollow(uid, data.uid, callback);
+ }
+};
+
+SocketUser.saveSettings = function(data, callback) {
+ if (uid) {
+ user.setUserFields(uid, {
+ showemail: data.showemail
+ }, function(err, result) {
+ callback(err);
+ });
+ }
+};
+
+SocketUser.get_online_users = function(data, callback) {
+ var returnData = [];
+
+ for (var i = 0; i < data.length; ++i) {
+ var uid = data[i];
+ if (module.parent.exports.isUserOnline(uid))
+ returnData.push(uid);
+ else
+ returnData.push(0);
+ }
+
+ callback(returnData);
+};
+
+SocketUser.getOnlineAnonCount = function(data, callback) {
+ callback(module.parent.exports.getOnlineAnonCount());
+};
+
+SocketUser.getUnreadCount = function(callback, sessionData) {
+ topics.getUnreadTids(sessionData.uid, 0, 19, function(err, tids) {
+ callback(tids.length);
+ });
+};
+
+SocketUser.getActiveUsers = function(callback) {
+ module.parent.exports.emitOnlineUserCount(callback);
+};
+
+/* Exports */
+
+module.exports = SocketUser;
\ No newline at end of file
diff --git a/src/websockets.js b/src/websockets.js
index 8c91a63320..045d4e7952 100644
--- a/src/websockets.js
+++ b/src/websockets.js
@@ -69,29 +69,7 @@ websockets.init = function(io) {
// BEGIN: API calls (todo: organize)
- socket.on('api:updateHeader', function(data) {
- if (uid) {
- user.getUserFields(uid, data.fields, function(err, fields) {
- if (!err && fields) {
- fields.uid = uid;
- socket.emit('api:updateHeader', fields);
- }
- });
- } else {
- socket.emit('api:updateHeader', {
- uid: 0,
- username: "Anonymous User",
- email: '',
- picture: gravatar.url('', {
- s: '24'
- }, nconf.get('https')),
- config: {
- allowGuestSearching: meta.config.allowGuestSearching
- }
- });
- }
- });
@@ -353,13 +331,7 @@ websockets.init = function(io) {
threadTools.move(data.tid, data.cid, socket);
});
- socket.on('api:categories.get', function(callback) {
- categories.getAllCategories(0, function(err, categories) {
- if(callback) {
- callback(categories);
- }
- });
- });
+
socket.on('api:posts.uploadImage', function(data, callback) {
posts.uploadPostImage(data, callback);
@@ -428,11 +400,6 @@ websockets.init = function(io) {
});
});
- socket.on('api:notifications.get', function(data, callback) {
- user.notifications.get(uid, function(notifs) {
- callback(notifs);
- });
- });
socket.on('api:notifications.mark_read', function(nid) {
notifications.mark_read(nid, uid);
@@ -446,17 +413,7 @@ websockets.init = function(io) {
});
});
- socket.on('api:notifications.getCount', function(callback) {
- user.notifications.getUnreadCount(uid, function(err, count) {
- callback(err ? err.message : null, count);
- });
- });
- socket.on('api:categories.getRecentReplies', function(tid) {
- categories.getRecentReplies(tid, uid, 4, function(err, replies) {
- socket.emit('api:categories.getRecentReplies', replies);
- });
- });
socket.on('api:chats.get', function(data, callback) {
var touid = data.touid;
@@ -641,23 +598,6 @@ websockets.init = function(io) {
});
});
- socket.on('api:unread.count', function(callback) {
- topics.getUnreadTids(uid, 0, 19, function(err, tids) {
- socket.emit('event:unread.updateCount', tids.length);
- });
- });
-
- socket.on('api:category.loadMore', function(data, callback) {
- var start = data.after,
- end = start + 9;
-
- categories.getCategoryTopics(data.cid, start, end, uid, function(topics) {
- callback({
- topics: topics
- });
- });
- });
-
socket.on('api:topics.loadMoreRecentTopics', function(data, callback) {
var start = data.after,
end = start + 9;