1
0

workflow.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. var
  2. /**
  3. * @type {number} next period to wait before next retry in case of failure, in seconds
  4. **/
  5. NEXT_RETRY = 0
  6. /**
  7. * @type {SlackChan|SlackIms|SlackGroup}
  8. **/
  9. ,SELECTED_ROOM = null
  10. ;
  11. /**
  12. * @param {SlackChan|SlackIms|SlackGroup} room
  13. * @param {function(boolean)} cb
  14. **/
  15. function fetchHistory(room, cb) {
  16. var xhr = new XMLHttpRequest();
  17. xhr.open('GET', 'api/hist?room=' +room.id, true);
  18. xhr.send(null);
  19. }
  20. function poll(callback) {
  21. var xhr = new XMLHttpRequest();
  22. xhr.timeout = 1000 * 60 * 1; // 3 min timeout
  23. xhr.onreadystatechange = function(e) {
  24. if (xhr.readyState === 4) {
  25. if (xhr.status === 0) {
  26. if (NEXT_RETRY) {
  27. NEXT_RETRY = 0;
  28. onNetworkStateUpdated(true);
  29. }
  30. poll(callback); // retry on timeout
  31. return;
  32. }
  33. var resp = null
  34. ,success = Math.floor(xhr.status / 100) === 2;
  35. if (success) {
  36. if (NEXT_RETRY) {
  37. NEXT_RETRY = 0;
  38. onNetworkStateUpdated(true);
  39. }
  40. resp = xhr.response;
  41. try {
  42. resp = JSON.parse(/** @type {string} */ (resp));
  43. } catch (e) {
  44. resp = null;
  45. }
  46. } else {
  47. if (NEXT_RETRY) {
  48. NEXT_RETRY += Math.floor((NEXT_RETRY || 5)/2);
  49. NEXT_RETRY = Math.min(60, NEXT_RETRY);
  50. } else {
  51. NEXT_RETRY = 5;
  52. onNetworkStateUpdated(false);
  53. }
  54. }
  55. callback(success, resp);
  56. }
  57. };
  58. xhr.open('GET', 'api?v=' +SLACK.lastServerVersion, true);
  59. xhr.send(null);
  60. }
  61. /**
  62. * @param {boolean} success
  63. * @param {*} response
  64. **/
  65. function onPollResponse(success, response) {
  66. if (success) {
  67. if (response) {
  68. SLACK.update(response);
  69. }
  70. startPolling();
  71. } else {
  72. setTimeout(startPolling, NEXT_RETRY * 1000);
  73. }
  74. }
  75. function startPolling() {
  76. poll(onPollResponse);
  77. }
  78. /**
  79. * @param {SlackChan|SlackIms|SlackGroup} room
  80. **/
  81. function selectRoom(room) {
  82. if (SELECTED_ROOM)
  83. unselectRoom();
  84. document.getElementById(room.id).classList.add(R.klass.selected);
  85. document.body.classList.remove(R.klass.noRoomSelected);
  86. SELECTED_ROOM = room;
  87. onRoomSelected();
  88. if (SELECTED_ROOM.lastRead && !SLACK.history[SELECTED_ROOM.id])
  89. fetchHistory(SELECTED_ROOM, function(success) {});
  90. }
  91. function unselectRoom() {
  92. document.getElementById(SELECTED_ROOM.id).classList.remove(R.klass.selected);
  93. }
  94. /**
  95. * @param {SlackGroup|SlackChan|SlackIms} chan
  96. * @param {string} filename
  97. * @param {File} file
  98. * @param {function(string?)} callback
  99. **/
  100. function uploadFile(chan, filename, file, callback) {
  101. var fileReader = new FileReader()
  102. ,formData = new FormData()
  103. ,xhr = new XMLHttpRequest();
  104. formData.append("file", file);
  105. formData.append("filename", filename);
  106. xhr.onreadystatechange = function() {
  107. if (xhr.readyState === 4) {
  108. if (xhr.status === 204) {
  109. callback(null);
  110. } else {
  111. callback(xhr.statusText);
  112. }
  113. }
  114. }
  115. xhr.open('POST', 'api/file?room=' +chan.id);
  116. xhr.send(formData);
  117. }
  118. /**
  119. * @param {SlackChan|SlackGroup|SlackIms} chan
  120. * @param {string} msg
  121. * @param {SlackMessage|null=} replyTo
  122. **/
  123. function sendMsg(chan, msg, replyTo) {
  124. var xhr = new XMLHttpRequest();
  125. var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
  126. if (replyTo) {
  127. var sender = SLACK.context.getMember(replyTo.userId)
  128. ,footer = "Message";
  129. if (chan.id[0] === 'C') {
  130. footer = "Channel message";
  131. } else if (chan.id[0] === 'D') {
  132. footer = "Direct message";
  133. } else if (chan.id[0] === 'G') {
  134. footer = "Group message";
  135. }
  136. var attachment = {
  137. "fallback": replyTo.raw["text"] || ""
  138. ,"author_name": "<@" +sender.id +"|" +sender.name +">"
  139. ,"author_icon": sender.icons.image_48
  140. ,"text": replyTo.raw["text"] || ""
  141. ,"footer": footer
  142. ,"ts": replyTo.ts
  143. };
  144. url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
  145. }
  146. xhr.open('POST', url, true);
  147. xhr.send(null);
  148. }
  149. /**
  150. * @param {string} channelId
  151. * @param {string} msgId
  152. * @param {string} reaction
  153. **/
  154. function addReaction(channelId, msgId, reaction) {
  155. var xhr = new XMLHttpRequest();
  156. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  157. xhr.open('POST', url, true);
  158. xhr.send(null);
  159. }
  160. /**
  161. * @param {string} channelId
  162. * @param {string} msgId
  163. * @param {string} reaction
  164. **/
  165. function removeReaction(channelId, msgId, reaction) {
  166. var xhr = new XMLHttpRequest();
  167. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  168. xhr.open('DELETE', url, true);
  169. xhr.send(null);
  170. }