slackManager.js 2.0 KB

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