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.

299 lines
7.6 KiB
JavaScript

var topics = require('../topics'),
threadTools = require('../threadTools'),
11 years ago
index = require('./index'),
user = require('../user'),
db = require('./../database'),
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('Invalid data'));
}
if (!socket.uid && !parseInt(meta.config.allowGuestPosting, 10)) {
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 callback(new 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) {
if (err.message === 'title-too-short') {
module.parent.exports.emitAlert(socket, 'Title too short', 'Please enter a longer title. At least ' + meta.config.minimumTitleLength + ' characters.');
} else if (err.message === 'title-too-long') {
module.parent.exports.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') {
module.parent.exports.emitContentTooShortAlert(socket);
} else if (err.message === 'too-many-posts') {
module.parent.exports.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 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('user/' + socket.uid).emit('event:new_post', {
posts: result.postData
});
module.parent.exports.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
});
11 years ago
callback(null);
}
});
};
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);
});
};
11 years ago
SocketTopics.markAllRead = function(socket, data, callback) {
11 years ago
topics.markAllRead(socket.uid, function(err) {
11 years ago
if(err) {
return callback(err);
}
11 years ago
index.server.sockets.in('uid_' + socket.uid).emit('event:unread.updateCount', null, []);
11 years ago
11 years ago
callback(null);
});
};
SocketTopics.markAsUnreadForAll = function(socket, tid, callback) {
topics.markAsUnreadForAll(tid, function(err) {
if(err) {
return callback(err);
}
db.sortedSetAdd('topics:recent', Date.now(), tid, function(err) {
if(err) {
return callback(err);
}
topics.pushUnreadCount();
callback();
});
});
}
11 years ago
function doTopicAction(action, socket, tid, callback) {
if(!tid) {
return callback(new Error('Invalid tid'));
}
11 years ago
threadTools.privileges(tid, socket.uid, function(err, privileges) {
11 years ago
if(err) {
return callback(err);
}
11 years ago
11 years ago
if(!privileges || !privileges.editable) {
11 years ago
return callback(new Error('not-allowed'));
}
11 years ago
if(threadTools[action]) {
threadTools[action](tid, socket.uid, callback);
}
});
};
11 years ago
SocketTopics.delete = function(socket, tid, callback) {
doTopicAction('delete', socket, tid, callback);
};
11 years ago
11 years ago
SocketTopics.restore = function(socket, tid, callback) {
doTopicAction('restore', socket, tid, callback);
};
11 years ago
11 years ago
SocketTopics.lock = function(socket, tid, callback) {
doTopicAction('lock', socket, tid, callback);
};
11 years ago
SocketTopics.unlock = function(socket, tid, callback) {
doTopicAction('unlock', socket, tid, callback);
};
11 years ago
11 years ago
SocketTopics.pin = function(socket, tid, callback) {
doTopicAction('pin', socket, tid, callback);
};
11 years ago
11 years ago
SocketTopics.unpin = function(socket, tid, callback) {
doTopicAction('unpin', socket, tid, callback);
};
11 years ago
SocketTopics.createTopicFromPosts = function(socket, data, callback) {
if(!socket.uid) {
socket.emit('event:alert', {
title: 'Can't fork',
message: 'Guests can't fork topics!',
type: 'warning',
timeout: 2000
});
return;
}
if(!data || !data.title || !data.pids || !Array.isArray(data.pids)) {
return callback(new Error('invalid data'));
}
topics.createTopicFromPosts(socket.uid, data.title, data.pids, callback);
};
11 years ago
SocketTopics.movePost = function(socket, data, callback) {
if(!socket.uid) {
socket.emit('event:alert', {
title: 'Can't fork',
message: 'Guests can't fork topics!',
type: 'warning',
timeout: 2000
});
return;
}
if(!data || !data.pid || !data.tid) {
return callback(new 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('not allowed'));
}
topics.movePostToTopic(data.pid, data.tid, callback);
});
};
11 years ago
SocketTopics.move = function(socket, data, callback) {
if(!data || !data.tid || !data.cid) {
return callback(new Error('invalid data'));
}
11 years ago
threadTools.move(data.tid, data.cid, function(err) {
if(err) {
return callback(err);
}
index.server.sockets.in('topic_' + data.tid).emit('event:topic_moved', {
tid: data.tid
11 years ago
});
callback(null);
11 years ago
});
};
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('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('invalid data'));
}
user.getSettings(socket.uid, function(err, settings) {
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);
}
}, function(err, results) {
callback(err, results);
});
});
};
11 years ago
SocketTopics.loadMoreRecentTopics = function(socket, data, callback) {
if(!data || !data.term || !data.after) {
return callback(new 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('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('invalid data'));
}
var start = parseInt(data.after, 10),
end = start + 9;
topics.getTopicsFromSet(socket.uid, data.set, start, end, callback);
};
SocketTopics.getPageCount = function(socket, tid, callback) {
topics.getPageCount(tid, socket.uid, callback);
11 years ago
};
module.exports = SocketTopics;