diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index ddde471aa4..71798a3311 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -1,11 +1,9 @@ 'use strict'; -const api = require('../api'); const topics = require('../topics'); const user = require('../user'); const meta = require('../meta'); const privileges = require('../privileges'); -const sockets = require('.'); const SocketTopics = module.exports; @@ -46,32 +44,6 @@ SocketTopics.createTopicFromPosts = async function (socket, data) { return await topics.createTopicFromPosts(socket.uid, data.title, data.pids, data.fromTid); }; -SocketTopics.changeWatching = async function (socket, data) { - if (!data || !data.tid || !data.type) { - throw new Error('[[error:invalid-data]]'); - } - const commands = ['follow', 'unfollow', 'ignore']; - if (!commands.includes(data.type)) { - throw new Error('[[error:invalid-command]]'); - } - - sockets.warnDeprecated(socket, 'PUT/DELETE /api/v3/topics/:tid/(follow|ignore)'); - await followCommand(data.type, socket, data.tid); -}; - -SocketTopics.follow = async function (socket, tid) { - sockets.warnDeprecated(socket, 'PUT /api/v3/topics/:tid/follow'); - await followCommand('follow', socket, tid); -}; - -async function followCommand(method, socket, tid) { - if (!socket.uid) { - throw new Error('[[error:not-logged-in]]'); - } - - await api.topics[method](socket, { tid }); -} - SocketTopics.isFollowed = async function (socket, tid) { const isFollowing = await topics.isFollowing([tid], socket.uid); return isFollowing[0]; diff --git a/src/topics/create.js b/src/topics/create.js index 1308827789..cdb304cad7 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -127,7 +127,7 @@ module.exports = function (Topics) { throw new Error('[[error:no-topic]]'); } - if (settings.followTopicsOnCreate) { + if (uid > 0 && settings.followTopicsOnCreate) { await Topics.follow(postData.tid, uid); } const topicData = topics[0]; diff --git a/src/topics/follow.js b/src/topics/follow.js index e9d895fc31..2cd856134c 100644 --- a/src/topics/follow.js +++ b/src/topics/follow.js @@ -35,8 +35,8 @@ module.exports = function (Topics) { }; async function setWatching(method1, method2, hook, tid, uid) { - if (parseInt(uid, 10) <= 0) { - return; + if (!(parseInt(uid, 10) > 0)) { + throw new Error('[[error:not-logged-in]]'); } const exists = await Topics.exists(tid); if (!exists) { diff --git a/test/topics.js b/test/topics.js index 9fe79b80b6..851ac3fd44 100644 --- a/test/topics.js +++ b/test/topics.js @@ -9,6 +9,8 @@ const nconf = require('nconf'); const request = require('request'); const util = require('util'); +const sleep = util.promisify(setTimeout); + const db = require('./mocks/databasemock'); const file = require('../src/file'); const topics = require('../src/topics'); @@ -1498,35 +1500,15 @@ describe('Topic\'s', () => { }); }); - it('should mark topic notifications read', (done) => { - async.waterfall([ - function (next) { - socketTopics.follow({ uid: adminUid }, tid, next); - }, - function (next) { - topics.reply({ uid: uid, timestamp: Date.now(), content: 'some content', tid: tid }, next); - }, - function (data, next) { - setTimeout(next, 2500); - }, - function (next) { - User.notifications.getUnreadCount(adminUid, next); - }, - function (count, next) { - assert.equal(count, 1); - socketTopics.markTopicNotificationsRead({ uid: adminUid }, [tid], next); - }, - function (next) { - User.notifications.getUnreadCount(adminUid, next); - }, - function (count, next) { - assert.equal(count, 0); - next(); - }, - ], (err) => { - assert.ifError(err); - done(); - }); + it('should mark topic notifications read', async () => { + await apiTopics.follow({ uid: adminUid }, { tid: tid }); + const data = await topics.reply({ uid: uid, timestamp: Date.now(), content: 'some content', tid: tid }); + await sleep(2500); + let count = await User.notifications.getUnreadCount(adminUid); + assert.strictEqual(count, 1); + await socketTopics.markTopicNotificationsRead({ uid: adminUid }, [tid]); + count = await User.notifications.getUnreadCount(adminUid); + assert.strictEqual(count, 0); }); it('should fail with invalid data', (done) => { @@ -2161,37 +2143,29 @@ describe('Topic\'s', () => { }); }); - it('should error if not logged in', (done) => { - socketTopics.changeWatching({ uid: 0 }, { tid: tid, type: 'ignore' }, (err) => { + it('should error if not logged in', async () => { + try { + await apiTopics.ignore({ uid: 0 }, { tid: tid }); + assert(false); + } catch (err) { assert.equal(err.message, '[[error:not-logged-in]]'); - done(); - }); + } }); - it('should filter ignoring uids', (done) => { - socketTopics.changeWatching({ uid: followerUid }, { tid: tid, type: 'ignore' }, (err) => { - assert.ifError(err); - topics.filterIgnoringUids(tid, [adminUid, followerUid], (err, uids) => { - assert.ifError(err); - assert.equal(uids.length, 1); - assert.equal(uids[0], adminUid); - done(); - }); - }); + it('should filter ignoring uids', async () => { + await apiTopics.ignore({ uid: followerUid }, { tid: tid }); + const uids = await topics.filterIgnoringUids(tid, [adminUid, followerUid]); + assert.equal(uids.length, 1); + assert.equal(uids[0], adminUid); }); - it('should error with invalid data', (done) => { - socketTopics.changeWatching({ uid: followerUid }, {}, (err) => { - assert.equal(err.message, '[[error:invalid-data]]'); - done(); - }); - }); - - it('should error with invalid type', (done) => { - socketTopics.changeWatching({ uid: followerUid }, { tid: tid, type: 'derp' }, (err) => { - assert.equal(err.message, '[[error:invalid-command]]'); - done(); - }); + it('should error with topic that does not exist', async () => { + try { + await apiTopics.follow({ uid: followerUid }, { tid: -1 }); + assert(false); + } catch (err) { + assert.equal(err.message, '[[error:no-topic]]'); + } }); it('should follow topic', (done) => {