workflow.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* jshint sub: true */
  2. var
  3. /**
  4. * @type {number} next period to wait before next retry in case of failure, in seconds
  5. **/
  6. NEXT_RETRY = 0,
  7. /**
  8. * @type {Room|null}
  9. **/
  10. SELECTED_ROOM = null,
  11. /**
  12. * @type {SimpleChatSystem|null}
  13. **/
  14. SELECTED_CONTEXT = null,
  15. /** @type {Message|null} */
  16. REPLYING_TO = null,
  17. /** @type {Message|null} */
  18. EDITING = null,
  19. /** @const @type {number} */
  20. KEEP_MESSAGES = 100,
  21. /** @type {Array<{channel: string, text: string, isMe: boolean, pendingId: (string|undefined), dom: Element}>} */
  22. PENDING_MESSAGES = []
  23. ;
  24. function initHljs() {
  25. new HttpRequest("highlight.pack.js")
  26. .callbackSuccess(function(code, head, resp) {
  27. var script = document.createElement("script"),
  28. link = document.createElement("link");
  29. script.innerHTML = resp;
  30. script.language = "text/javascript";
  31. link.href = "hljs-androidstudio.css";
  32. link.rel = "stylesheet";
  33. document.head.appendChild(link);
  34. document.body.appendChild(script);
  35. })
  36. .callbackError(function() {
  37. console.error("Failure loading hljs required files");
  38. })
  39. .setTimeout(1000 * 60 * 1) // 1 min timeout
  40. .send();
  41. }
  42. /**
  43. * @param {Room} room
  44. * @param {function(boolean)} cb
  45. **/
  46. function fetchHistory(room, cb) {
  47. new HttpRequest("api/hist?room=" +room.id)
  48. .setResponseType(HttpRequestResponseType.JSON)
  49. .callbackSuccess(function(code, status, resp) {
  50. if (resp) {
  51. var history = DATA.history[room.id],
  52. updated,
  53. now = Date.now();
  54. if (!history) {
  55. history = DATA.history[room.id] = new UiRoomHistory(room, KEEP_MESSAGES, /** @type {Array} */ (resp), now);
  56. updated = true;
  57. } else {
  58. updated = !!history.pushAll(/** @type {Array} */ (resp), now);
  59. }
  60. if (updated) {
  61. if (history.messages.length)
  62. room.setLastMsg(history.lastMessage().ts, now);
  63. onMsgReceived(DATA.context.getChannelContext(room.id).getChatContext(), room, /** @type {Array} */ (resp));
  64. if (room === SELECTED_ROOM)
  65. onRoomUpdated();
  66. }
  67. }
  68. }, this)
  69. .callback(function() {
  70. // TODO ui stop loading
  71. })
  72. .send();
  73. }
  74. function onConfigUpdated() {
  75. if (isObjectEmpty(CONFIG.services)) {
  76. Settings.setClosable(false).display(Settings.pages.services);
  77. }
  78. loadEmojiProvider(CONFIG.getEmojiProvider());
  79. document.getElementById(R.id.stylesheet).innerHTML = CONFIG.compileCSS();
  80. }
  81. function poll(callback) {
  82. new HttpRequest("api?v=" +DATA.lastServerVersion)
  83. .callback(function(statusCode, statusText, resp) {
  84. var success = Math.floor(statusCode / 100) === 2;
  85. if (success) {
  86. if (NEXT_RETRY) {
  87. NEXT_RETRY = 0;
  88. onNetworkStateUpdated(true);
  89. }
  90. } else {
  91. if (NEXT_RETRY) {
  92. NEXT_RETRY += Math.floor((NEXT_RETRY || 5)/2);
  93. NEXT_RETRY = Math.min(60, NEXT_RETRY);
  94. } else {
  95. NEXT_RETRY = 5;
  96. onNetworkStateUpdated(false);
  97. }
  98. }
  99. callback(success, resp);
  100. })
  101. .setResponseType(HttpRequestResponseType.JSON)
  102. .setTimeout(1000 * 60 * 1)
  103. .send();
  104. }
  105. function outOfSync() {
  106. DATA.lastServerVersion = 0;
  107. }
  108. /**
  109. * @param {Room} room
  110. **/
  111. function sendTyping(room) {
  112. new HttpRequest(HttpRequestMethod.POST, "api/typing?room=" +room.id).send();
  113. }
  114. /**
  115. * @param {boolean} success
  116. * @param {*} response
  117. **/
  118. function onPollResponse(success, response) {
  119. if (success) {
  120. if (response) {
  121. DATA.update(response);
  122. }
  123. startPolling();
  124. } else {
  125. setTimeout(startPolling, NEXT_RETRY * 1000);
  126. }
  127. }
  128. function startPolling() {
  129. poll(onPollResponse);
  130. }
  131. /**
  132. * @param {Room} room
  133. **/
  134. function selectRoom(room) {
  135. if (SELECTED_ROOM)
  136. unselectRoom();
  137. document.getElementById("room_" +room.id).classList.add(R.klass.selected);
  138. document.body.classList.remove(R.klass.noRoomSelected);
  139. SELECTED_ROOM = room;
  140. SELECTED_CONTEXT = /** @type {SimpleChatSystem} */ (DATA.context.getChannelContext(room.id));
  141. onRoomSelected();
  142. roomInfo.hide();
  143. createContextBackground(SELECTED_CONTEXT.getChatContext().team.id, SELECTED_CONTEXT.getChatContext().users, function(imgData) {
  144. document.getElementById(R.id.context).style.backgroundImage = 'url(' +imgData +')';
  145. });
  146. if (!DATA.history[SELECTED_ROOM.id] || DATA.history[SELECTED_ROOM.id].messages.length < KEEP_MESSAGES)
  147. fetchHistory(SELECTED_ROOM, function(success) {});
  148. document.getElementById(R.id.mainSection).classList.remove(R.klass.noRoomSelected);
  149. document.getElementById(R.id.context).classList.remove(R.klass.opened);
  150. }
  151. function setRoomFromHashBang() {
  152. var hashId = document.location.hash.substr(1),
  153. room = DATA.context.getChannel(hashId);
  154. if (room && room !== SELECTED_ROOM)
  155. selectRoom(room);
  156. else {
  157. var user = DATA.context.getUser(hashId);
  158. if (user && user.privateRoom)
  159. selectRoom(user.privateRoom);
  160. }
  161. }
  162. /**
  163. * @param {Room} room
  164. **/
  165. function starChannel(room) {
  166. new HttpRequest(HttpRequestMethod.POST, "api/starChannel?room=" +room.id).send();
  167. }
  168. /**
  169. * @param {Room} room
  170. **/
  171. function unstarChannel(room) {
  172. new HttpRequest(HttpRequestMethod.POST, "api/unstarChannel?room=" +room.id).send();
  173. }
  174. function unselectRoom() {
  175. document.getElementById("room_" +SELECTED_ROOM.id).classList.remove(R.klass.selected);
  176. document.getElementById(R.id.mainSection).classList.add(R.klass.noRoomSelected);
  177. }
  178. /**
  179. * @param {Room} chan
  180. * @param {string} filename
  181. * @param {File} file
  182. * @param {function(string?)} callback
  183. **/
  184. function uploadFile(chan, filename, file, callback) {
  185. var fileReader = new FileReader(),
  186. formData = new FormData();
  187. formData.append("file", file);
  188. formData.append("filename", filename);
  189. new HttpRequest(HttpRequestMethod.POST, "api/file?room=" +chan.id)
  190. .callbackSuccess(function() { callback(null); })
  191. .callbackError(function(sCode, sText, resp) { callback(sText); })
  192. .send(formData);
  193. }
  194. /**
  195. * @param {string} payload
  196. * @param {string} serviceId
  197. * @param {(function((string|null)))=} callback
  198. **/
  199. function sendCommand(payload, serviceId, callback) {
  200. var xhr = new HttpRequest(HttpRequestMethod.POST, "api/attachmentAction?serviceId=" +serviceId);
  201. if (callback)
  202. xhr.callbackSuccess(function() {
  203. callback(null);
  204. }).callbackError(function(code, head, resp) {
  205. callback(head);
  206. });
  207. xhr.send(JSON.stringify(payload));
  208. }
  209. function getActionPayload(channelId, msg, attachment, action) {
  210. var payload = {
  211. "actions": [ action ],
  212. "attachment_id": attachment["id"],
  213. "callback_id": attachment["callback_id"],
  214. "channel_id": channelId,
  215. "is_ephemeral": msg instanceof NoticeMessage,
  216. "message_ts": msg["id"]
  217. };
  218. return payload;
  219. }
  220. /**
  221. * @param {Room} chan
  222. * @param {Command!} cmd
  223. * @param {string} args
  224. **/
  225. function doCommand(chan, cmd, args) {
  226. new HttpRequest(HttpRequestMethod.POST, "api/cmd?room=" +chan.id +"&cmd=" +encodeURIComponent(cmd.name.substr(1)) +"&args=" +encodeURIComponent(args)).send();
  227. }
  228. /**
  229. * @param {Room} chan
  230. * @param {string} msg
  231. * @param {Object=} pendingObj
  232. * @param {Message|null=} replyTo
  233. **/
  234. function sendMsg(chan, msg, pendingObj, replyTo) {
  235. var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
  236. if (replyTo) {
  237. var sender = DATA.context.getUser(replyTo.userId),
  238. footer = chan.isPrivate ? locale.message : chan.name;
  239. var attachment = {
  240. "fallback": replyTo.text,
  241. "author_name": sender.getName(),
  242. "text": replyTo.text,
  243. "footer": footer,
  244. "ts": replyTo.ts
  245. };
  246. url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
  247. }
  248. new HttpRequest(HttpRequestMethod.POST, url).setResponseType(HttpRequestResponseType.JSON).callbackSuccess(function(code, text, resp) {
  249. if (resp && resp["pendingId"] !== undefined) {
  250. pendingObj.pendingId = resp["pendingId"];
  251. }
  252. }).send();
  253. }
  254. /**
  255. * @param {Room} chan
  256. * @param {string} text
  257. * @param {Message} msg
  258. **/
  259. function editMsg(chan, text, msg) {
  260. new HttpRequest(HttpRequestMethod.PUT, "api/msg?room=" +chan.id +"&ts=" +msg.id +"&text=" +encodeURIComponent(text)).send();
  261. }
  262. /**
  263. * @param {Room} chan
  264. * @param {Message} msg
  265. **/
  266. function removeMsg(chan, msg) {
  267. new HttpRequest(HttpRequestMethod.DELETE, "api/msg?room=" +chan.id +"&ts=" +msg.id).send();
  268. }
  269. /**
  270. * @param {Room} chan
  271. * @param {Message} msg
  272. **/
  273. function pinMsg(chan, msg) {
  274. new HttpRequest(HttpRequestMethod.POST, "api/pinMsg?room=" +chan.id +"&msgId=" +msg.id).send();
  275. }
  276. /**
  277. * @param {Room} chan
  278. * @param {Message} msg
  279. **/
  280. function starMsg(chan, msg) {
  281. new HttpRequest(HttpRequestMethod.POST, "api/starMsg?room=" +chan.id +"&msgId=" +msg.id).send();
  282. }
  283. /**
  284. * @param {Room} chan
  285. * @param {Message} msg
  286. **/
  287. function unpinMsg(chan, msg) {
  288. new HttpRequest(HttpRequestMethod.DELETE, "api/pinMsg?room=" +chan.id +"&msgId=" +msg.id).send();
  289. }
  290. /**
  291. * @param {Room} chan
  292. * @param {Message} msg
  293. **/
  294. function unstarMsg(chan, msg) {
  295. new HttpRequest(HttpRequestMethod.DELETE, "api/starMsg?room=" +chan.id +"&msgId=" +msg.id).send();
  296. }
  297. /**
  298. * @param {Room} chan
  299. * @param {string} id
  300. * @param {number} ts
  301. **/
  302. function sendReadMarker(chan, id, ts) {
  303. new HttpRequest(HttpRequestMethod.POST, "api/markread?room=" +chan.id +"&id=" +id +"&ts=" +ts).send();
  304. }
  305. /**
  306. * @param {string} channelId
  307. * @param {string} msgId
  308. * @param {string} reaction
  309. **/
  310. function addReaction(channelId, msgId, reaction) {
  311. new HttpRequest(HttpRequestMethod.POST, "api/reaction?room=" +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction)).send();
  312. }
  313. /**
  314. * @param {string} channelId
  315. * @param {string} msgId
  316. * @param {string} reaction
  317. **/
  318. function removeReaction(channelId, msgId, reaction) {
  319. new HttpRequest(HttpRequestMethod.DELETE, "api/reaction?room=" +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction)).send();
  320. }
  321. function logout() {
  322. new HttpRequest(HttpRequestMethod.POST, "api/logout").send();
  323. document.cookie = "sessID=;Path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
  324. document.location.reload();
  325. }
  326. /**
  327. * @this {Element}
  328. **/
  329. function filterChanList() {
  330. var chans = {},
  331. matchingChans = [],
  332. val = this.value;
  333. DATA.context.foreachChannels(function(chan) {
  334. chans[chan.id] = chan.matchString(val, Utils);
  335. });
  336. for (var chanId in chans) {
  337. var chanDom = document.getElementById("room_" +chanId);
  338. if (chanDom) {
  339. if (chans[chanId].name + chans[chanId].members + chans[chanId].topic +chans[chanId].purpose) {
  340. chanDom.classList.remove(R.klass.hidden);
  341. matchingChans.push(chanId);
  342. } else {
  343. chanDom.classList.add(R.klass.hidden);
  344. }
  345. }
  346. }
  347. //TODO sort
  348. }