diff --git a/components/profile.js b/components/profile.js index 57bac14..f24d1ad 100644 --- a/components/profile.js +++ b/components/profile.js @@ -1,7 +1,10 @@ -var SteamCommunity = require('../index.js'); -var SteamID = require('steamid'); -var Cheerio = require('cheerio'); -var fs = require('fs'); +const Cheerio = require('cheerio'); +const FormData = require('form-data'); +const FS = require('fs'); +const SteamID = require('steamid'); + +const Helpers = require('./helpers.js'); +const SteamCommunity = require('../index.js'); SteamCommunity.PrivacyState = { "Private": 1, @@ -10,9 +13,9 @@ SteamCommunity.PrivacyState = { }; var CommentPrivacyState = { - "1": "commentselfonly", - "2": "commentfriendsonly", - "3": "commentanyone" + "1": 2, // private + "2": 0, // friends only + "3": 1 // anyone }; SteamCommunity.prototype.setupProfile = function(callback) { @@ -148,10 +151,9 @@ SteamCommunity.prototype.editProfile = function(settings, callback) { }; SteamCommunity.prototype.profileSettings = function(settings, callback) { - var self = this; - this._myProfile("edit/settings", null, function(err, response, body) { - if(err || response.statusCode != 200) { - if(callback) { + this._myProfile("edit/settings", null, (err, response, body) => { + if (err || response.statusCode != 200) { + if (callback) { callback(err || new Error("HTTP error " + response.statusCode)); } @@ -159,8 +161,8 @@ SteamCommunity.prototype.profileSettings = function(settings, callback) { } var $ = Cheerio.load(body); - var form = $('#editForm'); - if(!form) { + var existingSettings = $('.ProfileReactRoot[data-privacysettings]').data('privacysettings'); + if (!existingSettings) { if(callback) { callback(new Error("Malformed response")); } @@ -168,46 +170,71 @@ SteamCommunity.prototype.profileSettings = function(settings, callback) { return; } - var values = {}; - form.serializeArray().forEach(function(item) { - values[item.name] = item.value; - }); + // PrivacySettings => {PrivacyProfile, PrivacyInventory, PrivacyInventoryGifts, PrivacyOwnedGames, PrivacyPlaytime} + // eCommentPermission + var privacy = existingSettings.PrivacySettings; + var commentPermission = existingSettings.eCommentPermission; - for(var i in settings) { - if(!settings.hasOwnProperty(i)) { + for (var i in settings) { + if (!settings.hasOwnProperty(i)) { continue; } - switch(i) { + switch (i) { case 'profile': - values.privacySetting = settings[i]; + privacy.PrivacyProfile = settings[i]; break; case 'comments': - values.commentSetting = CommentPrivacyState[settings[i]]; + commentPermission = CommentPrivacyState[settings[i]]; break; case 'inventory': - values.inventoryPrivacySetting = settings[i]; + privacy.PrivacyInventory = settings[i]; break; case 'inventoryGifts': - values.inventoryGiftPrivacy = settings[i] ? 1 : 0; + privacy.PrivacyInventoryGifts = settings[i] ? SteamCommunity.PrivacyState.Private : SteamCommunity.PrivacyState.Public; + break; + + case 'gameDetails': + privacy.PrivacyOwnedGames = settings[i]; + break; + + case 'playtime': + privacy.PrivacyPlaytime = settings[i] ? SteamCommunity.PrivacyState.Private : SteamCommunity.PrivacyState.Public; break; } } - self._myProfile("edit/settings", values, function(err, response, body) { - if(err || response.statusCode != 200) { - if(callback) { + this._myProfile({ + "method": "POST", + "endpoint": "ajaxsetprivacy/", + "json": true, + "formData": { // it's multipart because lolvalve + "sessionid": this.getSessionID(), + "Privacy": JSON.stringify(privacy), + "eCommentPermission": commentPermission + } + }, null, function(err, response, body) { + if (err || response.statusCode != 200) { + if (callback) { callback(err || new Error("HTTP error " + response.statusCode)); } return; } - if(callback) { - callback(null); + if (body.success != 1) { + if (callback) { + callback(new Error(body.success ? "Error " + body.success : "Request was not successful")); + } + + return; + } + + if (callback) { + callback(null, body.Privacy); } }); }); @@ -256,7 +283,7 @@ SteamCommunity.prototype.uploadAvatar = function(image, format, callback) { } } - fs.readFile(image, function(err, file) { + FS.readFile(image, function(err, file) { if(err) { if(callback) { callback(err); diff --git a/index.js b/index.js index f6aa22b..13e01a5 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ require('@doctormckay/stats-reporter').setup(require('./package.json')); -var Request = require('request'); -var RSA = require('node-bignumber').Key; -var hex2b64 = require('node-bignumber').hex2b64; -var SteamID = require('steamid'); +const hex2b64 = require('node-bignumber').hex2b64; +const Request = require('request'); +const RSA = require('node-bignumber').Key; +const SteamID = require('steamid'); const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"; @@ -460,11 +460,12 @@ SteamCommunity.prototype._myProfile = function(endpoint, form, callback) { function completeRequest(url) { var options = endpoint.endpoint ? endpoint : {}; options.uri = "https://steamcommunity.com" + url + "/" + (endpoint.endpoint || endpoint); - options.method = "GET"; if (form) { options.method = "POST"; options.form = form; + } else if (!options.method) { + options.method = "GET"; } self.httpRequest(options, callback, "steamcommunity");