| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- var
- /**
- * @type {SlackWrapper}
- **/
- SLACK
- /**
- * @type {Object.<string, {hl: number, unread: number}>} number of unread per chan
- **/
- ,UNREAD_CHANS = {};
- ;
- /**
- * @constructor
- **/
- function SlackWrapper() {
- /** @type {number} */
- this.lastServerVersion = 0;
- /** @type {SlackData} */
- this.context = new SlackData(null);
- /** @type {!Object.<string, SlackHistory>} **/
- 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"], Date.now());
- onContextUpdated();
- if (data["static"]["typing"])
- onTypingUpdated();
- }
- 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();
- }
- }
- }
- }
- };
- setInterval(function() {
- if (SLACK.context.cleanTyping(Date.now()))
- onTypingUpdated();
- }, 1000);
- /**
- * @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();
|