-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbotkit_redis_storage.js
727 lines (718 loc) · 26.8 KB
/
botkit_redis_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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
var redis = require('redis'); //https://github.com/NodeRedis/node_redis
/*
* All optional
*
* config = {
* namespace: namespace,
* host: host,
* port: port
* }
* // see
* https://github.com/NodeRedis/node_redis
* #options-is-an-object-with-the-following-possible-properties for a full list of the valid options
*/
module.exports = function(config) {
config = config || {};
config.namespace = config.namespace || 'gomez-dogbot:store';
var storage = {};
var client = redis.createClient(config); // could pass specific redis config here
var methods = [
'teams',
'users',
'channels'
];
// the following are all the currently stored dog fields.
// essentially, these are all the fields you should expect
// to use when interacting with a dogObject (basically, a JS object)
// 'setIndexed' controls whether a Redis set is created for this field
// so that you can easily search for dogs via that field
// 'required' controls whether a field is required when creating/saving
// 'treatAs' identified the data "type" and triggers validation of that
// field according to its declared "type".
// boolean, integer, datestring are supported values. any other value is
// treated as a string
var validDogFields = [
{
name: 'birthMonth',
setIndexed: true,
required: false,
treatAs: 'integer',
},
{
name: 'birthDay',
setIndexed: true,
required: false,
treatAs: 'integer',
},
{
name: 'birthYear',
setIndexed: true,
required: false,
treatAs: 'integer',
},
{
name: 'breed',
setIndexed: true,
required: false,
treatAs: 'string',
},
{
name: 'createdBy',
setIndexed: false,
required: false,
treatAs: 'string',
},
{
name: 'goneDate',
setIndexed: true,
required: false,
treatAs: 'datestring',
},
{
name: 'hereDate',
setIndexed: true,
required: false,
treatAs: 'datestring',
},
{
name: 'id',
setIndexed: false,
required: true,
treatAs: 'integer',
},
{
name: 'isGood',
setIndexed: true,
required: false,
treatAs: 'boolean',
},
{
name: 'isBad',
setIndexed: true,
required: false,
treatAs: 'boolean',
},
{
name: 'isHereEveryday',
setIndexed: true,
required: false,
treatAs: 'boolean',
},
{
name: 'isHereMonday',
setIndexed: true,
required: false,
treatAs: 'boolean',
},
{
name: 'isHereTuesday',
setIndexed: true,
required: false,
treatAs: 'boolean',
},
{
name: 'isHereWednesday',
setIndexed: true,
required: false,
treatAs: 'boolean',
},
{
name: 'isHereThursday',
setIndexed: true,
required: false,
treatAs: 'boolean',
},
{
name: 'isHereFriday',
setIndexed: true,
required: false,
treatAs: 'boolean',
},
{
name: 'isHereSaturday',
setIndexed: true,
required: false,
treatAs: 'boolean',
},
{
name: 'isHereSunday',
setIndexed: true,
required: false,
treatAs: 'boolean',
},
{
name: 'location',
setIndexed: true,
required: false,
treatAs: 'string',
},
{
name: 'name',
setIndexed: true,
required: true,
treatAs: 'string',
},
{
name: 'owner',
setIndexed: false,
required: false,
treatAs: 'string',
},
{
name: 'gender',
setIndexed: true,
required: false,
treatAs: 'boolean', // 1 = male, 0 = female
},
];
// Implements required slack botkit API methods and gomez-dogbot related API methods
for (var i = 0; i < methods.length; i++) {
storage[methods[i]] = function(slackEntity) {
var slackDataNamespace = config.namespace + ':slackdata:' + slackEntity;
var dogNamespace = config.namespace + ':dogdata:' + slackEntity;
var specialDogIdSequenceKey = '__dogIdSequence';
var specialDogNameSetKey = '__dogsByName';
var dogPicsSetKey = '__dogPics';
return {
// THESE FUNCTIONS ARE REQUIRED FOR BOTKIT-SLACK PERSISTENCE INTEGRATION
// DO NOT CHANGE
get: function(id, cb) {
client.hget(slackDataNamespace, id, function(err, res) {
cb(err, JSON.parse(res));
});
},
save: function(object, cb) {
if (!object.id) // Silently catch this error?
return cb(new Error('The given object must have an id property'), {});
client.hset(slackDataNamespace, object.id, JSON.stringify(object), cb);
},
all: function(cb, options) {
client.hgetall(slackDataNamespace, function(err, res) {
if (err)
return cb(err, {});
if (null === res)
return cb(err, res);
var parsed;
var array = [];
for (var i in res) {
parsed = JSON.parse(res[i]);
res[i] = parsed;
array.push(parsed);
}
cb(err, options && options.type === 'object' ? res : array);
});
},
allById: function(cb) {
this.all(cb, {type: 'object'});
},
// END BOTKIT-SLACK PERSISTENCE INTEGRATION FUNCTIONS
// BEGIN DOGBOT SPECIFIC PERSISTENCE FUNCTIONS
// makeDogKey: helper function to generate Redis keys for getting your data
makeDogKey: function(slackEntityId, type, key) {
return dogNamespace + ':' + slackEntityId + ':' + type + ':' + key;
},
// getDog: given a dogObject, gets the associated data for that dog
getDog: function(dogObject, slackEntityId, cb) {
var localThis = this;
client.hgetall(localThis.makeDogKey(slackEntityId, 'data', dogObject.id), function(err, res) {
if (err) {
return cb(new Error(err), {});
}
else {
var dog = res;
localThis.getRandPicForDog(dogObject, slackEntityId, function (err, res) {
if (err) {
return cb(new Error(err), {});
}
else {
if (null !== res) {
dog._imageURL = res;
}
return cb(null, dog);
}
});
}
});
},
// cleanUpSpecialNameSet: all dogs are identified by a unique integer ID
// to find them by name instead, a special Redis set is maintained
// this function makes sure the set is up to date. this function should
// ALWAYS be called whenever a dog is deleted or its name is updated
// if you are using the saveDog or deleteDog functions, this should already
// happen
cleanUpSpecialNameSet: function(name, slackEntityId, cb) {
var localThis = this;
if (null === name) {
return cb(null, null);
}
client.smembers(localThis.makeDogKey(slackEntityId, 'sets', 'name:' + name), function (err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not clean up special dog name set!'), {});
}
else {
if (res.length == 0) {
// nothing in that set, remove this extinct dog name from our specialset
client.srem(localThis.makeDogKey(slackEntityId, 'special', specialDogNameSetKey), name, cb);
}
else {
// nothing to do!
return cb(null, null);
}
}
});
},
// saveDog: main function for saving changes to an existing dog.
// takes a dogObject
saveDog: function(dogObject, slackEntityId, cb) {
var localThis = this;
// this function supports saving any number of fields. if it's in the object
// it will be saved, if not in the object, it won't be touched. if it's in the object
// but is null, it will be cleared.
if (!dogObject.id) {
return cb(new Error('Dog does not have an ID! (use addDog for new dogs)'), {});
}
else {
localThis.getDog(dogObject, slackEntityId, function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not retrieve dog ID '+ dogObject.id), {});
}
else { //main save logic
if (null === res && !dogObject._newDog) {
return cb(new Error('Dog ID "'+ dogObject.id + '" does not exist!'), {});
}
else if (null !== res && dogObject._newDog) {
return cb(new Error('Dog ID "'+ dogObject.id + '" is already in use!'), {});
}
else { // id checks out
var oldData = res;
var hmsetArray = [];
var hdelArray = [];
var setsToPopulate = [];
var setsToRemove = [];
for (var fieldIndex = 0; fieldIndex < validDogFields.length; fieldIndex++) {
var field = validDogFields[fieldIndex];
if (dogObject.hasOwnProperty(field.name)) {
// if the passed field exists but is null,
// we want to clear that field
// otherwise, process as usual
if (null !== dogObject[field.name]) {
// validate data conforms to expectations
if (field.treatAs == 'boolean') {
// standardize to 1 or 0
if (dogObject[field.name] && dogObject[field.name] != 0) {
dogObject[field.name] = 1;
}
else {
dogObject[field.name] = 0;
}
}
else if (field.treatAs == 'integer') {
if (isNaN(dogObject[field.name])) {
return cb(new Error(field.name + ' is not of type ' + field.treatAs), {});
}
else {
dogObject[field.name] = parseInt(dogObject[field.name], 10);
}
}
else if (field.treatAs == 'datestring') {
// make sure string is parseable as date
// but null is explicitly allowed
var dts = Date.parse(dogObject[field.name]);
if (isNaN(dts)) {
return cb(new Error(field.name + ' is not a parseable date string ('+dogObject[field.name]+')'), {});
}
else {
// store all dates as ISO strings
// this also means they will be returned as such
var dt = new Date(dts);
dogObject[field.name] = dt.toISOString();
}
}
}
else {
// if we are creating a new dog, we don't want nulls in the object at all (they serve no purpose)
// and we don't need to bother trying to process it
if (dogObject._newDog) {
delete dogObject[field.name];
continue;
}
}
// only attempt to edit set data if something is changing (or is being populated)
if (dogObject._newDog ||
(!oldData.hasOwnProperty(field.name) ||
(oldData.hasOwnProperty(field.name) && oldData[field.name] != dogObject[field.name]))) {
if (null === dogObject[field.name]) {
hdelArray.push(field.name);
}
else {
hmsetArray.push(field.name, dogObject[field.name]);
}
if (field.setIndexed) {
if (!dogObject._newDog &&
oldData.hasOwnProperty(field.name) &&
(null !== oldData[field.name] && "undefined" !== typeof oldData[field.name])) {
setsToRemove.push(field.name + ':' + oldData[field.name]);
}
if (null !== dogObject[field.name] && "undefined" !== typeof dogObject[field.name]) {
setsToPopulate.push(field.name + ':' + dogObject[field.name]);
}
else {
if (field.required) {
return cb(new Error(field.name + ' is required when saving dog data!'), {});
}
}
}
}
}
else {
if (field.required) {
return cb(new Error(field.name + ' is required when saving dog data!'), {});
}
}
}
var multi = client.multi();
var multiCommonError = function (err, res) {
if (err) {
console.log('MULTI COMMAND FAIL');
console.log(err);
multi.discard();
return cb(new Error('Could not save dog data!'), {});
}
};
if (hdelArray.length > 0) {
multi.hdel(localThis.makeDogKey(slackEntityId, 'data', dogObject.id), hdelArray, multiCommonError);
}
if (hmsetArray.length > 0) {
multi.hmset(localThis.makeDogKey(slackEntityId, 'data', dogObject.id), hmsetArray, multiCommonError);
}
for (var i = 0; i < setsToPopulate.length; i++) {
multi.sadd(localThis.makeDogKey(slackEntityId, 'sets', setsToPopulate[i]), dogObject.id, multiCommonError);
}
for (var i = 0; i < setsToRemove.length; i++) {
multi.srem(localThis.makeDogKey(slackEntityId, 'sets', setsToRemove[i]), dogObject.id, multiCommonError);
}
// special case, we want a set of extant dog names, so we can easily find what names
// exist in our database without having to run a 'keys *' command.
// by now, we have updated the set which maps name to dog ID. if we are changing a dog
// name (or adding a new one), we should update this special set appropriately
// if the name is already there, that's fine, redis handles set uniqueness
multi.sadd(localThis.makeDogKey(slackEntityId, 'special', specialDogNameSetKey), dogObject.name, multiCommonError);
multi.exec();
var nameToCleanUp = null;
if (oldData && oldData.name) {
nameToCleanUp = oldData.name;
}
localThis.cleanUpSpecialNameSet(nameToCleanUp, slackEntityId, function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not clean up set data!'), {});
}
else {
return cb(null, dogObject);
}
});
}
}
});
}
},
// addDog: main function for creating dogs
// takes a dobObject
addDog: function(dogObject, slackEntityId, cb) {
var localThis = this;
if (!dogObject.id) {
client.incr(localThis.makeDogKey(slackEntityId, 'special', specialDogIdSequenceKey), function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not get new ID for dog!'), {});
}
else {
dogObject.id = res;
dogObject._newDog = true;
// all dogs start as good if not otherwise specified
if (!dogObject.isBad) {
dogObject.isGood = true;
dogObject.isBad = false;
}
localThis.saveDog(dogObject, slackEntityId, function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not get save dog!'), {});
}
else {
console.log('Created dog '+res.id);
return cb(null, res);
}
});
}
});
}
else {
return cb(new Error('Dog already has an ID! (use saveDog for existing dogs)'), {});
}
},
// deleteDog: main function for deleting dogs
// takes a dogObject
deleteDog: function(dogObject, slackEntityId, cb) {
var localThis = this;
localThis.getDog(dogObject, slackEntityId, function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not delete dog data!'), {});
}
else {
if (null === res) {
return cb(new Error('Dog ID "'+ dogObject.id + '" does not exist!'), {});
}
else {
var dogToDelete = res;
var multi = client.multi();
var multiCommonError = function (err, res) {
if (err) {
console.log(err);
multi.discard();
return cb(new Error('Could not delete dog data!'), {});
}
};
multi.del(localThis.makeDogKey(slackEntityId, 'data', dogToDelete.id), multiCommonError);
for (var fieldIndex = 0; fieldIndex < validDogFields.length; fieldIndex++) {
var field = validDogFields[fieldIndex];
if (field.setIndexed && dogToDelete[field.name]) {
multi.srem(localThis.makeDogKey(slackEntityId, 'sets', field.name + ':' + dogToDelete[field.name]), dogToDelete.id, multiCommonError);
}
}
multi.del(localThis.makeDogKey(slackEntityId, 'sets', dogPicsSetKey + ':' + dogToDelete.id), multiCommonError);
multi.exec(); //error handling?
localThis.cleanUpSpecialNameSet(dogToDelete.name, slackEntityId, cb);
}
}
});
},
// below, basic functions for handling storage and retrieval of dog pictures
// pics are stored as simple URLs
addPicToDog: function(dogObject, imageURL, slackEntityId, cb) {
client.sadd(this.makeDogKey(slackEntityId, 'sets', dogPicsSetKey + ':' + dogObject.id), imageURL, cb);
},
deletePicForDog: function(dogObject, imageURL, slackEntityId, cb) {
client.srem(this.makeDogKey(slackEntityId, 'sets', dogPicsSetKey + ':' + dogObject.id), imageURL, cb);
},
checkPicForDog: function(dogObject, imageURL, slackEntityId, cb) {
client.sismember(this.makeDogKey(slackEntityId, 'sets', dogPicsSetKey + ':' + dogObject.id), imageURL, cb);
},
getPicsForDog: function(dogObject, slackEntityId, cb) {
client.smembers(this.makeDogKey(slackEntityId, 'sets', dogPicsSetKey + ':' + dogObject.id), cb);
},
getRandPicForDog: function(dogObject, slackEntityId, cb) {
client.srandmember(this.makeDogKey(slackEntityId, 'sets', dogPicsSetKey + ':' + dogObject.id), cb);
},
// getDogsFromIdList: given a list of dogIds, gets an array of dogObjects for you
getDogsFromIdList: function(dogIds, slackEntityId, cb) {
var localThis = this;
if (dogIds.length > 0) {
var script = "local res={}; for i, name in ipairs(KEYS) do local dog = redis.call('hgetall','"+localThis.makeDogKey(slackEntityId, 'data', '')+"'..name); table.insert(res, dog); end return res;"
// current support for EVAL is pretty crap, have to use this syntax to get it to work
var args = [script, dogIds.length].concat(dogIds);
client.eval(args, function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not get dogs from eval!'), {});
}
else {
// since we are using eval, we are getting back an array of arrays (of key value pairs)
// we want to convert to array of objects
var dogObjects = [];
for (var i=0; i < res.length; i++) {
var dogArray = res[i];
var dogObject = {};
for (var j=0; j < dogArray.length; j+=2) {
dogObject[dogArray[j].toString('binary')] = dogArray[j+1];
}
dogObjects.push(dogObject);
}
return cb(null, dogObjects);
}
});
}
else {
return cb(null, []);
}
},
// getAllMatchingDogsBySetMultiple: use this function to search for dogs using a particular indexed field
// supports one search field (string) and multiple search values (array)
// returns an array of dogObjects
getAllMatchingDogsBySetMultiple: function(setName, matchValues, slackEntityId, cb) {
var localThis = this;
if (matchValues && matchValues.length > 0) {
var unionKeys = [];
for (var i=0; i < matchValues.length; i++) {
unionKeys.push(localThis.makeDogKey(slackEntityId, 'sets', setName + ':' + matchValues[i]));
}
client.sunion(unionKeys, function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not get set union!'), null);
}
else {
return localThis.getDogsFromIdList(res, slackEntityId, cb);
}
});
}
else {
return cb(null, []);
}
},
// getAllMatchingDogsBySetPairwiseMultiple: use this function to search for dogs using multiple indexed fields
// and search values. the search parameters should be passed in as an array of objects where each object
// has a 'key' and 'value' property. search will be treated as an AND search (i.e. only dogs that match
// all parameters will be returned).
// returns an array of dogObjects
getAllMatchingDogsBySetPairwiseMultiple: function(searchParameters, slackEntityId, cb) {
var localThis = this;
if (searchParameters && searchParameters.length > 0) {
var interKeys = [];
for (var i=0; i < searchParameters.length; i++) {
var search = searchParameters[i];
if (search.hasOwnProperty('key') && null !== search.key &&
search.hasOwnProperty('value') && null !== search.value) {
interKeys.push(localThis.makeDogKey(slackEntityId, 'sets', search.key + ':' + search.value));
}
}
if (interKeys.length > 0) {
client.sinter(interKeys, function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not get set union!'), null);
}
else {
return localThis.getDogsFromIdList(res, slackEntityId, cb);
}
});
}
else {
return cb(null, []);
}
}
else {
return cb(null, []);
}
},
// getAllMatchingDogsBySet: use this function to search for dogs using a particular indexed field
// supports one search field (string) and one search value (string)
// returns an array of dogObjects
getAllMatchingDogsBySet: function(setName, matchValue, slackEntityId, cb) {
var localThis = this;
client.smembers(localThis.makeDogKey(slackEntityId, 'sets', setName + ':' + matchValue), function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not get dogs!'), {}); }
else {
return localThis.getDogsFromIdList(res, slackEntityId, cb);
}
});
},
// getDogNames: returns an array of all the dog names (strings) in the DB (according to the special name set)
getDogNames: function(slackEntityId, cb) {
client.smembers(this.makeDogKey(slackEntityId, 'special', specialDogNameSetKey), cb);
},
// getRandDog: get a random dog! supports filters on indexed fields (i.e. get a random good dog, get a random dog born in October)
// the filters parameter should be passed in as an array of objects where each object
// has a 'key' and 'value' property. filters will be treated as an AND search (i.e. only dogs that satisfy
// all parameters will be returned).
// if filters is not passed, all dogs are fair game.
// returns a dogObject
getRandDog: function(slackEntityId, filters, cb) {
localThis = this;
if (filters && filters.length > 0) {
var interKeys = [];
for (var i=0; i < filters.length; i++) {
var filter = filters[i];
if (filter.hasOwnProperty('key') && null !== filter.key &&
filter.hasOwnProperty('value') && null !== filter.value) {
interKeys.push(localThis.makeDogKey(slackEntityId, 'sets', filter.key + ':' + filter.value));
}
}
if (interKeys.length > 0) {
client.sinter(interKeys, function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not get set intersection!'), null);
}
else {
if (res.length > 0) {
var dogId = res[Math.floor(Math.random()*res.length)];
return localThis.getDog({id: dogId}, slackEntityId, cb);
}
else {
return cb(null, null);
}
}
});
}
else {
return cb(null, null);
}
}
else {
// get any random dog
localThis.getDogNames(slackEntityId, function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not get dog names!'), null);
}
else {
if (res.length > 0) {
var dogName = res[Math.floor(Math.random()*res.length)];
localThis.getAllMatchingDogsBySet('name', dogName, slackEntityId, function (err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not get dogs by name!'), null);
}
else {
if (res.length > 0) {
var dog = res[Math.floor(Math.random()*res.length)];
return cb(null, dog);
}
else {
return cb(null, null);
}
}
});
}
else {
return cb(null, null);
}
}
});
}
},
// getDogsHereOnDate: given a datetime, gets all the dogs that are marked as "here" on that date
// returns an array of dogObjects
getDogsHereOnDate: function(slackEntityId, dt, cb) {
localThis = this;
dt.setHours(0,0,0,0);
var weekDays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
// dogs that are here today are basically the union of:
// dogs here on this exact day,
// dogs here on this week day,
// dogs here every day
var hereKeys = [];
hereKeys.push(localThis.makeDogKey(slackEntityId, 'sets', 'hereDate:'+dt.toISOString()));
hereKeys.push(localThis.makeDogKey(slackEntityId, 'sets', 'isHere'+weekDays[dt.getDay()]+':1'));
hereKeys.push(localThis.makeDogKey(slackEntityId, 'sets', 'isHereEveryday:1'));
client.sunion(hereKeys, function(err, res) {
if (err) {
console.log(err);
return cb(new Error('Could not get dogs that are here!'), null);
}
else {
return localThis.getDogsFromIdList(res, slackEntityId, cb);
}
});
}
};
}(methods[i]);
}
return storage;
};