From 4152865f5eb2097124becd5c10deb53d7686c063 Mon Sep 17 00:00:00 2001 From: Shaun Barratt Date: Fri, 15 Jan 2016 17:48:57 +0000 Subject: [PATCH] Adding ability to edit an annoucement. Added an example. --- classes/CSteamGroup.js | 18 ++++--- components/groups.js | 39 ++++++++++++++ examples/edit-group-announcement.js | 79 +++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 examples/edit-group-announcement.js diff --git a/classes/CSteamGroup.js b/classes/CSteamGroup.js index 2ac4937..812f42b 100644 --- a/classes/CSteamGroup.js +++ b/classes/CSteamGroup.js @@ -6,27 +6,27 @@ SteamCommunity.prototype.getSteamGroup = function(id, callback) { if(typeof id !== 'string' && !(typeof id === 'object' && id.__proto__ === SteamID.prototype)) { throw new Error("id parameter should be a group URL string or a SteamID object"); } - + if(typeof id === 'object' && (id.universe != SteamID.Universe.PUBLIC || id.type != SteamID.Type.CLAN)) { throw new Error("SteamID must stand for a clan account in the public universe"); } - + var self = this; this.request("https://steamcommunity.com/" + (typeof id === 'string' ? "groups/" + id : "gid/" + id.toString()) + "/memberslistxml/?xml=1", function(err, response, body) { if(self._checkHttpError(err, response, callback)) { return; } - + if(self._checkCommunityError(body, callback)) { return; } - + xml2js.parseString(body, function(err, result) { if(err) { callback(err); return; } - + callback(null, new CSteamGroup(self, result.memberList)); }); }); @@ -34,7 +34,7 @@ SteamCommunity.prototype.getSteamGroup = function(id, callback) { function CSteamGroup(community, groupData) { this._community = community; - + this.steamID = new SteamID(groupData.groupID64[0]); this.name = groupData.groupDetails[0].groupName[0]; this.url = groupData.groupDetails[0].groupURL[0]; @@ -50,7 +50,7 @@ function CSteamGroup(community, groupData) { CSteamGroup.prototype.getAvatarURL = function(size, protocol) { size = size || ''; protocol = protocol || 'http://'; - + var url = protocol + "steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/" + this.avatarHash.substring(0, 2) + "/" + this.avatarHash; if(size == 'full' || size == 'medium') { return url + "_" + size + ".jpg"; @@ -80,6 +80,10 @@ CSteamGroup.prototype.postAnnouncement = function(headline, content, callback) { this._community.postGroupAnnouncement(this.steamID, headline, content, callback); }; +CSteamGroup.prototype.editAnnouncement = function(annoucementID, headline, content, callback) { + this._community.editGroupAnnouncement(this.steamID, annoucementID, headline, content, callback) +}; + CSteamGroup.prototype.scheduleEvent = function(name, type, description, time, server, callback) { this._community.scheduleGroupEvent(this.steamID, name, type, description, time, server, callback); }; diff --git a/components/groups.js b/components/groups.js index b12a6dc..767417b 100644 --- a/components/groups.js +++ b/components/groups.js @@ -159,6 +159,45 @@ SteamCommunity.prototype.postGroupAnnouncement = function(gid, headline, content }) }; +SteamCommunity.prototype.editGroupAnnouncement = function(gid, aid, headline, content, callback) { + if(typeof gid === 'string') { + gid = new SteamID(gid); + } + + var self = this; + + var submitData = { + "uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/announcements", + "form": { + "sessionID": this.getSessionID(), + "gid": aid, + "action": "update", + "headline": headline, + "body": content, + "languages[0][headline]": headline, + "languages[0][body]": content, + "languages[0][updated]": 1 + } + } + + this.request.post(submitData, function(err, response, body) { + if(!callback) { + return; + } + + if(err || response.statusCode >= 400) { + callback(err || new Error("HTTP error " + response.statusCode)); + return; + } + + if(self._checkCommunityError(body, callback)) { + return; + } + + callback(null); + }) +}; + SteamCommunity.prototype.scheduleGroupEvent = function(gid, name, type, description, time, server, callback) { if(typeof gid === 'string') { gid = new SteamID(gid); diff --git a/examples/edit-group-announcement.js b/examples/edit-group-announcement.js new file mode 100644 index 0000000..e59932b --- /dev/null +++ b/examples/edit-group-announcement.js @@ -0,0 +1,79 @@ +var SteamCommunity = require('../index.js'); +var ReadLine = require('readline'); +var fs = require('fs'); + +var community = new SteamCommunity(); +var rl = ReadLine.createInterface({ + "input": process.stdin, + "output": process.stdout +}); + +rl.question("Username: ", function(accountName) { + rl.question("Password: ", function(password) { + doLogin(accountName, password); + }); +}); + +function doLogin(accountName, password, authCode, twoFactorCode) { + community.login({ + "accountName": accountName, + "password": password, + "authCode": authCode, + "twoFactorCode": twoFactorCode + }, function(err, sessionID, cookies, steamguard) { + if(err) { + if(err.message == 'SteamGuardMobile') { + rl.question("Steam Authenticator Code: ", function(code) { + doLogin(accountName, password, null, code); + }); + + return; + } + + if(err.message == 'SteamGuard') { + console.log("An email has been sent to your address at " + err.emaildomain); + rl.question("Steam Guard Code: ", function(code) { + doLogin(accountName, password, code); + }); + + return; + } + + console.log(err); + process.exit(); + return; + } + + console.log("Logged on!"); + + rl.question("Group ID: ", function(gid) { + community.getSteamGroup(gid, function(err, group) { + if (err) { + console.log(err); + process.exit(1); + } + + rl.question("Annoucement ID: ", function(aid) { + rl.question("New title: ", function(header) { + rl.question("New body: ", function(content) { + // EW THE PYRAMID! + editAnnouncement(group, aid, header, content); + }); + }); + }); + }); + }); + }); +} + +function editAnnouncement(group, aid, header, content) { + // Actual community method. + group.editAnnouncement(aid, header, content, function(error) { + if(!error) { + console.log("Annoucement edited!"); + } else { + console.log("Unable to edit annoucement! %j", error); + process.exit(1); + } + }); +}