Files
NodeBB/src/categories/watch.js
Barış Soner Uşaklı eb7ae54f81 Category watch state (#7109)
* feat: wip, category watch change

* feat: pass data to client

* feat: allow changing state

* fix: account page categories

* fix: show in unread if topic is followed or category is watched

* feat: add default watch state to acp

* feat: save user category watch state

* feat: update unread recent pages

* fix: remove dupe code

* fix: flip conditions

* fix: handle empty arrays

* fix: ignore/watch on others profile

* feat: upgrade script for category states

if there are any users ignoring categories set their state in new zset and delete cid:<cid>:ignorers

* fix: upgrade

* fix: tests

* fix: redis count

* fix: more tests
2018-12-14 16:24:17 -05:00

81 lines
2.4 KiB
JavaScript

'use strict';
const async = require('async');
const db = require('../database');
const user = require('../user');
module.exports = function (Categories) {
Categories.watchStates = {
ignoring: 1,
notwatching: 2,
watching: 3,
};
Categories.isIgnored = function (cids, uid, callback) {
if (!(parseInt(uid, 10) > 0)) {
return setImmediate(callback, null, cids.map(() => false));
}
async.waterfall([
function (next) {
Categories.getWatchState(cids, uid, next);
},
function (states, next) {
next(null, states.map(state => state === Categories.watchStates.ignoring));
},
], callback);
};
Categories.getWatchState = function (cids, uid, callback) {
if (!(parseInt(uid, 10) > 0)) {
return setImmediate(callback, null, cids.map(() => Categories.watchStates.notwatching));
}
if (!Array.isArray(cids) || !cids.length) {
return setImmediate(callback, null, []);
}
async.waterfall([
function (next) {
const keys = cids.map(cid => 'cid:' + cid + ':uid:watch:state');
async.parallel({
userSettings: async.apply(user.getSettings, uid),
states: async.apply(db.sortedSetsScore, keys, uid),
}, next);
},
function (results, next) {
next(null, results.states.map(state => state || Categories.watchStates[results.userSettings.categoryWatchState]));
},
], callback);
};
Categories.getIgnorers = function (cid, start, stop, callback) {
const count = (stop === -1) ? -1 : (stop - start + 1);
db.getSortedSetRevRangeByScore('cid:' + cid + ':uid:watch:state', start, count, Categories.watchStates.ignoring, Categories.watchStates.ignoring, callback);
};
Categories.filterIgnoringUids = function (cid, uids, callback) {
async.waterfall([
function (next) {
Categories.getUidsWatchStates(cid, uids, next);
},
function (states, next) {
const readingUids = uids.filter((uid, index) => uid && states[index] !== Categories.watchStates.ignoring);
next(null, readingUids);
},
], callback);
};
Categories.getUidsWatchStates = function (cid, uids, callback) {
async.waterfall([
function (next) {
async.parallel({
userSettings: async.apply(user.getMultipleUserSettings, uids),
states: async.apply(db.sortedSetScores, 'cid:' + cid + ':uid:watch:state', uids),
}, next);
},
function (results, next) {
next(null, results.states.map((state, index) => state || Categories.watchStates[results.userSettings[index].categoryWatchState]));
},
], callback);
};
};