diff --git a/classes/CSteamGroup.js b/classes/CSteamGroup.js index f4d9df9..9988d57 100644 --- a/classes/CSteamGroup.js +++ b/classes/CSteamGroup.js @@ -114,6 +114,14 @@ CSteamGroup.prototype.getHistory = function(page, callback) { this._community.getGroupHistory(this.steamID, page, callback); }; + +CSteamGroup.prototype.getAllComments = function(from, count, callback) { + this._community.getAllGroupComments(this.steamID, from, count, callback); +}; + +CSteamGroup.prototype.deleteComment = function(cid, callback) { + this._community.deleteGroupComment(this.steamID, cid, callback); + /** * Get requests to join this restricted group. * @param {function} callback - First argument is null/Error, second is array of SteamID objects diff --git a/components/groups.js b/components/groups.js index 4e5ec3b..946c4c7 100644 --- a/components/groups.js +++ b/components/groups.js @@ -2,6 +2,7 @@ var SteamCommunity = require('../index.js'); var SteamID = require('steamid'); var xml2js = require('xml2js'); var Cheerio = require('cheerio'); +var Helpers = require('./helpers.js'); var EResult = SteamCommunity.EResult; SteamCommunity.prototype.getGroupMembers = function(gid, callback, members, link, addresses, addressIdx) { @@ -519,6 +520,70 @@ SteamCommunity.prototype.getGroupHistory = function(gid, page, callback) { }, "steamcommunity"); }; +SteamCommunity.prototype.getAllGroupComments = function(gid, from, count, callback) { + var options = { + uri: "http://steamcommunity.com/comment/Clan/render/" + gid.getSteamID64() + "/-1/", + form: { + start: from, + count: count + } + }; + + var self = this; + this.httpRequestPost(options, function(err, response, body) { + if (err) { + callback(err); + return; + } + + var comments = []; + + var $ = Cheerio.load(JSON.parse(body).comments_html); + + $(".commentthread_comment_content").each(function () { + var comment = {}; + var cachedSelector; + + var $selector = $(this).find(".commentthread_author_link"); + comment.authorName = $($selector).find("bdi").text(); + comment.authorId = $($selector).attr("href").replace(/http:\/\/steamcommunity.com\/(id|profiles)\//, ""); + comment.date = Helpers.decodeSteamTime($(this).find(".commentthread_comment_timestamp").text().trim()); + + $selector = $(this).find(".commentthread_comment_text"); + comment.commentId = $($selector).attr("id").replace("comment_content_", ""); + comment.text = $($selector).html().trim(); + + comments.push(comment); + }); + + callback(null, comments); + }, "steamcommunity"); +}; + +SteamCommunity.prototype.deleteGroupComment = function(gid, cid, callback) { + + if(typeof cid !== 'string') { + cid = cid.toString(); + } + + var options = { + uri: "http://steamcommunity.com/comment/Clan/delete/" + gid.getSteamID64() + "/-1/", + form: { + sessionid: this.getSessionID(), + gidcomment: cid + } + }; + + var self = this; + this.httpRequestPost(options, function(err, response, body) { + if(!callback) { + return; + } + + callback(err || null); + }, "steamcommunity"); +}; + /** * Get requests to join a restricted group. * @param {SteamID|string} gid - The SteamID of the group you want to manage diff --git a/components/helpers.js b/components/helpers.js index ec71ea3..d33d391 100644 --- a/components/helpers.js +++ b/components/helpers.js @@ -13,11 +13,26 @@ exports.isSteamID = function(input) { }; exports.decodeSteamTime = function(time) { - var parts = time.split('@'); - if (!parts[0].match(/,/)) { - // no year, assume current year - parts[0] += ", " + (new Date()).getFullYear(); + var date = new Date(); + + if (time.includes("@")) { + var parts = time.split('@'); + if (!parts[0].includes(",")) { + // no year, assume current year + parts[0] += ", " + date.getFullYear(); + } + + date = new Date(parts.join('@').replace(/(am|pm)/, ' $1') + " UTC"); // add a space so JS can decode it + } else { + // Relative date + var amount = time.replace(/(\d) (minutes|hour|hours) ago/, "$1"); + + if(time.includes("minutes")) { + date.setMinutes(date.getMinutes() - amount); + } else if(time.match(/hour|hours/)) { + date.setHours(date.getHours() - amount); + } } - return new Date(parts.join('@').replace(/(am|pm)/, ' $1') + " UTC"); // add a space so JS can decode it + return date; };