mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +01:00
thread tools (phew!) -- sessionData is getting larger :P
This commit is contained in:
@@ -235,7 +235,7 @@ var socket,
|
||||
return;
|
||||
}
|
||||
|
||||
socket.emit('event:enter_room', {
|
||||
socket.emit('api:meta.rooms.enter', {
|
||||
'enter': room,
|
||||
'leave': app.currentRoom
|
||||
});
|
||||
|
||||
@@ -14,29 +14,41 @@ define(function() {
|
||||
|
||||
switch (action) {
|
||||
case 'pin':
|
||||
if (!$this.hasClass('active')) socket.emit('api:topic.pin', {
|
||||
if (!$this.hasClass('active')) {
|
||||
socket.emit('api:topics.pin', {
|
||||
tid: tid
|
||||
});
|
||||
else socket.emit('api:topic.unpin', {
|
||||
}, Topics.pin);
|
||||
} else {
|
||||
socket.emit('api:topics.unpin', {
|
||||
tid: tid
|
||||
});
|
||||
}, Topics.unpin);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'lock':
|
||||
if (!$this.hasClass('active')) socket.emit('api:topic.lock', {
|
||||
if (!$this.hasClass('active')) {
|
||||
socket.emit('api:topics.lock', {
|
||||
tid: tid
|
||||
});
|
||||
else socket.emit('api:topic.unlock', {
|
||||
}, Topics.lock);
|
||||
} else {
|
||||
socket.emit('api:topics.unlock', {
|
||||
tid: tid
|
||||
});
|
||||
}, Topics.unlock);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
if (!$this.hasClass('active')) socket.emit('api:topic.delete', {
|
||||
if (!$this.hasClass('active')) {
|
||||
socket.emit('api:topics.delete', {
|
||||
tid: tid
|
||||
});
|
||||
else socket.emit('api:topic.restore', {
|
||||
}, Topics.setDeleted);
|
||||
} else {
|
||||
socket.emit('api:topics.restore', {
|
||||
tid: tid
|
||||
});
|
||||
}, Topics.restore);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
@@ -81,56 +93,6 @@ define(function() {
|
||||
});
|
||||
}
|
||||
}, false);
|
||||
|
||||
socket.on('api:topic.pin', function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="pin"]');
|
||||
|
||||
$(btnEl).addClass('active');
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('api:topic.unpin', function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="pin"]');
|
||||
|
||||
$(btnEl).removeClass('active');
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('api:topic.lock', function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="lock"]');
|
||||
|
||||
$(btnEl).addClass('active');
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('api:topic.unlock', function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="lock"]');
|
||||
|
||||
$(btnEl).removeClass('active');
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('api:topic.delete', function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="delete"]');
|
||||
|
||||
$(btnEl).addClass('active');
|
||||
$(btnEl).siblings('[data-action="lock"]').addClass('active');
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('api:topic.restore', function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="delete"]');
|
||||
|
||||
$(btnEl).removeClass('active');
|
||||
$(btnEl).siblings('[data-action="lock"]').removeClass('active');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Topics.resolveButtonStates = function() {
|
||||
@@ -154,5 +116,56 @@ define(function() {
|
||||
}
|
||||
}
|
||||
|
||||
Topics.setDeleted = function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="delete"]');
|
||||
|
||||
$(btnEl).addClass('active');
|
||||
$(btnEl).siblings('[data-action="lock"]').addClass('active');
|
||||
}
|
||||
};
|
||||
|
||||
Topics.restore = function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="delete"]');
|
||||
|
||||
$(btnEl).removeClass('active');
|
||||
$(btnEl).siblings('[data-action="lock"]').removeClass('active');
|
||||
}
|
||||
};
|
||||
|
||||
Topics.lock = function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="lock"]');
|
||||
|
||||
$(btnEl).addClass('active');
|
||||
}
|
||||
};
|
||||
|
||||
Topics.unlock = function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="lock"]');
|
||||
|
||||
$(btnEl).removeClass('active');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Topics.unpin = function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="pin"]');
|
||||
|
||||
$(btnEl).removeClass('active');
|
||||
}
|
||||
};
|
||||
|
||||
Topics.pin = function(response) {
|
||||
if (response.status === 'ok') {
|
||||
var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="pin"]');
|
||||
|
||||
$(btnEl).addClass('active');
|
||||
}
|
||||
};
|
||||
|
||||
return Topics;
|
||||
});
|
||||
@@ -13,7 +13,7 @@ define(function() {
|
||||
$('#stats_users').html(utils.makeNumberHumanReadable(data.count)).attr('title', data.count);
|
||||
});
|
||||
|
||||
socket.emit('post.stats', function(data) {
|
||||
socket.emit('api:meta.getUsageStats', function(data) {
|
||||
$('#stats_topics').html(utils.makeNumberHumanReadable(data.topics)).attr('title', data.topics);
|
||||
$('#stats_posts').html(utils.makeNumberHumanReadable(data.posts)).attr('title', data.posts);
|
||||
});
|
||||
|
||||
@@ -52,16 +52,16 @@ define(['composer'], function(composer) {
|
||||
if (thread_state.deleted !== '1') {
|
||||
bootbox.confirm('Are you sure you want to delete this thread?', function(confirm) {
|
||||
if (confirm) {
|
||||
socket.emit('api:topic.delete', {
|
||||
socket.emit('api:topics.delete', {
|
||||
tid: tid
|
||||
});
|
||||
}, null);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
bootbox.confirm('Are you sure you want to restore this thread?', function(confirm) {
|
||||
if (confirm) socket.emit('api:topic.restore', {
|
||||
if (confirm) socket.emit('api:topics.restore', {
|
||||
tid: tid
|
||||
});
|
||||
}, null);
|
||||
});
|
||||
}
|
||||
return false;
|
||||
@@ -69,26 +69,26 @@ define(['composer'], function(composer) {
|
||||
|
||||
$('.lock_thread').on('click', function(e) {
|
||||
if (thread_state.locked !== '1') {
|
||||
socket.emit('api:topic.lock', {
|
||||
socket.emit('api:topics.lock', {
|
||||
tid: tid
|
||||
});
|
||||
}, null);
|
||||
} else {
|
||||
socket.emit('api:topic.unlock', {
|
||||
socket.emit('api:topics.unlock', {
|
||||
tid: tid
|
||||
});
|
||||
}, null);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
$('.pin_thread').on('click', function(e) {
|
||||
if (thread_state.pinned !== '1') {
|
||||
socket.emit('api:topic.pin', {
|
||||
socket.emit('api:topics.pin', {
|
||||
tid: tid
|
||||
});
|
||||
}, null);
|
||||
} else {
|
||||
socket.emit('api:topic.unpin', {
|
||||
socket.emit('api:topics.unpin', {
|
||||
tid: tid
|
||||
});
|
||||
}, null);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
@@ -145,7 +145,10 @@ define(['composer'], function(composer) {
|
||||
$(moveThreadModal).find('.modal-header button').fadeOut(250);
|
||||
commitEl.innerHTML = 'Moving <i class="fa-spin fa-refresh"></i>';
|
||||
|
||||
socket.once('api:topic.move', function(data) {
|
||||
socket.emit('api:topics.move', {
|
||||
tid: tid,
|
||||
cid: targetCid
|
||||
}, function(data) {
|
||||
moveThreadModal.modal('hide');
|
||||
if (data.status === 'ok') {
|
||||
app.alert({
|
||||
@@ -165,10 +168,6 @@ define(['composer'], function(composer) {
|
||||
});
|
||||
}
|
||||
});
|
||||
socket.emit('api:topic.move', {
|
||||
tid: tid,
|
||||
cid: targetCid
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -193,7 +192,7 @@ define(['composer'], function(composer) {
|
||||
forkCommit.on('click', createTopicFromPosts);
|
||||
|
||||
function createTopicFromPosts() {
|
||||
socket.emit('api:topic.createTopicFromPosts', {
|
||||
socket.emit('api:topics.createTopicFromPosts', {
|
||||
title: forkModal.find('#fork-title').val(),
|
||||
pids: pids
|
||||
}, function(err) {
|
||||
@@ -298,10 +297,13 @@ define(['composer'], function(composer) {
|
||||
}
|
||||
}
|
||||
};
|
||||
socket.on('api:topic.followCheck', function(state) {
|
||||
|
||||
socket.emit('api:topics.followCheck', tid, function(state) {
|
||||
set_follow_state(state, true);
|
||||
});
|
||||
socket.on('api:topic.follow', function(data) {
|
||||
if (followEl[0]) {
|
||||
followEl[0].addEventListener('click', function() {
|
||||
socket.emit('api:topics.follow', tid, function(data) {
|
||||
if (data.status && data.status === 'ok') set_follow_state(data.follow);
|
||||
else {
|
||||
app.alert({
|
||||
@@ -313,11 +315,6 @@ define(['composer'], function(composer) {
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
socket.emit('api:topic.followCheck', tid);
|
||||
if (followEl[0]) {
|
||||
followEl[0].addEventListener('click', function() {
|
||||
socket.emit('api:topic.follow', tid);
|
||||
}, false);
|
||||
}
|
||||
|
||||
@@ -491,7 +488,7 @@ define(['composer'], function(composer) {
|
||||
});
|
||||
|
||||
moveBtn.on('click', function() {
|
||||
socket.emit('api:topic.movePost', {pid: pid, tid: topicId.val()}, function(err) {
|
||||
socket.emit('api:topics.movePost', {pid: pid, tid: topicId.val()}, function(err) {
|
||||
if(err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
@@ -1113,7 +1110,7 @@ define(['composer'], function(composer) {
|
||||
indicatorEl.fadeIn();
|
||||
}
|
||||
|
||||
socket.emit('api:topic.loadMore', {
|
||||
socket.emit('api:topics.loadMore', {
|
||||
tid: tid,
|
||||
after: parseInt($('#post-container .post-row.infiniteloaded').last().attr('data-index'), 10) + 1
|
||||
}, function (data) {
|
||||
|
||||
@@ -147,25 +147,36 @@ Sockets.init = function() {
|
||||
executeHandler = function(args) {
|
||||
// Session data
|
||||
var sessionData = {
|
||||
uid: uid
|
||||
};
|
||||
uid: uid,
|
||||
socket: socket,
|
||||
rooms: rooms,
|
||||
server: io
|
||||
},
|
||||
socketArgs = [];
|
||||
|
||||
winston.info('[socket.io] Executing: ' + payload.name);
|
||||
// Construct the arguments that'll get passed into each socket method
|
||||
if (args.length) {
|
||||
socketArgs = socketArgs.concat(args);
|
||||
}
|
||||
if (callback !== undefined) {
|
||||
socketArgs.push(callback);
|
||||
}
|
||||
socketArgs.push(sessionData);
|
||||
|
||||
// winston.info('[socket.io] Executing: ' + payload.name);
|
||||
if (!subcommand) {
|
||||
Namespaces[namespace][command].call(Namespaces[namespace], args.length ? args[0] : callback ? callback : sessionData, args.length ? callback : sessionData, args.length && callback ? sessionData : undefined);
|
||||
Namespaces[namespace][command].apply(Namespaces[namespace], socketArgs);
|
||||
} else {
|
||||
Namespaces[namespace][command][subcommand].call(Namespaces[namespace][command], args.length ? args[0] : callback ? callback : sessionData, args.length ? callback : sessionData, args.length && callback ? sessionData : undefined);
|
||||
Namespaces[namespace][command][subcommand].apply(Namespaces[namespace][command], socketArgs);
|
||||
}
|
||||
};
|
||||
|
||||
if (Namespaces[namespace]) {
|
||||
console.log(payload);
|
||||
executeHandler(payload.args);
|
||||
} else {
|
||||
winston.warn('[socket.io] Unrecognized message: ' + payload.name);
|
||||
}
|
||||
}
|
||||
console.log('message!', arguments);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -210,6 +221,7 @@ function isUserOnline(uid) {
|
||||
}
|
||||
Sockets.isUserOnline = isUserOnline;
|
||||
|
||||
Sockets.updateRoomBrowsingText = updateRoomBrowsingText;
|
||||
function updateRoomBrowsingText(roomName) {
|
||||
|
||||
function getUidsInRoom(room) {
|
||||
@@ -247,7 +259,8 @@ function updateRoomBrowsingText(roomName) {
|
||||
}
|
||||
}
|
||||
|
||||
function emitTopicPostStats() {
|
||||
Sockets.emitTopicPostStats = emitTopicPostStats;
|
||||
function emitTopicPostStats(callback) {
|
||||
db.getObjectFields('global', ['topicCount', 'postCount'], function(err, data) {
|
||||
if (err) {
|
||||
return winston.err(err);
|
||||
@@ -258,7 +271,11 @@ function emitTopicPostStats() {
|
||||
posts: data.postCount ? data.postCount : 0
|
||||
};
|
||||
|
||||
if (!callback) {
|
||||
io.sockets.emit('post.stats', stats);
|
||||
} else {
|
||||
callback(stats);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
var meta = require('../meta'),
|
||||
user = require('../user'),
|
||||
|
||||
gravatar = require('gravatar'),
|
||||
|
||||
SocketMeta = {};
|
||||
|
||||
SocketMeta.buildTitle = function(text, callback) {
|
||||
@@ -10,7 +12,6 @@ SocketMeta.buildTitle = function(text, callback) {
|
||||
};
|
||||
|
||||
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) {
|
||||
@@ -33,6 +34,41 @@ SocketMeta.updateHeader = function(data, callback, sessionData) {
|
||||
}
|
||||
};
|
||||
|
||||
SocketMeta.getUsageStats = function(callback) {
|
||||
module.parent.exports.emitTopicPostStats(callback);
|
||||
};
|
||||
|
||||
/* Rooms */
|
||||
|
||||
SocketMeta.rooms = {};
|
||||
|
||||
SocketMeta.rooms.enter = function(data, sessionData) {
|
||||
if (data.leave !== null) {
|
||||
sessionData.socket.leave(data.leave);
|
||||
}
|
||||
|
||||
sessionData.socket.join(data.enter);
|
||||
sessionData.rooms[data.enter] = sessionData.rooms[data.enter] || {};
|
||||
|
||||
if (sessionData.uid) {
|
||||
sessionData.rooms[data.enter][sessionData.socket.id] = sessionData.uid;
|
||||
|
||||
if (data.leave && sessionData.rooms[data.leave] && sessionData.rooms[data.leave][sessionData.socket.id] && data.enter !== data.leave) {
|
||||
delete sessionData.rooms[data.leave][sessionData.socket.id];
|
||||
}
|
||||
}
|
||||
|
||||
if (data.leave) {
|
||||
module.parent.exports.updateRoomBrowsingText(data.leave);
|
||||
}
|
||||
|
||||
module.parent.exports.updateRoomBrowsingText(data.enter);
|
||||
|
||||
if (data.enter != 'admin') {
|
||||
sessionData.server.sockets.in('admin').emit('api:get_all_rooms', sessionData.server.sockets.manager.rooms);
|
||||
}
|
||||
};
|
||||
|
||||
/* Exports */
|
||||
|
||||
module.exports = SocketMeta;
|
||||
233
src/socket.io/topics.js
Normal file
233
src/socket.io/topics.js
Normal file
@@ -0,0 +1,233 @@
|
||||
var topics = require('../topics'),
|
||||
threadTools = require('../threadTools'),
|
||||
|
||||
SocketTopics = {};
|
||||
|
||||
SocketTopics.post = function(data, sessionData) {
|
||||
if (sessionData.uid < 1 && parseInt(meta.config.allowGuestPosting, 10) === 0) {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Post Unsuccessful',
|
||||
message: 'You don't seem to be logged in, so you cannot reply.',
|
||||
type: 'danger',
|
||||
timeout: 2000
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
topics.post(sessionData.uid, data.title, data.content, data.category_id, function(err, result) {
|
||||
if(err) {
|
||||
if (err.message === 'title-too-short') {
|
||||
emitAlert(socket, 'Title too short', 'Please enter a longer title. At least ' + meta.config.minimumTitleLength + ' characters.');
|
||||
} else if (err.message === 'title-too-long') {
|
||||
emitAlert(socket, 'Title too long', 'Please enter a shorter title. Titles can\'t be longer than ' + meta.config.maximumTitleLength + ' characters.');
|
||||
} else if (err.message === 'content-too-short') {
|
||||
emitContentTooShortAlert(socket);
|
||||
} else if (err.message === 'too-many-posts') {
|
||||
emitTooManyPostsAlert(socket);
|
||||
} else if (err.message === 'no-privileges') {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Unable to post',
|
||||
message: 'You do not have posting privileges in this category.',
|
||||
type: 'danger',
|
||||
timeout: 7500
|
||||
});
|
||||
} else {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Error',
|
||||
message: err.message,
|
||||
type: 'warning',
|
||||
timeout: 7500
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (result) {
|
||||
sessionData.server.sockets.in('category_' + data.category_id).emit('event:new_topic', result.topicData);
|
||||
sessionData.server.sockets.in('recent_posts').emit('event:new_topic', result.topicData);
|
||||
sessionData.server.sockets.in('user/' + sessionData.uid).emit('event:new_post', {
|
||||
posts: result.postData
|
||||
});
|
||||
|
||||
module.parent.exports.emitTopicPostStats();
|
||||
|
||||
sessionData.socket.emit('event:alert', {
|
||||
title: 'Thank you for posting',
|
||||
message: 'You have successfully posted. Click here to view your post.',
|
||||
type: 'success',
|
||||
timeout: 2000
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.postcount = function(tid, callback) {
|
||||
topics.getTopicField(tid, 'postcount', callback);
|
||||
};
|
||||
|
||||
SocketTopics.markAllRead = function(data, callback, sessionData) {
|
||||
topics.markAllRead(sessionData.uid, function(err, success) {
|
||||
if (!err && success) {
|
||||
callback(true);
|
||||
} else {
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.delete = function(data, callback, sessionData) {
|
||||
threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.delete(data.tid, sessionData.uid, function(err) {
|
||||
if (!err) {
|
||||
module.parent.exports.emitTopicPostStats();
|
||||
if (callback) {
|
||||
callback('api:topic.delete', {
|
||||
status: 'ok',
|
||||
tid: data.tid
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.restore = function(data, callback, sessionData) {
|
||||
threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.restore(data.tid, sessionData.uid, function(err) {
|
||||
module.parent.exports.emitTopicPostStats();
|
||||
|
||||
if (callback) {
|
||||
callback('api:topic.restore', {
|
||||
status: 'ok',
|
||||
tid: data.tid
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.lock = function(data, callback, sessionData) {
|
||||
threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.lock(data.tid, callback);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.unlock = function(data, callback, sessionData) {
|
||||
threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.unlock(data.tid, callback);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.pin = function(data, callback, sessionData) {
|
||||
threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.pin(data.tid, callback);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.unpin = function(data, callback, sessionData) {
|
||||
threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.unpin(data.tid, callback);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.createTopicFromPosts = function(data, callback, sessionData) {
|
||||
if(!sessionData.uid) {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Can't fork',
|
||||
message: 'Guests can't fork topics!',
|
||||
type: 'warning',
|
||||
timeout: 2000
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
topics.createTopicFromPosts(sessionData.uid, data.title, data.pids, function(err, data) {
|
||||
callback(err?{message:err.message}:null, data);
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.movePost = function(data, callback, sessionData) {
|
||||
if(!sessionData.uid) {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Can't fork',
|
||||
message: 'Guests can't fork topics!',
|
||||
type: 'warning',
|
||||
timeout: 2000
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
topics.movePostToTopic(data.pid, data.tid, function(err, data) {
|
||||
callback(err?{message:err.message}:null, data);
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.move = function(data) {
|
||||
threadTools.move(data.tid, data.cid, socket);
|
||||
};
|
||||
|
||||
SocketTopics.followCheck = function(tid, callback, sessionData) {
|
||||
threadTools.isFollowing(tid, sessionData.uid, function(following) {
|
||||
callback(following);
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.follow = function(tid, callback, sessionData) {
|
||||
if (sessionData.uid && sessionData.uid > 0) {
|
||||
threadTools.toggleFollow(tid, sessionData.uid, function(follow) {
|
||||
if (follow.status === 'ok') callback(follow);
|
||||
});
|
||||
} else {
|
||||
callback({
|
||||
status: 'error',
|
||||
error: 'not-logged-in'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
SocketTopics.loadMore = function(data, callback, sessionData) {
|
||||
var start = data.after,
|
||||
end = start + 9;
|
||||
|
||||
topics.getTopicPosts(data.tid, start, end, sessionData.uid, function(err, posts) {
|
||||
callback({
|
||||
posts: posts
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.loadMoreRecentTopics = function(data, callback, sessionData) {
|
||||
var start = data.after,
|
||||
end = start + 9;
|
||||
|
||||
topics.getLatestTopics(sessionData.uid, start, end, data.term, function(err, latestTopics) {
|
||||
if (!err) {
|
||||
callback(latestTopics);
|
||||
} else {
|
||||
winston.error('[socket api:topics.loadMoreRecentTopics] ' + err.message);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.loadMoreUnreadTopics = function(data, callback, sessionData) {
|
||||
var start = data.after,
|
||||
end = start + 9;
|
||||
|
||||
topics.getUnreadTopics(sessionData.uid, start, end, function(unreadTopics) {
|
||||
callback(unreadTopics);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = SocketTopics;
|
||||
@@ -55,42 +55,6 @@ var winston = require('winston'),
|
||||
});
|
||||
}
|
||||
|
||||
ThreadTools.lock = function(tid, socket) {
|
||||
topics.setTopicField(tid, 'locked', 1);
|
||||
|
||||
if (socket) {
|
||||
websockets.in('topic_' + tid).emit('event:topic_locked', {
|
||||
tid: tid,
|
||||
status: 'ok'
|
||||
});
|
||||
|
||||
if (socket) {
|
||||
socket.emit('api:topic.lock', {
|
||||
status: 'ok',
|
||||
tid: tid
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ThreadTools.unlock = function(tid, socket) {
|
||||
topics.setTopicField(tid, 'locked', 0);
|
||||
|
||||
if (socket) {
|
||||
websockets.in('topic_' + tid).emit('event:topic_unlocked', {
|
||||
tid: tid,
|
||||
status: 'ok'
|
||||
});
|
||||
|
||||
if (socket) {
|
||||
socket.emit('api:topic.unlock', {
|
||||
status: 'ok',
|
||||
tid: tid
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ThreadTools.delete = function(tid, uid, callback) {
|
||||
topics.delete(tid);
|
||||
|
||||
@@ -133,46 +97,75 @@ var winston = require('winston'),
|
||||
}
|
||||
}
|
||||
|
||||
ThreadTools.pin = function(tid, socket) {
|
||||
ThreadTools.lock = function(tid, callback) {
|
||||
topics.setTopicField(tid, 'locked', 1);
|
||||
|
||||
websockets.in('topic_' + tid).emit('event:topic_locked', {
|
||||
tid: tid,
|
||||
status: 'ok'
|
||||
});
|
||||
|
||||
if (callback) {
|
||||
callback({
|
||||
status: 'ok',
|
||||
tid: tid
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ThreadTools.unlock = function(tid, callback) {
|
||||
topics.setTopicField(tid, 'locked', 0);
|
||||
|
||||
websockets.in('topic_' + tid).emit('event:topic_unlocked', {
|
||||
tid: tid,
|
||||
status: 'ok'
|
||||
});
|
||||
|
||||
if (callback) {
|
||||
callback({
|
||||
status: 'ok',
|
||||
tid: tid
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ThreadTools.pin = function(tid, callback) {
|
||||
topics.setTopicField(tid, 'pinned', 1);
|
||||
topics.getTopicField(tid, 'cid', function(err, cid) {
|
||||
db.sortedSetAdd('categories:' + cid + ':tid', Math.pow(2, 53), tid);
|
||||
});
|
||||
|
||||
if (socket) {
|
||||
websockets.in('topic_' + tid).emit('event:topic_pinned', {
|
||||
tid: tid,
|
||||
status: 'ok'
|
||||
});
|
||||
|
||||
if (socket) {
|
||||
socket.emit('api:topic.pin', {
|
||||
if (callback) {
|
||||
callback({
|
||||
status: 'ok',
|
||||
tid: tid
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ThreadTools.unpin = function(tid, socket) {
|
||||
ThreadTools.unpin = function(tid, callback) {
|
||||
topics.setTopicField(tid, 'pinned', 0);
|
||||
topics.getTopicFields(tid, ['cid', 'lastposttime'], function(err, topicData) {
|
||||
db.sortedSetAdd('categories:' + topicData.cid + ':tid', topicData.lastposttime, tid);
|
||||
});
|
||||
if (socket) {
|
||||
|
||||
websockets.in('topic_' + tid).emit('event:topic_unpinned', {
|
||||
tid: tid,
|
||||
status: 'ok'
|
||||
});
|
||||
|
||||
if (socket) {
|
||||
socket.emit('api:topic.unpin', {
|
||||
if (callback) {
|
||||
callback({
|
||||
status: 'ok',
|
||||
tid: tid
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ThreadTools.move = function(tid, cid, socket) {
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ console.log('HEY NIB, I STILL GOT CALLED');
|
||||
var cookie = require('cookie'),
|
||||
|
||||
|
||||
gravatar = require('gravatar'),
|
||||
|
||||
|
||||
S = require('string'),
|
||||
|
||||
@@ -39,33 +39,7 @@ websockets.init = function(io) {
|
||||
socket.emit('api:get_all_rooms', io.sockets.manager.rooms);
|
||||
});
|
||||
|
||||
socket.on('event:enter_room', function(data) {
|
||||
|
||||
if (data.leave !== null) {
|
||||
socket.leave(data.leave);
|
||||
}
|
||||
|
||||
socket.join(data.enter);
|
||||
rooms[data.enter] = rooms[data.enter] || {};
|
||||
|
||||
if (uid) {
|
||||
rooms[data.enter][socket.id] = uid;
|
||||
|
||||
if (data.leave && rooms[data.leave] && rooms[data.leave][socket.id] && data.enter !== data.leave) {
|
||||
delete rooms[data.leave][socket.id];
|
||||
}
|
||||
}
|
||||
|
||||
if (data.leave) {
|
||||
updateRoomBrowsingText(data.leave);
|
||||
}
|
||||
|
||||
updateRoomBrowsingText(data.enter);
|
||||
|
||||
if (data.enter != 'admin') {
|
||||
io.sockets.in('admin').emit('api:get_all_rooms', io.sockets.manager.rooms);
|
||||
}
|
||||
});
|
||||
|
||||
// BEGIN: API calls (todo: organize)
|
||||
|
||||
@@ -75,9 +49,6 @@ websockets.init = function(io) {
|
||||
|
||||
|
||||
|
||||
socket.on('post.stats', function(data) {
|
||||
emitTopicPostStats();
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -87,78 +58,8 @@ websockets.init = function(io) {
|
||||
|
||||
|
||||
|
||||
socket.on('api:topics.post', function(data) {
|
||||
if (uid < 1 && parseInt(meta.config.allowGuestPosting, 10) === 0) {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Post Unsuccessful',
|
||||
message: 'You don't seem to be logged in, so you cannot reply.',
|
||||
type: 'danger',
|
||||
timeout: 2000
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
topics.post(uid, data.title, data.content, data.category_id, function(err, result) {
|
||||
if(err) {
|
||||
if (err.message === 'title-too-short') {
|
||||
emitAlert(socket, 'Title too short', 'Please enter a longer title. At least ' + meta.config.minimumTitleLength + ' characters.');
|
||||
} else if (err.message === 'title-too-long') {
|
||||
emitAlert(socket, 'Title too long', 'Please enter a shorter title. Titles can\'t be longer than ' + meta.config.maximumTitleLength + ' characters.');
|
||||
} else if (err.message === 'content-too-short') {
|
||||
emitContentTooShortAlert(socket);
|
||||
} else if (err.message === 'too-many-posts') {
|
||||
emitTooManyPostsAlert(socket);
|
||||
} else if (err.message === 'no-privileges') {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Unable to post',
|
||||
message: 'You do not have posting privileges in this category.',
|
||||
type: 'danger',
|
||||
timeout: 7500
|
||||
});
|
||||
} else {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Error',
|
||||
message: err.message,
|
||||
type: 'warning',
|
||||
timeout: 7500
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (result) {
|
||||
io.sockets.in('category_' + data.category_id).emit('event:new_topic', result.topicData);
|
||||
io.sockets.in('recent_posts').emit('event:new_topic', result.topicData);
|
||||
io.sockets.in('user/' + uid).emit('event:new_post', {
|
||||
posts: result.postData
|
||||
});
|
||||
|
||||
emitTopicPostStats();
|
||||
|
||||
socket.emit('event:alert', {
|
||||
title: 'Thank you for posting',
|
||||
message: 'You have successfully posted. Click here to view your post.',
|
||||
type: 'success',
|
||||
timeout: 2000
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
socket.on('api:topics.postcount', function(tid, callback) {
|
||||
topics.getTopicField(tid, 'postcount', callback);
|
||||
});
|
||||
|
||||
socket.on('api:topics.markAllRead', function(data, callback) {
|
||||
topics.markAllRead(uid, function(err, success) {
|
||||
if (!err && success) {
|
||||
callback(true);
|
||||
} else {
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:posts.reply', function(data) {
|
||||
if (uid < 1 && parseInt(meta.config.allowGuestPosting, 10) === 0) {
|
||||
@@ -232,104 +133,7 @@ websockets.init = function(io) {
|
||||
favourites.unfavourite(data.pid, data.room_id, uid, socket);
|
||||
});
|
||||
|
||||
socket.on('api:topic.delete', function(data) {
|
||||
threadTools.privileges(data.tid, uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.delete(data.tid, uid, function(err) {
|
||||
if (!err) {
|
||||
emitTopicPostStats();
|
||||
socket.emit('api:topic.delete', {
|
||||
status: 'ok',
|
||||
tid: data.tid
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topic.restore', function(data) {
|
||||
threadTools.privileges(data.tid, uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.restore(data.tid, uid, function(err) {
|
||||
emitTopicPostStats();
|
||||
|
||||
socket.emit('api:topic.restore', {
|
||||
status: 'ok',
|
||||
tid: data.tid
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topic.lock', function(data) {
|
||||
threadTools.privileges(data.tid, uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.lock(data.tid, socket);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topic.unlock', function(data) {
|
||||
threadTools.privileges(data.tid, uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.unlock(data.tid, socket);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topic.pin', function(data) {
|
||||
threadTools.privileges(data.tid, uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.pin(data.tid, socket);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topic.unpin', function(data) {
|
||||
threadTools.privileges(data.tid, uid, function(err, privileges) {
|
||||
if (!err && privileges.editable) {
|
||||
threadTools.unpin(data.tid, socket);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topic.createTopicFromPosts', function(data, callback) {
|
||||
if(!uid) {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Can't fork',
|
||||
message: 'Guests can't fork topics!',
|
||||
type: 'warning',
|
||||
timeout: 2000
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
topics.createTopicFromPosts(uid, data.title, data.pids, function(err, data) {
|
||||
callback(err?{message:err.message}:null, data);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topic.movePost', function(data, callback) {
|
||||
if(!uid) {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Can't fork',
|
||||
message: 'Guests can't fork topics!',
|
||||
type: 'warning',
|
||||
timeout: 2000
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
topics.movePostToTopic(data.pid, data.tid, function(err, data) {
|
||||
callback(err?{message:err.message}:null, data);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topic.move', function(data) {
|
||||
threadTools.move(data.tid, data.cid, socket);
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -568,57 +372,7 @@ websockets.init = function(io) {
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topic.followCheck', function(tid) {
|
||||
threadTools.isFollowing(tid, uid, function(following) {
|
||||
socket.emit('api:topic.followCheck', following);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topic.follow', function(tid) {
|
||||
if (uid && uid > 0) {
|
||||
threadTools.toggleFollow(tid, uid, function(follow) {
|
||||
if (follow.status === 'ok') socket.emit('api:topic.follow', follow);
|
||||
});
|
||||
} else {
|
||||
socket.emit('api:topic.follow', {
|
||||
status: 'error',
|
||||
error: 'not-logged-in'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('api:topic.loadMore', function(data, callback) {
|
||||
var start = data.after,
|
||||
end = start + 9;
|
||||
|
||||
topics.getTopicPosts(data.tid, start, end, uid, function(err, posts) {
|
||||
callback({
|
||||
posts: posts
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topics.loadMoreRecentTopics', function(data, callback) {
|
||||
var start = data.after,
|
||||
end = start + 9;
|
||||
|
||||
topics.getLatestTopics(uid, start, end, data.term, function(err, latestTopics) {
|
||||
if (!err) {
|
||||
callback(latestTopics);
|
||||
} else {
|
||||
winston.error('[socket api:topics.loadMoreRecentTopics] ' + err.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:topics.loadMoreUnreadTopics', function(data, callback) {
|
||||
var start = data.after,
|
||||
end = start + 9;
|
||||
|
||||
topics.getUnreadTopics(uid, start, end, function(unreadTopics) {
|
||||
callback(unreadTopics);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('api:users.loadMore', function(data, callback) {
|
||||
var start = data.after,
|
||||
|
||||
Reference in New Issue
Block a user