var /** * @type {SlackWrapper} **/ SLACK /** * @type {Object.} number of unread per chan **/ ,UNREAD_CHANS = {}; ; /** * @constructor **/ function SlackWrapper() { /** @type {number} */ this.lastServerVersion = 0; /** @type {SlackData} */ this.context = new SlackData(null); /** @type {!Object.} **/ this.history = {}; } SlackWrapper.prototype.update = function(data) { var now = Date.now(); if (data["v"]) this.lastServerVersion = data["v"]; if (data["static"]) { this.context.updateStatic(data["static"], 0); onContextUpdated(); } if (data["live"]) { for (var i in data["live"]) { var history = this.history[i]; if (!history) this.history[i] = new SlackHistory(i, 250, data["live"][i], now); else history.pushAll(data["live"][i], now); } for (var roomId in data["live"]) { var chan = this.context.getChannel(roomId); if (chan) { if (!chan.archived) { onMsgReceived(chan, data["live"][roomId]); if (SELECTED_ROOM && data["live"][SELECTED_ROOM.id]) onRoomUpdated(); } } } } }; /** * @param {string} text * @return {boolean} **/ function isHighlighted(text) { var highlights = SLACK.context.self.prefs.highlights; for (var i =0, nbHighlights = highlights.length; i < nbHighlights; i++) if (text.indexOf(highlights[i]) !== -1) { console.log("Found highlight " +highlights[i] +" in " +text); return true; } return false; } /** * @param {SlackChan|SlackGroup|SlackIms} chan * @param {Array.<*>} msg **/ function onMsgReceived(chan, msg) { if (chan !== SELECTED_ROOM || !window.hasFocus) { var selfReg = new RegExp("<@" +SLACK.context.self.id) ,highligted = false ,newHighlited = false; if (!UNREAD_CHANS[chan.id]) { UNREAD_CHANS[chan.id] = { hl: 0, unread: 0 }; } msg.forEach(function(i) { // TODO check read if (chan.id[0] === 'D' || i.text.match(selfReg) || isHighlighted(i.text)) { newHighlited |= !UNREAD_CHANS[chan.id].hl; UNREAD_CHANS[chan.id].hl++; highligted = true; } else { UNREAD_CHANS[chan.id].unread++; } }); updateTitle(); document.getElementById(chan.id).classList.add(R.klass.unread); if (highligted) { document.getElementById(chan.id).classList.add(R.klass.unreadHi); } if (newHighlited && !window.hasFocus) { // TODO setting spawnNotification(); } } } /** * @param {SlackChan|SlackGroup|SlackIms} room **/ function markRoomAsRead(room) { if (UNREAD_CHANS[room.id]) { UNREAD_CHANS[room.id] = { hl: 0, unread: 0 }; updateTitle(); } var roomLi = document.getElementById(room.id); roomLi.classList.remove(R.klass.unread); roomLi.classList.remove(R.klass.unreadHi); } SLACK = new SlackWrapper();