2017-03-01 22:42:10 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-07-18 13:22:17 -04:00
|
|
|
const db = require('../database');
|
|
|
|
|
const user = require('../user');
|
2017-03-01 22:42:10 +03:00
|
|
|
|
|
|
|
|
module.exports = function (Groups) {
|
2019-07-18 13:22:17 -04:00
|
|
|
Groups.getUsersFromSet = async function (set, fields) {
|
|
|
|
|
const uids = await db.getSetMembers(set);
|
|
|
|
|
|
|
|
|
|
if (fields) {
|
|
|
|
|
return await user.getUsersFields(uids, fields);
|
2017-09-15 17:07:43 -04:00
|
|
|
}
|
2019-07-18 13:22:17 -04:00
|
|
|
return await user.getUsersData(uids);
|
2017-03-01 22:42:10 +03:00
|
|
|
};
|
|
|
|
|
|
2019-07-18 13:22:17 -04:00
|
|
|
Groups.getUserGroups = async function (uids) {
|
|
|
|
|
return await Groups.getUserGroupsFromSet('groups:visible:createtime', uids);
|
2017-03-01 22:42:10 +03:00
|
|
|
};
|
|
|
|
|
|
2019-07-18 13:22:17 -04:00
|
|
|
Groups.getUserGroupsFromSet = async function (set, uids) {
|
|
|
|
|
const memberOf = await Groups.getUserGroupMembership(set, uids);
|
|
|
|
|
return await Promise.all(memberOf.map(memberOf => Groups.getGroupsData(memberOf)));
|
2017-05-04 16:32:51 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-18 13:22:17 -04:00
|
|
|
Groups.getUserGroupMembership = async function (set, uids) {
|
|
|
|
|
const groupNames = await db.getSortedSetRevRange(set, 0, -1);
|
|
|
|
|
return await Promise.all(uids.map(uid => findUserGroups(uid, groupNames)));
|
2017-03-01 22:42:10 +03:00
|
|
|
};
|
2019-07-18 13:22:17 -04:00
|
|
|
|
|
|
|
|
async function findUserGroups(uid, groupNames) {
|
|
|
|
|
const isMembers = await Groups.isMemberOfGroups(uid, groupNames);
|
|
|
|
|
return groupNames.filter((name, i) => isMembers[i]);
|
|
|
|
|
}
|
2017-03-01 22:42:10 +03:00
|
|
|
};
|