categories.js refactor
parent
daebc322eb
commit
8fdc03eaad
@ -0,0 +1,48 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
db = require('../database'),
|
||||
utils = require('../../public/src/utils');
|
||||
|
||||
module.exports = function(Categories) {
|
||||
|
||||
Categories.create = function(data, callback) {
|
||||
db.incrObjectField('global', 'nextCid', function(err, cid) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var slug = cid + '/' + utils.slugify(data.name);
|
||||
|
||||
var category = {
|
||||
cid: cid,
|
||||
name: data.name,
|
||||
description: data.description,
|
||||
icon: data.icon,
|
||||
bgColor: data.bgColor,
|
||||
color: data.color,
|
||||
slug: slug,
|
||||
parentCid: 0,
|
||||
topic_count: 0,
|
||||
post_count: 0,
|
||||
disabled: 0,
|
||||
order: data.order,
|
||||
link: '',
|
||||
numRecentReplies: 1,
|
||||
class: 'col-md-3 col-xs-6',
|
||||
imageClass: 'auto'
|
||||
};
|
||||
|
||||
async.series([
|
||||
async.apply(db.setObject, 'category:' + cid, category),
|
||||
async.apply(db.sortedSetAdd, 'categories:cid', data.order, cid)
|
||||
], function(err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
callback(null, category);
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
@ -0,0 +1,89 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
db = require('../database'),
|
||||
topics = require('../topics');
|
||||
|
||||
module.exports = function(Categories) {
|
||||
|
||||
Categories.getCategoryTopics = function(data, callback) {
|
||||
var tids;
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tids' : 'cid:' + data.cid + ':tids', data.start, data.stop, next);
|
||||
},
|
||||
function(topicIds, next) {
|
||||
tids = topicIds;
|
||||
topics.getTopicsByTids(tids, data.uid, next);
|
||||
},
|
||||
function(topics, next) {
|
||||
if (!Array.isArray(topics) || !topics.length) {
|
||||
return next(null, {
|
||||
topics: [],
|
||||
nextStart: 1
|
||||
});
|
||||
}
|
||||
|
||||
var indices = {},
|
||||
i = 0;
|
||||
for(i=0; i<tids.length; ++i) {
|
||||
indices[tids[i]] = data.start + i;
|
||||
}
|
||||
|
||||
for(i=0; i<topics.length; ++i) {
|
||||
topics[i].index = indices[topics[i].tid];
|
||||
}
|
||||
|
||||
next(null, {
|
||||
topics: topics,
|
||||
nextStart: data.stop + 1
|
||||
});
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
|
||||
Categories.getTopicIds = function(set, start, stop, callback) {
|
||||
db.getSortedSetRevRange(set, start, stop, callback);
|
||||
};
|
||||
|
||||
Categories.getTopicIndex = function(tid, callback) {
|
||||
topics.getTopicField(tid, 'cid', function(err, cid) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
db.sortedSetRevRank('cid:' + cid + ':tids', tid, callback);
|
||||
});
|
||||
};
|
||||
|
||||
Categories.onNewPostMade = function(postData, callback) {
|
||||
topics.getTopicFields(postData.tid, ['cid', 'pinned'], function(err, topicData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!topicData || !topicData.cid) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
var cid = topicData.cid;
|
||||
|
||||
async.parallel([
|
||||
function(next) {
|
||||
db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid, next);
|
||||
},
|
||||
function(next) {
|
||||
db.incrObjectField('category:' + cid, 'post_count', next);
|
||||
},
|
||||
function(next) {
|
||||
if (parseInt(topicData.pinned, 10) === 1) {
|
||||
next();
|
||||
} else {
|
||||
db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next);
|
||||
}
|
||||
}
|
||||
], callback);
|
||||
});
|
||||
};
|
||||
|
||||
};
|
@ -0,0 +1,56 @@
|
||||
|
||||
"use strict";
|
||||
|
||||
var async = require('async'),
|
||||
db = require('../database');
|
||||
|
||||
module.exports = function(Categories) {
|
||||
|
||||
Categories.markAsRead = function(cids, uid, callback) {
|
||||
callback = callback || function() {};
|
||||
if (!Array.isArray(cids) || !cids.length) {
|
||||
return callback();
|
||||
}
|
||||
var keys = cids.map(function(cid) {
|
||||
return 'cid:' + cid + ':read_by_uid';
|
||||
});
|
||||
|
||||
db.isMemberOfSets(keys, uid, function(err, hasRead) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
keys = keys.filter(function(key, index) {
|
||||
return !hasRead[index];
|
||||
});
|
||||
|
||||
if (!keys.length) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
db.setsAdd(keys, uid, callback);
|
||||
});
|
||||
};
|
||||
|
||||
Categories.markAsUnreadForAll = function(cid, callback) {
|
||||
callback = callback || function() {};
|
||||
db.delete('cid:' + cid + ':read_by_uid', function(err) {
|
||||
callback(err);
|
||||
});
|
||||
};
|
||||
|
||||
Categories.hasReadCategories = function(cids, uid, callback) {
|
||||
var sets = [];
|
||||
|
||||
for (var i = 0, ii = cids.length; i < ii; i++) {
|
||||
sets.push('cid:' + cids[i] + ':read_by_uid');
|
||||
}
|
||||
|
||||
db.isMemberOfSets(sets, uid, callback);
|
||||
};
|
||||
|
||||
Categories.hasReadCategory = function(cid, uid, callback) {
|
||||
db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback);
|
||||
};
|
||||
|
||||
};
|
Loading…
Reference in New Issue