data.js 2.7 KB

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