data.js 3.1 KB

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