workflow.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 {Room}
  8. **/
  9. ,SELECTED_ROOM = null
  10. /** @type {Message|null} */
  11. ,REPLYING_TO = null
  12. /** @type {Message|null} */
  13. ,EDITING = null
  14. ;
  15. /**
  16. * @param {Room} room
  17. * @param {function(boolean)} cb
  18. **/
  19. function fetchHistory(room, cb) {
  20. var xhr = new XMLHttpRequest();
  21. xhr.open('GET', 'api/hist?room=' +room.id, true);
  22. xhr.send(null);
  23. }
  24. function poll(callback) {
  25. var xhr = new XMLHttpRequest();
  26. xhr.timeout = 1000 * 60 * 1; // 3 min timeout
  27. xhr.onreadystatechange = function(e) {
  28. if (xhr.readyState === 4) {
  29. if (xhr.status === 0) {
  30. if (NEXT_RETRY) {
  31. NEXT_RETRY = 0;
  32. onNetworkStateUpdated(true);
  33. }
  34. poll(callback); // retry on timeout
  35. return;
  36. }
  37. var resp = null
  38. ,success = Math.floor(xhr.status / 100) === 2;
  39. if (success) {
  40. if (NEXT_RETRY) {
  41. NEXT_RETRY = 0;
  42. onNetworkStateUpdated(true);
  43. }
  44. resp = xhr.response;
  45. try {
  46. resp = JSON.parse(/** @type {string} */ (resp));
  47. } catch (e) {
  48. resp = null;
  49. }
  50. } else {
  51. if (NEXT_RETRY) {
  52. NEXT_RETRY += Math.floor((NEXT_RETRY || 5)/2);
  53. NEXT_RETRY = Math.min(60, NEXT_RETRY);
  54. } else {
  55. NEXT_RETRY = 5;
  56. onNetworkStateUpdated(false);
  57. }
  58. }
  59. callback(success, resp);
  60. }
  61. };
  62. xhr.open('GET', 'api?v=' +SLACK.lastServerVersion, true);
  63. xhr.send(null);
  64. }
  65. function outOfSync() {
  66. SLACK.lastServerVersion = 0;
  67. }
  68. /**
  69. * @param {Room} room
  70. **/
  71. function sendTyping(room) {
  72. var xhr = new XMLHttpRequest()
  73. ,url = 'api/typing?room=' +room.id;
  74. xhr.open('POST', url, true);
  75. xhr.send(null);
  76. }
  77. /**
  78. * @param {boolean} success
  79. * @param {*} response
  80. **/
  81. function onPollResponse(success, response) {
  82. if (success) {
  83. if (response) {
  84. SLACK.update(response);
  85. }
  86. startPolling();
  87. } else {
  88. setTimeout(startPolling, NEXT_RETRY * 1000);
  89. }
  90. }
  91. function startPolling() {
  92. poll(onPollResponse);
  93. }
  94. /**
  95. * @param {Room} room
  96. **/
  97. function selectRoom(room) {
  98. if (SELECTED_ROOM)
  99. unselectRoom();
  100. document.getElementById(room.id).classList.add(R.klass.selected);
  101. document.body.classList.remove(R.klass.noRoomSelected);
  102. SELECTED_ROOM = room;
  103. onRoomSelected();
  104. if (SELECTED_ROOM.lastMsg && !SLACK.history[SELECTED_ROOM.id])
  105. fetchHistory(SELECTED_ROOM, function(success) {});
  106. }
  107. function unselectRoom() {
  108. document.getElementById(SELECTED_ROOM.id).classList.remove(R.klass.selected);
  109. }
  110. /**
  111. * @param {Room} chan
  112. * @param {string} filename
  113. * @param {File} file
  114. * @param {function(string?)} callback
  115. **/
  116. function uploadFile(chan, filename, file, callback) {
  117. var fileReader = new FileReader()
  118. ,formData = new FormData()
  119. ,xhr = new XMLHttpRequest();
  120. formData.append("file", file);
  121. formData.append("filename", filename);
  122. xhr.onreadystatechange = function() {
  123. if (xhr.readyState === 4) {
  124. if (xhr.status === 204) {
  125. callback(null);
  126. } else {
  127. callback(xhr.statusText);
  128. }
  129. }
  130. }
  131. xhr.open('POST', 'api/file?room=' +chan.id);
  132. xhr.send(formData);
  133. }
  134. /**
  135. * @param {Room} chan
  136. * @param {Command!} cmd
  137. * @param {string} args
  138. **/
  139. function doCommand(chan, cmd, args) {
  140. var xhr = new XMLHttpRequest()
  141. ,url = 'api/cmd?room=' +chan.id +"&cmd=" +encodeURIComponent(cmd.name.substr(1)) +"&args=" +encodeURIComponent(args);
  142. xhr.open('POST', url, true);
  143. xhr.send(null);
  144. }
  145. /**
  146. * @param {Room} chan
  147. * @param {string} msg
  148. * @param {Message|null=} replyTo
  149. **/
  150. function sendMsg(chan, msg, replyTo) {
  151. var xhr = new XMLHttpRequest();
  152. var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
  153. if (replyTo) {
  154. var sender = SLACK.context.users[replyTo.userId]
  155. ,footer = "Message";
  156. if (chan.id[0] === 'C') {
  157. footer = "Channel message";
  158. } else if (chan.id[0] === 'D') {
  159. footer = "Direct message";
  160. } else if (chan.id[0] === 'G') {
  161. footer = "Group message";
  162. }
  163. var attachment = {
  164. "fallback": replyTo.text
  165. ,"author_name": "<@" +sender.id +"|" +sender.name +">"
  166. ,"author_icon": sender.icons.small
  167. ,"text": replyTo.text
  168. ,"footer": footer
  169. ,"ts": replyTo.ts
  170. };
  171. url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
  172. }
  173. xhr.open('POST', url, true);
  174. xhr.send(null);
  175. }
  176. /**
  177. * @param {string} input
  178. * @param {boolean=} skipCommand
  179. * @return {boolean} true on recognized input
  180. **/
  181. function onTextEntered(input, skipCommand) {
  182. var success = true;
  183. if (EDITING) {
  184. editMsg(SELECTED_ROOM, input, EDITING);
  185. return true;
  186. }
  187. if (input[0] === '/' && skipCommand !== true) {
  188. var endCmd = input.indexOf(' ')
  189. ,cmd = input.substr(0, endCmd === -1 ? undefined : endCmd)
  190. ,args = endCmd === -1 ? "" : input.substr(endCmd)
  191. ,cmdObject = SLACK.context.commands.data[cmd];
  192. if (cmdObject) {
  193. doCommand(SELECTED_ROOM, cmdObject, args.trim());
  194. return true;
  195. }
  196. return false;
  197. }
  198. sendMsg(SELECTED_ROOM, input, REPLYING_TO);
  199. return true;
  200. }
  201. /**
  202. * @param {Room} chan
  203. * @param {string} text
  204. * @param {Message|null=} msg
  205. **/
  206. function editMsg(chan, text, msg) {
  207. var xhr = new XMLHttpRequest();
  208. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id +"&text=" +encodeURIComponent(text);
  209. xhr.open('PUT', url, true);
  210. xhr.send(null);
  211. }
  212. /**
  213. * @param {Room} chan
  214. * @param {Message|null=} msg
  215. **/
  216. function removeMsg(chan, msg) {
  217. var xhr = new XMLHttpRequest();
  218. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id;
  219. xhr.open('DELETE', url, true);
  220. xhr.send(null);
  221. }
  222. /**
  223. * @param {Room} chan
  224. * @param {number} ts
  225. **/
  226. function sendReadMArker(chan, ts) {
  227. var xhr = new XMLHttpRequest();
  228. var url = 'api/markread?room=' +chan.id +"&ts=" +ts;
  229. xhr.open('POST', url, true);
  230. xhr.send(null);
  231. }
  232. /**
  233. * @param {string} channelId
  234. * @param {string} msgId
  235. * @param {string} reaction
  236. **/
  237. function addReaction(channelId, msgId, reaction) {
  238. var xhr = new XMLHttpRequest();
  239. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  240. xhr.open('POST', url, true);
  241. xhr.send(null);
  242. }
  243. /**
  244. * @param {string} channelId
  245. * @param {string} msgId
  246. * @param {string} reaction
  247. **/
  248. function removeReaction(channelId, msgId, reaction) {
  249. var xhr = new XMLHttpRequest();
  250. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  251. xhr.open('DELETE', url, true);
  252. xhr.send(null);
  253. }