| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624 |
- /**
- * @constructor
- **/
- function SlackTeam(teamId) {
- /** @const @type {string} */
- this.id = teamId;
- /** @type {string} */
- this.name;
- /** @type {string} */
- this.domain;
- /** @type {string} */
- this.callApp;
- /** @type {string} */
- this.callAppName;
- /** @type {boolean} */
- this.fileUploadPermission;
- /** @type {boolean} */
- this.fileEditPermission;
- /** @type {boolean} */
- this.fileDeletePermission;
- /** @type {Object.<string, string>} */
- this.icons = {
- image_34: ""
- ,image_44: ""
- ,image_68: ""
- ,image_88: ""
- ,image_102: ""
- ,image_132: ""
- ,image_230: ""
- ,image_default: ""
- };
- }
- SlackTeam.prototype.update = function(teamData) {
- this.name = teamData["name"];
- this.domain = teamData["domain"];
- this.callApp = teamData["prefs"]["calling_app_id"];
- this.callAppName = teamData["prefs"]["calling_app_name"];
- this.fileUploadPermission = teamData["prefs"]["disable_file_uploads"];
- this.fileEditPermission = teamData["prefs"]["disable_file_editing"];
- this.fileDeletePermission = teamData["prefs"]["disable_file_deleting"];
- this.icons.image_34 = teamData["icon"]["image_34"];
- this.icons.image_44 = teamData["icon"]["image_44"];
- this.icons.image_68 = teamData["icon"]["image_68"];
- this.icons.image_88 = teamData["icon"]["image_88"];
- this.icons.image_102 = teamData["icon"]["image_102"];
- this.icons.image_132 = teamData["icon"]["image_132"];
- this.icons.image_230 = teamData["icon"]["image_230"];
- this.icons.image_default = teamData["icon"]["image_default"];
- }
- /**
- * @constructor
- **/
- function SlackChan(chanId) {
- /** @const @type {string} */
- this.id = chanId;
- /** @type {string} */
- this.name;
- /** @type {string} */
- this.created;
- /** @type {SlackUser|SlackBot} */
- this.creator;
- /** @type {boolean} */
- this.archived;
- /** @type {boolean} */
- this.isMember;
- /** @type {number} */
- this.lastRead;
- /** @type {Object.<string, SlackBot|SlackUser>} */
- this.members = {};
- /** @type {string|undefined} */
- this.topic;
- /** @type {number|undefined} */
- this.topicTs;
- /** @type {SlackUser|SlackBot|undefined} */
- this.topicCreator;
- /** @type {string|undefined} */
- this.purpose;
- /** @type {number|undefined} */
- this.purposeTs;
- /** @type {SlackUser|SlackBot|undefined} */
- this.purposeCreator;
- }
- /**
- * @param {*} chanData
- * @param {SlackData} slackData
- **/
- SlackChan.prototype.update = function(chanData, slackData) {
- this.name = chanData["name"];
- this.created = chanData["created"];
- this.creator = slackData.getMember(chanData["creator"]);
- this.archived = chanData["is_archived"];
- this.isMember = chanData["is_member"];
- this.lastRead = parseFloat(chanData["last_read"]);
- this.members = {};
- if (chanData["members"]) for (var i =0, nbMembers = chanData["members"].length; i < nbMembers; i++) {
- var member = slackData.getMember(chanData["members"][i]);
- this.members[member.id] = member;
- member.channels[this.id] = this;
- }
- if (chanData["topic"]) {
- this.topic = chanData["topic"]["value"];
- this.topicCreator = slackData.getMember(chanData["topic"]["creator"]);
- this.topicTs = chanData["topic"]["last_set"];
- }
- if (chanData["purpose"]) {
- this.purpose = chanData["purpose"]["value"];
- this.purposeCreator = slackData.getMember(chanData["purpose"]["creator"]);
- this.purposeTs = chanData["purpose"]["last_set"];
- }
- }
- /**
- * @constructor
- * @param {string} id
- **/
- function SlackGroup(id) {
- /** @const @type {string} */
- this.id = id;
- /** @type {Object.<string, SlackUser|SlackBot>} */
- this.members = {};
- /** @type {string} */
- this.name;
- /** @type {number} */
- this.created;
- /** @type {SlackUser|SlackBot} */
- this.creator;
- /** @type {boolean} */
- this.archived;
- /** @type {number} */
- this.lastRead;
- /** @type {string|undefined} */
- this.topic;
- /** @type {number|undefined} */
- this.topicTs;
- /** @type {SlackUser|SlackBot|undefined} */
- this.topicCreator;
- /** @type {string|undefined} */
- this.purpose;
- /** @type {number|undefined} */
- this.purposeTs;
- /** @type {SlackUser|SlackBot|undefined} */
- this.purposeCreator;
- }
- /**
- * @param {SlackData} slack
- * @param {*} groupData
- **/
- SlackGroup.prototype.update = function(slack, groupData) {
- var memberNames = [];
- /** @type {Object.<string, SlackUser|SlackBot>} */
- this.members = {};
- for (var i =0, nbMembers = groupData["members"].length; i < nbMembers; i++) {
- var member = slack.getMember(groupData["members"][i]);
- this.members[groupData["members"][i]] = member;
- member.channels[this.id] = this;
- memberNames.push(member.name);
- }
- this.name = memberNames.join(", ");
- this.created = groupData["created"];
- this.creator = slack.getMember(groupData["creator"]);
- this.archived = groupData["is_archived"];
- this.lastRead = parseFloat(groupData["last_read"]);
- if (groupData["topic"]) {
- this.topic = groupData["topic"]["value"];
- this.topicCreator = slack.getMember(groupData["topic"]["creator"]);
- this.topicTs = groupData["topic"]["last_set"];
- }
- if (groupData["purpose"]) {
- this.purpose = groupData["purpose"]["value"];
- this.purposeCreator = slack.getMember(groupData["purpose"]["creator"]);
- this.purposeTs = groupData["purpose"]["last_set"];
- }
- }
- /**
- * @constructor
- * @param {string} id
- * @param {SlackUser|SlackBot} user
- **/
- function SlackIms(id, user) {
- /** @const @type {string} */
- this.id = id;
- /** @type {number} */
- this.created;
- /** @type {SlackUser|SlackBot} */
- this.user = user;
- /** @type {number} */
- this.lastRead;
- /** @type {boolean} */
- this.archived;
- }
- /**
- * @param {SlackUser|SlackBot} user
- * @param {*} imsData
- **/
- SlackIms.prototype.update = function(user, imsData) {
- this.created = imsData["created"];
- this.lastRead = parseFloat(imsData["last_read"]);
- this.archived = user.deleted;
- }
- /**
- * @constructor
- **/
- function SlackUser(id) {
- /** @const @type {string} */
- this.id = id;
- /** @type {string} */
- this.name;
- /** @type {boolean} */
- this.deleted;
- /** @type {string} */
- this.status;
- /** @type {string} */
- this.realName;
- /** @type {boolean} */
- this.presence;
- /** @type {Object.<string, string>} */
- this.icons = {
- image_24: ""
- ,image_32: ""
- ,image_48: ""
- ,image_72: ""
- ,image_192: ""
- ,image_512: ""
- };
- /** @type {string} */
- this.email;
- /** @type {string} */
- this.firstName;
- /** @type {string} */
- this.lastName;
- /** @type {!Object.<string, SlackChan|SlackGroup>} */
- this.channels = {};
- /** @type {SlackIms} */
- this.ims = null;
- /** @type {SelfPreferences|null} */
- this.prefs = null;
- }
- /**
- * @param {*} userData
- **/
- SlackUser.prototype.update = function(userData) {
- this.name = userData["name"];
- this.deleted = userData["deleted"];
- this.status = userData["status"];
- this.realName = userData["real_name"] || userData["profile"]["real_name"];
- this.presence = userData["presence"] !== 'away';
- this.icons.image_24 = userData["profile"]["image_24"];
- this.icons.image_32 = userData["profile"]["image_32"];
- this.icons.image_48 = userData["profile"]["image_48"];
- this.icons.image_72 = userData["profile"]["image_72"];
- this.icons.image_192 = userData["profile"]["image_192"];
- this.icons.image_512 = userData["profile"]["image_512"];
- this.email = userData["profile"]["email"];
- this.firstName = userData["profile"]["first_name"];
- this.lastName = userData["profile"]["last_name"];
- }
- /**
- * @constructor
- **/
- function SelfPreferences() {
- /** @type {Object.<string, number>} */
- this.favoriteEmojis = {};
- /** @type {Array.<string>} */
- this.highlights = [];
- }
- /**
- * @param {*} prefs
- **/
- SelfPreferences.prototype.update = function(prefs) {
- this.favoriteEmojis = /** @type {Object<string,number>} */ (JSON.parse(prefs["emoji_use"]));
- if (prefs["highlight_words"])
- this.highlights = (prefs["highlight_words"]||"").split(',').filter(function(i) {
- return i.trim() !== '';
- });
- else if (prefs["highlights"])
- this.highlights = prefs["highlights"];
- }
- /**
- * @constructor
- **/
- function SlackBot(id) {
- /** @const @type {string} */
- this.id = id;
- /** @type {boolean} */
- this.deleted;
- /** @type {string} */
- this.name;
- /** @type {string} */
- this.appId;
- /** @type {Object.<string, string>} */
- this.icons = {
- image_36: ""
- ,image_48: ""
- ,image_72: ""
- };
- /** @type {!Object.<string, SlackGroup|SlackChan>} */
- this.channels;
- /** @type {SlackIms|null} */
- this.ims = null;
- /** @type {SelfPreferences|null} */
- this.prefs = null;
- }
- /** @param {*} botData */
- SlackBot.prototype.update = function(botData) {
- this.deleted = botData["deleted"];
- this.name = botData["name"];
- this.appId = botData["app_id"];
- this.icons.image_36 = botData["icons"]["image_36"]
- this.icons.image_48 = botData["icons"]["image_48"]
- this.icons.image_72 = botData["icons"]["image_72"]
- }
- /**
- * @constructor
- **/
- function SlackData(slack) {
- /** @type {SlackTeam} */
- this.team = null;
- /** @type {Object.<string, SlackChan>} */
- this.channels = {};
- /** @type {Object.<string, SlackGroup>} */
- this.groups = {};
- /** @type {Object.<string, SlackIms>} */
- this.ims = {};
- /** @type {Object.<string, SlackUser>} */
- this.users = {};
- /** @type {SlackUser|SlackBot} */
- this.self = null;
- /** @type {Object.<string, SlackBot>} */
- this.bots = {};
- /** @type {Object.<string, string>} */
- this.emojis = {};
- /**
- * Node serv handler
- * @type {*}
- **/
- this.slack = slack;
- /** @type {number} */
- this.staticV = 0;
- /** @type {number} */
- this.liveV = 0;
- }
- /**
- * @return {Object}
- **/
- 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
- };
- };
- /**
- * @return {Object}
- **/
- SlackChan.prototype.toStatic = function() {
- var res = {
- "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
- };
- if (this.isMember) {
- res["members"] = this.members ? Object.keys(this.members) : [];
- res["topic"] = {
- "value": this.topic
- ,"creator": this.topicCreator ? this.topicCreator.id : null
- ,"last_set": this.topicTs
- }
- res["purpose"] = {
- "value": this.purpose
- ,"creator": this.purposeCreator ? this.purposeCreator.id : null
- ,"last_set": this.purposeTs
- }
- }
- return res;
- };
- /**
- * @return {Object}
- **/
- SlackUser.prototype.toStatic = function() {
- return {
- "id": this.id
- ,"name": this.name
- ,"deleted": this.deleted
- ,"status": this.status
- ,"real_name": this.realName
- ,"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
- }
- };
- };
- SlackGroup.prototype.toStatic = function() {
- var res = {
- "id": this.id
- ,"members": []
- ,"created": this.created
- ,"creator": this.creator.id
- ,"is_archived": this.archived
- ,"last_read": this.lastRead
- };
- for (var mId in this.members) {
- res["members"].push(mId);
- }
- if (this.topic) {
- res["topic"] = {
- "value": this.topic
- ,"creator": this.topicCreator.id
- ,"last_set": this.topicTs
- };
- }
- if (this.purpose) {
- res["purpose"] = {
- "value": this.purpose
- ,"creator": this.purposeCreator.id
- ,"last_set": this.purposeTs
- };
- }
- return res;
- };
- SlackIms.prototype.toStatic = function() {
- return {
- "id": this.id
- ,"created": this.created
- ,"user": this.user.id
- ,"last_read": this.lastRead
- };
- }
- SlackBot.prototype.toStatic = function() {
- return {
- "id": this.id
- ,"deleted": this.deleted
- ,"name": this.name
- ,"app_id": this.appId
- ,"icons": {
- "image_36": this.icons.image_36
- ,"image_48": this.icons.image_48
- ,"image_72": this.icons.image_72
- }
- };
- }
- /**
- * @param {*} data
- **/
- SlackData.prototype.updateStatic = function(data) {
- if (data["bots"]) for (var i =0, nbBots = data["bots"].length; i < nbBots; i++) {
- var botObj = this.bots[data["bots"][i]["id"]];
- if (!botObj)
- botObj = this.bots[data["bots"][i]["id"]] = new SlackBot(data["bots"][i]["id"]);
- botObj.update(data["bots"][i]);
- }
- if (data["users"]) for (var i =0, nbUsers = data["users"].length; i < nbUsers; i++) {
- var userObj = this.users[data["users"][i]["id"]];
- if (!userObj)
- userObj = this.users[data["users"][i]["id"]] = new SlackUser(data["users"][i]["id"]);
- userObj.update(data["users"][i]);
- }
- if (data["ims"]) for (var i =0, nbIms = data["ims"].length; i < nbIms; i++) {
- var user = this.getMember(data["ims"][i]["user"]);
- if (user) {
- if (!user.ims)
- this.ims[data["ims"][i]["id"]] = user.ims = new SlackIms(data["ims"][i]["id"], user);
- user.ims.update(user, data["ims"][i]);
- }
- }
- if (data["channels"]) for (var i =0, nbChan = data["channels"].length; i < nbChan; i++) {
- var chanObj = this.channels[data["channels"][i]["id"]];
- if (!chanObj)
- chanObj = this.channels[data["channels"][i]["id"]] = new SlackChan(data["channels"][i]["id"]);
- chanObj.update(data["channels"][i], this);
- }
- for (var i =0, nbGroups = data["groups"].length; i < nbGroups; i++) {
- var groupObj = this.groups[data["groups"][i]["id"]];
- if (!groupObj)
- groupObj = this.groups[data["groups"][i]["id"]] = new SlackGroup(data["groups"][i]["id"]);
- groupObj.update(this, data["groups"][i]);
- }
- if (data["emojis"]) this.emojis = data["emojis"];
- if (!this.team) this.team = new SlackTeam(data["team"]["id"]);
- this.team.update(data["team"]);
- this.staticV = parseFloat(data["latest_event_ts"]);
- if (data["self"]) {
- this.self = this.getMember(data["self"]["id"]);
- if (!this.self.prefs) this.self.prefs = new SelfPreferences();
- this.self.prefs.update(data["self"]["prefs"]);
- }
- };
- SelfPreferences.prototype.toStatic = function() {
- return {
- "emoji_use": JSON.stringify(this.favoriteEmojis)
- ,"highlights": this.highlights
- };
- }
- /**
- * @param {string} appId
- * @return {Array.<SlackBot>}
- **/
- SlackData.prototype.getBotsByAppId = function(appId) {
- var bots = [];
- for (var botId in this.bots) {
- if (this.bots[botId].appId === appId) {
- bots.push(this.bots[botId]);
- }
- }
- return bots;
- };
- /**
- * @param {string} mId
- * @return {SlackUser|SlackBot|null}
- **/
- SlackData.prototype.getMember = function(mId) {
- return this.users[mId] || this.bots[mId] || null;
- };
- /**
- * @param {string} chanId
- * @return {SlackChan|SlackGroup|SlackIms|null}
- **/
- SlackData.prototype.getChannel = function(chanId) {
- return this.channels[chanId] || this.ims[chanId] || this.groups[chanId] || null;
- };
- /** @return {Object} */
- SlackData.prototype.buildStatic = function() {
- var res = {
- "team": this.team.toStatic()
- ,"channels": []
- ,"groups": []
- ,"ims": []
- ,"users": []
- ,"bots": []
- ,"self": {
- "id": this.self.id
- ,"prefs": this.self.prefs.toStatic()
- }
- ,"emojis": this.emojis
- };
- for (var chanId in this.channels) {
- res["channels"].push(this.channels[chanId].toStatic());
- }
- for (var userId in this.users) {
- res["users"].push(this.users[userId].toStatic());
- res["ims"].push(this.users[userId].ims.toStatic());
- }
- for (var botId in this.bots) {
- res["bots"].push(this.bots[botId].toStatic());
- }
- for (var groupId in this.groups) {
- res["groups"].push(this.groups[groupId].toStatic());
- }
- return res;
- };
- /**
- * @param {Object} msg
- * @return {boolean}
- **/
- SlackData.prototype.onMessage = function(msg) {
- //TODO
- return false;
- };
- /**
- * @param {number} knownVersion
- * @return {Object|undefined}
- **/
- SlackData.prototype.getUpdates = function(knownVersion) {
- return (this.staticV > knownVersion) ? this.buildStatic() : undefined;
- };
- /** @suppress {undefinedVars,checkTypes} */
- (function() {
- if (typeof module !== "undefined") {
- module.exports.SlackData = SlackData;
- }
- })();
|