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.

291 lines
6.9 KiB
JavaScript

var topics = require('../topics'),
threadTools = require('../threadTools'),
11 years ago
index = require('./index'),
SocketTopics = {};
11 years ago
SocketTopics.post = function(socket, data, callback) {
11 years ago
if (socket.uid < 1 && parseInt(meta.config.allowGuestPosting, 10) === 0) {
socket.emit('event:alert', {
title: 'Post Unsuccessful',
message: 'You don&apos;t seem to be logged in, so you cannot reply.',
type: 'danger',
timeout: 2000
});
return;
}
11 years ago
topics.post(socket.uid, data.title, data.content, data.category_id, 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;
}
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
});
callback();
}
});
};
11 years ago
SocketTopics.postcount = function(socket, tid, callback) {
topics.getTopicField(tid, 'postcount', callback);
};
11 years ago
SocketTopics.markAllRead = function(socket, data, callback) {
topics.markAllRead(socket.uid, function(err, success) {
if (!err && success) {
callback(true);
11 years ago
index.server.sockets.in('uid_' + socket.uid).emit('event:unread.updateCount', 0);
} else {
callback(false);
}
});
};
11 years ago
SocketTopics.delete = function(socket, data, callback) {
threadTools.privileges(data.tid, socket.uid, function(err, privileges) {
if(err) {
return callback(err);
}
11 years ago
if(!privileges.editable) {
return callback(new Error('not-allowed'));
}
threadTools.delete(data.tid, socket.uid, function(err) {
if(err) {
return callback(err);
}
module.parent.exports.emitTopicPostStats();
callback(null, 'topic.delete', {
status: 'ok',
tid: data.tid
});
});
});
};
11 years ago
SocketTopics.restore = function(socket, data, callback) {
threadTools.privileges(data.tid, socket.uid, function(err, privileges) {
if(err) {
return callback(err);
}
if(!privileges.editable) {
return callback(new Error('not-allowed'));
}
11 years ago
threadTools.restore(data.tid, socket.uid, function(err) {
if(err) {
return callback(err);
}
module.parent.exports.emitTopicPostStats();
callback(null, 'topic.restore', {
status: 'ok',
tid: data.tid
});
});
});
};
11 years ago
SocketTopics.lock = function(socket, data, callback) {
threadTools.privileges(data.tid, socket.uid, function(err, privileges) {
if(err) {
return callback(err);
}
11 years ago
if (!privileges.editable) {
return callback(new Error('not-allowed'));
}
threadTools.lock(data.tid, callback);
});
};
11 years ago
SocketTopics.unlock = function(socket, data, callback) {
threadTools.privileges(data.tid, socket.uid, function(err, privileges) {
if(err) {
return callback(err);
}
11 years ago
if (!privileges.editable) {
return callback(new Error('not-allowed'));
}
threadTools.unlock(data.tid, callback);
});
};
11 years ago
SocketTopics.pin = function(socket, data, callback) {
threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) {
11 years ago
if(err) {
return callback(err);
}
11 years ago
if (!privileges.editable) {
return callback(new Error('not-allowed'));
}
threadTools.pin(data.tid, callback);
});
};
11 years ago
SocketTopics.unpin = function(socket, data, callback) {
threadTools.privileges(data.tid, socket.uid, function(err, privileges) {
if(err) {
return callback(err);
}
if (!privileges.editable) {
return callback(new Error('not-allowed'));
}
11 years ago
threadTools.unpin(data.tid, callback);
});
};
11 years ago
SocketTopics.createTopicFromPosts = function(socket, data, callback) {
if(!socket.uid) {
socket.emit('event:alert', {
title: 'Can&apos;t fork',
message: 'Guests can&apos;t fork topics!',
type: 'warning',
timeout: 2000
});
return;
}
11 years ago
topics.createTopicFromPosts(socket.uid, data.title, data.pids, function(err, data) {
callback(err, data);
});
};
11 years ago
SocketTopics.movePost = function(socket, data, callback) {
if(!socket.uid) {
socket.emit('event:alert', {
title: 'Can&apos;t fork',
message: 'Guests can&apos;t fork topics!',
type: 'warning',
timeout: 2000
});
return;
}
topics.movePostToTopic(data.pid, data.tid, function(err, data) {
11 years ago
callback(err, data);
});
};
11 years ago
SocketTopics.move = function(socket, data, callback) {
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: tid
});
});
};
11 years ago
SocketTopics.followCheck = function(socket, tid, callback) {
threadTools.isFollowing(tid, socket.uid, function(following) {
callback(following);
});
};
11 years ago
SocketTopics.follow = function(socket, tid, callback) {
if (socket.uid) {
threadTools.toggleFollow(tid, socket.uid, function(follow) {
if (follow.status === 'ok') {
callback(follow);
}
});
} else {
callback({
status: 'error',
error: 'not-logged-in'
});
}
};
11 years ago
SocketTopics.loadMore = function(socket, data, callback) {
var start = data.after,
end = start + 9;
11 years ago
topics.getTopicPosts(data.tid, start, end, socket.uid, function(err, posts) {
if(err) {
return callback(err);
}
callback(null, {
posts: posts
});
});
};
11 years ago
SocketTopics.loadMoreRecentTopics = function(socket, data, callback) {
var start = data.after,
end = start + 9;
11 years ago
topics.getLatestTopics(socket.uid, start, end, data.term, function(err, latestTopics) {
if(err) {
return callback(err);
}
11 years ago
callback(null, latestTopics);
});
};
11 years ago
SocketTopics.loadMoreUnreadTopics = function(socket, data, callback) {
var start = data.after,
end = start + 9;
11 years ago
topics.getUnreadTopics(socket.uid, start, end, function(unreadTopics) {
callback(null, unreadTopics);
});
};
module.exports = SocketTopics;