data.js 3.7 KB

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