Pārlūkot izejas kodu

Merge branch 'slack-presence' of isundil/mimouchat into master

isundil 6 gadi atpakaļ
vecāks
revīzija
7be1fc1908
2 mainītis faili ar 18 papildinājumiem un 7 dzēšanām
  1. 10 2
      srv/src/slack.js
  2. 8 5
      srv/src/slackData.js

+ 10 - 2
srv/src/slack.js

@@ -120,7 +120,7 @@ Slack.prototype.connect = function(cb) {
     var _this = this;
 
     this.connected = undefined;
-    httpsRequest(SLACK_ENDPOINT +GETAPI.rtmStart +"?token=" +this.token, (status, body) => {
+    httpsRequest(SLACK_ENDPOINT +GETAPI.rtmStart +"?batch_presence_aware=1&token=" +this.token, (status, body) => {
         if (!body || !body.ok) {
             _this.error = "Slack API error";
             _this.connected = false;
@@ -321,6 +321,7 @@ Slack.prototype.onMessage = function(msg, t) {
                     _this.unstackPendingMessages();
                     _this.ping();
                     MultiChatManager.onCtxUpdated(_this);
+                    _this.getPresenceForAll();
                 });
             });
         });
@@ -353,6 +354,13 @@ Slack.prototype.onMessage = function(msg, t) {
     }
 };
 
+Slack.prototype.getPresenceForAll = function() {
+    var userIds = {};
+    for (var i in this.data.users)
+        userIds[this.data.users[i].remoteId] = null;
+    this.send('{"id":' +this.rtmId++ +',"type":"presence_sub","ids":' +JSON.stringify(Object.keys(userIds))+'}');
+};
+
 Slack.prototype.removeExpiredMsgs = function(msg) {
     // TODO retry
 };
@@ -408,7 +416,7 @@ Slack.prototype.connectRtm = function(url, cb) {
     url = url.substr(protocol.length);
     url = protocol +url.substr(0, url.indexOf('/'))+
         "/?flannel=1&token=" +this.token+
-        "&start_args=" +encodeURIComponent("?simple_latest=true&presence_sub=true&canonical_avatars=true")
+        "&start_args=" +encodeURIComponent("?simple_latest=true&batch_presence_aware=1&canonical_avatars=true")
     this.rtm = new WebSocket(url);
     this.rtm.on("message", function(msg) {
         if (!_this.connected && cb) {

+ 8 - 5
srv/src/slackData.js

@@ -343,11 +343,14 @@ SlackData.prototype.commandFactory = function(data) {
 SlackData.prototype.onMessage = function(msg, t) {
     var updated = false;
     if (msg["type"] === "presence_change") {
-        var member = this.users[this.team.id +'|' +msg["user"]];
-        if (member)
-            member.setPresence(msg["presence"], t);
-        this.staticV = Math.max(this.staticV, t);
-        updated = true;
+        msg["users"].forEach(userId => {
+            var member = this.users[this.team.id +'|' +userId];
+            if (member) {
+                member.setPresence(msg["presence"], t);
+                this.staticV = Math.max(this.staticV, t);
+                updated = true;
+            }
+        }, this);
     } else if (msg["type"] === "manual_presence_change") {
         if (this.self) {
             this.self.setPresence(msg["presence"], t);