|
|
@@ -37,7 +37,6 @@ const SLACK_ENDPOINT = "https://slack.com/api/"
|
|
|
}
|
|
|
}
|
|
|
,HISTORY_LENGTH = 35
|
|
|
- ,INACTIVE_DELAY = 120000
|
|
|
|
|
|
,UPDATE_LIVE = [
|
|
|
"message"
|
|
|
@@ -50,30 +49,15 @@ const SLACK_ENDPOINT = "https://slack.com/api/"
|
|
|
] // Message type that affect live history
|
|
|
;
|
|
|
|
|
|
-var
|
|
|
- SLACK_SESSIONS = []
|
|
|
-;
|
|
|
-
|
|
|
-setInterval(function() {
|
|
|
- var t = Date.now();
|
|
|
- SLACK_SESSIONS.forEach(function(slackInst) {
|
|
|
- if (!slackInst.closeIfInnactive(t) && slackInst.active)
|
|
|
- slackInst.sendActive();
|
|
|
- });
|
|
|
-}, 60000);
|
|
|
-
|
|
|
-function Slack(sess) {
|
|
|
+function Slack(sess, manager) {
|
|
|
this.token = sess.slackToken;
|
|
|
+ this.sessId = sess.id;
|
|
|
+ this.manager = manager;
|
|
|
this.rtm = null;
|
|
|
this.data = new SlackData(this);
|
|
|
this.history = {};
|
|
|
this.connected = false;
|
|
|
- this.active = true;
|
|
|
- this.lastPing = Date.now();
|
|
|
-}
|
|
|
-
|
|
|
-Slack.prototype.onUserInterract = function(t) {
|
|
|
- this.lastPing = Math.max(t, this.lastPing);
|
|
|
+ this.closing = false;
|
|
|
}
|
|
|
|
|
|
Slack.prototype.onRequest = function(knownVersion, cb) {
|
|
|
@@ -282,7 +266,6 @@ Slack.prototype.connectRtm = function(url, cb) {
|
|
|
}
|
|
|
if (!_this.connected) {
|
|
|
_this.connected = true;
|
|
|
- SLACK_SESSIONS.push(_this);
|
|
|
}
|
|
|
_this.onMessage(JSON.parse(msg), Date.now());
|
|
|
});
|
|
|
@@ -290,8 +273,6 @@ Slack.prototype.connectRtm = function(url, cb) {
|
|
|
_this.connected = false;
|
|
|
console.error(e);
|
|
|
_this.close();
|
|
|
- var arrIndex = SLACK_SESSIONS.indexOf(_this);
|
|
|
- if (arrIndex >= 0) SLACK_SESSIONS.splice(arrIndex, 1);
|
|
|
});
|
|
|
this.rtm.once("end", function() {
|
|
|
console.error("RTM hang up");
|
|
|
@@ -300,30 +281,20 @@ Slack.prototype.connectRtm = function(url, cb) {
|
|
|
};
|
|
|
|
|
|
Slack.prototype.onClose = function() {
|
|
|
- var arrIndex = SLACK_SESSIONS.indexOf(this);
|
|
|
- this.connected = false;
|
|
|
- if (arrIndex >= 0) SLACK_SESSIONS.splice(arrIndex, 1);
|
|
|
+ this.manager.suicide(this);
|
|
|
};
|
|
|
|
|
|
-/**
|
|
|
- * @return {boolean} true if innactive and closeding
|
|
|
-**/
|
|
|
-Slack.prototype.closeIfInnactive = function(t) {
|
|
|
- if (t - this.lastPing >INACTIVE_DELAY) {
|
|
|
- this.close();
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
-};
|
|
|
-
|
|
|
-Slack.prototype.sendActive = function() {
|
|
|
+Slack.prototype.ping = function() {
|
|
|
httpsRequest(SLACK_ENDPOINT+GETAPI.setActive
|
|
|
+"?token=" +this.token);
|
|
|
};
|
|
|
|
|
|
Slack.prototype.close = function() {
|
|
|
- this.rtm.close();
|
|
|
- this.onClose();
|
|
|
+ if (!this.closing) {
|
|
|
+ this.closing = true;
|
|
|
+ this.rtm.close();
|
|
|
+ this.onClose();
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
Slack.getOauthToken = function(code, redirectUri, cb) {
|