slack.min.js 7.5 KB

1234567891011121314151617
  1. function SlackTeam(a){this.id=a.id;this.name=a.name;this.domain=a.domain;this.callApp=a.prefs.calling_app_id;this.callAppName=a.prefs.calling_app_name;this.fileUploadPermission=a.prefs.disable_file_uploads;this.fileEditPermission=a.prefs.disable_file_editing;this.fileDeletePermission=a.prefs.disable_file_deleting;this.icons={image_34:a.icon.image_34,image_44:a.icon.image_44,image_68:a.icon.image_68,image_88:a.icon.image_88,image_102:a.icon.image_102,image_132:a.icon.image_132,image_230:a.icon.image_230,
  2. image_default:a.icon.image_default}}
  3. function SlackChan(a,c){this.id=a.id;this.name=a.name;this.created=a.created;this.creator=c.getMember(a.creator);this.archived=a.is_archived;this.isMember=a.is_member;this.lastRead=a.last_read;this.members={};if(a.members)for(var b=0,d=a.members.length;b<d;b++){var e=c.getMember(a.members[b]);this.members[e.id]=e;e.channels[this.id]=this}a.topic&&(this.topic=a.topic.value,this.topicCreator=c.getMember(a.topic.creator),this.topicTs=a.topic.last_set);a.purpose&&(this.purpose=a.purpose.value,this.purposeCreator=
  4. c.getMember(a.purpose.creator),this.purposeTs=a.purpose.last_set)}function SlackGroup(a){}function SlackIms(a){}
  5. function SlackUser(a){this.id=a.id;this.name=a.name;this.deleted=a.deleted;this.status=a.status;this.realName=a.real_name||a.profile.real_name;this.presence="away"!==a.presence;this.icons={image_24:a.profile.image_24,image_32:a.profile.image_32,image_48:a.profile.image_48,image_72:a.profile.image_72,image_192:a.profile.image_192,image_512:a.profile.image_512};this.email=a.profile.email;this.firstName=a.profile.first_name;this.lastName=a.profile.last_name;this.channels={}}
  6. function SlackBot(a){this.id=a.id;this.deleted=a.deleted;this.name=a.name;this.appId=a.app_id;this.icons={image_36:a.icons.image_36,image_48:a.icons.image_48,image_72:a.icons.image_72};this.channels={}}function SlackHistory(a){this.id=a.id;this.target=a;this.v=0;this.messages=[]}function SlackData(a){this.team=null;this.channels={};this.groups=[];this.ims=[];this.users={};this.self=null;this.bots={};this.history={};this.slack=a;this.liveV=this.staticV=0}
  7. SlackTeam.prototype.toStatic=function(){return{id:this.id,name:this.name,domain:this.domain,prefs:{calling_app_id:this.callApp,calling_app_name:this.callAppName,disable_file_uploads:this.fileUploadPermission,disable_file_editing:this.fileEditPermission,disable_file_deleting:this.fileDeletePermission},icon:this.icons}};SlackHistory.prototype.update=function(a){};SlackHistory.prototype.getUpdates=function(a){};
  8. SlackChan.prototype.toStatic=function(){var a={id:this.id,name:this.name,created:this.created,creator:this.creator.id,is_archived:this.archived,is_member:this.isMember,last_read:this.lastRead};this.isMember&&(a.members=Object.keys(this.members),a.topic={value:this.topic,creator:this.topicCreator?this.topicCreator.id:null,last_set:this.topicTs},a.purpose={value:this.purpose,creator:this.purposeCreator?this.purposeCreator.id:null,last_set:this.purposeTs});return a};
  9. SlackUser.prototype.toStatic=function(){return{id:this.id,name:this.name,deleted:this.deleted,status:this.status,real_name:this.realName,presence:this.presence?"present":"away",profile:{email:this.email,first_name:this.firstName,last_name:this.lastName,image_24:this.icons.image_24,image_32:this.icons.image_32,image_48:this.icons.image_48,image_72:this.icons.image_72,image_192:this.icons.image_192,image_512:this.icons.image_512}}};
  10. SlackData.prototype.updateStatic=function(a,c){for(var b=0,d=a.bots.length;b<d;b++)this.bots[a.bots[b].id]=new SlackBot(a.bots[b]);this.team=new SlackTeam(a.team);b=0;for(d=a.users.length;b<d;b++)this.users[a.users[b].id]=new SlackUser(a.users[b]);b=0;for(d=a.channels.length;b<d;b++)this.channels[a.channels[b].id]=new SlackChan(a.channels[b],this);this.staticV=parseFloat(a.latest_event_ts);this.self=this.getMember(a.self.id);if(this.slack){var e={},d=!1;for(b in this.channels)this.history[b]||(d=
  11. !0,e[b]=this.channels[b]);if(d)for(b in e){var f=this;this.slack.fetchHistory(b,function(a,b){if(null!=b){f.setHistory(e[a],b);e[a]=null;for(var d in e)if(e[d])return}c()})}else c()}};SlackData.prototype.setHistory=function(a,c){this.history[a.id]||(this.history[a.id]=new SlackHistory(a));this.history[a.id].update(c)};SlackData.prototype.getBotsByAppId=function(a){var c=[],b;for(b in this.bots)this.bots[b].appId===a&&c.push(this.bots[b]);return c};
  12. SlackData.prototype.getMember=function(a){return this.users[a]||this.bots[a]||null};SlackData.prototype.buildLive=function(a){var c={},b;for(b in this.history)this.history[b].v>a&&(c[b]=this.history[b].getUpdates(a));return c};
  13. SlackData.prototype.buildStatic=function(){var a={team:this.team.toStatic(),channels:[],groups:[],ims:[],users:[],bots:[],self:{id:this.self.id}},c;for(c in this.channels)a.channels.push(this.channels[c].toStatic());for(var b in this.users)a.users.push(this.users[b].toStatic());return a};SlackData.prototype.getUpdates=function(a){var c={};this.liveV>a&&(c.live=this.buildLive(a));this.staticV>a&&(c["static"]=this.buildStatic());return c};
  14. (function(){"undefined"!==typeof module&&(module.exports.SlackData=SlackData)})();var R={id:{chanList:"chanList",chatList:"chatList",currentRoom:{title:"currentRoomTitle"}},klass:{selected:"selected",noRoomSelected:"no-room-selected",chatList:{entry:"slack-context-room"}}};function createChanListItem(a){var c=document.createElement("li");c.id=a.id;c.className=R.klass.chatList.entry;c.textContent=a.name;return c}function onContextUpdated(){var a=document.createDocumentFragment(),c;for(c in SLACK.context.self.channels){var b=createChanListItem(SLACK.context.channels[c]);b&&a.appendChild(b)}document.getElementById(R.id.chanList).textContent="";document.getElementById(R.id.chanList).appendChild(a)}
  15. function onRoomSelected(){var a=SELECTED_ROOM.name;if(!a){var a=[],c;for(c in SELECTED_ROOM.members)a.push(SELECTED_ROOM.members[c].name);a=a.join(", ")}document.getElementById(R.id.currentRoom.title).textContent=a}
  16. function onChanClick(a){for(;a.target!==a.currentTarget&&a.target;){if(a.target.classList.contains(R.klass.chatList.entry)){(a=SLACK.context.channels[a.target.id]||SLACK.context.ims[a.target.id]||SLACK.context.groups[a.target.id])&&a!==SELECTED_ROOM&&selectRoom(a);break}a.target=a.target.parentElement}}document.addEventListener("DOMContentLoaded",function(){document.getElementById(R.id.chatList).addEventListener("click",onChanClick);startPolling()});var SLACK;function SlackWrapper(){this.lastServerVersion=0;this.context=new SlackData(null)}SlackWrapper.prototype.update=function(a){a.v&&(this.lastServerVersion=a.v);a["static"]&&(this.context.updateStatic(a["static"]),onContextUpdated());a.live&&console.log("updated LIVE");console.log(this)};SLACK=new SlackWrapper;var NEXT_RETRY=5,SELECTED_ROOM=null;function poll(a){var c=new XMLHttpRequest;c.timeout=6E4;c.onreadystatechange=function(b){if(4===c.readyState)if(0===c.status)poll(a),NEXT_RETRY=5;else{b=null;var d=2===Math.floor(c.status/100);if(d){NEXT_RETRY=5;b=c.response;try{b=JSON.parse(b)}catch(e){b=null}}else NEXT_RETRY+=Math.floor(NEXT_RETRY/2),NEXT_RETRY=Math.min(60,NEXT_RETRY);a(d,b)}};c.open("GET","api?v="+SLACK.lastServerVersion,!0);c.send(null)}
  17. function onPollResponse(a,c){a?(c&&SLACK.update(c),startPolling()):setTimeout(startPolling,1E3*NEXT_RETRY)}function startPolling(){poll(onPollResponse)}function selectRoom(a){SELECTED_ROOM&&unselectRoom();document.getElementById(a.id).classList.add(R.klass.selected);document.body.classList.remove(R.klass.noRoomSelected);SELECTED_ROOM=a;onRoomSelected()}function unselectRoom(){document.getElementById(SELECTED_ROOM.id).classList.remove(R.klass.selected)};