1
0

workflow.js 11 KB

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