Skip to content

Commit

Permalink
Added privacy controls for gameDetails and playtime
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorMcKay committed Apr 11, 2018
1 parent 051d376 commit 8d163d0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 35 deletions.
87 changes: 57 additions & 30 deletions components/profile.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -148,66 +151,90 @@ 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));
}

return;
}

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"));
}

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);
}
});
});
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 8d163d0

Please sign in to comment.