Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / modules / groups / lib / group-room-finder.js
blob56587a3c6c0b01d16e5f3f50768e13de2ca80b69
1 'use strict';
3 var Promise = require('bluebird');
4 var TroupeUser = require('gitter-web-persistence').TroupeUser;
5 var _ = require('lodash');
7 function findUserRoomIdsWithinGroup(groupId, userId) {
8 return TroupeUser.aggregate([
9 { $match: { userId: userId } },
10 { $project: { _id: '$troupeId' } },
12 $lookup: {
13 from: 'troupes',
14 localField: '_id',
15 foreignField: '_id',
16 as: 'troupe'
20 $unwind: '$troupe'
23 $match: {
24 'troupe.groupId': groupId
28 $project: {
29 _id: 1
33 .exec()
34 .then(function(results) {
35 return _.map(results, function(f) {
36 return f._id;
37 });
38 });
41 function queryForPublicRooms(groupId) {
42 return { groupId: groupId, 'sd.public': true };
45 function queryForPublicRoomsInGroupIds(groupIds) {
46 return { groupId: { $in: groupIds }, 'sd.public': true };
49 function queryForAccessibleRooms(groupId, userId) {
50 if (!userId) return queryForPublicRooms(groupId);
52 return findUserRoomIdsWithinGroup(groupId, userId).then(function(memberTroupeIds) {
53 return {
54 groupId: groupId,
55 $or: [
56 { _id: { $in: memberTroupeIds } },
57 { 'sd.public': true },
58 { 'sd.extraAdmins': { $elemMatch: { $eq: userId } } },
59 { 'sd.extraMembers': { $elemMatch: { $eq: userId } } }
62 });
65 module.exports = {
66 queryForPublicRooms: queryForPublicRooms,
67 queryForPublicRoomsInGroupIds: queryForPublicRoomsInGroupIds,
68 queryForAccessibleRooms: Promise.method(queryForAccessibleRooms)