workflow.js 7.0 KB

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