From adbf1d47a77d52a0f3854e557b5a3d0c90f8762a Mon Sep 17 00:00:00 2001 From: Alex Corn Date: Tue, 10 Jul 2018 23:59:57 -0400 Subject: [PATCH] Added postProfileStatus and deleteProfileStatus (closes #200) --- components/helpers.js | 18 ++++++++++++ components/profile.js | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/components/helpers.js b/components/helpers.js index d33d391..1da5443 100644 --- a/components/helpers.js +++ b/components/helpers.js @@ -1,3 +1,5 @@ +const EResult = require('../resources/EResult.js'); + exports.isSteamID = function(input) { var keys = Object.keys(input); if (keys.length != 4) { @@ -36,3 +38,19 @@ exports.decodeSteamTime = function(time) { return date; }; + +/** + * Get an Error object for a particular EResult + * @param {int} eresult + * @returns {null|Error} + */ +exports.eresultError = function(eresult) { + if (eresult == EResult.OK) { + // no error + return null; + } + + var err = new Error(EResult[eresult] || ("Error " + eresult)); + err.eresult = eresult; + return err; +}; diff --git a/components/profile.js b/components/profile.js index 40098b8..e865202 100644 --- a/components/profile.js +++ b/components/profile.js @@ -393,3 +393,68 @@ SteamCommunity.prototype.uploadAvatar = function(image, format, callback) { }, "steamcommunity"); } }; + +/** + * Post a new status to your profile activity feed. + * @param {string} statusText - The text of this status update + * @param {{appID: int}} [options] - Options for this status update. All are optional. If you don't pass any options, this can be omitted. + * @param {function} callback - err, postID + */ +SteamCommunity.prototype.postProfileStatus = function(statusText, options, callback) { + if (typeof options === 'function') { + callback = options; + options = {}; + } + + this._myProfile("ajaxpostuserstatus/", { + "appid": options.appID || 0, + "sessionid": this.getSessionID(), + "status_text": statusText + }, (err, res, body) => { + try { + body = JSON.parse(body); + if (body.message) { + callback(new Error(body.message)); + return; + } + + var match = body.blotter_html.match(/id="userstatus_(\d+)_/); + if (!match) { + callback(new Error("Malformed response")); + return; + } + + callback(null, parseInt(match[1], 10)); + } catch (ex) { + callback(ex); + } + }); +}; + +/** + * Delete a previously-posted profile status update. + * @param {int} postID + * @param {function} [callback] + */ +SteamCommunity.prototype.deleteProfileStatus = function(postID, callback) { + this._myProfile("ajaxdeleteuserstatus/", { + "sessionid": this.getSessionID(), + "postid": postID + }, (err, res, body) => { + if (!callback) { + return; + } + + try { + body = JSON.parse(body); + if (!body.success) { + callback(new Error("Malformed response")); + return; + } + + callback(Helpers.eresultError(body.success)); + } catch (ex) { + callback(ex); + } + }); +};