|
|
@@ -23,11 +23,13 @@ const SLACK_ENDPOINT = "https://slack.com/api/"
|
|
|
,editMsg: "chat.update"
|
|
|
,removeMsg: "chat.delete"
|
|
|
,postFile: "files.upload"
|
|
|
+ ,setActive: "users.setActive"
|
|
|
,emojiList: "emoji.list"
|
|
|
,addReaction: "reactions.add"
|
|
|
,removeReaction: "reactions.remove"
|
|
|
}
|
|
|
,HISTORY_LENGTH = 35
|
|
|
+ ,INACTIVE_DELAY = 120000
|
|
|
|
|
|
,UPDATE_LIVE = [
|
|
|
"message"
|
|
|
@@ -45,9 +47,10 @@ var
|
|
|
;
|
|
|
|
|
|
setInterval(function() {
|
|
|
+ var t = Date.now();
|
|
|
SLACK_SESSIONS.forEach(function(slackInst) {
|
|
|
- if (!slackInst.closeIfInnactive())
|
|
|
- slackInst.ping();
|
|
|
+ if (!slackInst.closeIfInnactive(t) && slackInst.active)
|
|
|
+ slackInst.sendActive();
|
|
|
});
|
|
|
}, 60000);
|
|
|
|
|
|
@@ -58,6 +61,11 @@ function Slack(sess) {
|
|
|
this.history = {};
|
|
|
this.connected = false;
|
|
|
this.active = true;
|
|
|
+ this.lastPing = Date.now();
|
|
|
+}
|
|
|
+
|
|
|
+Slack.prototype.onUserInterract = function(t) {
|
|
|
+ this.lastPing = Math.max(t, this.lastPing);
|
|
|
}
|
|
|
|
|
|
Slack.prototype.onRequest = function(knownVersion, cb) {
|
|
|
@@ -221,32 +229,40 @@ 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() {
|
|
|
- _this.connected = false;
|
|
|
console.error("RTM hang up");
|
|
|
- _this.close();
|
|
|
+ _this.onClose();
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+Slack.prototype.onClose = function() {
|
|
|
+ var arrIndex = SLACK_SESSIONS.indexOf(this);
|
|
|
+ this.connected = false;
|
|
|
+ if (arrIndex >= 0) SLACK_SESSIONS.splice(arrIndex, 1);
|
|
|
+};
|
|
|
+
|
|
|
/**
|
|
|
* @return {boolean} true if innactive and closeding
|
|
|
**/
|
|
|
-Slack.prototype.closeIfInnactive = function() {
|
|
|
- //TODO
|
|
|
+Slack.prototype.closeIfInnactive = function(t) {
|
|
|
+ if (t - this.lastPing >INACTIVE_DELAY) {
|
|
|
+ this.close();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
return false;
|
|
|
};
|
|
|
|
|
|
-Slack.prototype.ping = function() {
|
|
|
- console.log("pinging");
|
|
|
- this.rtm.send(JSON.stringify({
|
|
|
- "id": ++this.rtmId
|
|
|
- ,"type": "ping"
|
|
|
- ,"set_active": this.active
|
|
|
- }));
|
|
|
+Slack.prototype.sendActive = function() {
|
|
|
+ httpsRequest(SLACK_ENDPOINT+GETAPI.setActive
|
|
|
+ +"?token=" +this.token);
|
|
|
};
|
|
|
|
|
|
Slack.prototype.close = function() {
|
|
|
+ this.rtm.close();
|
|
|
+ this.onClose();
|
|
|
};
|
|
|
|
|
|
Slack.getOauthToken = function(code, redirectUri, cb) {
|