Skip to content

Node.js based forum software built for the modern web

closes #1138

barisusaklia0e784a612ed
diff --git a/public/src/forum/home.js b/public/src/forum/home.js
index efaaba4033..5a70082de1 100644
--- a/public/src/forum/home.js
+++ b/public/src/forum/home.js
@@ -1,31 +1,89 @@
'use strict';
+/* globals define, socket, app, templates, translator*/
+
define(function() {
var home = {};
- $(window).on('action:ajaxify.end', function(ev, data) {
- if (data.url === '') {
- socket.removeListener('event:new_topic', home.onNewTopic);
+ $(window).on('action:ajaxify.start', function(ev, data) {
+ if (data.url !== '') {
socket.removeListener('event:new_post', home.onNewPost);
}
});
home.init = function() {
-
app.enterRoom('home');
- socket.on('event:new_topic', home.onNewTopic);
socket.on('event:new_post', home.onNewPost);
};
- home.onNewTopic = function(data) {
+ home.onNewPost = function(data) {
- };
+ if (data && data.posts && data.posts.length) {
- home.onNewPost = function(data) {
+ socket.emit('posts.getCategory', data.posts[0].pid, function(err, cid) {
+ if (err) {
+ return;
+ }
+ renderNewPost(cid, data.posts[0]);
+ });
+ }
};
+ function renderNewPost(cid, post) {
+ var category = $('.home .category-item[data-cid="' + cid + '"]');
+ var categoryBox = category.find('.category-box');
+ var numRecentReplies = category.attr('data-numRecentReplies');
+ if (!numRecentReplies) {
+ return;
+ }
+
+ var recentPosts = categoryBox.find('.post-preview');
+ var insertBefore = recentPosts.first();
+
+ parseAndTranslate([post], function(html) {
+ html.hide();
+ if(recentPosts.length === 0) {
+ html.appendTo(categoryBox);
+ } else {
+ html.insertBefore(recentPosts.first());
+ }
+
+ html.fadeIn();
+
+ app.createUserTooltips();
+
+ if (categoryBox.find('.post-preview').length > parseInt(numRecentReplies, 10)) {
+ recentPosts.last().remove();
+ }
+ });
+ }
+
+ function parseAndTranslate(posts, callback) {
+ templates.preload_template('home', function() {
+
+ templates.home.parse({
+ categories: {
+ posts: []
+ }
+ });
+
+ var html = templates.prepare(templates.home.blocks['categories.posts']).parse({
+ categories: {
+ posts: posts
+ }
+ });
+
+ translator.translate(html, function(translatedHTML) {
+ translatedHTML = $(translatedHTML);
+ translatedHTML.find('img').addClass('img-responsive');
+ translatedHTML.find('span.timeago').timeago();
+ callback(translatedHTML);
+ });
+ });
+ }
+
return home;
});
diff --git a/src/posts.js b/src/posts.js
index be10f66417..dff6af855d 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -407,15 +407,10 @@ var db = require('./database'),
}
topics.getTopicField(tid, 'cid', function(err, cid) {
- if(err) {
- return callback(err);
- }
-
- if (cid) {
- callback(null, cid);
- } else {
- callback(new Error('invalid-category-id'));
+ if(err || !cid) {
+ return callback(err || new Error('invalid-category-id'));
}
+ callback(null, cid);
});
});
};
diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js
index fedb968522..6723d16684 100644
--- a/src/socket.io/posts.js
+++ b/src/socket.io/posts.js
@@ -304,5 +304,8 @@ SocketPosts.getRecentPosts = function(socket, data, callback) {
posts.getRecentPosts(socket.uid, 0, data.count - 1, data.term, callback);
};
+SocketPosts.getCategory = function(socket, pid, callback) {
+ posts.getCidByPid(pid, callback);
+}
module.exports = SocketPosts;
\ No newline at end of file