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.

570 lines
14 KiB
JavaScript

11 years ago
'use strict';
11 years ago
var nconf = require('nconf'),
async = require('async'),
topics = require('../topics'),
11 years ago
categories = require('../categories'),
privileges = require('../privileges'),
11 years ago
notifications = require('../notifications'),
threadTools = require('../threadTools'),
websockets = require('./index'),
user = require('../user'),
11 years ago
db = require('../database'),
meta = require('../meta'),
11 years ago
utils = require('../../public/src/utils'),
11 years ago
SocketPosts = require('./posts'),
11 years ago
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
}
topics.post({
uid: socket.uid,
title: data.title,
content: data.content,
cid: data.category_id,
thumb: data.topic_thumb,
tags: data.tags,
req: websockets.reqFromSocket(socket)
}, function(err, result) {
if(err) {
return callback(err);
}
if (result) {
websockets.server.sockets.in('category_' + data.category_id).emit('event:new_topic', result.topicData);
websockets.server.sockets.in('recent_posts').emit('event:new_topic', result.topicData);
websockets.server.sockets.in('home').emit('event:new_topic', result.topicData);
websockets.server.sockets.in('home').emit('event:new_post', {
posts: result.postData
});
websockets.server.sockets.in('user/' + socket.uid).emit('event:new_post', {
posts: result.postData
});
11 years ago
websockets.emitTopicPostStats();
topics.pushUnreadCount();
11 years ago
callback(null, result.topicData);
}
});
};
SocketTopics.enter = function(socket, tid, callback) {
if (!tid || !socket.uid) {
return;
}
SocketTopics.markAsRead(socket, [tid], callback);
topics.increaseViewCount(tid);
websockets.updateRoomBrowsingText('topic_' + tid);
};
11 years ago
SocketTopics.postcount = function(socket, tid, callback) {
topics.getTopicField(tid, 'postcount', callback);
11 years ago
};
SocketTopics.increaseViewCount = function(socket, tid) {
topics.increaseViewCount(tid);
};
SocketTopics.markAsRead = function(socket, tids, callback) {
if(!Array.isArray(tids) || !socket.uid) {
return callback(new Error('[[error:invalid-data]]'));
}
if (!tids.length) {
return callback();
11 years ago
}
topics.markAsRead(tids, socket.uid, function(err) {
if (err) {
11 years ago
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.markTopicNotificationsRead = function(socket, tid, callback) {
if(!tid || !socket.uid) {
return callback(new Error('[[error:invalid-data]]'));
}
topics.markTopicNotificationsRead(tid, socket.uid);
};
SocketTopics.markAllRead = function(socket, data, callback) {
topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) {
if (err) {
return callback(err);
}
SocketTopics.markAsRead(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 topic && parseInt(topic.cid, 10) === parseInt(cid, 10);
}).map(function(topic) {
return topic.tid;
});
SocketTopics.markAsRead(socket, tids, callback);
});
});
};
SocketTopics.markAsUnreadForAll = function(socket, tids, callback) {
if (!Array.isArray(tids)) {
return callback(new Error('[[error:invalid-tid]]'));
}
if (!socket.uid) {
return callback(new Error('[[error:no-privileges]]'));
}
user.isAdministrator(socket.uid, function(err, isAdmin) {
if (err) {
return callback(err);
}
async.each(tids, function(tid, next) {
async.waterfall([
function(next) {
threadTools.exists(tid, next);
},
function(exists, next) {
if (!exists) {
return next(new Error('[[error:invalid-tid]]'));
}
topics.getTopicField(tid, 'cid', next);
},
function(cid, next) {
user.isModerator(socket.uid, cid, next);
}
], function(err, isMod) {
if (err) {
return next(err);
}
if (!isAdmin && !isMod) {
return next(new Error('[[error:no-privileges]]'));
}
topics.markAsUnreadForAll(tid, function(err) {
if(err) {
return next(err);
}
11 years ago
topics.updateRecent(tid, Date.now(), function(err) {
if(err) {
return next(err);
}
topics.pushUnreadCount();
next();
});
});
});
}, callback);
});
};
SocketTopics.delete = function(socket, data, callback) {
doTopicAction('delete', socket, data, callback);
11 years ago
};
11 years ago
SocketTopics.restore = function(socket, data, callback) {
doTopicAction('restore', socket, data, callback);
11 years ago
};
11 years ago
SocketTopics.purge = function(socket, data, callback) {
doTopicAction('purge', socket, data, function(err) {
if (err) {
return callback(err);
}
websockets.emitTopicPostStats();
websockets.in('category_' + data.cid).emit('event:topic_purged', data.tids);
async.each(data.tids, function(tid, next) {
websockets.in('topic_' + tid).emit('event:topic_purged', tid);
next();
}, callback);
});
11 years ago
};
SocketTopics.lock = function(socket, data, callback) {
doTopicAction('lock', socket, data, callback);
};
SocketTopics.unlock = function(socket, data, callback) {
doTopicAction('unlock', socket, data, callback);
11 years ago
};
11 years ago
SocketTopics.pin = function(socket, data, callback) {
doTopicAction('pin', socket, data, callback);
11 years ago
};
11 years ago
SocketTopics.unpin = function(socket, data, callback) {
doTopicAction('unpin', socket, data, callback);
};
function doTopicAction(action, socket, data, callback) {
if(!data || !Array.isArray(data.tids) || !data.cid) {
return callback(new Error('[[error:invalid-tid]]'));
}
async.each(data.tids, function(tid, next) {
privileges.topics.canEdit(tid, socket.uid, function(err, canEdit) {
if(err) {
return next(err);
}
if(!canEdit) {
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]]'));
}
privileges.posts.canMove(data.pid, socket.uid, function(err, canMove) {
if (err || !canMove) {
return callback(err || new Error('[[error:no-privileges]]'));
}
11 years ago
topics.movePostToTopic(data.pid, data.tid, function(err) {
if (err) {
return callback(err);
}
SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, 'notifications:moved_your_post');
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]]'));
}
11 years ago
async.eachLimit(data.tids, 10, function(tid, next) {
var oldCid;
async.waterfall([
function(next) {
privileges.topics.canMove(tid, socket.uid, next);
},
function(canMove, next) {
if (!canMove) {
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, socket.uid, next);
}
], function(err) {
if(err) {
return next(err);
}
11 years ago
websockets.server.sockets.in('topic_' + tid).emit('event:topic_moved', {
tid: tid
});
websockets.server.sockets.in('category_' + oldCid).emit('event:topic_moved', {
tid: tid
});
11 years ago
SocketTopics.sendNotificationToTopicOwner(tid, socket.uid, 'notifications:moved_your_topic');
next();
});
}, callback);
};
11 years ago
SocketTopics.sendNotificationToTopicOwner = function(tid, fromuid, notification) {
if(!tid || !fromuid) {
return;
}
async.parallel({
username: async.apply(user.getUserField, fromuid, 'username'),
topicData: async.apply(topics.getTopicFields, tid, ['uid', 'slug']),
}, function(err, results) {
if (err || fromuid === parseInt(results.topicData.uid, 10)) {
return;
}
notifications.create({
bodyShort: '[[' + notification + ', ' + results.username + ']]',
path: nconf.get('relative_path') + '/topic/' + results.topicData.slug,
nid: 'tid:' + tid + ':uid:' + fromuid,
11 years ago
from: fromuid
}, function(err, notification) {
if (!err && notification) {
notifications.push(notification, [results.topicData.uid]);
11 years ago
}
});
});
};
11 years ago
SocketTopics.moveAll = function(socket, data, callback) {
if(!data || !data.cid || !data.currentCid) {
return callback(new Error('[[error:invalid-data]]'));
}
11 years ago
privileges.categories.canMoveAllTopics(data.currentCid, data.cid, data.uid, function(err, canMove) {
if (err || canMove) {
return callback(err || new Error('[[error:no-privileges]]'));
11 years ago
}
categories.getTopicIds(data.currentCid, 0, -1, function(err, tids) {
if (err) {
return callback(err);
}
async.eachLimit(tids, 10, function(tid, next) {
threadTools.move(tid, data.cid, socket.uid, next);
11 years ago
}, callback);
});
});
};
11 years ago
SocketTopics.followCheck = function(socket, tid, callback) {
topics.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]]'));
}
async.parallel({
settings: function(next) {
user.getSettings(socket.uid, next);
},
privileges: function(next) {
privileges.topics.get(data.tid, socket.uid, next);
},
postCount: function(next) {
topics.getPostCount(data.tid, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
if (!results.privileges.read) {
return callback(new Error('[[error:no-privileges]]'));
}
var set = 'tid:' + data.tid + ':posts',
reverse = false,
start = Math.max(parseInt(data.after, 10) - 1, 0);
if (results.settings.topicPostSort === 'newest_to_oldest') {
reverse = true;
data.after = results.postCount - data.after;
start = Math.max(parseInt(data.after, 10), 0);
} else if (results.settings.topicPostSort === 'most_votes') {
reverse = true;
data.after = results.postCount - data.after;
start = Math.max(parseInt(data.after, 10), 0);
set = 'tid:' + data.tid + ':posts:votes';
}
var end = start + results.settings.postsPerPage - 1;
async.parallel({
posts: function(next) {
topics.getTopicPosts(data.tid, set, start, end, socket.uid, reverse, next);
},
privileges: function(next) {
next(null, results.privileges);
},
'reputation:disabled': function(next) {
next(null, parseInt(meta.config['reputation:disabled'], 10) === 1);
},
'downvote:disabled': function(next) {
next(null, parseInt(meta.config['downvote:disabled'], 10) === 1);
}
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);
};
SocketTopics.searchTags = function(socket, data, callback) {
topics.searchTags(data, callback);
};
SocketTopics.search = function(socket, data, callback) {
topics.search(data.tid, data.term, callback);
};
11 years ago
SocketTopics.searchAndLoadTags = function(socket, data, callback) {
topics.searchTags(data, function(err, tags) {
if (err) {
return callback(err);
}
async.parallel({
counts: function(next) {
db.sortedSetScores('tags:topic:count', tags, next);
},
tagData: function(next) {
tags = tags.map(function(tag) {
return {value: tag};
});
topics.getTagData(tags, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
results.tagData.forEach(function(tag, index) {
tag.score = results.counts[index];
});
results.tagData.sort(function(a, b) {
return b.score - a.score;
11 years ago
});
callback(null, results.tagData);
});
});
};
SocketTopics.loadMoreTags = function(socket, data, callback) {
if(!data || !data.after) {
return callback(new Error('[[error:invalid-data]]'));
}
var start = parseInt(data.after, 10),
end = start + 99;
topics.getTags(start, end, function(err, tags) {
if (err) {
return callback(err);
}
callback(null, {tags: tags, nextStart: end + 1});
});
};
module.exports = SocketTopics;