-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.js
151 lines (132 loc) · 3.79 KB
/
storage.js
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
const deap = require('deap')
const Persist = require('beepboop-persist')
const Teams = require('./teams')
module.exports = function (config) {
config = config || {}
var persist = Persist(config)
var bbTeams = Teams(config)
var storage = {
teams: {
get (teamId, cb) {
if (!teamId) {
return cb(new Error('Missing teamId in storage.teams.get()'))
}
// load slack team data from teams api and overlay it onto any data stored in persist
bbTeams.get(teamId, (err, team) => {
if (err) {
return cb(err)
}
if (!team) {
return cb(new Error(`No team found for team id ${teamId}`))
}
persist.get(teamKey(teamId), (err, teamData) => {
if (err) {
return cb(err)
}
// overlay botkit team onto custom team meta-data
cb(null, deap(teamData || {}, bbTeamToBotkitTeam(team)))
})
})
},
// Stores additional team metadata in persist
save (team, cb) {
if (!team || !team.id) {
return cb(new Error('Missing team.id in storage.teams.save()'))
}
persist.set(teamKey(team.id), team, cb)
},
// Deletes team data in persist
delete (teamId, cb) {
persist.del(teamKey(teamId), cb)
},
all (cb) {
// TODO: currently this will only grab the first 1000 teams - need to make paginated calls
// First get all of the installed teams from Beep Boop
bbTeams.list((err, teams) => {
if (err) {
return cb(err)
}
let teamKeys = teams.map(team => teamKey(team.slack_team_id))
// Next get any metadata saved for each team
persist.mget(teamKeys, (err, teamsData) => {
if (err) {
return cb(err)
}
// merge team metadata w/ beep boop teams and transform to botkit team
// indexes between two should be the same
let botkitTeams = teamsData.map((teamData, idx) => {
return deap(teamData || {}, bbTeamToBotkitTeam(teams[idx]))
})
cb(null, botkitTeams)
})
})
}
},
users: {
get (userId, cb) {
persist.get(userKey(userId), cb)
},
save (user, cb) {
persist.set(userKey(user.id), user, cb)
},
delete (userId, cb) {
persist.del(userKey(userId), cb)
},
all (cb) {
// get all the user keys
persist.list('users/', (err, keys) => {
if (err) {
return cb(err)
}
persist.mget(keys, cb)
})
}
},
channels: {
get (channelId, cb) {
persist.get(channelKey(channelId), cb)
},
save (channel, cb) {
persist.set(channelKey(channel.id), channel, cb)
},
delete (channelId, cb) {
persist.del(channelKey(channelId), cb)
},
all (cb) {
// get all the channel keys
persist.list('channels/', (err, keys) => {
if (err) {
return cb(err)
}
persist.mget(keys, cb)
})
}
}
}
return storage
}
function teamKey (id) {
return `teams:${id}`
}
function userKey (id) {
return `users:${id}`
}
function channelKey (id) {
return `channels:${id}`
}
// transform beep boop team into what botkit expects
function bbTeamToBotkitTeam (team) {
return {
id: team.slack_team_id,
createdBy: team.slack_user_id,
url: `https://${team.slack_team_domain}.slack.com/`,
name: team.slack_team_name,
bot: {
name: team.slack_bot_user_name,
token: team.slack_bot_access_token,
user_id: team.slack_bot_user_id,
createdBy: team.slack_user_id,
app_token: team.slack_access_token
}
}
}