slackManager.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const Slack = require('./slack.js').Slack
  2. ,INACTIVE_DELAY = 120000
  3. ,PING_DELAY = 600000
  4. ,RTM_PING_DELAY = 15000
  5. ;
  6. function SlackManager() {
  7. this.cache = require('memory-cache');
  8. }
  9. var instance = new SlackManager();
  10. setInterval(function() {
  11. var keys = instance.cache.keys();
  12. keys.forEach(function(instKey) {
  13. instance.cache.get(instKey).ping();
  14. });
  15. }, PING_DELAY);
  16. SlackManager.prototype.getAuthScope = function() {
  17. return [
  18. "identity.basic",
  19. "identity.email"
  20. ];
  21. };
  22. setInterval(function() {
  23. var keys = instance.cache.keys();
  24. keys.forEach(function(instKey) {
  25. instance.cache.get(instKey).rtmPing();
  26. });
  27. }, RTM_PING_DELAY);
  28. SlackManager.prototype.getScope = function() {
  29. return [
  30. "client"
  31. ];
  32. };
  33. SlackManager.prototype.lazyGet = function(servId, slackToken, reqT) {
  34. var key = "SLACK_" +servId,
  35. val = this.cache.get(key);
  36. if (val) {
  37. this.cache.put(key, val, INACTIVE_DELAY, function(key, val) {
  38. instance.removed(key, val);
  39. });
  40. } else {
  41. val = new Slack(slackToken, this);
  42. this.cache.put(key, val, INACTIVE_DELAY, function(key, val) {
  43. instance.removed(key, val);
  44. val.close();
  45. });
  46. }
  47. return val;
  48. };
  49. SlackManager.prototype.removed = function(key, slack) {
  50. slack.close();
  51. };
  52. SlackManager.prototype.suicide = function(slack) {
  53. this.cache.del("SLACK_" +slack.sessId);
  54. };
  55. module.exports.SlackManager = instance;