From c9c454825ee539eda9231da44f4fae1d7b8c2a5e Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 13 Aug 2014 21:42:04 -0400 Subject: [PATCH] closes #1369 --- public/language/en_GB/user.json | 5 ++- src/threadTools.js | 15 ++++++++ src/topics/create.js | 63 +++++++++++++++++++-------------- src/user/settings.js | 6 +++- 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/public/language/en_GB/user.json b/public/language/en_GB/user.json index ab8c1cdae699..29c6dc983b24 100644 --- a/public/language/en_GB/user.json +++ b/public/language/en_GB/user.json @@ -71,5 +71,8 @@ "notification_sounds" : "Play a sound when you receive a notification.", "browsing": "Browsing Settings", - "open_links_in_new_tab": "Open outgoing links in new tab?" + "open_links_in_new_tab": "Open outgoing links in new tab?", + + "follow_topics_you_reply_to": "Follow topics that you reply to.", + "follow_topics_you_create": "Follow topics you create." } diff --git a/src/threadTools.js b/src/threadTools.js index 5e52d043c7ed..4bff68bdf102 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -240,4 +240,19 @@ var winston = require('winston'), ], callback); }; + ThreadTools.follow = function(tid, uid, callback) { + callback = callback || function() {}; + async.waterfall([ + function (next) { + ThreadTools.exists(tid, next); + }, + function (exists, next) { + if (!exists) { + return next(new Error('[[error:no-topic]]')); + } + db.setAdd('tid:' + tid + ':followers', uid, next); + } + ], callback); + }; + }(exports)); diff --git a/src/topics/create.js b/src/topics/create.js index d7a847d94a5b..955d42d182ec 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -105,9 +105,6 @@ module.exports = function(Topics) { if(!canCreate) { return next(new Error('[[error:no-privileges]]')); } - next(); - }, - function(next) { user.isReadyToPost(uid, next); }, function(next) { @@ -115,35 +112,46 @@ module.exports = function(Topics) { }, function(filteredData, next) { content = filteredData.content || data.content; - next(); - }, - function(next) { Topics.create({uid: uid, title: title, cid: cid, thumb: data.thumb, tags: data.tags}, next); }, function(tid, next) { Topics.reply({uid:uid, tid:tid, content:content, req: data.req}, next); }, function(postData, next) { - threadTools.toggleFollow(postData.tid, uid); - next(null, postData); - }, - function(postData, next) { - Topics.getTopicsByTids([postData.tid], 0, function(err, topicData) { - if(err) { - return next(err); + async.parallel({ + postData: function(next) { + next(null, postData); + }, + settings: function(next) { + user.getSettings(uid, function(err, settings) { + if (err) { + return next(err); + } + if (settings.followTopicsOnCreate) { + threadTools.follow(postData.tid, uid, next); + } else { + next(); + } + }); + }, + topicData: function(next) { + Topics.getTopicsByTids([postData.tid], 0, next); } - if(!topicData || !topicData.length) { - return next(new Error('[[error:no-topic]]')); - } - topicData = topicData[0]; - topicData.unreplied = 1; + }, next); + }, + function(data, next) { + if(!Array.isArray(data.topicData) || !data.topicData.length) { + return next(new Error('[[error:no-topic]]')); + } + + data.topicData = data.topicData[0]; + data.topicData.unreplied = 1; - plugins.fireHook('action:topic.post', topicData); + plugins.fireHook('action:topic.post', data.topicData); - next(null, { - topicData: topicData, - postData: postData - }); + next(null, { + topicData: data.topicData, + postData: data.postData }); } ], callback); @@ -178,9 +186,6 @@ module.exports = function(Topics) { if (!canReply) { return next(new Error('[[error:no-privileges]]')); } - next(); - }, - function(next) { user.isReadyToPost(uid, next); }, function(next) { @@ -215,6 +220,12 @@ module.exports = function(Topics) { function(topicData, next) { topicData.title = validator.escape(topicData.title); postData.topic = topicData; + user.getSettings(uid, next); + }, + function(settings, next) { + if (settings.followTopicsOnReply) { + threadTools.follow(postData.tid, uid); + } posts.getPidIndex(postData.pid, next); }, function(index, next) { diff --git a/src/user/settings.js b/src/user/settings.js index e023eda5ebd4..2cf9fba084f2 100644 --- a/src/user/settings.js +++ b/src/user/settings.js @@ -33,6 +33,8 @@ module.exports = function(User) { settings.notificationSounds = settings.notificationSounds ? parseInt(settings.notificationSounds, 10) === 1 : true; settings.language = settings.language || meta.config.defaultLang || 'en_GB'; settings.topicPostSort = settings.topicPostSort || meta.config.topicPostSort || 'oldest_to_newest'; + settings.followTopicsOnCreate = settings.followTopicsOnCreate ? parseInt(settings.followTopicsOnCreate, 10) === 1 : true; + settings.followTopicsOnReply = settings.followTopicsOnReply ? parseInt(settings.followTopicsOnReply, 10) === 1 : false; callback(null, settings); }); }); @@ -80,7 +82,9 @@ module.exports = function(User) { topicsPerPage: data.topicsPerPage, postsPerPage: data.postsPerPage, notificationSounds: data.notificationSounds, - language: data.language || meta.config.defaultLang + language: data.language || meta.config.defaultLang, + followTopicsOnCreate: data.followTopicsOnCreate, + followTopicsOnReply: data.followTopicsOnReply }, callback); };