workflow.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. function outOfSync() {
  62. SLACK.lastServerVersion = 0;
  63. }
  64. /**
  65. * @param {boolean} success
  66. * @param {*} response
  67. **/
  68. function onPollResponse(success, response) {
  69. if (success) {
  70. if (response) {
  71. SLACK.update(response);
  72. }
  73. startPolling();
  74. } else {
  75. setTimeout(startPolling, NEXT_RETRY * 1000);
  76. }
  77. }
  78. function startPolling() {
  79. poll(onPollResponse);
  80. }
  81. /**
  82. * @param {SlackChan|SlackIms|SlackGroup} room
  83. **/
  84. function selectRoom(room) {
  85. if (SELECTED_ROOM)
  86. unselectRoom();
  87. document.getElementById(room.id).classList.add(R.klass.selected);
  88. document.body.classList.remove(R.klass.noRoomSelected);
  89. SELECTED_ROOM = room;
  90. onRoomSelected();
  91. if (SELECTED_ROOM.lastRead && !SLACK.history[SELECTED_ROOM.id])
  92. fetchHistory(SELECTED_ROOM, function(success) {});
  93. }
  94. function unselectRoom() {
  95. document.getElementById(SELECTED_ROOM.id).classList.remove(R.klass.selected);
  96. }
  97. /**
  98. * @param {SlackGroup|SlackChan|SlackIms} chan
  99. * @param {string} filename
  100. * @param {File} file
  101. * @param {function(string?)} callback
  102. **/
  103. function uploadFile(chan, filename, file, callback) {
  104. var fileReader = new FileReader()
  105. ,formData = new FormData()
  106. ,xhr = new XMLHttpRequest();
  107. formData.append("file", file);
  108. formData.append("filename", filename);
  109. xhr.onreadystatechange = function() {
  110. if (xhr.readyState === 4) {
  111. if (xhr.status === 204) {
  112. callback(null);
  113. } else {
  114. callback(xhr.statusText);
  115. }
  116. }
  117. }
  118. xhr.open('POST', 'api/file?room=' +chan.id);
  119. xhr.send(formData);
  120. }
  121. /**
  122. * @param {SlackChan|SlackGroup|SlackIms} chan
  123. * @param {SlackCommand!} cmd
  124. * @param {string} args
  125. **/
  126. function doCommand(chan, cmd, args) {
  127. var xhr = new XMLHttpRequest()
  128. ,url = 'api/cmd?room=' +chan.id +"&cmd=" +encodeURIComponent(cmd.name.substr(1)) +"&args=" +encodeURIComponent(args);
  129. xhr.open('POST', url, true);
  130. xhr.send(null);
  131. }
  132. /**
  133. * @param {SlackChan|SlackGroup|SlackIms} chan
  134. * @param {string} msg
  135. * @param {SlackMessage|null=} replyTo
  136. **/
  137. function sendMsg(chan, msg, replyTo) {
  138. var xhr = new XMLHttpRequest();
  139. var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
  140. if (replyTo) {
  141. var sender = SLACK.context.getMember(replyTo.userId)
  142. ,footer = "Message";
  143. if (chan.id[0] === 'C') {
  144. footer = "Channel message";
  145. } else if (chan.id[0] === 'D') {
  146. footer = "Direct message";
  147. } else if (chan.id[0] === 'G') {
  148. footer = "Group message";
  149. }
  150. var attachment = {
  151. "fallback": replyTo.text
  152. ,"author_name": "<@" +sender.id +"|" +sender.name +">"
  153. ,"author_icon": sender.icons.image_48
  154. ,"text": replyTo.text
  155. ,"footer": footer
  156. ,"ts": replyTo.ts
  157. };
  158. url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
  159. }
  160. xhr.open('POST', url, true);
  161. xhr.send(null);
  162. }
  163. /**
  164. * @param {string} input
  165. * @param {boolean=} skipCommand
  166. * @return {boolean} true on recognized input
  167. **/
  168. function onTextEntered(input, skipCommand) {
  169. var success = true;
  170. if (EDITING) {
  171. editMsg(SELECTED_ROOM, input, EDITING);
  172. return true;
  173. }
  174. if (input[0] === '/' && skipCommand !== true) {
  175. var endCmd = input.indexOf(' ')
  176. ,cmd = input.substr(0, endCmd === -1 ? undefined : endCmd)
  177. ,args = endCmd === -1 ? "" : input.substr(endCmd)
  178. ,cmdObject = SLACK.context.commands.data[cmd];
  179. if (cmdObject) {
  180. doCommand(SELECTED_ROOM, cmdObject, args.trim());
  181. return true;
  182. }
  183. return false;
  184. }
  185. sendMsg(SELECTED_ROOM, input, REPLYING_TO);
  186. return true;
  187. }
  188. /**
  189. * @param {SlackChan|SlackGroup|SlackIms} chan
  190. * @param {string} text
  191. * @param {SlackMessage|null=} msg
  192. **/
  193. function editMsg(chan, text, msg) {
  194. var xhr = new XMLHttpRequest();
  195. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id +"&text=" +encodeURIComponent(text);
  196. xhr.open('PUT', url, true);
  197. xhr.send(null);
  198. }
  199. /**
  200. * @param {SlackChan|SlackGroup|SlackIms} chan
  201. * @param {SlackMessage|null=} msg
  202. **/
  203. function removeMsg(chan, msg) {
  204. var xhr = new XMLHttpRequest();
  205. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id;
  206. xhr.open('DELETE', url, true);
  207. xhr.send(null);
  208. }
  209. /**
  210. * @param {string} channelId
  211. * @param {string} msgId
  212. * @param {string} reaction
  213. **/
  214. function addReaction(channelId, msgId, reaction) {
  215. var xhr = new XMLHttpRequest();
  216. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  217. xhr.open('POST', url, true);
  218. xhr.send(null);
  219. }
  220. /**
  221. * @param {string} channelId
  222. * @param {string} msgId
  223. * @param {string} reaction
  224. **/
  225. function removeReaction(channelId, msgId, reaction) {
  226. var xhr = new XMLHttpRequest();
  227. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  228. xhr.open('DELETE', url, true);
  229. xhr.send(null);
  230. }