r
.table('labels')
.getAll(owner, { index: 'owner' })
.map(function (label) {
return r.table('threads')
.getAll(label('id'), { index: 'labels'})
.coerceTo('array')
.do(function (threads) {
return label.merge({
'total_threads_count': threads.count(),
'unread_threads_count': threads.filter(function (thread) {
return thread('is_read').not().and(
thread('labels').contains(function (label) {
return r.expr(spamTrashSent)
.map(function (row) { return row('id'); })
.contains(label);
}).not()
)
}).count()
});
});
});