mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 03:55:55 +01:00
socket rooms! and almost finished rep (socket updates) except for one small bug where current_user isnt being pulled
This commit is contained in:
@@ -21,6 +21,13 @@ var ajaxify = {};
|
||||
|
||||
|
||||
ajaxify.go = function(url, callback) {
|
||||
// leave room and join global
|
||||
if (current_room != 'global') {
|
||||
socket.emit('event:enter_room', 'global');
|
||||
current_room = 'global';
|
||||
}
|
||||
|
||||
|
||||
var url = url.replace(/\/$/, "");
|
||||
var tpl_url = (url === '' || url === '/') ? 'home' : url.split('/')[0];
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
var socket,
|
||||
config,
|
||||
app = {},
|
||||
|
||||
current_room,
|
||||
API_URL = null;
|
||||
|
||||
// todo: cleanup,etc
|
||||
@@ -173,6 +173,12 @@ var socket,
|
||||
};
|
||||
|
||||
jQuery('document').ready(function() {
|
||||
if (current_room != 'global') {
|
||||
socket.emit('event:enter_room', 'global');
|
||||
current_room = 'global';
|
||||
}
|
||||
|
||||
|
||||
// On menu click, change "active" state
|
||||
var menuEl = document.querySelector('.nav'),
|
||||
liEls = menuEl.querySelectorAll('li'),
|
||||
|
||||
@@ -35,6 +35,12 @@
|
||||
|
||||
|
||||
<script type="text/javascript">
|
||||
jQuery('document').ready(function() {
|
||||
// join room for this thread - DRY failure, see ajaxify and app.js
|
||||
socket.emit('event:enter_room', 'topic_' + '{topic_id}');
|
||||
current_room = 'topic_' + '{topic_id}';
|
||||
});
|
||||
|
||||
jQuery('.post_reply').click(function() {
|
||||
app.open_post_window('reply', "{topic_id}", "{topic_name}");
|
||||
});
|
||||
@@ -46,34 +52,46 @@ jQuery('.quote').click(function() {
|
||||
document.getElementById('post_content').innerHTML = '> ' + document.getElementById('content_' + this.id.replace('quote_', '')).innerHTML;
|
||||
});
|
||||
|
||||
jQuery('.favourite').click(function() {
|
||||
var ids = this.id.replace('favs_', '').split('_'),
|
||||
pid = ids[0],
|
||||
uid = ids[1],
|
||||
post_rep = document.getElementById('post_rep_' + pid),
|
||||
|
||||
|
||||
ajaxify.register_events(['event:rep_up', 'event:rep_down']);
|
||||
|
||||
socket.on('event:rep_up', function(data) {
|
||||
adjust_rep(1, data.pid, data.uid);
|
||||
});
|
||||
|
||||
socket.on('event:rep_down', function(data) {
|
||||
adjust_rep(-1, data.pid, data.uid);
|
||||
});
|
||||
|
||||
function adjust_rep(value, pid, uid) {
|
||||
var post_rep = document.getElementById('post_rep_' + pid),
|
||||
user_rep = document.getElementById('user_rep_' + uid);
|
||||
|
||||
var ptotal = parseInt(post_rep.innerHTML, 10),
|
||||
utotal = parseInt(user_rep.innerHTML, 10);
|
||||
|
||||
if (this.children[1].className == 'icon-star-empty') {
|
||||
this.children[1].className = 'icon-star';
|
||||
ptotal++;
|
||||
utotal++;
|
||||
ptotal += value;
|
||||
utotal += value;
|
||||
|
||||
post_rep.innerHTML = ptotal;
|
||||
user_rep.innerHTML = utotal;
|
||||
socket.emit('api:posts.favourite', {pid: pid});
|
||||
}
|
||||
|
||||
|
||||
jQuery('.favourite').click(function() {
|
||||
var ids = this.id.replace('favs_', '').split('_'),
|
||||
pid = ids[0],
|
||||
uid = ids[1];
|
||||
|
||||
|
||||
if (this.children[1].className == 'icon-star-empty') {
|
||||
this.children[1].className = 'icon-star';
|
||||
socket.emit('api:posts.favourite', {pid: pid, room_id: current_room});
|
||||
}
|
||||
else {
|
||||
this.children[1].className = 'icon-star-empty';
|
||||
ptotal--;
|
||||
utotal--;
|
||||
|
||||
|
||||
post_rep.innerHTML = ptotal;
|
||||
user_rep.innerHTML = utotal;
|
||||
socket.emit('api:posts.unfavourite', {pid: pid});
|
||||
socket.emit('api:posts.unfavourite', {pid: pid, room_id: current_room});
|
||||
}
|
||||
})
|
||||
</script>
|
||||
27
src/posts.js
27
src/posts.js
@@ -14,7 +14,8 @@ var RDB = require('./redis.js'),
|
||||
|
||||
|
||||
|
||||
Posts.get = function(callback, tid, start, end) {
|
||||
Posts.get = function(callback, tid, current_user, start, end) {
|
||||
console.log(current_user);
|
||||
if (start == null) start = 0;
|
||||
if (end == null) end = start + 10;
|
||||
|
||||
@@ -53,7 +54,7 @@ var RDB = require('./redis.js'),
|
||||
|
||||
for (var i=0, ii=content.length; i<ii; i++) {
|
||||
(function(i) {
|
||||
Posts.hasFavourited(pid[i], uid[i], function(hasFavourited) {
|
||||
Posts.hasFavourited(pid[i], current_user, function(hasFavourited) {
|
||||
posts.push({
|
||||
'pid' : pid[i],
|
||||
'content' : marked(content[i] || ''),
|
||||
@@ -120,29 +121,35 @@ var RDB = require('./redis.js'),
|
||||
}
|
||||
|
||||
|
||||
Posts.favourite = function(socket, pid) {
|
||||
RDB.get('pid:' + pid + ':uid', function(uid) {
|
||||
Posts.favourite = function(io, pid, room_id, uid) {
|
||||
RDB.get('pid:' + pid + ':uid', function(uid_of_poster) {
|
||||
Posts.hasFavourited(pid, uid, function(hasFavourited) {
|
||||
if (hasFavourited == false) {
|
||||
RDB.sadd('pid:' + pid + ':users_favourited', uid);
|
||||
RDB.incr('uid:' + uid + ':rep');
|
||||
RDB.incr('uid:' + uid_of_poster + ':rep');
|
||||
RDB.incr('pid:' + pid + ':rep');
|
||||
|
||||
if (room_id) {
|
||||
io.sockets.in(room_id).emit('event:rep_up', {uid: uid_of_poster, pid: pid});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
Posts.unfavourite = function(socket, pid) {
|
||||
RDB.get('pid:' + pid + ':uid', function(uid) {
|
||||
Posts.unfavourite = function(io, pid, room_id, uid) {
|
||||
RDB.get('pid:' + pid + ':uid', function(uid_of_poster) {
|
||||
Posts.hasFavourited(pid, uid, function(hasFavourited) {
|
||||
if (hasFavourited == true) {
|
||||
RDB.srem('pid:' + pid + ':users_favourited', uid);
|
||||
RDB.decr('uid:' + uid + ':rep');
|
||||
RDB.decr('uid:' + uid_of_poster + ':rep');
|
||||
RDB.decr('pid:' + pid + ':rep');
|
||||
|
||||
if (room_id) {
|
||||
io.sockets.in(room_id).emit('event:rep_down', {uid: uid_of_poster, pid: pid});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -173,7 +173,7 @@ passport.deserializeUser(function(uid, done) {
|
||||
case 'topic' :
|
||||
global.modules.posts.get(function(data) {
|
||||
res.send(JSON.stringify(data));
|
||||
}, req.params.id);
|
||||
}, req.params.id, req.user.uid);
|
||||
break;
|
||||
default :
|
||||
res.send('{}');
|
||||
|
||||
@@ -69,6 +69,10 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
|
||||
|
||||
// BEGIN: API calls (todo: organize)
|
||||
// julian: :^)
|
||||
socket.on('event:enter_room', function(room) {
|
||||
socket.join(room);
|
||||
});
|
||||
|
||||
socket.on('api:user.get', function(data) {
|
||||
modules.user.get(socket, uid, data.fields);
|
||||
});
|
||||
@@ -125,11 +129,11 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
|
||||
});
|
||||
|
||||
socket.on('api:posts.favourite', function(data) {
|
||||
modules.posts.favourite(socket, data.pid);
|
||||
modules.posts.favourite(io, data.pid, data.room_id, uid);
|
||||
});
|
||||
|
||||
socket.on('api:posts.unfavourite', function(data) {
|
||||
modules.posts.unfavourite(socket, data.pid);
|
||||
modules.posts.unfavourite(io, data.pid, data.room_id, uid);
|
||||
});
|
||||
|
||||
socket.on('api:user.active.get_record', function() {
|
||||
|
||||
Reference in New Issue
Block a user