From 8e9e3723f29f33601e1eb82e87f508605aa1da07 Mon Sep 17 00:00:00 2001 From: Alex Corn Date: Tue, 24 Sep 2019 04:23:40 -0400 Subject: [PATCH] Code cleanup for chat.js --- components/chat.js | 141 ++++++++++++++++++++++----------------------- 1 file changed, 68 insertions(+), 73 deletions(-) diff --git a/components/chat.js b/components/chat.js index adc476a..d2eceef 100644 --- a/components/chat.js +++ b/components/chat.js @@ -12,7 +12,7 @@ SteamCommunity.PersonaStateFlag = require('../resources/EPersonaStateFlag.js'); * @param {string} uiMode */ SteamCommunity.prototype.chatLogon = function(interval, uiMode) { - if(this.chatState == SteamCommunity.ChatState.LoggingOn || this.chatState == SteamCommunity.ChatState.LoggedOn) { + if (this.chatState == SteamCommunity.ChatState.LoggingOn || this.chatState == SteamCommunity.ChatState.LoggedOn) { return; } @@ -22,48 +22,47 @@ SteamCommunity.prototype.chatLogon = function(interval, uiMode) { this.emit('debug', 'Requesting chat WebAPI token'); this.chatState = SteamCommunity.ChatState.LoggingOn; - var self = this; - this.getWebApiOauthToken(function(err, token) { - if(err) { - var fatal = err.message.indexOf('not authorized') != -1; + this.getWebApiOauthToken((err, token) => { + if (err) { + let fatal = err.message.indexOf('not authorized') != -1; if (!fatal) { - self.chatState = SteamCommunity.ChatState.LogOnFailed; - setTimeout(self.chatLogon.bind(self), 5000); + this.chatState = SteamCommunity.ChatState.LogOnFailed; + setTimeout(this.chatLogon.bind(this), 5000); } else { - self.chatState = SteamCommunity.ChatState.Offline; + this.chatState = SteamCommunity.ChatState.Offline; } - self.emit('chatLogOnFailed', err, fatal); - self.emit('debug', "Cannot get oauth token: " + err.message); + this.emit('chatLogOnFailed', err, fatal); + this.emit('debug', "Cannot get oauth token: " + err.message); return; } - self.httpRequestPost({ + this.httpRequestPost({ "uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logon/v1", "form": { "ui_mode": uiMode, "access_token": token }, "json": true - }, function(err, response, body) { - if(err || response.statusCode != 200) { - self.chatState = SteamCommunity.ChatState.LogOnFailed; - self.emit('chatLogOnFailed', err ? err : new Error("HTTP error " + response.statusCode), false); - self.emit('debug', 'Error logging into webchat: ' + (err ? err.message : "HTTP error " + response.statusCode)); - setTimeout(self.chatLogon.bind(self), 5000); + }, (err, response, body) => { + if (err || response.statusCode != 200) { + this.chatState = SteamCommunity.ChatState.LogOnFailed; + this.emit('chatLogOnFailed', err ? err : new Error("HTTP error " + response.statusCode), false); + this.emit('debug', 'Error logging into webchat: ' + (err ? err.message : "HTTP error " + response.statusCode)); + setTimeout(this.chatLogon.bind(this), 5000); return; } - if(body.error != 'OK') { - self.chatState = SteamCommunity.ChatState.LogOnFailed; - self.emit('chatLogOnFailed', new Error(body.error), false); - self.emit('debug', 'Error logging into webchat: ' + body.error); - setTimeout(self.chatLogon.bind(self), 5000); + if (body.error != 'OK') { + this.chatState = SteamCommunity.ChatState.LogOnFailed; + this.emit('chatLogOnFailed', new Error(body.error), false); + this.emit('debug', 'Error logging into webchat: ' + body.error); + setTimeout(this.chatLogon.bind(this), 5000); return; } - self._chat = { + this._chat = { "umqid": body.umqid, "message": body.message, "accessToken": token, @@ -71,11 +70,11 @@ SteamCommunity.prototype.chatLogon = function(interval, uiMode) { "uiMode": uiMode }; - self.chatFriends = {}; + this.chatFriends = {}; - self.chatState = SteamCommunity.ChatState.LoggedOn; - self.emit('chatLoggedOn'); - self._chatPoll(); + this.chatState = SteamCommunity.ChatState.LoggedOn; + this.emit('chatLoggedOn'); + this._chatPoll(); }, "steamcommunity"); }); }; @@ -88,22 +87,21 @@ SteamCommunity.prototype.chatLogon = function(interval, uiMode) { * @param {function} [callback] */ SteamCommunity.prototype.chatMessage = function(recipient, text, type, callback) { - if(this.chatState != SteamCommunity.ChatState.LoggedOn) { + if (this.chatState != SteamCommunity.ChatState.LoggedOn) { throw new Error("Chat must be logged on before messages can be sent"); } - if(typeof recipient === 'string') { + if (typeof recipient === 'string') { recipient = new SteamID(recipient); } - if(typeof type === 'function') { + if (typeof type === 'function') { callback = type; type = 'saytext'; } type = type || 'saytext'; - var self = this; this.httpRequestPost({ "uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Message/v1", "form": { @@ -114,8 +112,8 @@ SteamCommunity.prototype.chatMessage = function(recipient, text, type, callback) "umqid": this._chat.umqid }, "json": true - }, function(err, response, body) { - if(!callback) { + }, (err, response, body) => { + if (!callback) { return; } @@ -124,7 +122,7 @@ SteamCommunity.prototype.chatMessage = function(recipient, text, type, callback) return; } - if(body.error != 'OK') { + if (body.error != 'OK') { callback(new Error(body.error)); } else { callback(null); @@ -136,23 +134,22 @@ SteamCommunity.prototype.chatMessage = function(recipient, text, type, callback) * @deprecated No support for new Steam chat. Use steam-user instead. */ SteamCommunity.prototype.chatLogoff = function() { - var self = this; this.httpRequestPost({ "uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logoff/v1", "form": { "access_token": this._chat.accessToken, "umqid": this._chat.umqid } - }, function(err, response, body) { - if(err || response.statusCode != 200) { - self.emit('debug', 'Error logging off of chat: ' + (err ? err.message : "HTTP error " + response.statusCode)); - setTimeout(self.chatLogoff.bind(self), 1000); + }, (err, response, body) => { + if (err || response.statusCode != 200) { + this.emit('debug', 'Error logging off of chat: ' + (err ? err.message : "HTTP error " + response.statusCode)); + setTimeout(this.chatLogoff.bind(this), 1000); } else { - self.emit('chatLoggedOff'); - clearTimeout(self._chat.timer); - delete self._chat; - delete self.chatFriends; - self.chatState = SteamCommunity.ChatState.Offline; + this.emit('chatLoggedOff'); + clearTimeout(this._chat.timer); + delete this._chat; + delete this.chatFriends; + this.chatState = SteamCommunity.ChatState.Offline; } }, "steamcommunity"); }; @@ -163,68 +160,67 @@ SteamCommunity.prototype.chatLogoff = function() { SteamCommunity.prototype._chatPoll = function() { this.emit('debug', 'Doing chat poll'); - var self = this; this.httpRequestPost({ "uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Poll/v1", "form": { - "umqid": self._chat.umqid, - "message": self._chat.message, + "umqid": this._chat.umqid, + "message": this._chat.message, "pollid": 1, "sectimeout": 20, "secidletime": 0, "use_accountids": 1, - "access_token": self._chat.accessToken + "access_token": this._chat.accessToken }, "json": true - }, function(err, response, body) { - if (self.chatState == SteamCommunity.ChatState.Offline) { + }, (err, response, body) => { + if (this.chatState == SteamCommunity.ChatState.Offline) { return; } - self._chat.timer = setTimeout(self._chatPoll.bind(self), self._chat.interval); + this._chat.timer = setTimeout(this._chatPoll.bind(this), this._chat.interval); - if(err || response.statusCode != 200) { - self.emit('debug', 'Error in chat poll: ' + (err ? err.message : "HTTP error " + response.statusCode)); + if (err || response.statusCode != 200) { + this.emit('debug', 'Error in chat poll: ' + (err ? err.message : "HTTP error " + response.statusCode)); if (err.message == "Not Logged On") { - self._relogWebChat(); + this._relogWebChat(); } return; } - if(!body || body.error != 'OK') { - self.emit('debug', 'Error in chat poll: ' + (body && body.error ? body.error : "Malformed response")); + if (!body || body.error != 'OK') { + this.emit('debug', 'Error in chat poll: ' + (body && body.error ? body.error : "Malformed response")); if (body && body.error && body.error == "Not Logged On") { - self._relogWebChat(); + this._relogWebChat(); } return; } - self._chat.message = body.messagelast; + this._chat.message = body.messagelast; (body.messages || []).forEach(function(message) { - var sender = new SteamID(); + let sender = new SteamID(); sender.universe = SteamID.Universe.PUBLIC; sender.type = SteamID.Type.INDIVIDUAL; sender.instance = SteamID.Instance.DESKTOP; sender.accountid = message.accountid_from; - switch(message.type) { + switch (message.type) { case 'personastate': - self._chatUpdatePersona(sender); + this._chatUpdatePersona(sender); break; case 'saytext': - self.emit('chatMessage', sender, message.text); + this.emit('chatMessage', sender, message.text); break; case 'typing': - self.emit('chatTyping', sender); + this.emit('chatTyping', sender); break; default: - self.emit('debug', 'Unhandled chat message type: ' + message.type); + this.emit('debug', 'Unhandled chat message type: ' + message.type); } }); }, "steamcommunity"); @@ -250,24 +246,23 @@ SteamCommunity.prototype._chatUpdatePersona = function(steamID) { } this.emit('debug', 'Updating persona data for ' + steamID); - var self = this; this.httpRequest({ "uri": "https://steamcommunity.com/chat/friendstate/" + steamID.accountid, "json": true - }, function(err, response, body) { - if (!self.chatFriends || self.chatState == SteamCommunity.ChatState.Offline) { + }, (err, response, body) => { + if (!this.chatFriends || this.chatState == SteamCommunity.ChatState.Offline) { return; // welp } - if(err || response.statusCode != 200) { - self.emit('debug', 'Chat update persona error: ' + (err ? err.message : "HTTP error " + response.statusCode)); + if (err || response.statusCode != 200) { + this.emit('debug', 'Chat update persona error: ' + (err ? err.message : "HTTP error " + response.statusCode)); setTimeout(function() { - self._chatUpdatePersona(steamID); + this._chatUpdatePersona(steamID); }, 2000); return; } - var persona = { + let persona = { "steamID": steamID, "personaName": body.m_strName, "personaState": body.m_ePersonaState, @@ -278,7 +273,7 @@ SteamCommunity.prototype._chatUpdatePersona = function(steamID) { "inGameName": body.m_strInGameName || null }; - self.emit('chatPersonaState', steamID, persona); - self.chatFriends[steamID.getSteamID64()] = persona; + this.emit('chatPersonaState', steamID, persona); + this.chatFriends[steamID.getSteamID64()] = persona; }, "steamcommunity"); };