-
Notifications
You must be signed in to change notification settings - Fork 2
/
appHome.mjs
55 lines (52 loc) · 2.18 KB
/
appHome.mjs
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
import {SlackProfileManager} from "./SlackProfileManager.mjs"
// Called when user opens the app - return a view of available profiles
export const createHome = async(profileConfig, userEmail) => {
if(!profileConfig.profiles){
return;
}
// Note: iterate profiles might be better solution here as it can flag the groups with the same name.
// Note: though the method used below is using less API calls
const profileManager = new SlackProfileManager()
const userWithGroups = await profileManager.lookupUserGroupByEmail(userEmail);
const view = {
"type": "home",
"blocks": [
{
type: "section",
text: {
type: "mrkdwn",
text: "*Welcome!* \nUsing this tool you can change your group profiles.\n*Please note all changes can typically take at least 20 seconds to propagate.*"
}
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: "<https://github.com/Twingate-Labs/tg-group-profile-manager|GitHub> and <https://github.com/Twingate-Labs/tg-group-profile-manager/blob/main/README.md|User Guide>"
}
]
}
]
}
if (!userWithGroups) {
console.log(`Email '${userEmail}' not found in Twingate`)
view.blocks.push({
type: "divider"
},
{
type: "section",
"text": {
"type": "mrkdwn",
"text": `*ERROR*\n Email '${userEmail}' not found in Twingate, please ensure your Slack account email address is the same as your Twingate email address.`
}
})
return view
}
const permittedProfiles = profileConfig.profiles.filter(profile => userWithGroups.groups.map(group=>group.name).includes(profile.applicableToGroup))
for (const permittedProfile of permittedProfiles){
const block = await permittedProfile.getAppHomeBlock(userWithGroups);
if ( block != null ) view.blocks.push({type: "divider"}, block);
}
return view
};