-
Notifications
You must be signed in to change notification settings - Fork 4
/
Code.gs
209 lines (185 loc) · 4.51 KB
/
Code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var COMPANY_NAME = "Socialbakers";
var COMPANY_GSUITE_DOMAIN = "socialbakers.com";
var TYPE_ALL = "all";
var TYPE_USER = "user";
var TYPE_GROUP = "group";
var TYPE_DOMAIN = "domain";
var TYPE_TRANSLATE = {
USER: TYPE_USER,
GROUP: TYPE_GROUP,
CUSTOMER: TYPE_DOMAIN,
};
var STATUS_ACTIVE = "ACTIVE";
var ROLE_OWNER = "owner";
var ROLE_MANAGER = "manager";
var ROLE_MEMBER = "member";
var ROLE_TRANSLATE = {
OWNER: ROLE_OWNER,
MANAGER: ROLE_MANAGER,
MEMBER: ROLE_MEMBER,
};
var WEB_ARCHIV_URL = "https://groups.google.com/a/" + COMPANY_GSUITE_DOMAIN + "/group/%s";
function doGet() {
return HtmlService.createHtmlOutputFromFile('UI')
.setTitle(COMPANY_NAME + ' Google Groups Directory')
.addMetaTag('viewport', 'width=device-width, initial-scale=1');
}
function search(id, type) {
if(type==TYPE_ALL) {
return getAllGroupsResponse(id, type);
}
var email = completeEmail(id);
var entity = searchEntity(email, type);
if(!entity) {
return getErrorResponse("Email address '" + email + "' doesn't exist on the domain.");
}
if(entity.type == TYPE_GROUP) {
return getGroupMembersResponse(email, entity.object);
}
if(entity.type == TYPE_USER) {
return getUserGroupsResponse(email, entity.object);
}
}
function getAllGroupsResponse(id, type) {
var groups = fetchGroups({customer:'my_customer'});
return {
status: true,
type: TYPE_ALL,
groups: groups,
};
}
function getUserGroupsResponse(id, user) {
var groups = fetchGroups({userKey:id});
return {
status: true,
type: TYPE_USER,
groups: groups,
user: user,
};
}
function fetchGroups(filter) {
var groups = [];
var pageToken = null;
do {
var result = AdminDirectory.Groups.list(filter);
filter.pageToken = result.nextPageToken;
if(result.groups) {
groups = groups.concat(result.groups.map(function(group) {
return {
email: group.email,
name: group.name,
description: group.description,
aliases: group.aliases,
};
}));
}
}
while(result.nextPageToken);
return groups;
}
function getGroupMembersResponse(id, group) {
var members = fetchMembers(id);
return {
status: true,
type: TYPE_GROUP,
members: members,
group: group,
};
}
function fetchMembers(email) {
var userEmail = Session.getActiveUser().getEmail();
var members = {
owner: [],
manager: [],
member: [],
};
var pageToken = null;
do {
var result = AdminDirectory.Members.list(email, {pageToken:pageToken});
pageToken = result.nextPageToken;
result.members.forEach(function(member){
var role = ROLE_TRANSLATE[member.role];
var type = TYPE_TRANSLATE[member.type];
var email= member.email ? member.email : null;
members[role].push({
email: email,
type: type,
role: role,
status: member.status == STATUS_ACTIVE,
me: email == userEmail,
});
});
}
while(result.nextPageToken);
return members;
}
function getErrorResponse(error) {
return {
status: false,
type: null,
error: error,
}
}
function getGroupInfo(groupKey) {
var group = AdminDirectory.Groups.get(groupKey);
return {
name: group.name,
email: group.email,
description: group.description,
aliases: group.aliases,
webUrl: WEB_ARCHIV_URL.replace('%s', cropUsername(group.email)),
};
}
function getUserInfo(userKey) {
var user = AdminDirectory.Users.get(userKey);
return {
name: user.name,
email: user.primaryEmail,
photo: user.thumbnailPhotoUrl,
aliases: user.aliases,
active: ! user.suspended,
};
}
function completeEmail(shortEmail) {
if (shortEmail.indexOf('@') == -1) {
var currentUser = Session.getEffectiveUser().getEmail();
var domain = currentUser.substring(currentUser.indexOf('@'));
return shortEmail + domain;
}
return shortEmail;
}
function cropUsername(email) {
return email.substring(0, email.indexOf('@'));
}
function searchEntity(id, type) {
try {
var entity = {
type: type,
object: null,
};
if(type==TYPE_GROUP) {
entity.object = getGroupInfo(id);
}
else if(type==TYPE_USER) {
entity.object = getUserInfo(id);
}
else {
try {
entity.object = getGroupInfo(id);
entity.type=TYPE_GROUP;
}
catch(error){
entity.object = getUserInfo(id);
entity.type=TYPE_USER;
}
}
return entity;
}
catch(error){
Logger.log(error);
return false;
}
}
function getUrl(){
return ScriptApp.getService().getUrl();
}