You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
8.7 KiB
JavaScript

11 years ago
'use strict';
var topics = require('../topics'),
11 years ago
categories = require('../categories'),
threadTools = require('../threadTools'),
11 years ago
index = require('./index'),
user = require('../user'),
db = require('./../database'),
meta = require('./../meta'),
11 years ago
utils = require('../../public/src/utils'),
11 years ago
async = require('async'),
SocketTopics = {};
11 years ago
SocketTopics.post = function(socket, data, callback) {
11 years ago
if(!data) {
return callback(new Error('[[error:invalid-data]]'));
11 years ago
}
if (!socket.uid && !parseInt(meta.config.allowGuestPosting, 10)) {
return callback(new Error('[[error:not-logged-in]]'));
}
topics.post({uid: socket.uid, title: data.title, content: data.content, cid: data.category_id, thumb: data.topic_thumb}, function(err, result) {
if(err) {
return callback(err);
}
if (result) {
11 years ago
index.server.sockets.in('category_' + data.category_id).emit('event:new_topic', result.topicData);
index.server.sockets.in('recent_posts').emit('event:new_topic', result.topicData);
index.server.sockets.in('home').emit('event:new_topic', result.topicData);
index.server.sockets.in('home').emit('event:new_post', {
posts: result.postData
});
11 years ago
index.server.sockets.in('user/' + socket.uid).emit('event:new_post', {
posts: result.postData
});
module.parent.exports.emitTopicPostStats();
11 years ago
callback();
}
});
};
11 years ago
SocketTopics.postcount = function(socket, tid, callback) {
topics.getTopicField(tid, 'postcount', callback);
};
SocketTopics.markAsRead = function(socket, data) {
if(!data || !data.tid || !data.uid) {
return;
}
topics.markAsRead(data.tid, data.uid, function(err) {
topics.pushUnreadCount(data.uid);
topics.markTopicNotificationsRead(data.tid, data.uid);
});
};
SocketTopics.markTidsRead = function(socket, tids, callback) {
11 years ago
if (!Array.isArray(tids)) {
return callback(new Error('[[error:invalid-data]]'));
11 years ago
}
topics.markTidsRead(socket.uid, tids, function(err) {
11 years ago
if(err) {
return callback(err);
}
11 years ago
topics.pushUnreadCount(socket.uid);
11 years ago
for (var i=0; i<tids.length; ++i) {
topics.markTopicNotificationsRead(tids[i], socket.uid);
}
11 years ago
callback();
});
};
SocketTopics.markAllRead = function(socket, data, callback) {
topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) {
if (err) {
return callback(err);
}
SocketTopics.markTidsRead(socket, tids, callback);
});
};
SocketTopics.markCategoryTopicsRead = function(socket, cid, callback) {
topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) {
if (err) {
return callback(err);
}
var keys = tids.map(function(tid) {
return 'topic:' + tid;
});
db.getObjectsFields(keys, ['tid', 'cid'], function(err, topicData) {
if (err) {
return callback(err);
}
tids = topicData.filter(function(topic) {
return parseInt(topic.cid, 10) === parseInt(cid, 10);
}).map(function(topic) {
return topic.tid;
});
SocketTopics.markTidsRead(socket, tids, callback);
});
});
};
SocketTopics.markAsUnreadForAll = function(socket, tids, callback) {
if(!Array.isArray(tids)) {
return callback(new Error('[[error:invalid-tid]]'));
}
async.each(tids, function(tid, next) {
topics.markAsUnreadForAll(tid, function(err) {
if(err) {
return next(err);
}
db.sortedSetAdd('topics:recent', Date.now(), tid, function(err) {
if(err) {
return next(err);
}
topics.pushUnreadCount();
next();
});
});
}, callback);
};
SocketTopics.delete = function(socket, tids, callback) {
doTopicAction('delete', socket, tids, callback);
11 years ago
};
11 years ago
SocketTopics.restore = function(socket, tids, callback) {
doTopicAction('restore', socket, tids, callback);
11 years ago
};
11 years ago
SocketTopics.lock = function(socket, tids, callback) {
doTopicAction('lock', socket, tids, callback);
};
SocketTopics.unlock = function(socket, tids, callback) {
doTopicAction('unlock', socket, tids, callback);
11 years ago
};
11 years ago
SocketTopics.pin = function(socket, tids, callback) {
doTopicAction('pin', socket, tids, callback);
11 years ago
};
11 years ago
SocketTopics.unpin = function(socket, tids, callback) {
doTopicAction('unpin', socket, tids, callback);
};
function doTopicAction(action, socket, tids, callback) {
if(!tids) {
return callback(new Error('[[error:invalid-tid]]'));
}
async.each(tids, function(tid, next) {
threadTools.privileges(tid, socket.uid, function(err, privileges) {
if(err) {
return next(err);
}
if(!privileges || !privileges.editable) {
return next(new Error('[[error:no-privileges]]'));
}
if(typeof threadTools[action] === 'function') {
threadTools[action](tid, socket.uid, next);
}
});
}, callback);
}
11 years ago
SocketTopics.createTopicFromPosts = function(socket, data, callback) {
if(!socket.uid) {
return callback(new Error('[[error:not-logged-in]]'));
}
if(!data || !data.title || !data.pids || !Array.isArray(data.pids)) {
return callback(new Error('[[error:invalid-data]]'));
}
topics.createTopicFromPosts(socket.uid, data.title, data.pids, callback);
};
11 years ago
SocketTopics.movePost = function(socket, data, callback) {
if(!socket.uid) {
return callback(new Error('[[error:not-logged-in]]'));
}
if(!data || !data.pid || !data.tid) {
return callback(new Error('[[error:invalid-data]]'));
}
threadTools.privileges(data.tid, socket.uid, function(err, privileges) {
if(err) {
return callback(err);
}
if(!(privileges.admin || privileges.moderator)) {
return callback(new Error('[[error:no-privileges]]'));
}
topics.movePostToTopic(data.pid, data.tid, callback);
});
};
11 years ago
SocketTopics.move = function(socket, data, callback) {
if(!data || !Array.isArray(data.tids) || !data.cid) {
return callback(new Error('[[error:invalid-data]]'));
}
async.each(data.tids, function(tid, next) {
var oldCid;
async.waterfall([
function(next) {
threadTools.privileges(tid, socket.uid, next);
},
function(privileges, next) {
if(!(privileges.admin || privileges.moderator)) {
return next(new Error('[[error:no-privileges]]'));
}
next();
},
function(next) {
topics.getTopicField(tid, 'cid', next);
},
function(cid, next) {
oldCid = cid;
threadTools.move(tid, data.cid, next);
}
], function(err) {
if(err) {
return next(err);
}
11 years ago
index.server.sockets.in('topic_' + tid).emit('event:topic_moved', {
tid: tid
});
index.server.sockets.in('category_' + oldCid).emit('event:topic_moved', {
tid: tid
});
next();
});
}, callback);
};
11 years ago
SocketTopics.followCheck = function(socket, tid, callback) {
threadTools.isFollowing(tid, socket.uid, callback);
};
11 years ago
SocketTopics.follow = function(socket, tid, callback) {
if(!socket.uid) {
return callback(new Error('[[error:not-logged-in]]'));
}
threadTools.toggleFollow(tid, socket.uid, callback);
};
11 years ago
SocketTopics.loadMore = function(socket, data, callback) {
if(!data || !data.tid || !(parseInt(data.after, 10) >= 0)) {
return callback(new Error('[[error:invalid-data]]'));
}
user.getSettings(socket.uid, function(err, settings) {
if(err) {
return callback(err);
}
var start = parseInt(data.after, 10),
end = start + settings.postsPerPage - 1;
async.parallel({
posts: function(next) {
topics.getTopicPosts(data.tid, start, end, socket.uid, false, next);
},
privileges: function(next) {
threadTools.privileges(data.tid, socket.uid, next);
}
11 years ago
}, callback);
});
};
11 years ago
SocketTopics.loadMoreRecentTopics = function(socket, data, callback) {
if(!data || !data.term || !data.after) {
return callback(new Error('[[error:invalid-data]]'));
}
11 years ago
var start = parseInt(data.after, 10),
end = start + 9;
topics.getLatestTopics(socket.uid, start, end, data.term, callback);
};
11 years ago
SocketTopics.loadMoreUnreadTopics = function(socket, data, callback) {
if(!data || !data.after) {
return callback(new Error('[[error:invalid-data]]'));
}
11 years ago
var start = parseInt(data.after, 10),
end = start + 9;
topics.getUnreadTopics(socket.uid, start, end, callback);
};
11 years ago
SocketTopics.loadMoreFromSet = function(socket, data, callback) {
if(!data || !data.after || !data.set) {
return callback(new Error('[[error:invalid-data]]'));
11 years ago
}
var start = parseInt(data.after, 10),
end = start + 9;
topics.getTopicsFromSet(socket.uid, data.set, start, end, callback);
};
11 years ago
SocketTopics.loadTopics = function(socket, data, callback) {
if(!data || !data.set || !utils.isNumber(data.start) || !utils.isNumber(data.end)) {
return callback(new Error('[[error:invalid-data]]'));
}
topics.getTopicsFromSet(socket.uid, data.set, data.start, data.end, callback);
};
SocketTopics.getPageCount = function(socket, tid, callback) {
topics.getPageCount(tid, socket.uid, callback);
11 years ago
};
11 years ago
SocketTopics.getTidPage = function(socket, tid, callback) {
topics.getTidPage(tid, socket.uid, callback);
};
SocketTopics.getTidIndex = function(socket, tid, callback) {
categories.getTopicIndex(tid, callback);
};
module.exports = SocketTopics;