workflow.js 8.5 KB

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