| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- var
- /**
- * @type {number} next period to wait before next retry in case of failure, in seconds
- **/
- NEXT_RETRY = 0
- /**
- * @type {SlackChan|SlackIms|SlackGroup}
- **/
- ,SELECTED_ROOM = null
- ;
- /**
- * @param {SlackChan|SlackIms|SlackGroup} room
- * @param {function(boolean)} cb
- **/
- function fetchHistory(room, cb) {
- var xhr = new XMLHttpRequest();
- xhr.open('GET', 'api/hist?room=' +room.id, true);
- xhr.send(null);
- }
- function poll(callback) {
- var xhr = new XMLHttpRequest();
- xhr.timeout = 1000 * 60 * 1; // 3 min timeout
- xhr.onreadystatechange = function(e) {
- if (xhr.readyState === 4) {
- if (xhr.status === 0) {
- if (NEXT_RETRY) {
- NEXT_RETRY = 0;
- onNetworkStateUpdated(true);
- }
- poll(callback); // retry on timeout
- return;
- }
- var resp = null
- ,success = Math.floor(xhr.status / 100) === 2;
- if (success) {
- if (NEXT_RETRY) {
- NEXT_RETRY = 0;
- onNetworkStateUpdated(true);
- }
- resp = xhr.response;
- try {
- resp = JSON.parse(/** @type {string} */ (resp));
- } catch (e) {
- resp = null;
- }
- } else {
- if (NEXT_RETRY) {
- NEXT_RETRY += Math.floor((NEXT_RETRY || 5)/2);
- NEXT_RETRY = Math.min(60, NEXT_RETRY);
- } else {
- NEXT_RETRY = 5;
- onNetworkStateUpdated(false);
- }
- }
- callback(success, resp);
- }
- };
- xhr.open('GET', 'api?v=' +SLACK.lastServerVersion, true);
- xhr.send(null);
- }
- /**
- * @param {boolean} success
- * @param {*} response
- **/
- function onPollResponse(success, response) {
- if (success) {
- if (response) {
- SLACK.update(response);
- }
- startPolling();
- } else {
- setTimeout(startPolling, NEXT_RETRY * 1000);
- }
- }
- function startPolling() {
- poll(onPollResponse);
- }
- /**
- * @param {SlackChan|SlackIms|SlackGroup} room
- **/
- function selectRoom(room) {
- if (SELECTED_ROOM)
- unselectRoom();
- document.getElementById(room.id).classList.add(R.klass.selected);
- document.body.classList.remove(R.klass.noRoomSelected);
- SELECTED_ROOM = room;
- onRoomSelected();
- if (SELECTED_ROOM.lastRead && !SLACK.history[SELECTED_ROOM.id])
- fetchHistory(SELECTED_ROOM, function(success) {});
- }
- function unselectRoom() {
- document.getElementById(SELECTED_ROOM.id).classList.remove(R.klass.selected);
- }
- /**
- * @param {SlackGroup|SlackChan|SlackIms} chan
- * @param {string} filename
- * @param {File} file
- * @param {function(string?)} callback
- **/
- function uploadFile(chan, filename, file, callback) {
- var fileReader = new FileReader()
- ,formData = new FormData()
- ,xhr = new XMLHttpRequest();
- formData.append("file", file);
- formData.append("filename", filename);
- xhr.onreadystatechange = function() {
- if (xhr.readyState === 4) {
- if (xhr.status === 204) {
- callback(null);
- } else {
- callback(xhr.statusText);
- }
- }
- }
- xhr.open('POST', 'api/file?room=' +chan.id);
- xhr.send(formData);
- }
- /**
- * @param {SlackChan|SlackGroup|SlackIms} chan
- * @param {string} cmd
- * @param {string} args
- **/
- function doCommand(chan, cmd, args) {
- if (cmd == "me") {
- if (REPLYING_TO)
- return false;
- sendMeMessage(chan, args);
- return true;
- } else if (cmd == "shrug") {
- if (args.length)
- args += ' ';
- return onTextEntered(args + "¯_(ツ)_/¯", true);
- }
- console.log("Unknown command " +cmd);
- return false;
- }
- /**
- * @param {SlackChan|SlackGroup|SlackIms} chan
- * @param {string} msg
- * @param {SlackMessage|null=} replyTo
- **/
- function sendMsg(chan, msg, replyTo) {
- var xhr = new XMLHttpRequest();
- var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
- if (replyTo) {
- var sender = SLACK.context.getMember(replyTo.userId)
- ,footer = "Message";
- if (chan.id[0] === 'C') {
- footer = "Channel message";
- } else if (chan.id[0] === 'D') {
- footer = "Direct message";
- } else if (chan.id[0] === 'G') {
- footer = "Group message";
- }
- var attachment = {
- "fallback": replyTo.text
- ,"author_name": "<@" +sender.id +"|" +sender.name +">"
- ,"author_icon": sender.icons.image_48
- ,"text": replyTo.text
- ,"footer": footer
- ,"ts": replyTo.ts
- };
- url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
- }
- xhr.open('POST', url, true);
- xhr.send(null);
- }
- /**
- * @param {SlackChan|SlackGroup|SlackIms} chan
- * @param {string} msg
- **/
- function sendMeMessage(chan, msg) {
- var xhr = new XMLHttpRequest()
- ,url = 'api/msg?room=' +chan.id +"&me&text=" +encodeURIComponent(msg);
- xhr.open('POST', url, true);
- xhr.send(null);
- }
- /**
- * @param {string} input
- * @param {boolean=} skipCommand
- * @return {boolean} true on recognized input
- **/
- function onTextEntered(input, skipCommand) {
- var success = true;
- if (EDITING) {
- editMsg(SELECTED_ROOM, input, EDITING);
- return true;
- }
- if (input[0] === '/' && skipCommand !== true) {
- var endCmd = input.indexOf(' ')
- ,cmd = input.substr(1, endCmd === -1 ? undefined : endCmd -1)
- ,args = endCmd === -1 ? "" : input.substr(endCmd);
- return doCommand(SELECTED_ROOM, cmd, args.trim());
- }
- sendMsg(SELECTED_ROOM, input, REPLYING_TO);
- return true;
- }
- /**
- * @param {SlackChan|SlackGroup|SlackIms} chan
- * @param {string} text
- * @param {SlackMessage|null=} msg
- **/
- function editMsg(chan, text, msg) {
- var xhr = new XMLHttpRequest();
- var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id +"&text=" +encodeURIComponent(text);
- xhr.open('PUT', url, true);
- xhr.send(null);
- }
- /**
- * @param {SlackChan|SlackGroup|SlackIms} chan
- * @param {SlackMessage|null=} msg
- **/
- function removeMsg(chan, msg) {
- var xhr = new XMLHttpRequest();
- var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id;
- xhr.open('DELETE', url, true);
- xhr.send(null);
- }
- /**
- * @param {string} channelId
- * @param {string} msgId
- * @param {string} reaction
- **/
- function addReaction(channelId, msgId, reaction) {
- var xhr = new XMLHttpRequest();
- var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
- xhr.open('POST', url, true);
- xhr.send(null);
- }
- /**
- * @param {string} channelId
- * @param {string} msgId
- * @param {string} reaction
- **/
- function removeReaction(channelId, msgId, reaction) {
- var xhr = new XMLHttpRequest();
- var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
- xhr.open('DELETE', url, true);
- xhr.send(null);
- }
|