data.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var
  2. /**
  3. * @type {SlackWrapper}
  4. **/
  5. SLACK
  6. /**
  7. * @type {Object.<string, {hl: number, unread: number}>} number of unread per chan
  8. **/
  9. ,UNREAD_CHANS = {};
  10. ;
  11. /**
  12. * @constructor
  13. **/
  14. function SlackWrapper() {
  15. /** @type {number} */
  16. this.lastServerVersion = 0;
  17. /** @type {SlackData} */
  18. this.context = new SlackData(null);
  19. /** @type {!Object.<string, SlackHistory>} **/
  20. this.history = {};
  21. }
  22. SlackWrapper.prototype.update = function(data) {
  23. var now = Date.now();
  24. if (data["v"])
  25. this.lastServerVersion = data["v"];
  26. if (data["static"]) {
  27. this.context.updateStatic(data["static"], Date.now());
  28. onContextUpdated();
  29. if (data["static"]["typing"])
  30. onTypingUpdated();
  31. }
  32. if (data["live"]) {
  33. for (var i in data["live"]) {
  34. var history = this.history[i];
  35. if (!history)
  36. this.history[i] = new SlackHistory(i, 250, data["live"][i], now);
  37. else
  38. history.pushAll(data["live"][i], now);
  39. }
  40. for (var roomId in data["live"]) {
  41. var chan = this.context.getChannel(roomId);
  42. if (chan) {
  43. if (!chan.archived) {
  44. onMsgReceived(chan, data["live"][roomId]);
  45. if (SELECTED_ROOM && data["live"][SELECTED_ROOM.id])
  46. onRoomUpdated();
  47. }
  48. }
  49. }
  50. }
  51. };
  52. setInterval(function() {
  53. if (SLACK.context.cleanTyping(Date.now()))
  54. onTypingUpdated();
  55. }, 1000);
  56. /**
  57. * @param {string} text
  58. * @return {boolean}
  59. **/
  60. function isHighlighted(text) {
  61. var highlights = SLACK.context.self.prefs.highlights;
  62. for (var i =0, nbHighlights = highlights.length; i < nbHighlights; i++)
  63. if (text.indexOf(highlights[i]) !== -1) {
  64. console.log("Found highlight " +highlights[i] +" in " +text);
  65. return true;
  66. }
  67. return false;
  68. }
  69. /**
  70. * @param {SlackChan|SlackGroup|SlackIms} chan
  71. * @param {Array.<*>} msg
  72. **/
  73. function onMsgReceived(chan, msg) {
  74. if (chan !== SELECTED_ROOM || !window.hasFocus) {
  75. var selfReg = new RegExp("<@" +SLACK.context.self.id)
  76. ,highligted = false
  77. ,newHighlited = false;
  78. if (!UNREAD_CHANS[chan.id]) {
  79. UNREAD_CHANS[chan.id] = { hl: 0, unread: 0 };
  80. }
  81. msg.forEach(function(i) {
  82. // TODO check read
  83. if (chan.id[0] === 'D' || i.text.match(selfReg) || isHighlighted(i.text)) {
  84. newHighlited |= !UNREAD_CHANS[chan.id].hl;
  85. UNREAD_CHANS[chan.id].hl++;
  86. highligted = true;
  87. } else {
  88. UNREAD_CHANS[chan.id].unread++;
  89. }
  90. });
  91. updateTitle();
  92. document.getElementById(chan.id).classList.add(R.klass.unread);
  93. if (highligted) {
  94. document.getElementById(chan.id).classList.add(R.klass.unreadHi);
  95. }
  96. if (newHighlited && !window.hasFocus) {
  97. // TODO setting
  98. spawnNotification();
  99. }
  100. }
  101. }
  102. /**
  103. * @param {SlackChan|SlackGroup|SlackIms} room
  104. **/
  105. function markRoomAsRead(room) {
  106. if (UNREAD_CHANS[room.id]) {
  107. UNREAD_CHANS[room.id] = { hl: 0, unread: 0 };
  108. updateTitle();
  109. }
  110. var roomLi = document.getElementById(room.id);
  111. roomLi.classList.remove(R.klass.unread);
  112. roomLi.classList.remove(R.klass.unreadHi);
  113. }
  114. SLACK = new SlackWrapper();