Browse Source

[bugfix] receive updates for reactions when message not in db

B Thibault 8 năm trước cách đây
mục cha
commit
c78c7604de
2 tập tin đã thay đổi với 30 bổ sung7 xóa
  1. 5 4
      srv/src/slack.js
  2. 25 3
      srv/src/slackHistory.js

+ 5 - 4
srv/src/slack.js

@@ -40,7 +40,7 @@ const SLACK_ENDPOINT = "https://slack.com/api/"
         }
     }
     ,HISTORY_LENGTH = 35
-    ,HISTORY_MAX_AGE = 10// * 60 * 1000
+    ,HISTORY_MAX_AGE = 10000// * 60 * 1000
 
     ,UPDATE_LIVE = [
         "message"
@@ -217,7 +217,7 @@ Slack.prototype.onMessage = function(msg, t) {
                 ,histo = this.history[channelId];
             // FIXME remove typing for user
             if (!histo) {
-                histo = this.history[channelId] = new SlackHistory(channel.remoteId, channelId, this.data.team.id +'|', HISTORY_LENGTH);
+                histo = this.history[channelId] = new SlackHistory(this, channel.remoteId, channelId, this.data.team.id +'|', HISTORY_LENGTH);
                 histo.isNew = true;
             }
             var lastMsg = histo.push(msg, t);
@@ -598,7 +598,7 @@ Slack.prototype.editMsg = function(channel, msgId, text) {
 /**
  * @param {SlackChan|SlackGroup|SlackIms} target
 **/
-Slack.prototype.fetchHistory = function(target, cb, count) {
+Slack.prototype.fetchHistory = function(target, cb, count, firstMsgId) {
     var _this = this
         ,baseUrl = ""
         ,targetId = target.remoteId;
@@ -613,13 +613,14 @@ Slack.prototype.fetchHistory = function(target, cb, count) {
     httpsRequest(baseUrl
         +"?token="+this.token
         +"&channel=" +targetId
+        +(firstMsgId ? ("&inclusive=true&latest=" +firstMsgId) : "")
         +"&count=" +(count || 100),
     (status, resp) => {
         var history = [];
         if (status === 200 && resp && resp.ok) {
             var histo = this.history[target.id];
             if (!histo)
-                histo = this.history[target.id] = new SlackHistory(target.remoteId, target.id, this.data.team.id +'|', HISTORY_LENGTH, HISTORY_MAX_AGE);
+                histo = this.history[target.id] = new SlackHistory(_this, target.remoteId, target.id, this.data.team.id +'|', HISTORY_LENGTH, HISTORY_MAX_AGE);
             resp.messages.forEach((respMsg) => {
                 respMsg["id"] = respMsg["ts"];
                 history.push(histo.messageFactory(histo.prepareMessage(respMsg)));

+ 25 - 3
srv/src/slackHistory.js

@@ -6,6 +6,7 @@ const Message = require('./message.js').Message
 /**
  * @constructor
  * @extends {RoomHistory}
+ * @param {Slack} ctx
  * @param {string} serviceId
  * @param {Room|string} room or roomId
  * @param {string} idPrefix
@@ -14,7 +15,7 @@ const Message = require('./message.js').Message
  * @param {Array=} evts
  * @param {number=} now
 **/
-function SlackHistory(remoteId, room, idPrefix, keepMessages, maxAge, evts, now) {
+function SlackHistory(ctx, remoteId, room, idPrefix, keepMessages, maxAge, evts, now) {
     RoomHistory.call(this, room, keepMessages, maxAge, evts, now);
 
     /** @const @type {string} */
@@ -22,6 +23,9 @@ function SlackHistory(remoteId, room, idPrefix, keepMessages, maxAge, evts, now)
 
     /** @const @type {string} */
     this.idPrefix = idPrefix;
+
+    /** @const @type {Slack} */
+    this.ctx = ctx;
 }
 SlackHistory.prototype = Object.create(RoomHistory.prototype);
 SlackHistory.prototype.constructor = SlackHistory;
@@ -103,12 +107,30 @@ SlackHistory.prototype.push = function(ev, t) {
     } else if (ev["type"] === "reaction_added") {
         msg = this.addReaction(ev["reaction"], this.idPrefix +ev["user"], ev["item"]["ts"], t);
         if (!msg) {
-            //FIXME fetch ev.item.channel / ev.item.ts
+            let self = this;
+            this.ctx.fetchHistory(this.ctx.getChatContext().channels[this.id], (msg) => {
+                if (msg && msg.length) {
+                    msg[0].version = t;
+                    self.messages.push(msg[0]);
+                    self.resort();
+                    self.v = Math.max(self.v, t);
+                    self.ctx.getChatContext().liveV = Math.max(self.ctx.getChatContext().liveV, t);
+                }
+            }, 1, ev["item"]["ts"]);
         }
     } else if (ev["type"] === "reaction_removed") {
+        let self = this;
         msg = this.removeReaction(ev["reaction"], this.idPrefix +ev["user"], ev["item"]["ts"], t);
         if (!msg) {
-            //FIXME fetch ev.item.channel / ev.item.ts
+            this.ctx.fetchHistory(this.ctx.getChatContext().channels[this.id], (msg) => {
+                if (msg && msg.length) {
+                    msg[0].version = t;
+                    self.messages.push(msg[0]);
+                    self.resort();
+                    self.v = Math.max(self.v, t);
+                    self.ctx.getChatContext().liveV = Math.max(self.ctx.getChatContext().liveV, t);
+                }
+            }, 1, ev["item"]["ts"]);
         }
     } else {
         return 0;