feat: #7743 , user/block, user/categories
parent
6f738c2b44
commit
4541caa4f8
@ -1,89 +1,64 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const async = require('async');
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
const db = require('../database');
|
const db = require('../database');
|
||||||
const categories = require('../categories');
|
const categories = require('../categories');
|
||||||
|
|
||||||
module.exports = function (User) {
|
module.exports = function (User) {
|
||||||
User.setCategoryWatchState = function (uid, cid, state, callback) {
|
User.setCategoryWatchState = async function (uid, cid, state) {
|
||||||
if (!(parseInt(uid, 10) > 0)) {
|
if (!(parseInt(uid, 10) > 0)) {
|
||||||
return setImmediate(callback);
|
return;
|
||||||
}
|
}
|
||||||
const isStateValid = Object.keys(categories.watchStates).some(key => categories.watchStates[key] === parseInt(state, 10));
|
const isStateValid = Object.values(categories.watchStates).includes(parseInt(state, 10));
|
||||||
if (!isStateValid) {
|
if (!isStateValid) {
|
||||||
return setImmediate(callback, new Error('[[error:invalid-watch-state]]'));
|
throw new Error('[[error:invalid-watch-state]]');
|
||||||
}
|
}
|
||||||
async.waterfall([
|
const exists = await categories.exists(cid);
|
||||||
function (next) {
|
|
||||||
categories.exists(cid, next);
|
|
||||||
},
|
|
||||||
function (exists, next) {
|
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
return next(new Error('[[error:no-category]]'));
|
throw new Error('[[error:no-category]]');
|
||||||
}
|
}
|
||||||
|
await db.sortedSetAdd('cid:' + cid + ':uid:watch:state', state, uid);
|
||||||
db.sortedSetAdd('cid:' + cid + ':uid:watch:state', state, uid, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
User.getCategoryWatchState = function (uid, callback) {
|
User.getCategoryWatchState = async function (uid) {
|
||||||
if (parseInt(uid, 10) <= 0) {
|
if (!(parseInt(uid, 10) > 0)) {
|
||||||
return setImmediate(callback, null, {});
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
let cids;
|
const cids = await categories.getAllCidsFromSet('categories:cid');
|
||||||
async.waterfall([
|
const states = await categories.getWatchState(cids, uid);
|
||||||
function (next) {
|
return _.zipObject(cids, states);
|
||||||
categories.getAllCidsFromSet('categories:cid', next);
|
|
||||||
},
|
|
||||||
function (_cids, next) {
|
|
||||||
cids = _cids;
|
|
||||||
categories.getWatchState(cids, uid, next);
|
|
||||||
},
|
|
||||||
function (states, next) {
|
|
||||||
next(null, _.zipObject(cids, states));
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
User.getIgnoredCategories = function (uid, callback) {
|
User.getIgnoredCategories = async function (uid) {
|
||||||
if (parseInt(uid, 10) <= 0) {
|
if (!(parseInt(uid, 10) > 0)) {
|
||||||
return setImmediate(callback, null, []);
|
return [];
|
||||||
}
|
}
|
||||||
User.getCategoriesByStates(uid, [categories.watchStates.ignoring], callback);
|
return await User.getCategoriesByStates(uid, [categories.watchStates.ignoring]);
|
||||||
};
|
};
|
||||||
|
|
||||||
User.getWatchedCategories = function (uid, callback) {
|
User.getWatchedCategories = async function (uid) {
|
||||||
if (parseInt(uid, 10) <= 0) {
|
if (!(parseInt(uid, 10) > 0)) {
|
||||||
return setImmediate(callback, null, []);
|
return [];
|
||||||
}
|
}
|
||||||
User.getCategoriesByStates(uid, [categories.watchStates.watching], callback);
|
return await User.getCategoriesByStates(uid, [categories.watchStates.watching]);
|
||||||
};
|
};
|
||||||
|
|
||||||
User.getCategoriesByStates = function (uid, states, callback) {
|
User.getCategoriesByStates = async function (uid, states) {
|
||||||
if (!(parseInt(uid, 10) > 0)) {
|
if (!(parseInt(uid, 10) > 0)) {
|
||||||
return categories.getAllCidsFromSet('categories:cid', callback);
|
return await categories.getAllCidsFromSet('categories:cid');
|
||||||
}
|
}
|
||||||
|
const userState = await User.getCategoryWatchState(uid);
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
User.getCategoryWatchState(uid, next);
|
|
||||||
},
|
|
||||||
function (userState, next) {
|
|
||||||
const cids = Object.keys(userState);
|
const cids = Object.keys(userState);
|
||||||
next(null, cids.filter(cid => states.includes(userState[cid])));
|
return cids.filter(cid => states.includes(userState[cid]));
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
User.ignoreCategory = function (uid, cid, callback) {
|
User.ignoreCategory = async function (uid, cid) {
|
||||||
User.setCategoryWatchState(uid, cid, categories.watchStates.ignoring, callback);
|
await User.setCategoryWatchState(uid, cid, categories.watchStates.ignoring);
|
||||||
};
|
};
|
||||||
|
|
||||||
User.watchCategory = function (uid, cid, callback) {
|
User.watchCategory = async function (uid, cid) {
|
||||||
User.setCategoryWatchState(uid, cid, categories.watchStates.watching, callback);
|
await User.setCategoryWatchState(uid, cid, categories.watchStates.watching);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue