Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

[DON'T MERGE] Add support for nested groups. #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion vacation-calendar/src/Code.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ function sync() {
lastRun = lastRun ? new Date(lastRun) : null;

// Get the list of users in the Google Group.
var users = GroupsApp.getGroupByEmail(GROUP_EMAIL).getUsers();
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
var users = getAllUsers(group);

// For each user, find events having one or more of the keywords in the event
// summary in the specified date range. Import each of those to the team
Expand All @@ -57,6 +58,31 @@ function sync() {
console.log('Imported ' + count + ' events');
}

/**
* Gets all the users that are members of a Google Group, including indirect
* members (for groups that contain other groups).
* @param {GoogleAppsScript.GroupsApp.Group} group The target group.
* @return {GoogleAppsScript.User[]} All users within the group.
*/
function getAllUsers(group) {
var users = {};
var queue = [group];
var visited = {};
var current;
while (current = queue.pop()) {
var key = current.getEmail();
if (visited[key]) continue;
current.getUsers().forEach(function(user) {
users[user.getEmail()] = user;
});
queue = queue.concat(current.getGroups());
visited[key] = true;
}
return Object.keys(users).map(function(email) {
return users[email];
});
}

/**
* Imports the given event from the user's calendar into the shared team
* calendar.
Expand Down