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.

389 lines
9.4 KiB
JavaScript

11 years ago
'use strict';
var topics = require('../topics'),
11 years ago
categories = require('../categories'),
privileges = require('../privileges'),
threadTools = require('../threadTools'),
websockets = 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
}
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
});
module.parent.exports.emitTopicPostStats();
11 years ago
callback(null, result.topicData);
}
});
};
11 years ago
SocketTopics.postcount = function(socket, tid, callback) {
topics.getTopicField(tid, 'postcount', callback);
};
SocketTopics.increaseViewCount = function(socket, tid) {
topics.increaseViewCount(tid);
}
SocketTopics.markAsRead = function(socket, tid) {
if(!tid || !socket.uid) {
return;
}
topics.markAsRead(tid, socket.uid, function(err) {
topics.pushUnreadCount(socket.uid);
topics.markTopicNotificationsRead(tid, socket.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) {
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.tid, socket.uid, function(err, canMove) {
if (err || !canMove) {
return callback(err || 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) {
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, 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
});
next();
});
}, callback);
};
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.each(tids, function(tid, next) {
threadTools.move(tid, data.cid, next);
}, 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]]'));
}
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) {
privileges.topics.get(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);
};
SocketTopics.searchTags = function(socket, query, callback) {
topics.searchTags(query, callback);
};
module.exports = SocketTopics;