slackData.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /**
  2. * @constructor
  3. **/
  4. function SlackTeam(teamData) {
  5. /** @type {string} */
  6. this.id = teamData["id"];
  7. /** @type {string} */
  8. this.name = teamData["name"];
  9. /** @type {string} */
  10. this.domain = teamData["domain"];
  11. /** @type {string} */
  12. this.callApp = teamData["prefs"]["calling_app_id"];
  13. /** @type {string} */
  14. this.callAppName = teamData["prefs"]["calling_app_name"];
  15. /** @type {boolean} */
  16. this.fileUploadPermission = teamData["prefs"]["disable_file_uploads"];
  17. /** @type {boolean} */
  18. this.fileEditPermission = teamData["prefs"]["disable_file_editing"];
  19. /** @type {boolean} */
  20. this.fileDeletePermission = teamData["prefs"]["disable_file_deleting"];
  21. /** @type {Object.<string, string>} */
  22. this.icons = {
  23. image_34: teamData["icon"]["image_34"]
  24. ,image_44: teamData["icon"]["image_44"]
  25. ,image_68: teamData["icon"]["image_68"]
  26. ,image_88: teamData["icon"]["image_88"]
  27. ,image_102: teamData["icon"]["image_102"]
  28. ,image_132: teamData["icon"]["image_132"]
  29. ,image_230: teamData["icon"]["image_230"]
  30. ,image_default: teamData["icon"]["image_default"]
  31. };
  32. }
  33. /**
  34. * @constructor
  35. **/
  36. function SlackChan(chanData, slackData) {
  37. /** @type {string} */
  38. this.id = chanData["id"];
  39. /** @type {string} */
  40. this.name = chanData["name"];
  41. /** @type {string} */
  42. this.created = chanData["created"];
  43. /** @type {SlackUser|SlackBot} */
  44. this.creator = slackData.getMember(chanData["creator"]);
  45. /** @type {boolean} */
  46. this.archived = chanData["is_archived"];
  47. /** @type {boolean} */
  48. this.isMember = chanData["is_member"];
  49. /** @type {number} */
  50. this.lastRead = parseFloat(chanData["last_read"]);
  51. /** @type {Object.<string, SlackBot|SlackUser>} */
  52. this.members = {};
  53. if (chanData["members"]) for (var i =0, nbMembers = chanData["members"].length; i < nbMembers; i++) {
  54. var member = slackData.getMember(chanData["members"][i]);
  55. this.members[member.id] = member;
  56. member.channels[this.id] = this;
  57. }
  58. /** @type {string|undefined} */
  59. this.topic;
  60. /** @type {number|undefined} */
  61. this.topicTs;
  62. /** @type {SlackUser|SlackBot|undefined} */
  63. this.topicCreator;
  64. /** @type {string|undefined} */
  65. this.purpose;
  66. /** @type {number|undefined} */
  67. this.purposeTs;
  68. /** @type {SlackUser|SlackBot|undefined} */
  69. this.purposeCreator;
  70. if (chanData["topic"]) {
  71. this.topic = chanData["topic"]["value"];
  72. this.topicCreator = slackData.getMember(chanData["topic"]["creator"]);
  73. this.topicTs = chanData["topic"]["last_set"];
  74. }
  75. if (chanData["purpose"]) {
  76. this.purpose = chanData["purpose"]["value"];
  77. this.purposeCreator = slackData.getMember(chanData["purpose"]["creator"]);
  78. this.purposeTs = chanData["purpose"]["last_set"];
  79. }
  80. }
  81. /**
  82. * @constructor
  83. * @param {SlackData} slack
  84. * @param {*} groupData
  85. **/
  86. function SlackGroup(slack, groupData) {
  87. var memberNames = [];
  88. /** @type {string} */
  89. this.id = groupData["id"];
  90. /** @type {Object.<string, SlackUser|SlackBot>} */
  91. this.members = {};
  92. for (var i =0, nbMembers = groupData["members"].length; i < nbMembers; i++) {
  93. var member = slack.getMember(groupData["members"][i]);
  94. this.members[groupData["members"][i]] = member;
  95. member.channels[this.id] = this;
  96. memberNames.push(member.name);
  97. }
  98. /** @type {string} */
  99. this.name = memberNames.join(", ");
  100. /** @type {number} */
  101. this.created = groupData["created"];
  102. /** @type {SlackUser|SlackBot} */
  103. this.creator = slack.getMember(groupData["creator"]);
  104. /** @type {boolean} */
  105. this.archived = groupData["is_archived"];
  106. /** @type {number} */
  107. this.lastRead = parseFloat(groupData["last_read"]);
  108. /** @type {string|undefined} */
  109. this.topic;
  110. /** @type {number|undefined} */
  111. this.topicTs;
  112. /** @type {SlackUser|SlackBot|undefined} */
  113. this.topicCreator;
  114. /** @type {string|undefined} */
  115. this.purpose;
  116. /** @type {number|undefined} */
  117. this.purposeTs;
  118. /** @type {SlackUser|SlackBot|undefined} */
  119. this.purposeCreator;
  120. if (groupData["topic"]) {
  121. this.topic = groupData["topic"]["value"];
  122. this.topicCreator = slack.getMember(groupData["topic"]["creator"]);
  123. this.topicTs = groupData["topic"]["last_set"];
  124. }
  125. if (groupData["purpose"]) {
  126. this.purpose = groupData["purpose"]["value"];
  127. this.purposeCreator = slack.getMember(groupData["purpose"]["creator"]);
  128. this.purposeTs = groupData["purpose"]["last_set"];
  129. }
  130. }
  131. /**
  132. * @constructor
  133. * @param {SlackUser|SlackBot} user
  134. * @param {*} imsData
  135. **/
  136. function SlackIms(user, imsData) {
  137. /** @type {string} */
  138. this.id = imsData["id"];
  139. /** @type {number} */
  140. this.created = imsData["created"];
  141. /** @type {SlackUser|SlackBot} */
  142. this.user = user;
  143. /** @type {number} */
  144. this.lastRead = parseFloat(imsData["last_read"]);
  145. }
  146. /**
  147. * @constructor
  148. **/
  149. function SlackUser(userData) {
  150. /** @type {string} */
  151. this.id = userData["id"];
  152. /** @type {string} */
  153. this.name = userData["name"];
  154. /** @type {boolean} */
  155. this.deleted = userData["deleted"];
  156. /** @type {string} */
  157. this.status = userData["status"];
  158. /** @type {string} */
  159. this.realName = userData["real_name"] || userData["profile"]["real_name"];
  160. /** @type {boolean} */
  161. this.presence = userData["presence"] !== 'away';
  162. /** @type {Object.<string, string>} */
  163. this.icons = {
  164. image_24: userData["profile"]["image_24"]
  165. ,image_32: userData["profile"]["image_32"]
  166. ,image_48: userData["profile"]["image_48"]
  167. ,image_72: userData["profile"]["image_72"]
  168. ,image_192: userData["profile"]["image_192"]
  169. ,image_512: userData["profile"]["image_512"]
  170. };
  171. /** @type {string} */
  172. this.email = userData["profile"]["email"];
  173. /** @type {string} */
  174. this.firstName = userData["profile"]["first_name"];
  175. /** @type {string} */
  176. this.lastName = userData["profile"]["last_name"];
  177. /** @type {!Object.<string, SlackChan|SlackGroup>} */
  178. this.channels = {};
  179. /** @type {SlackIms} */
  180. this.ims = null;
  181. }
  182. /**
  183. * @constructor
  184. **/
  185. function SlackBot(botData) {
  186. /** @type {string} */
  187. this.id = botData["id"];
  188. /** @type {boolean} */
  189. this.deleted = botData["deleted"];
  190. /** @type {string} */
  191. this.name = botData["name"];
  192. /** @type {string} */
  193. this.appId = botData["app_id"];
  194. /** @type {Object.<string, string>} */
  195. this.icons = {
  196. image_36: botData["icons"]["image_36"]
  197. ,image_48: botData["icons"]["image_48"]
  198. ,image_72: botData["icons"]["image_72"]
  199. };
  200. /** @type {!Object.<string, SlackGroup|SlackChan>} */
  201. this.channels = {};
  202. /** @type {SlackIms|null} */
  203. this.ims = null;
  204. }
  205. /**
  206. * @constructor
  207. **/
  208. function SlackData(slack) {
  209. /** @type {SlackTeam|null} */
  210. this.team = null;
  211. /** @type {Object.<string, SlackChan>} */
  212. this.channels = {};
  213. /** @type {Object.<string, SlackGroup>} */
  214. this.groups = {};
  215. /** @type {Object.<string, SlackIms>} */
  216. this.ims = {};
  217. /** @type {Object.<string, SlackUser>} */
  218. this.users = {};
  219. /** @type {SlackUser|SlackBot} */
  220. this.self = null;
  221. /** @type {Object.<string, SlackBot>} */
  222. this.bots = {};
  223. /**
  224. * Node serv handler
  225. * @type {*}
  226. **/
  227. this.slack = slack;
  228. /** @type {number} */
  229. this.staticV = 0;
  230. /** @type {number} */
  231. this.liveV = 0;
  232. }
  233. /**
  234. * @return {Object}
  235. **/
  236. SlackTeam.prototype.toStatic = function() {
  237. return {
  238. "id": this.id
  239. ,"name": this.name
  240. ,"domain": this.domain
  241. ,"prefs": {
  242. "calling_app_id": this.callApp
  243. ,"calling_app_name": this.callAppName
  244. ,"disable_file_uploads": this.fileUploadPermission
  245. ,"disable_file_editing": this.fileEditPermission
  246. ,"disable_file_deleting": this.fileDeletePermission
  247. }
  248. ,"icon": this.icons
  249. };
  250. };
  251. /**
  252. * @return {Object}
  253. **/
  254. SlackChan.prototype.toStatic = function() {
  255. var res = {
  256. "id": this.id
  257. ,"name": this.name
  258. ,"created": this.created
  259. ,"creator": this.creator.id
  260. ,"is_archived": this.archived
  261. ,"is_member": this.isMember
  262. ,"last_read": this.lastRead
  263. };
  264. if (this.isMember) {
  265. res["members"] = this.members ? Object.keys(this.members) : [];
  266. res["topic"] = {
  267. "value": this.topic
  268. ,"creator": this.topicCreator ? this.topicCreator.id : null
  269. ,"last_set": this.topicTs
  270. }
  271. res["purpose"] = {
  272. "value": this.purpose
  273. ,"creator": this.purposeCreator ? this.purposeCreator.id : null
  274. ,"last_set": this.purposeTs
  275. }
  276. }
  277. return res;
  278. };
  279. /**
  280. * @return {Object}
  281. **/
  282. SlackUser.prototype.toStatic = function() {
  283. return {
  284. "id": this.id
  285. ,"name": this.name
  286. ,"deleted": this.deleted
  287. ,"status": this.status
  288. ,"real_name": this.realName
  289. ,"profile": {
  290. "email": this.email
  291. ,"first_name": this.firstName
  292. ,"last_name": this.lastName
  293. ,"image_24": this.icons.image_24
  294. ,"image_32": this.icons.image_32
  295. ,"image_48": this.icons.image_48
  296. ,"image_72": this.icons.image_72
  297. ,"image_192": this.icons.image_192
  298. ,"image_512": this.icons.image_512
  299. }
  300. };
  301. };
  302. SlackGroup.prototype.toStatic = function() {
  303. var res = {
  304. "id": this.id
  305. ,"members": []
  306. ,"created": this.created
  307. ,"creator": this.creator.id
  308. ,"is_archived": this.archived
  309. ,"last_read": this.lastRead
  310. };
  311. for (var mId in this.members) {
  312. res["members"].push(mId);
  313. }
  314. if (this.topic) {
  315. res["topic"] = {
  316. "value": this.topic
  317. ,"creator": this.topicCreator.id
  318. ,"last_set": this.topicTs
  319. };
  320. }
  321. if (this.purpose) {
  322. res["purpose"] = {
  323. "value": this.purpose
  324. ,"creator": this.purposeCreator.id
  325. ,"last_set": this.purposeTs
  326. };
  327. }
  328. return res;
  329. };
  330. SlackIms.prototype.toStatic = function() {
  331. return {
  332. "id": this.id
  333. ,"created": this.created
  334. ,"user": this.user.id
  335. ,"last_read": this.lastRead
  336. };
  337. }
  338. SlackBot.prototype.toStatic = function() {
  339. return {
  340. "id": this.id
  341. ,"deleted": this.deleted
  342. ,"name": this.name
  343. ,"app_id": this.appId
  344. ,"icons": {
  345. "image_36": this.icons.image_36
  346. ,"image_48": this.icons.image_48
  347. ,"image_72": this.icons.image_72
  348. }
  349. };
  350. }
  351. /**
  352. * @param {*} data
  353. **/
  354. SlackData.prototype.updateStatic = function(data) {
  355. for (var i =0, nbBots = data["bots"].length; i < nbBots; i++)
  356. this.bots[data["bots"][i].id] = new SlackBot(data["bots"][i]);
  357. for (var i =0, nbUsers = data["users"].length; i < nbUsers; i++)
  358. this.users[data["users"][i].id] = new SlackUser(data["users"][i]);
  359. for (var i =0, nbIms = data["ims"].length; i < nbIms; i++) {
  360. var user = this.getMember(data["ims"][i]["user"]);
  361. if (user) {
  362. user.ims = new SlackIms(user, data["ims"][i]);
  363. this.ims[user.ims.id] = user.ims;
  364. }
  365. }
  366. for (var i =0, nbChan = data["channels"].length; i < nbChan; i++)
  367. this.channels[data["channels"][i].id] = new SlackChan(data["channels"][i], this);
  368. for (var i =0, nbGroups = data["groups"].length; i < nbGroups; i++)
  369. this.groups[data["groups"][i]["id"]] = new SlackGroup(this, data["groups"][i]);
  370. this.team = new SlackTeam(data["team"]);
  371. this.staticV = parseFloat(data["latest_event_ts"]);
  372. this.self = this.getMember(data["self"]["id"]);
  373. if (!this.slack) {
  374. return;
  375. }
  376. };
  377. /**
  378. * @param {string} appId
  379. * @return {Array.<SlackBot>}
  380. **/
  381. SlackData.prototype.getBotsByAppId = function(appId) {
  382. var bots = [];
  383. for (var botId in this.bots) {
  384. if (this.bots[botId].appId === appId) {
  385. bots.push(this.bots[botId]);
  386. }
  387. }
  388. return bots;
  389. };
  390. /**
  391. * @param {string} mId
  392. * @return {SlackUser|SlackBot|null}
  393. **/
  394. SlackData.prototype.getMember = function(mId) {
  395. return this.users[mId] || this.bots[mId] || null;
  396. };
  397. /** @return {Object} */
  398. SlackData.prototype.buildStatic = function() {
  399. var res = {
  400. "team": this.team.toStatic()
  401. ,"channels": []
  402. ,"groups": []
  403. ,"ims": []
  404. ,"users": []
  405. ,"bots": []
  406. ,"self": {
  407. "id": this.self.id
  408. }
  409. };
  410. for (var chanId in this.channels) {
  411. res["channels"].push(this.channels[chanId].toStatic());
  412. }
  413. for (var userId in this.users) {
  414. res["users"].push(this.users[userId].toStatic());
  415. res["ims"].push(this.users[userId].ims.toStatic());
  416. }
  417. for (var botId in this.bots) {
  418. res["bots"].push(this.bots[botId].toStatic());
  419. }
  420. for (var groupId in this.groups) {
  421. res["groups"].push(this.groups[groupId].toStatic());
  422. }
  423. return res;
  424. };
  425. /**
  426. * @param {Object} msg
  427. * @return {boolean}
  428. **/
  429. SlackData.prototype.onMessage = function(msg) {
  430. //TODO
  431. return false;
  432. };
  433. /**
  434. * @param {number} knownVersion
  435. * @return {Object|undefined}
  436. **/
  437. SlackData.prototype.getUpdates = function(knownVersion) {
  438. return (this.staticV > knownVersion) ? this.buildStatic() : undefined;
  439. };
  440. /** @suppress {undefinedVars,checkTypes} */
  441. (function() {
  442. if (typeof module !== "undefined") {
  443. module.exports.SlackData = SlackData;
  444. }
  445. })();