@@ -55,16 +55,12 @@ function sync() {
55
55
maxDate . setMonth ( maxDate . getMonth ( ) + MONTHS_IN_ADVANCE ) ;
56
56
57
57
// Determines the time the the script was last run.
58
- let lastRun = PropertiesService . getScriptProperties ( ) . getProperty ( 'lastRun' ) ;
59
- lastRun = lastRun ? new Date ( lastRun ) : null ;
58
+ //let lastRun = PropertiesService.getScriptProperties().getProperty('lastRun');
59
+ //lastRun = lastRun ? new Date(lastRun) : null;
60
+ let lastRun = null ;
60
61
61
62
// Gets the list of users in the Google Group.
62
- let users = getAllMembers ( GROUP_EMAIL ) ;
63
- if ( ONLY_DIRECT_MEMBERS ) {
64
- users = GroupsApp . getGroupByEmail ( GROUP_EMAIL ) . getUsers ( ) ;
65
- } else if ( Array . isArray ( GROUP_EMAIL ) ) {
66
- users = getUsersFromGroups ( GROUP_EMAIL ) ;
67
- }
63
+ let users = getUsersFromGroups ( GROUP_EMAIL ) ;
68
64
69
65
// For each user, finds events having one or more of the keywords in the event
70
66
// summary in the specified date range. Imports each of those to the team
@@ -199,43 +195,47 @@ function formatDateAsRFC3339(date) {
199
195
/**
200
196
* Get both direct and indirect members (and delete duplicates).
201
197
* @param {string } the e-mail address of the group.
198
+ * @param {array } the list of already added users.
199
+ * @param {array } the list of already added addresses.
202
200
* @return {object } direct and indirect members.
203
201
*/
204
- function getAllMembers ( groupEmail ) {
205
- var group = GroupsApp . getGroupByEmail ( groupEmail ) ;
206
- var users = group . getUsers ( ) ;
207
- var childGroups = group . getGroups ( ) ;
208
- for ( var i = 0 ; i < childGroups . length ; i ++ ) {
209
- var childGroup = childGroups [ i ] ;
210
- users = users . concat ( getAllMembers ( childGroup . getEmail ( ) ) ) ;
202
+ function getAllMembers ( groupEmail , userEntities , addresses ) {
203
+ let group = GroupsApp . getGroupByEmail ( groupEmail ) ;
204
+ let users = group . getUsers ( ) ;
205
+ if ( ! ONLY_DIRECT_MEMBERS ) {
206
+ try {
207
+ let childGroups = group . getGroups ( ) ;
208
+ for ( let i = 0 ; i < childGroups . length ; i ++ ) {
209
+ let childGroup = childGroups [ i ] ;
210
+ [ userEntities , addresses ] = getAllMembers ( childGroup . getEmail ( ) , userEntities , addresses ) ;
211
+ }
212
+ } catch ( e ) {
213
+ console . error ( 'Error attempting to pull groups due to %s. Skipping.' ,
214
+ e . toString ( ) ) ;
215
+ }
211
216
}
212
217
// Remove duplicate members
213
- var uniqueUsers = [ ] ;
214
- var userEmails = { } ;
215
- for ( var i = 0 ; i < users . length ; i ++ ) {
216
- var user = users [ i ] ;
217
- if ( ! userEmails [ user . getEmail ( ) ] ) {
218
- uniqueUsers . push ( user ) ;
219
- userEmails [ user . getEmail ( ) ] = true ;
218
+ for ( let i = 0 ; i < users . length ; i ++ ) {
219
+ let user = users [ i ] ;
220
+ if ( ! addresses . includes ( user . getEmail ( ) ) ) {
221
+ userEntities . push ( user ) ;
222
+ addresses . push ( user . getEmail ( ) ) ;
220
223
}
221
224
}
222
- return uniqueUsers ;
225
+ return [ userEntities , addresses ] ;
223
226
}
224
-
225
227
/**
226
228
* Get indirect members from multiple groups (and delete duplicates).
227
- * @param {array } the e-mail addresses of multiple groups.
229
+ * @param {string or array } the e-mail addresses of multiple groups.
228
230
* @return {object } indirect members of multiple groups.
229
231
*/
230
232
function getUsersFromGroups ( groupEmails ) {
231
- let users = [ ] ;
233
+ let users = [ ] , addresses = [ ] ;
234
+ if ( ! Array . isArray ( groupEmails ) ) {
235
+ groupEmails = [ groupEmails ] ;
236
+ }
232
237
for ( let groupEmail of groupEmails ) {
233
- let groupUsers = GroupsApp . getGroupByEmail ( groupEmail ) . getUsers ( ) ;
234
- for ( let user of groupUsers ) {
235
- if ( ! users . some ( u => u . getEmail ( ) === user . getEmail ( ) ) ) {
236
- users . push ( user ) ;
237
- }
238
- }
238
+ [ users , addresses ] = getAllMembers ( groupEmail , users , addresses ) ;
239
239
}
240
240
return users ;
241
241
}
0 commit comments