-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathkarma.js
More file actions
302 lines (284 loc) · 10.4 KB
/
Copy pathkarma.js
File metadata and controls
302 lines (284 loc) · 10.4 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Description:
// A simple karma tracking script for hubot.
//
// Commands:
// karma <name> - shows karma for the named user
//
// Notes
// <name>++ - adds karma to a user
// <name>-- - removes karma from a user
// Adaptado por @clsource Camilo Castro
// Basado en
// https://www.npmjs.com/package/hubot-karma
//
// Author
// @clsource
const theme = require('./theme.js')
const exceptions = ['c', 'C']
module.exports = robot => {
const hubotHost = process.env.HEROKU_URL || process.env.HUBOT_URL || 'http://localhost:8080'
const hubotWebSite = `${hubotHost}/${robot.name}`
const getCleanName = name => `${name[0]}.${name.substr(1)}`
const userForMentionName = mentionName => {
const users = robot.brain.users()
return Object.keys(users)
.map(key => users[key])
.find(user => mentionName === user.mention_name)
}
const userFromWeb = token => {
return robot.adapter.client.web.users.list().then(users => {
const localUsers = robot.brain.users()
const user1 = users.members.find(x => x.name === token)
if (!user1) return
return localUsers[user1.id]
})
}
const usersForToken = token => {
return new Promise((resolve, reject) => {
let user
if ((user = robot.brain.userForName(token))) {
return resolve([user])
}
if ((user = userForMentionName(token))) {
return resolve([user])
}
if (robot.adapter.constructor.name === 'SlackBot') {
userFromWeb(token)
.then(webUser => {
if (webUser) {
return resolve([webUser])
} else {
return resolve(robot.brain.usersForFuzzyName(token))
}
})
.catch(reject)
} else {
user = robot.brain.usersForFuzzyName(token)
resolve(user)
}
})
}
const userForToken = (token, response) => {
return usersForToken(token).then(users => {
let user
if (users.length === 1) {
user = users[0]
} else if (users.length > 1) {
robot.messageRoom(
`@${response.message.user.name}`,
`Se más específico, hay ${users.length} personas que se parecen a: ${users
.map(user => user.name)
.join(', ')}.`
)
} else {
response.send(`Chaucha, no encuentro al usuario '${token}'.`)
}
return user
})
}
const canUpvote = (user, victim) => {
karmaLimits = robot.brain.get('karmaLimits') || {}
karmaLimits[user.id] = karmaLimits[user.id] || {}
if (!karmaLimits[user.id][victim.id]) {
karmaLimits[user.id][victim.id] = new Date()
robot.brain.set('karmaLimits', karmaLimits)
robot.brain.save()
return true
} else {
const limit1 = robot.golden.isGold(user.name) ? 15 : 60
const limit2 = limit1 - 1
const oldDate = karmaLimits[user.id][victim.id]
const timePast = Math.round(new Date().getTime() - oldDate.getTime()) / 60000
if (timePast > limit2) {
karmaLimits[user.id][victim.id] = new Date()
robot.brain.set('karmaLimits', karmaLimits)
robot.brain.save()
return true
} else {
return Math.floor(limit1 - timePast)
}
}
}
const applyKarma = (userToken, op, response) => {
if (exceptions.indexOf(userToken) < 0) {
const thisUser = response.message.user
userForToken(userToken, response)
.then(targetUser => {
if (!targetUser) return
if (thisUser.name === targetUser.name && op !== '--')
return response.send('¡Oe no po, el karma es pa otros no pa ti!')
if (targetUser.length === '') return response.send('¡Oe no seai pillo, escribe un nombre!')
const limit = canUpvote(thisUser, targetUser)
if (Number.isFinite(limit)) {
return response.send(`¡No abuses! Intenta en ${limit} minutos.`)
}
const modifyingKarma = op === '++' ? 1 : -1
const karmaLog = robot.brain.get('karmaLog') || []
karmaLog.push({
name: thisUser.name,
id: thisUser.id,
karma: modifyingKarma,
targetName: targetUser.name,
targetId: targetUser.id,
date: Date.now(),
msg: response.envelope.message.text
})
robot.brain.set('karmaLog', karmaLog)
robot.brain.save()
response.send(`${getCleanName(targetUser.name)} ahora tiene ${getUserKarma(targetUser.id)} puntos de karma.`)
})
.catch(err => robot.emit('error', err, response, 'karma'))
}
}
const getUserKarma = userId => {
const karmaLog = robot.brain.get('karmaLog') || []
return karmaLog.reduce((prev, curr) => {
if (curr.targetId === userId) {
prev += curr.karma
}
return prev
}, 0)
}
const removeURLFromTokens = (tokens, message) => {
const urls = message.match(/\bhttps?:\/\/\S+/gi)
if (!urls) return tokens
// if a token match with a URL, it gets remove
return tokens.filter(token => urls.reduce((acc, url) => acc && url.indexOf(token) === -1, true))
}
// Sometimes we want to use code that contains ++ or --. This function allow us
// to find the text that is inside code blocks so we can compare and avoid false positives
// in karma assignation.
const findTextInsideCodeBlocks = message => {
const regex = /`(.*?)`/g
let matches = []
let match
while ((match = regex.exec(message)) !== null) {
matches.push(match[0])
}
return matches
}
robot.hear(/([a-zA-Z0-9-_\.]|[^\,\-\s\+$!(){}"'`~%=^:;#°|¡¿?]+?)(\b\+{2}|-{2})([^,]?|\s|$)/g, response => {
stripRegex = /~!@#$`%^&*()|\=?;:'",<>\{\}/gi
const tokens = removeURLFromTokens(response.match, response.message.text)
if (!tokens) return
if (robot.adapter.constructor.name === 'SlackBot') {
if (!robot.adapter.client.rtm.dataStore.getChannelGroupOrDMById(response.envelope.room).is_channel) return
}
const codeBlocks = findTextInsideCodeBlocks(response.message.text)
tokens
.filter(token => {
// If the token match with a codeBlock it is remove from the karma assignation
return codeBlocks.filter(codeBlock => codeBlock.includes(token)).length === 0
})
.slice(0, 5)
.map(token => {
const opRegex = /(\+{2}|-{2})/g
const specialChars = /@/
return {
userToken: token
.trim()
.replace(specialChars, '')
.replace(opRegex, ''),
op: token.match(opRegex)[0]
}
})
.filter(karma => karma.userToken && karma.userToken !== '')
.forEach(karma => {
applyKarma(karma.userToken, karma.op, response)
})
})
robot.hear(/^karma(?:\s+@?(.*))?$/, response => {
if (!response.match[1]) return
const targetToken = response.match[1].trim()
if (['todos', 'all'].includes(targetToken.toLowerCase())) {
response.send(`Karma de todos: ${hubotWebSite}/karma/todos`)
} else if (targetToken.toLowerCase().split(' ')[0] === 'reset') {
const thisUser = response.message.user
if (thisUser.name.toLowerCase() !== 'hector') {
return response.send('Tienes que ser :hector: para realizar esta función.')
}
const resetCommand = targetToken.toLowerCase().split(' ')[1]
if (!resetCommand) return
if (['todos', 'all'].includes(resetCommand)) {
robot.brain.set('karmaLog', [])
response.send('Todo el mundo ha quedado libre de toda bendición o pecado.')
robot.brain.save()
} else {
userForToken(resetCommand, response).then(targetUser => {
const karmaLog = robot.brain.get('karmaLog') || []
const filteredKarmaLog = karmaLog.filter(item => item.targetId !== targetUser.id)
robot.brain.set('karmaLog', filteredKarmaLog)
response.send(`${getCleanName(targetUser.name)} ha quedado libre de toda bendición o pecado.`)
robot.brain.save()
})
}
} else {
userForToken(targetToken, response).then(targetUser => {
if (!targetUser) return
response.send(
`${getCleanName(targetUser.name)} tiene ${getUserKarma(
targetUser.id
)} puntos de karma. Más detalles en: ${hubotWebSite}/karma/log/${targetUser.name}`
)
})
}
})
robot.router.get(`/${robot.name}/karma/todos`, (req, res) => {
const karmaLog = robot.brain.get('karmaLog') || []
const liKarma = Array.from(
karmaLog
// Suma el karma por usuarios
.reduce((acc, { karma, targetId }) => {
acc.set(targetId, (acc.get(targetId) || 0) + karma)
return acc
}, new Map())
)
// Ordena de mayor a menor el karma
.sort((a, b) => {
if (a[1] < b[1]) {
return 1
} else if (a[1] > b[1]) {
return -1
} else {
return 0
}
})
// Transform el karma a li. Deja fuera a los usuarios con karma 0
.reduce((acc, [targetId, karma]) => {
if (karma !== 0) {
acc += `<li><strong>${robot.brain.userForId(targetId).name}</strong>: ${karma}</li>`
}
return acc
}, '')
res.setHeader('content-type', 'text/html')
res.end(theme('Karma Todos', 'Listado de karma de usuarios devsChile', liKarma))
})
robot.router.get(`/${robot.name}/karma/log`, (req, res) => {
const karmaLog = robot.brain.get('karmaLog') || []
const processedKarmaLog = karmaLog.map(line => {
if (typeof line !== 'string') {
line = `${line.name} le ha dado ${line.karma} karma a ${line.targetName} - ${new Date(line.date).toJSON()}`
}
return line
})
res.setHeader('content-type', 'text/html')
res.end(theme('Karma Todos', 'Karmalog:', `<li>${processedKarmaLog.join('</li><li>')}</li>`))
})
robot.router.get(`/${robot.name}/karma/log/:user`, (req, res) => {
const karmaLog = robot.brain.get('karmaLog') || []
const filteredKarmaLog = karmaLog.filter(log => {
if (typeof log !== 'string' && log.msg) {
return log.targetName === req.params.user
}
})
const processedKarmaLog = filteredKarmaLog.map(log => `${new Date(log.date).toJSON()} - ${log.name}: ${log.msg}`)
let msg
if (filteredKarmaLog.length > 0) {
msg = `<li>${processedKarmaLog.join('</li><li>')}</li>`
} else {
msg = `<li>No hay detalles sobre el karma de ${req.params.user}</li>`
}
res.setHeader('content-type', 'text/html')
res.end(theme('Karma Todos', 'Karmalog:', msg))
})
}