node-steamcommunity/components/users.js

338 lines
7.8 KiB
JavaScript
Raw Normal View History

var SteamCommunity = require('../index.js');
var SteamID = require('steamid');
2015-09-07 12:00:19 +08:00
var CEconItem = require('../classes/CEconItem.js');
var Helpers = require('./helpers.js');
SteamCommunity.prototype.addFriend = function(userID, callback) {
if(typeof userID === 'string') {
userID = new SteamID(userID);
}
var self = this;
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/AddFriendAjax",
"form": {
"accept_invite": 0,
"sessionID": this.getSessionID(),
"steamid": userID.toString()
},
"json": true
}, function(err, response, body) {
if(!callback) {
return;
}
if (err) {
callback(err);
return;
}
if(body.success) {
callback(null);
} else {
callback(new Error("Unknown error"));
}
2016-03-05 08:35:04 +08:00
}, "steamcommunity");
};
SteamCommunity.prototype.acceptFriendRequest = function(userID, callback) {
if(typeof userID === 'string') {
userID = new SteamID(userID);
}
var self = this;
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/AddFriendAjax",
"form": {
"accept_invite": 1,
"sessionID": this.getSessionID(),
"steamid": userID.toString()
}
}, function(err, response, body) {
if(!callback) {
return;
}
callback(err || null);
2016-03-05 08:35:04 +08:00
}, "steamcommunity");
};
SteamCommunity.prototype.removeFriend = function(userID, callback) {
if(typeof userID === 'string') {
userID = new SteamID(userID);
}
var self = this;
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/RemoveFriendAjax",
"form": {
"sessionID": this.getSessionID(),
"steamid": userID.toString()
}
}, function(err, response, body) {
if(!callback) {
return;
}
callback(err || null);
2016-03-05 08:35:04 +08:00
}, "steamcommunity");
};
SteamCommunity.prototype.blockCommunication = function(userID, callback) {
if(typeof userID === 'string') {
userID = new SteamID(userID);
}
var self = this;
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/BlockUserAjax",
"form": {
"sessionID": this.getSessionID(),
"steamid": userID.toString()
}
}, function(err, response, body) {
if(!callback) {
return;
}
callback(err || null);
2016-03-05 08:35:04 +08:00
}, "steamcommunity");
};
SteamCommunity.prototype.unblockCommunication = function(userID, callback) {
if(typeof userID === 'string') {
userID = new SteamID(userID);
}
var form = {"action": "unignore"};
form['friends[' + userID.toString() + ']'] = 1;
this._myProfile('friends/blocked/', form, function(err, response, body) {
if(!callback) {
return;
}
if(err || response.statusCode >= 400) {
callback(err || new Error("HTTP error " + response.statusCode));
return;
}
callback(null);
});
};
SteamCommunity.prototype.postUserComment = function(userID, message, callback) {
if(typeof userID === 'string') {
userID = new SteamID(userID);
}
var self = this;
this.httpRequestPost({
"uri": "https://steamcommunity.com/comment/Profile/post/" + userID.toString() + "/-1",
"form": {
"comment": message,
"count": 6,
"sessionid": this.getSessionID()
},
"json": true
}, function(err, response, body) {
if(!callback) {
return;
}
if (err) {
callback(err);
return;
}
if(body.success) {
callback(null);
2015-11-10 12:29:23 +08:00
} else if(body.error) {
callback(new Error(body.error));
} else {
callback(new Error("Unknown error"));
}
2016-03-05 08:35:04 +08:00
}, "steamcommunity");
};
SteamCommunity.prototype.inviteUserToGroup = function(userID, groupID, callback) {
if(typeof userID === 'string') {
userID = new SteamID(userID);
}
var self = this;
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/GroupInvite",
"form": {
"group": groupID.toString(),
"invitee": userID.toString(),
"json": 1,
"sessionID": this.getSessionID(),
"type": "groupInvite"
},
"json": true
}, function(err, response, body) {
if(!callback) {
return;
}
if (err) {
callback(err);
return;
}
if(body.results == 'OK') {
callback(null);
} else if(body.results) {
callback(new Error(body.results));
} else {
callback(new Error("Unknown error"));
}
2016-03-05 08:35:04 +08:00
}, "steamcommunity");
};
2015-09-07 11:22:10 +08:00
2016-07-07 07:20:42 +08:00
SteamCommunity.prototype.getUserAliases = function(userID, callback) {
if (typeof userID === 'string') {
2016-07-07 07:20:42 +08:00
userID = new SteamID(userID);
}
this.httpRequestGet({
"uri": "https://steamcommunity.com/profiles/" + userID.getSteamID64() + "/ajaxaliases",
2016-07-07 07:20:42 +08:00
"json": true
}, function(err, response, body) {
if (err) {
callback(err);
return;
}
if (typeof body !== 'object') {
2016-07-07 07:20:42 +08:00
callback(new Error("Malformed response"));
return;
}
callback(null, body.map(function(entry) {
entry.timechanged = Helpers.decodeSteamTime(entry.timechanged);
return entry;
}));
2016-07-07 07:20:42 +08:00
}, "steamcommunity");
};
2015-09-07 11:22:10 +08:00
SteamCommunity.prototype.getUserInventoryContexts = function(userID, callback) {
if(typeof userID === 'string') {
userID = new SteamID(userID);
}
if(typeof userID === 'function') {
callback = userID;
userID = this.steamID;
}
if(!userID) {
callback(new Error("No SteamID specified and not logged in"));
return;
}
var self = this;
this.httpRequest("https://steamcommunity.com/profiles/" + userID.getSteamID64() + "/inventory/", function(err, response, body) {
if (err) {
callback(err);
2015-09-07 11:22:10 +08:00
return;
}
var match = body.match(/var g_rgAppContextData = ([^\n]+);\r?\n/);
if(!match) {
callback(new Error("Malformed response"));
return;
}
var data;
try {
data = JSON.parse(match[1]);
} catch(e) {
callback(new Error("Malformed response"));
return;
}
callback(null, data);
2016-03-05 08:35:04 +08:00
}, "steamcommunity");
2015-09-07 11:22:10 +08:00
};
2015-09-07 12:00:19 +08:00
SteamCommunity.prototype.getUserInventory = function(userID, appID, contextID, tradableOnly, callback) {
var self = this;
if(typeof userID === 'string') {
userID = new SteamID(userID);
}
get([], [], 1);
2015-09-07 12:00:19 +08:00
function get(inventory, currency, step, start) {
self.httpRequest({
"uri": "https://steamcommunity.com/inventory/" + userID.getSteamID64() + "/" + appID + "/" + contextID,
2015-09-07 12:00:19 +08:00
"qs": {
"l": "english", // Default language
"count": 5000, // Max items per 'page'
"start_assetid": start
2015-09-07 12:00:19 +08:00
},
"json": true
}, function(err, response, body) {
if (err) {
if (err.message == "HTTP error 403" && body === null) {
// 403 with a body of "null" means the inventory/profile is private.
callback(new Error("This profile is private."));
return;
}
callback(err);
2015-09-07 12:00:19 +08:00
return;
}
if (!body || !body.success || !body.assets || !body.descriptions) {
if (body) {
// Dunno if the error/Error property even exists on this new endpoint
callback(new Error(body.error || body.Error || "Malformed response"));
} else {
callback(new Error("Malformed response"));
}
2015-09-07 12:00:19 +08:00
return;
}
for (var i = 0; i < body.assets.length; i++) {
var description = getDescription(body.descriptions, body.assets[i].classid, body.assets[i].instanceid);
if (!tradableOnly || (description && description.tradable)) {
(body.assets[i].currencyid ? currency : inventory).push(new CEconItem(body.assets[i], description, contextID));
2015-09-07 12:00:19 +08:00
}
}
if(body.total_inventory_count > 5000 * step) {
get(inventory, currency, step + 1, body.assets[body.assets.length - 1].assetid);
2015-09-07 12:00:19 +08:00
} else {
callback(null, inventory, currency, body.total_inventory_count);
2015-09-07 12:00:19 +08:00
}
2016-03-05 08:35:04 +08:00
}, "steamcommunity");
2015-09-07 12:00:19 +08:00
}
// A bit of optimization; objects are hash tables so it's more efficient to look up by key than to iterate an array
var quickDescriptionLookup = {};
function getDescription(descriptions, classID, instanceID) {
instanceID = instanceID || '0'; // instanceID can be undefined, in which case it's 0.
var key = classID + '_' + instanceID;
if (quickDescriptionLookup[key]) {
return quickDescriptionLookup[key];
}
for (var i = 0; i < descriptions.length; i++) {
quickDescriptionLookup[key] = descriptions[i];
if (descriptions[i].classid == classID && descriptions[i].instanceid == instanceID) {
return descriptions[i];
}
}
}
};