1
0

data.js 3.1 KB

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