workflow.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_" +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("room_" +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 {string} payload
  136. * @param {string} serviceId
  137. * @param {(function((string|null)))=} callback
  138. **/
  139. function sendCommand(payload, serviceId, callback) {
  140. var formData = new FormData()
  141. ,xhr = new XMLHttpRequest();
  142. formData.append("payload", payload);
  143. formData.append("service_id", serviceId);
  144. if (callback) {
  145. xhr.onreadystatechange = function() {
  146. if (xhr.readyState === 4) {
  147. if (xhr.status === 204) {
  148. callback(null);
  149. } else {
  150. callback(xhr.statusText);
  151. }
  152. }
  153. }
  154. }
  155. xhr.open('POST', "api/attachmentAction");
  156. xhr.send(formData);
  157. }
  158. function getActionPayload(channelId, msg, attachment, action) {
  159. var payload = {
  160. "actions": [ action ]
  161. ,"attachment_id": attachment["id"]
  162. ,"callback_id": attachment["callback_id"]
  163. ,"channel_id": channelId
  164. ,"is_ephemeral": msg instanceof NoticeMessage
  165. ,"message_ts": msg["id"]
  166. };
  167. return JSON.stringify(payload);
  168. }
  169. /**
  170. * @param {Room} chan
  171. * @param {Command!} cmd
  172. * @param {string} args
  173. **/
  174. function doCommand(chan, cmd, args) {
  175. var xhr = new XMLHttpRequest()
  176. ,url = 'api/cmd?room=' +chan.id +"&cmd=" +encodeURIComponent(cmd.name.substr(1)) +"&args=" +encodeURIComponent(args);
  177. xhr.open('POST', url, true);
  178. xhr.send(null);
  179. }
  180. /**
  181. * @param {Room} chan
  182. * @param {string} msg
  183. * @param {Message|null=} replyTo
  184. **/
  185. function sendMsg(chan, msg, replyTo) {
  186. var xhr = new XMLHttpRequest();
  187. var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
  188. if (replyTo) {
  189. var sender = SLACK.context.users[replyTo.userId]
  190. ,footer = "Message";
  191. if (chan.id[0] === 'C') {
  192. footer = "Channel message";
  193. } else if (chan.id[0] === 'D') {
  194. footer = "Direct message";
  195. } else if (chan.id[0] === 'G') {
  196. footer = "Group message";
  197. }
  198. var attachment = {
  199. "fallback": replyTo.text
  200. ,"author_name": "<@" +sender.id +"|" +sender.name +">"
  201. ,"author_icon": sender.icons.small
  202. ,"text": replyTo.text
  203. ,"footer": footer
  204. ,"ts": replyTo.ts
  205. };
  206. url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
  207. }
  208. xhr.open('POST', url, true);
  209. xhr.send(null);
  210. }
  211. /**
  212. * @param {string} input
  213. * @param {boolean=} skipCommand
  214. * @return {boolean} true on recognized input
  215. **/
  216. function onTextEntered(input, skipCommand) {
  217. var success = true;
  218. if (EDITING) {
  219. editMsg(SELECTED_ROOM, input, EDITING);
  220. return true;
  221. }
  222. if (input[0] === '/' && skipCommand !== true) {
  223. var endCmd = input.indexOf(' ')
  224. ,cmd = input.substr(0, endCmd === -1 ? undefined : endCmd)
  225. ,args = endCmd === -1 ? "" : input.substr(endCmd)
  226. ,cmdObject = SLACK.context.commands.data[cmd];
  227. if (cmdObject) {
  228. doCommand(SELECTED_ROOM, cmdObject, args.trim());
  229. return true;
  230. }
  231. return false;
  232. }
  233. sendMsg(SELECTED_ROOM, input, REPLYING_TO);
  234. return true;
  235. }
  236. /**
  237. * @param {Room} chan
  238. * @param {string} text
  239. * @param {Message|null=} msg
  240. **/
  241. function editMsg(chan, text, msg) {
  242. var xhr = new XMLHttpRequest();
  243. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id +"&text=" +encodeURIComponent(text);
  244. xhr.open('PUT', url, true);
  245. xhr.send(null);
  246. }
  247. /**
  248. * @param {Room} chan
  249. * @param {Message|null=} msg
  250. **/
  251. function removeMsg(chan, msg) {
  252. var xhr = new XMLHttpRequest();
  253. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id;
  254. xhr.open('DELETE', url, true);
  255. xhr.send(null);
  256. }
  257. /**
  258. * @param {Room} chan
  259. * @param {number} ts
  260. **/
  261. function sendReadMArker(chan, ts) {
  262. var xhr = new XMLHttpRequest();
  263. var url = 'api/markread?room=' +chan.id +"&ts=" +ts;
  264. xhr.open('POST', url, true);
  265. xhr.send(null);
  266. }
  267. /**
  268. * @param {string} channelId
  269. * @param {string} msgId
  270. * @param {string} reaction
  271. **/
  272. function addReaction(channelId, msgId, reaction) {
  273. var xhr = new XMLHttpRequest();
  274. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  275. xhr.open('POST', url, true);
  276. xhr.send(null);
  277. }
  278. /**
  279. * @param {string} channelId
  280. * @param {string} msgId
  281. * @param {string} reaction
  282. **/
  283. function removeReaction(channelId, msgId, reaction) {
  284. var xhr = new XMLHttpRequest();
  285. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  286. xhr.open('DELETE', url, true);
  287. xhr.send(null);
  288. }