Files
NodeBB/src/socket.io/topics.js

119 lines
3.5 KiB
JavaScript
Raw Normal View History

2014-03-03 15:26:15 -05:00
'use strict';
2019-09-12 23:27:36 -04:00
const topics = require('../topics');
const posts = require('../posts');
const user = require('../user');
const meta = require('../meta');
const apiController = require('../controllers/api');
const privileges = require('../privileges');
const socketHelpers = require('./helpers');
2017-03-02 19:03:49 +03:00
2019-09-12 23:27:36 -04:00
const SocketTopics = module.exports;
require('./topics/unread')(SocketTopics);
require('./topics/move')(SocketTopics);
require('./topics/tools')(SocketTopics);
require('./topics/infinitescroll')(SocketTopics);
require('./topics/tags')(SocketTopics);
2017-11-30 12:39:03 -05:00
require('./topics/merge')(SocketTopics);
2019-09-12 23:27:36 -04:00
SocketTopics.post = async function (socket, data) {
if (!data) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:invalid-data]]');
2014-01-16 19:58:57 -05:00
}
socketHelpers.setDefaultPostData(data, socket);
2019-09-12 23:27:36 -04:00
await meta.blacklist.test(data.req.ip);
const shouldQueue = await posts.shouldQueue(socket.uid, data);
if (shouldQueue) {
return await posts.addToQueue(data);
}
return await postTopic(socket, data);
};
2019-09-12 23:27:36 -04:00
async function postTopic(socket, data) {
const result = await topics.post(data);
2016-03-24 20:55:10 +02:00
2019-09-12 23:27:36 -04:00
socket.emit('event:new_post', { posts: [result.postData] });
socket.emit('event:new_topic', result.topicData);
2019-09-12 23:27:36 -04:00
socketHelpers.notifyNew(socket.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
return result.topicData;
}
2019-09-12 23:27:36 -04:00
SocketTopics.postcount = async function (socket, tid) {
const canRead = await privileges.topics.can('topics:read', tid, socket.uid);
if (!canRead) {
throw new Error('[[no-privileges]]');
}
return await topics.getTopicField(tid, 'postcount');
2014-06-27 15:35:53 -04:00
};
2019-09-12 23:27:36 -04:00
SocketTopics.bookmark = async function (socket, data) {
2015-09-25 00:08:25 -04:00
if (!socket.uid || !data) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:invalid-data]]');
2015-09-25 00:08:25 -04:00
}
const postcount = await topics.getTopicField(data.tid, 'postcount');
if (data.index > meta.config.bookmarkThreshold && postcount > meta.config.bookmarkThreshold) {
await topics.setUserBookmark(data.tid, socket.uid, data.index);
}
};
2019-09-12 23:27:36 -04:00
SocketTopics.createTopicFromPosts = async function (socket, data) {
if (!socket.uid) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:not-logged-in]]');
}
if (!data || !data.title || !data.pids || !Array.isArray(data.pids)) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:invalid-data]]');
2014-01-16 20:53:32 -05:00
}
2019-09-12 23:27:36 -04:00
return await topics.createTopicFromPosts(socket.uid, data.title, data.pids, data.fromTid);
};
2019-09-12 23:27:36 -04:00
SocketTopics.changeWatching = async function (socket, data) {
2017-03-02 19:03:49 +03:00
if (!data || !data.tid || !data.type) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:invalid-data]]');
2016-05-18 19:02:43 +03:00
}
2019-09-12 23:27:36 -04:00
const commands = ['follow', 'unfollow', 'ignore'];
if (!commands.includes(data.type)) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:invalid-command]]');
2016-05-18 19:02:43 +03:00
}
2019-09-12 23:27:36 -04:00
await followCommand(topics[data.type], socket, data.tid);
};
2019-09-12 23:27:36 -04:00
SocketTopics.follow = async function (socket, tid) {
await followCommand(topics.follow, socket, tid);
2015-09-25 15:34:53 -04:00
};
2019-09-12 23:27:36 -04:00
async function followCommand(method, socket, tid) {
if (!socket.uid) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:not-logged-in]]');
2015-01-25 16:47:47 -05:00
}
2019-09-12 23:27:36 -04:00
await method(tid, socket.uid);
2015-09-25 15:34:53 -04:00
}
2015-01-25 16:47:47 -05:00
2019-09-12 23:27:36 -04:00
SocketTopics.isFollowed = async function (socket, tid) {
const isFollowing = await topics.isFollowing([tid], socket.uid);
return isFollowing[0];
2016-05-11 16:40:40 +03:00
};
2019-09-12 23:27:36 -04:00
SocketTopics.search = async function (socket, data) {
2017-03-02 19:03:49 +03:00
if (!data) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:invalid-data]]');
2017-03-02 19:03:49 +03:00
}
2019-09-12 23:27:36 -04:00
return await topics.search(data.tid, data.term);
2014-07-24 17:30:37 -04:00
};
2019-09-12 23:27:36 -04:00
SocketTopics.isModerator = async function (socket, tid) {
const cid = await topics.getTopicField(tid, 'cid');
return await user.isModerator(socket.uid, cid);
2015-03-18 17:50:47 -04:00
};
2019-09-12 23:27:36 -04:00
SocketTopics.getTopic = async function (socket, tid) {
return await apiController.getTopicData(tid, socket.uid);
2016-03-08 11:24:32 +02:00
};
2019-09-09 19:19:56 -04:00
require('../promisify')(SocketTopics);