From 862fd1a94a7aac4254f9f846ad2c380c662ce715 Mon Sep 17 00:00:00 2001 From: Ben Lubar Date: Mon, 22 Aug 2016 17:16:52 -0500 Subject: [PATCH] ignore/watch categories recursively --- public/language/en_GB/category.json | 4 +-- src/socket.io/categories.js | 55 ++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/public/language/en_GB/category.json b/public/language/en_GB/category.json index d4da76f356..d581c8277d 100644 --- a/public/language/en_GB/category.json +++ b/public/language/en_GB/category.json @@ -18,8 +18,8 @@ "watching.description": "Show topics in unread", "ignoring.description": "Do not show topics in unread", - "watch.message": "You are now watching updates from this category", - "ignore.message": "You are now ignoring updates from this category", + "watch.message": "You are now watching updates from this category and all subcategories", + "ignore.message": "You are now ignoring updates from this category and all subcategories", "watched-categories": "Watched categories" } diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js index 545938d718..1c13adebea 100644 --- a/src/socket.io/categories.js +++ b/src/socket.io/categories.js @@ -1,6 +1,6 @@ 'use strict'; -var async = require('async'); +var async = require('async'); var db = require('../database'); var categories = require('../categories'); var privileges = require('../privileges'); @@ -171,23 +171,52 @@ SocketCategories.getMoveCategories = function(socket, data, callback) { }; SocketCategories.watch = function(socket, cid, callback) { - user.watchCategory(socket.uid, cid, function(err) { - if (err) { - return callback(err); - } - topics.pushUnreadCount(socket.uid, callback); - }); + ignoreOrWatch(user.watchCategory, socket, cid, callback); }; SocketCategories.ignore = function(socket, cid, callback) { - user.ignoreCategory(socket.uid, cid, function(err) { - if (err) { - return callback(err); - } - topics.pushUnreadCount(socket.uid, callback); - }); + ignoreOrWatch(user.ignoreCategory, socket, cid, callback); }; +function ignoreOrWatch(fn, socket, cid, callback) { + async.waterfall([ + function(next) { + db.getSortedSetRange('categories:cid', 0, -1, next); + }, + function(cids, next) { + categories.getCategoriesFields(cids, ['cid', 'parentCid'], next); + }, + function(categoryData, next) { + categoryData.forEach(function(c) { + c.cid = parseInt(c.cid, 10); + c.parentCid = parseInt(c.parentCid, 10); + }); + + var cids = [parseInt(cid, 10)]; + + // filter to subcategories of cid + + var any = true; + while (any) { + any = false; + categoryData.forEach(function(c) { + if (cids.indexOf(c.cid) === -1 && cids.indexOf(c.parentCid) !== -1) { + cids.push(c.cid); + any = true; + } + }); + } + + async.each(cids, function(cid, next) { + fn(socket.uid, cid, next); + }, next); + }, + function(next) { + topics.pushUnreadCount(socket.uid, next); + } + ], callback); +} + SocketCategories.isModerator = function(socket, cid, callback) { user.isModerator(socket.uid, cid, callback); };