Browse Source

[add] pin/unpin api

isundil 8 years ago
parent
commit
d326be89d8
3 changed files with 99 additions and 11 deletions
  1. 25 0
      srv/src/controller/apiController.js
  2. 40 11
      srv/src/slack.js
  3. 34 0
      srv/src/slackData.js

+ 25 - 0
srv/src/controller/apiController.js

@@ -327,6 +327,31 @@ module.exports.ApiController = {
                 res.end();
             }
         } else if (req.urlObj.match(["api", "pinMsg"])) {
+            if (req.urlObj.queryTokens.room && req.urlObj.queryTokens.msgId) {
+                var ctx = res.chatContext.getChannelContext(req.urlObj.queryTokens.room[0]);
+
+                if (!ctx) {
+                    res.writeHeader("404", "Not Found");
+                    res.end();
+                } else if (!ctx.getChatContext().capacities.starMsg) {
+                    res.writeHeader("400", "Bad Request");
+                    res.end();
+                } else if (req.method === 'POST') {
+                    ctx.pinMsg(ctx.getChatContext().channels[req.urlObj.queryTokens.room[0]], req.urlObj.queryTokens.msgId[0]);
+                    res.writeHeader("204", "No Content");
+                    res.end();
+                } else if (req.method === 'DELETE') {
+                    ctx.unpinMsg(ctx.getChatContext().channels[req.urlObj.queryTokens.room[0]], req.urlObj.queryTokens.msgId[0]);
+                    res.writeHeader("204", "No Content");
+                    res.end();
+                } else {
+                    res.writeHeader("400", "Bad Request");
+                    res.end();
+                }
+            } else {
+                res.writeHeader("400", "Bad Request");
+                res.end();
+            }
         } else if (req.urlObj.match(["api"])) {
             res.chatContext.poll(
                 (req.urlObj.queryTokens.v ? parseInt(req.urlObj.queryTokens.v[0], 10) : 0) || 0, (newData) => {

+ 40 - 11
srv/src/slack.js

@@ -33,6 +33,8 @@ const SLACK_ENDPOINT = "https://slack.com/api/"
         ,setPresence: "users.setPresence"
         ,emojiList: "emoji.list"
         ,slashList: "commands.list"
+        ,pinMsg: "pins.add"
+        ,unpinMsg: "pins.remove"
         ,listPinned: "pins.list"
         ,listMpims: "mpim.list"
         ,slashExec: "chat.command"
@@ -237,12 +239,8 @@ Slack.prototype.onMessage = function(msg, t) {
         if ((msg["channel"] || msg["channel_id"] || (msg["item"] && msg["item"]["channel"])) && msg["type"] && UPDATE_LIVE.indexOf(msg["type"]) !== -1) {
             var channelId = this.data.team.id +'|' +(msg["channel"] || msg["channel_id"] || msg["item"]["channel"])
                 ,channel = this.data.channels[channelId]
-                ,histo = this.history[channelId];
+                ,histo = this.lazyHistory(channel);
             // FIXME remove typing for user
-            if (!histo) {
-                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);
             if (lastMsg)
                 this.data.liveV = t;
@@ -604,6 +602,28 @@ Slack.prototype.unstarMsg = function(channel, msgId) {
         +"&timestamp=" +msgId);
 };
 
+/**
+ * @param {SlackChan|SlackGroup|SlackIms} channel
+ * @param {string} msgId
+**/
+Slack.prototype.pinMsg = function(channel, msgId) {
+    httpsRequest(SLACK_ENDPOINT +GETAPI.pinMsg
+        +"?token=" +this.token
+        +"&channel=" +channel.remoteId
+        +"&timestamp=" +msgId);
+};
+
+/**
+ * @param {SlackChan|SlackGroup|SlackIms} channel
+ * @param {string} msgId
+**/
+Slack.prototype.unpinMsg = function(channel, msgId) {
+    httpsRequest(SLACK_ENDPOINT +GETAPI.unpinMsg
+        +"?token=" +this.token
+        +"&channel=" +channel.remoteId
+        +"&timestamp=" +msgId);
+};
+
 /**
  * @param {SlackChan|SlackGroup|SlackIms} channel
  * @param {Array.<string>} text
@@ -663,6 +683,18 @@ Slack.prototype.editMsg = function(channel, msgId, text) {
         +"&as_user=true");
 };
 
+/**
+ * @param {SlackChan|SlackGroup|SlackIms} target
+**/
+Slack.prototype.lazyHistory = function(target) {
+    var histo = this.history[target.id];
+    if (!histo) {
+        histo = this.history[target.id] = new SlackHistory(this, target.remoteId, target.id, this.data.team.id +'|', HISTORY_LENGTH, HISTORY_MAX_AGE);
+        histo.isNew = true;
+    }
+    return histo;
+};
+
 /**
  * @param {SlackChan|SlackGroup|SlackIms} target
 **/
@@ -675,11 +707,9 @@ Slack.prototype.fetchPinned = function(target) {
     (status, resp) => {
         if (status === 200 && resp && resp.ok) {
             var msgs = [],
-                histo = _this.history[target.id],
+                histo = _this.lazyHistory(target);
                 now = Date.now();
 
-            if (!histo)
-                histo = _this.history[target.id] = new SlackHistory(_this, target.remoteId, target.id, this.data.team.id +'|', HISTORY_LENGTH, HISTORY_MAX_AGE);
             resp.items.forEach(function(msg) {
                 msgs.push(histo.messageFactory(msg.message, now));
             });
@@ -714,9 +744,8 @@ Slack.prototype.fetchHistory = function(target, cb, count, firstMsgId) {
     (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(_this, target.remoteId, target.id, this.data.team.id +'|', HISTORY_LENGTH, HISTORY_MAX_AGE);
+            var histo = this.lazyHistory(target);
+
             resp.messages.forEach((respMsg) => {
                 respMsg["id"] = respMsg["ts"];
                 history.push(histo.messageFactory(histo.prepareMessage(respMsg)));

+ 34 - 0
srv/src/slackData.js

@@ -356,6 +356,40 @@ SlackData.prototype.onMessage = function(msg, t) {
                 this.staticV = Math.max(this.staticV, t);
             }
         }
+    } else if (msg["type"] === "pin_added") {
+        var targetChan = this.getChannelByRemoteId(msg["channel_id"] || msg["item"]["channel"]);
+        if (!targetChan.pins) {
+            this.slack.fetchPinned(targetChan);
+        } else {
+            var histo = this.slack.lazyHistory(targetChan);
+
+            targetChan.pins.push(histo.messageFactory(msg["item"]["message"], t));
+            targetChan.version = Math.max(targetChan.version, t);
+            this.staticV = Math.max(this.staticV, t);
+        }
+    } else if (msg["type"] === "pin_removed") {
+        var targetChan = this.getChannelByRemoteId(msg["channel_id"] || msg["item"]["channel"]);
+        if (!msg["pin_count"]) {
+            targetChan.pins = [];
+            targetChan.version = Math.max(targetChan.version, t);
+            this.staticV = Math.max(this.staticV, t);
+        }
+        if (!targetChan.pins) {
+            this.slack.fetchPinned(targetChan);
+        } else {
+            var found = false;
+            for (var i =0, nbPins = targetChan.pins.length; i < nbPins; i++) {
+                if (targetChan.pins[i].id === msg["item"]["message"]["ts"]) {
+                    targetChan.pins.splice(i, 1);
+                    targetChan.version = Math.max(targetChan.version, t);
+                    this.staticV = Math.max(this.staticV, t);
+                    found = true;
+                    break;
+                }
+            }
+            if (!found) // wtf out-of-sync
+                this.slack.fetchPinned(targetChan);
+        }
     /*
     } else {
         console.log(msg);