Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / modules / groups / lib / group-favourites-core.js
blob0091753a773cec5e993daaf791cd71322b36da83
1 'use strict';
3 var _ = require('lodash');
4 var lazy = require('lazy.js');
5 var persistence = require('gitter-web-persistence');
6 const mongoReadPrefs = require('gitter-web-persistence-utils/lib/mongo-read-prefs');
8 /**
9 * For exporting things
11 function getCursorByUserId(userId) {
12 const cursor = persistence.UserGroupFavourites.find({
13 userId
15 .lean()
16 .read(mongoReadPrefs.secondaryPreferred)
17 .batchSize(100)
18 .cursor();
20 return cursor;
23 /**
24 * Internal call
25 * Based on gitter-web-rooms/lib/recent-room-core.js
27 function addGroupAsFavouriteInLastPosition(userId, groupId) {
28 return findFavouriteGroupsForUser(userId).then(function(userGroupFavourites) {
29 var lastPosition =
30 lazy(userGroupFavourites)
31 .values()
32 .concat(0)
33 .max() + 1;
35 var setOp = {};
36 setOp['favs.' + groupId] = lastPosition;
38 return persistence.UserGroupFavourites.update(
39 { userId: userId },
40 { $set: setOp },
41 { upsert: true, new: true }
43 .exec()
44 .thenReturn(lastPosition);
45 });
48 /**
49 * Internal call
50 * Based on gitter-web-rooms/lib/recent-room-core.js
52 function addGroupAsFavouriteInPosition(userId, groupId, position) {
53 return findFavouriteGroupsForUser(userId).then(function(userGroupFavourites) {
54 var values = lazy(userGroupFavourites)
55 .pairs()
56 .filter(function(a) {
57 return a[1] >= position && a[0] !== groupId;
59 .sortBy(function(a) {
60 return a[1];
62 .toArray();
64 var next = position;
65 // NB: used to be i = 1
66 for (var i = 0; i < values.length; i++) {
67 var item = values[i];
69 if (item[1] > next) {
70 /* Only increment those values before this one */
71 values.splice(i, values.length);
72 break;
74 /* This dude needs an increment */
75 item[1]++;
76 next = item[1];
79 var inc = lazy(values)
80 .map(function(a) {
81 return ['favs.' + a[0], 1];
83 .toObject();
85 var set = {};
86 set['favs.' + groupId] = position;
88 var update = { $set: set };
89 if (!_.isEmpty(inc)) update.$inc = inc; // Empty $inc is invalid
91 return persistence.UserGroupFavourites.update({ userId: userId }, update, {
92 upsert: true,
93 new: true
95 .exec()
96 .thenReturn(position);
97 });
101 * Internal call
102 * Based on gitter-web-rooms/lib/recent-room-core.js
104 function clearFavourite(userId, groupId) {
105 var setOp = {};
106 setOp['favs.' + groupId] = 1;
108 return persistence.UserGroupFavourites.update({ userId: userId }, { $unset: setOp }, {})
109 .exec()
110 .thenReturn(null);
113 function findFavouriteGroupsForUser(userId) {
114 return persistence.UserGroupFavourites.findOne({ userId: userId }, { favs: 1 }, { lean: true })
115 .exec()
116 .then(function(userGroupFavourites) {
117 if (!userGroupFavourites || !userGroupFavourites.favs) return {};
119 return lazy(userGroupFavourites.favs)
120 .pairs()
121 .toObject();
125 function updateFavourite(userId, groupId, favouritePosition) {
126 if (favouritePosition) {
127 /* Deal with legacy, or when the star button is toggled */
128 if (favouritePosition === true) {
129 return addGroupAsFavouriteInLastPosition(userId, groupId);
130 } else {
131 return addGroupAsFavouriteInPosition(userId, groupId, favouritePosition);
133 } else {
134 // Unset the favourite
135 return clearFavourite(userId, groupId);
139 module.exports = {
140 getCursorByUserId,
141 findFavouriteGroupsForUser: findFavouriteGroupsForUser,
142 updateFavourite: updateFavourite