2020-10-16 11:01:10 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const user = require('../user');
|
|
|
|
|
const topics = require('../topics');
|
|
|
|
|
const posts = require('../posts');
|
|
|
|
|
const meta = require('../meta');
|
|
|
|
|
|
2020-10-16 21:05:00 -04:00
|
|
|
const apiHelpers = require('./helpers');
|
2020-10-16 21:36:59 -04:00
|
|
|
const doTopicAction = apiHelpers.doTopicAction;
|
|
|
|
|
|
2020-11-30 11:08:00 -05:00
|
|
|
const websockets = require('../socket.io');
|
2020-10-16 11:01:10 -04:00
|
|
|
const socketHelpers = require('../socket.io/helpers');
|
|
|
|
|
|
|
|
|
|
const topicsAPI = module.exports;
|
|
|
|
|
|
|
|
|
|
topicsAPI.create = async function (caller, data) {
|
|
|
|
|
if (!data) {
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const payload = { ...data };
|
|
|
|
|
payload.tags = payload.tags || [];
|
|
|
|
|
payload.uid = caller.uid;
|
2020-10-16 21:05:00 -04:00
|
|
|
payload.req = apiHelpers.buildReqObject(caller);
|
2020-10-16 11:01:10 -04:00
|
|
|
payload.timestamp = Date.now();
|
|
|
|
|
payload.fromQueue = false;
|
|
|
|
|
|
|
|
|
|
// Blacklist & Post Queue
|
|
|
|
|
await meta.blacklist.test(caller.ip);
|
|
|
|
|
const shouldQueue = await posts.shouldQueue(caller.uid, payload);
|
|
|
|
|
if (shouldQueue) {
|
|
|
|
|
const queueObj = await posts.addToQueue(payload);
|
|
|
|
|
return queueObj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await topics.post(payload);
|
|
|
|
|
|
|
|
|
|
socketHelpers.emitToUids('event:new_post', { posts: [result.postData] }, [caller.uid]);
|
|
|
|
|
socketHelpers.emitToUids('event:new_topic', result.topicData, [caller.uid]);
|
|
|
|
|
socketHelpers.notifyNew(caller.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
|
|
|
|
|
|
|
|
|
|
return result.topicData;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
topicsAPI.reply = async function (caller, data) {
|
|
|
|
|
var payload = {
|
|
|
|
|
tid: data.tid,
|
|
|
|
|
uid: caller.uid,
|
2020-10-16 21:05:00 -04:00
|
|
|
req: apiHelpers.buildReqObject(caller), // For IP recording
|
2020-10-16 11:01:10 -04:00
|
|
|
content: data.content,
|
2020-11-28 22:03:08 -05:00
|
|
|
timestamp: Date.now(),
|
2020-10-16 11:01:10 -04:00
|
|
|
fromQueue: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (data.toPid) { payload.toPid = data.toPid; }
|
|
|
|
|
|
|
|
|
|
// Blacklist & Post Queue
|
|
|
|
|
await meta.blacklist.test(caller.ip);
|
|
|
|
|
const shouldQueue = await posts.shouldQueue(caller.uid, payload);
|
|
|
|
|
if (shouldQueue) {
|
|
|
|
|
const queueObj = await posts.addToQueue(payload);
|
|
|
|
|
return queueObj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const postData = await topics.reply(payload); // postData seems to be a subset of postObj, refactor?
|
|
|
|
|
const postObj = await posts.getPostSummaryByPids([postData.pid], caller.uid, {});
|
|
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
|
posts: [postData],
|
|
|
|
|
'reputation:disabled': meta.config['reputation:disabled'] === 1,
|
|
|
|
|
'downvote:disabled': meta.config['downvote:disabled'] === 1,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
user.updateOnlineUsers(caller.uid);
|
2020-11-30 11:08:00 -05:00
|
|
|
if (caller.uid) {
|
|
|
|
|
socketHelpers.emitToUids('event:new_post', result, [caller.uid]);
|
2020-11-30 11:51:52 -05:00
|
|
|
} else if (caller.uid === 0) {
|
|
|
|
|
websockets.in('online_guests').emit('event:new_post', result);
|
2020-11-30 11:08:00 -05:00
|
|
|
}
|
|
|
|
|
|
2020-10-16 11:01:10 -04:00
|
|
|
socketHelpers.notifyNew(caller.uid, 'newPost', result);
|
|
|
|
|
|
|
|
|
|
return postObj[0];
|
|
|
|
|
};
|
2020-10-16 12:02:51 -04:00
|
|
|
|
|
|
|
|
topicsAPI.delete = async function (caller, data) {
|
|
|
|
|
await doTopicAction('delete', 'event:topic_deleted', caller, {
|
2020-10-16 21:36:59 -04:00
|
|
|
tids: data.tids,
|
2020-10-16 12:02:51 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
topicsAPI.restore = async function (caller, data) {
|
|
|
|
|
await doTopicAction('restore', 'event:topic_restored', caller, {
|
2020-10-16 21:36:59 -04:00
|
|
|
tids: data.tids,
|
2020-10-16 12:02:51 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
topicsAPI.purge = async function (caller, data) {
|
|
|
|
|
await doTopicAction('purge', 'event:topic_purged', caller, {
|
2020-10-16 21:36:59 -04:00
|
|
|
tids: data.tids,
|
2020-10-16 12:02:51 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
topicsAPI.pin = async function (caller, data) {
|
|
|
|
|
await doTopicAction('pin', 'event:topic_pinned', caller, {
|
2020-10-16 21:36:59 -04:00
|
|
|
tids: data.tids,
|
2020-10-16 12:02:51 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
topicsAPI.unpin = async function (caller, data) {
|
|
|
|
|
await doTopicAction('unpin', 'event:topic_unpinned', caller, {
|
2020-10-16 21:36:59 -04:00
|
|
|
tids: data.tids,
|
2020-10-16 12:02:51 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
topicsAPI.lock = async function (caller, data) {
|
|
|
|
|
await doTopicAction('lock', 'event:topic_locked', caller, {
|
2020-10-16 21:36:59 -04:00
|
|
|
tids: data.tids,
|
2020-10-16 12:02:51 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
topicsAPI.unlock = async function (caller, data) {
|
|
|
|
|
await doTopicAction('unlock', 'event:topic_unlocked', caller, {
|
2020-10-16 21:36:59 -04:00
|
|
|
tids: data.tids,
|
2020-10-16 12:02:51 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-16 12:30:14 -04:00
|
|
|
topicsAPI.follow = async function (caller, data) {
|
|
|
|
|
await topics.follow(data.tid, caller.uid);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
topicsAPI.ignore = async function (caller, data) {
|
|
|
|
|
await topics.ignore(data.tid, caller.uid);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
topicsAPI.unfollow = async function (caller, data) {
|
|
|
|
|
await topics.unfollow(data.tid, caller.uid);
|
|
|
|
|
};
|