|
|
@@ -0,0 +1,215 @@
|
|
|
+
|
|
|
+function SlackTeam(teamData) {
|
|
|
+ this.id = teamData.id;
|
|
|
+ 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 = teamData.icon; // image_34, image_44, image_68, image_88, image_102, image_132, image_230, image_default
|
|
|
+}
|
|
|
+
|
|
|
+function SlackChan(chanData, slackData) {
|
|
|
+ this.id = chanData.id;
|
|
|
+ this.name = chanData.name;
|
|
|
+ this.created = chanData.created;
|
|
|
+ this.creator = chanData.creator;
|
|
|
+ this.archived = chanData.is_archived;
|
|
|
+ this.isMember = chanData.is_member;
|
|
|
+ this.lastRead = chanData.last_read;
|
|
|
+ this.members = {};
|
|
|
+ for (var i =0, nbMembers = chanData.members.length; i < nbMembers; i++)
|
|
|
+ this.members[chanData.members[i]] = slackData.getMember(chanData.members[i]);
|
|
|
+ this.topic = chanData.topic.value;
|
|
|
+ this.topicCreator = slackData.getMember(chanData.topic.creator);
|
|
|
+ this.topicTs = chanData.topic.last_set;
|
|
|
+ this.purpose = chanData.purpose.value;
|
|
|
+ this.purposeCreator = slackData.getMember(chanData.purpose.creator);
|
|
|
+ this.purposeTs = chanData.purpose.last_set;
|
|
|
+}
|
|
|
+
|
|
|
+function SlackGroup(groupData) {
|
|
|
+ // TODO
|
|
|
+}
|
|
|
+
|
|
|
+function SlackIms(imsData) {
|
|
|
+ /*
|
|
|
+ * TODO
|
|
|
+ * { id: 'D4BTFBDT5',
|
|
|
+ created: 1488361588,
|
|
|
+ is_im: true,
|
|
|
+ is_org_shared: false,
|
|
|
+ user: 'USLACKBOT',
|
|
|
+ has_pins: false,
|
|
|
+ last_read: '1488364738.619420',
|
|
|
+ latest:
|
|
|
+ { type: 'message',
|
|
|
+ user: 'USLACKBOT',
|
|
|
+ text: 'Right now anyone on your team can invite a new member. If you’d like, you can restrict that to just administrators.\n_<slack-action://BSLACKBOT/users-invites/invites-only-admins/restrict/|Only allow administrators to invite>_',
|
|
|
+ ts: '1488364738.619420' },
|
|
|
+ unread_count: 0,
|
|
|
+ unread_count_display: 0,
|
|
|
+ is_open: true }
|
|
|
+ */
|
|
|
+}
|
|
|
+
|
|
|
+function SlackUser(userData) {
|
|
|
+ this.id = userData.id;
|
|
|
+ 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
|
|
|
+ ,image_32: userData.profile.image_32
|
|
|
+ ,image_48: userData.profile.image_48
|
|
|
+ ,image_72: userData.profile.image_72
|
|
|
+ ,image_192: userData.profile.image_192
|
|
|
+ ,image_512: userData.profile.image_512
|
|
|
+ };
|
|
|
+ this.email = userData.profile.email;
|
|
|
+ this.firstName = userData.profile.first_name;
|
|
|
+ this.lastName = userData.profile.last_name;
|
|
|
+}
|
|
|
+
|
|
|
+function SlackBot(botData) {
|
|
|
+ this.id = botData.id;
|
|
|
+ this.deleted = botData.deleted;
|
|
|
+ this.name = botData.name;
|
|
|
+ this.appId = botData.app_id;
|
|
|
+ // { image_36, image_48, image_72 }
|
|
|
+ this.icons = botData.icons;
|
|
|
+}
|
|
|
+
|
|
|
+function SlackHistory(target) {
|
|
|
+ this.id = target.id;
|
|
|
+ this.target = target;
|
|
|
+ this.v = 0;
|
|
|
+ this.messages = [];
|
|
|
+}
|
|
|
+
|
|
|
+function SlackData(slack) {
|
|
|
+ this.team = null;
|
|
|
+ this.channels = {};
|
|
|
+ this.groups = [];
|
|
|
+ this.ims = [];
|
|
|
+ this.users = {};
|
|
|
+ this.self = null;
|
|
|
+ this.bots = {};
|
|
|
+ // channel/ims id -> array of events
|
|
|
+ this.history = {};
|
|
|
+ this.slack = slack;
|
|
|
+
|
|
|
+ this.staticV = 0;
|
|
|
+ this.liveV = 0;
|
|
|
+}
|
|
|
+
|
|
|
+SlackTeam.prototype.toStatic = function() {
|
|
|
+ return this;
|
|
|
+}
|
|
|
+
|
|
|
+SlackHistory.prototype.update = function(messages) {
|
|
|
+ // TODO
|
|
|
+}
|
|
|
+
|
|
|
+SlackHistory.prototype.getUpdates = function(knownVersion) {
|
|
|
+ // TODO
|
|
|
+}
|
|
|
+
|
|
|
+SlackData.prototype.updateStatic = function(data, callback) {
|
|
|
+ for (var i =0, nbBots = data.bots.length; i < nbBots; i++)
|
|
|
+ this.bots[data.bots[i].id] = new SlackBot(data.bots[i]);
|
|
|
+ this.team = new SlackTeam(data.team);
|
|
|
+ for (var i =0, nbUsers = data.users.length; i < nbUsers; i++)
|
|
|
+ this.users[data.users[i].id] = new SlackUser(data.users[i]);
|
|
|
+ for (var i =0, nbChan = data.channels.length; i < nbChan; i++)
|
|
|
+ this.channels[data.channels[i].id] = new SlackChan(data.channels[i], this);
|
|
|
+ //this.groups.push(new SlackGroup(data.groups)); TODO
|
|
|
+ //this.ims.push(new SlackIms(data.ims[0])); TODO
|
|
|
+ this.staticV = parseFloat(data.latest_event_ts);
|
|
|
+
|
|
|
+ var fetchHistory = {}
|
|
|
+ ,doFetch = false;
|
|
|
+ for (var i in this.channels)
|
|
|
+ if (!this.history[i]) {
|
|
|
+ doFetch = true;
|
|
|
+ fetchHistory[i] = this.channels[i];
|
|
|
+ }
|
|
|
+ //TODO fetch group & ims history
|
|
|
+ if (!doFetch)
|
|
|
+ callback();
|
|
|
+ else for (var i in fetchHistory) {
|
|
|
+ var _this = this;
|
|
|
+
|
|
|
+ this.slack.fetchHistory(i, (currentUpdated, data) => {
|
|
|
+ if (data != null) {
|
|
|
+ _this.setHistory(fetchHistory[currentUpdated], data);
|
|
|
+ fetchHistory[currentUpdated] = null;
|
|
|
+ for (var i in fetchHistory) {
|
|
|
+ if (fetchHistory[i])
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ callback();
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+SlackData.prototype.setHistory = function(target, data) {
|
|
|
+ if (!this.history[target.id])
|
|
|
+ this.history[target.id] = new SlackHistory(target);
|
|
|
+ this.history[target.id].update(data);
|
|
|
+}
|
|
|
+
|
|
|
+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;
|
|
|
+}
|
|
|
+
|
|
|
+SlackData.prototype.getMember = function(mId) {
|
|
|
+ return this.users[mId] || this.bots[mId] || null;
|
|
|
+}
|
|
|
+
|
|
|
+SlackData.prototype.buildLive = function(knownVersion) {
|
|
|
+ var res = {};
|
|
|
+ for (var i in this.history)
|
|
|
+ if (this.history[i].v > knownVersion)
|
|
|
+ res[i] = this.history[i].getUpdates(knownVersion);
|
|
|
+}
|
|
|
+
|
|
|
+SlackData.prototype.buildStatic = function() {
|
|
|
+ var res = {
|
|
|
+ team: this.team.toStatic()
|
|
|
+ ,channels: {}
|
|
|
+ ,groups: {}
|
|
|
+ ,ims: {}
|
|
|
+ ,users: {}
|
|
|
+ ,bots: {}
|
|
|
+ };
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+SlackData.prototype.getUpdates = function(knownVersion) {
|
|
|
+ var res = {};
|
|
|
+ if (this.liveV > knownVersion) {
|
|
|
+ res.live = this.buildLive();
|
|
|
+ }
|
|
|
+ if (this.staticV > knownVersion) {
|
|
|
+ res.static = this.buildStatic();
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+if (module.exports) {
|
|
|
+ module.exports.SlackData = SlackData;
|
|
|
+}
|
|
|
+
|