socketizing topic creation so that a new topic shows up automatically when one is created

v1.18.x
Julian Lam 12 years ago
parent 3df8d7279b
commit 297c4b8173

@ -8,7 +8,7 @@
<div class="category row">
<div class="span9">
<ul>
<ul id="topics-container">
<!-- BEGIN topics -->
<a href="../../topic/{topics.slug}"><li>
<div class="row-fluid">
@ -77,8 +77,25 @@
<script type="text/javascript">
var new_post = document.getElementById('new_post');
new_post.onclick = function() {
app.open_post_window('topic', {category_id});
}
(function() {
var room = 'category_' + '{category_id}';
app.enter_room(room);
var new_post = document.getElementById('new_post');
new_post.onclick = function() {
app.open_post_window('topic', {category_id});
}
ajaxify.register_events([
'event:new_topic'
]);
socket.on('event:new_topic', function(data) {
console.log(data);
var html = templates.prepare(templates['category'].blocks['topics']).parse({ topics: [data] });
jQuery('<div></div>').appendTo("#topics-container").hide().append(html).fadeIn('slow');
// set_up_posts(uniqueid);
});
})();
</script>

@ -169,6 +169,59 @@ marked.setOptions({
});
}
Topics.get_topic = function(tid, uid, callback) {
var topicData = {};
async.parallel([
function(next) {
RDB.mget([
'tid:' + tid + ':title',
'tid:' + tid + ':uid',
'tid:' + tid + ':timestamp',
'tid:' + tid + ':slug',
'tid:' + tid + ':postcount',
'tid:' + tid + ':locked',
'tid:' + tid + ':pinned',
'tid:' + tid + ':deleted'
], function(err, topic) {
topicData.title = topic[0];
topicData.uid = topic[1];
topicData.timestamp = topic[2];
topicData.relativeTime = utils.relativeTime(topic[2]),
topicData.slug = topic[3];
topicData.post_count = topic[4];
topicData.locked = topic[5];
topicData.pinned = topic[6];
topicData.deleted = topic[7];
user.getUserField(topic[1], 'username', function(username) {
topicData.username = username;
next(null);
})
});
},
function(next) {
if (uid && parseInt(uid) > 0) {
RDB.sismember('tid:' + tid + ':read_by_uid', uid, function(err, read) {
topicData.badgeclass = read ? '' : 'badge-important';
next(null);
});
} else next(null);
},
function(next) {
Topics.get_teaser(tid, function(teaser) {
topicData.teaser_text = teaser.text;
topicData.teaser_username = teaser.username;
next(null);
});
}
], function(err) {
if (!err) {
callback(topicData);
}
});
}
Topics.get_cid_by_tid = function(tid, callback) {
RDB.get('tid:' + pid + ':cid', function(err, cid) {
if (cid && parseInt(cid) > 0) callback(cid);
@ -269,7 +322,14 @@ marked.setOptions({
// Posts
posts.create(uid, tid, content, function(pid) {
if (pid > 0) RDB.lpush('tid:' + tid + ':posts', pid);
if (pid > 0) {
RDB.lpush('tid:' + tid + ':posts', pid);
// Notify any users looking at the category that a new post has arrived
Topics.get_topic(tid, uid, function(topicData) {
io.sockets.in('category_' + category_id).emit('event:new_topic', topicData);
});
}
});
Topics.markAsRead(tid, uid);
@ -284,7 +344,6 @@ marked.setOptions({
timeout: 2000
});
// in future it may be possible to add topics to several categories, so leaving the door open here.
RDB.sadd('categories:' + category_id + ':tid', tid);
RDB.set('tid:' + tid + ':cid', category_id);

@ -227,8 +227,8 @@ var express = require('express'),
app.get('/api/:method/:id*', api_method);
app.get('/test', function(req, res) {
topics.get_teasers([1, 2, 3], function(teasers) {
res.send(JSON.stringify(teasers));
topics.get_topic(3, 1, function(data) {
res.send(JSON.stringify(data));
});
});

Loading…
Cancel
Save