data.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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"], 0);
  28. onContextUpdated();
  29. }
  30. if (data["live"]) {
  31. for (var i in data["live"]) {
  32. var history = this.history[i];
  33. if (!history)
  34. this.history[i] = new SlackHistory(i, 250, data["live"][i], now);
  35. else
  36. history.pushAll(data["live"][i], now);
  37. }
  38. for (var roomId in data["live"]) {
  39. var chan = this.context.getChannel(roomId);
  40. if (chan) {
  41. if (!chan.archived) {
  42. onMsgReceived(chan, data["live"][roomId]);
  43. if (SELECTED_ROOM && data["live"][SELECTED_ROOM.id])
  44. onRoomUpdated();
  45. }
  46. }
  47. }
  48. }
  49. };
  50. /**
  51. * @param {string} text
  52. * @return {boolean}
  53. **/
  54. function isHighlighted(text) {
  55. var highlights = SLACK.context.self.prefs.highlights;
  56. for (var i =0, nbHighlights = highlights.length; i < nbHighlights; i++)
  57. if (text.indexOf(highlights[i]) !== -1) {
  58. console.log("Found highlight " +highlights[i] +" in " +text);
  59. return true;
  60. }
  61. return false;
  62. }
  63. /**
  64. * @param {SlackChan|SlackGroup|SlackIms} chan
  65. * @param {Array.<*>} msg
  66. **/
  67. function onMsgReceived(chan, msg) {
  68. if (chan !== SELECTED_ROOM || !window.hasFocus) {
  69. var selfReg = new RegExp("<@" +SLACK.context.self.id)
  70. ,highligted = false
  71. ,newHighlited = false;
  72. if (!UNREAD_CHANS[chan.id]) {
  73. UNREAD_CHANS[chan.id] = { hl: 0, unread: 0 };
  74. }
  75. msg.forEach(function(i) {
  76. // TODO check read
  77. if (chan.id[0] === 'D' || i.text.match(selfReg) || isHighlighted(i.text)) {
  78. newHighlited |= !UNREAD_CHANS[chan.id].hl;
  79. UNREAD_CHANS[chan.id].hl++;
  80. highligted = true;
  81. } else {
  82. UNREAD_CHANS[chan.id].unread++;
  83. }
  84. });
  85. updateTitle();
  86. document.getElementById(chan.id).classList.add(R.klass.unread);
  87. if (highligted) {
  88. document.getElementById(chan.id).classList.add(R.klass.unreadHi);
  89. }
  90. if (newHighlited && !window.hasFocus) {
  91. // TODO setting
  92. spawnNotification();
  93. }
  94. }
  95. }
  96. /**
  97. * @param {SlackChan|SlackGroup|SlackIms} room
  98. **/
  99. function markRoomAsRead(room) {
  100. if (UNREAD_CHANS[room.id]) {
  101. UNREAD_CHANS[room.id] = { hl: 0, unread: 0 };
  102. updateTitle();
  103. }
  104. var roomLi = document.getElementById(room.id);
  105. roomLi.classList.remove(R.klass.unread);
  106. roomLi.classList.remove(R.klass.unreadHi);
  107. }
  108. SLACK = new SlackWrapper();