data.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 {SlackChan|SlackGroup|SlackIms} chan
  46. * @param {Array.<*>} msg
  47. **/
  48. function onMsgReceived(chan, msg) {
  49. if (chan && (chan !== SELECTED_ROOM || !window.hasFocus)) {
  50. var selfReg = new RegExp("<@" +SLACK.context.self.id)
  51. ,highligted = false;
  52. if (!UNREAD_CHANS[chan.id]) {
  53. UNREAD_CHANS[chan.id] = { hl: 0, unread: 0 };
  54. }
  55. msg.forEach(function(i) {
  56. // TODO check read
  57. if (i.type === "message" && i.text) {
  58. if (chan.id[0] === 'D' || i.text.match(selfReg)) {
  59. UNREAD_CHANS[chan.id].hl++;
  60. highligted = true;
  61. } else {
  62. UNREAD_CHANS[chan.id].unread++;
  63. }
  64. }
  65. });
  66. updateTitle();
  67. document.getElementById(chan.id).classList.add(R.klass.unread);
  68. if (highligted) {
  69. document.getElementById(chan.id).classList.add(R.klass.unreadHi);
  70. }
  71. }
  72. }
  73. /**
  74. * @param {SlackChan|SlackGroup|SlackIms} room
  75. **/
  76. function markRoomAsRead(room) {
  77. if (UNREAD_CHANS[room.id]) {
  78. UNREAD_CHANS[room.id] = { hl: 0, unread: 0 };
  79. updateTitle();
  80. }
  81. var roomLi = document.getElementById(room.id);
  82. roomLi.classList.remove(R.klass.unread);
  83. roomLi.classList.remove(R.klass.unreadHi);
  84. }
  85. SLACK = new SlackWrapper();