This repository has been archived by the owner on Sep 9, 2019. It is now read-only.
forked from TradeMe/ReviewMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reviews.js
85 lines (70 loc) · 2.66 KB
/
reviews.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const request = require('request');
const appstore = require('./appstorereviews.js');
const googlePlay = require('./googleplayreviews.js');
const fs = require('fs');
const REVIEWS_STORES = {
"APP_STORE": "app-store",
"GOOGLE_PLAY": "google-play"
};
var published_reviews;
try {
published_reviews = JSON.parse(fs.readFileSync('./published_reviews.json'));
} catch (err) {
published_reviews = {}
}
(function () {
exports.start = function start(config) {
if (!config.store) {
// Determine from which store reviews are downloaded
config.store = (config.appId.indexOf("\.") > -1) ? REVIEWS_STORES.GOOGLE_PLAY : REVIEWS_STORES.APP_STORE;
}
if (config.store === REVIEWS_STORES.APP_STORE) {
appstore.startReview(config, !published_reviews[config.appId]);
} else {
googlePlay.startReview(config, !published_reviews[config.appId])
}
}
}).call(this);
// Published reviews
exports.markReviewAsPublished = function (config, review) {
if (!review || !review.id || this.reviewPublished(config, review)) return;
if (!published_reviews[config.appId]) {
published_reviews[config.appId] = []
}
if (config.verbose) {
console.log("INFO: Checking if we need to prune published reviews have (" + published_reviews[config.appId].length + ") limit (" + REVIEWS_LIMIT + ")");
}
if (published_reviews[config.appId].length >= REVIEWS_LIMIT) {
published_reviews[config.appId] = published_reviews[config.appId].slice(0, REVIEWS_LIMIT);
}
published_reviews[config.appId].unshift(review.id);
if (config.verbose) {
console.log("INFO: Review marked as published: " + JSON.stringify(published_reviews[config.appId]));
}
fs.writeFileSync('./published_reviews.json', JSON.stringify(published_reviews), { flag: 'w' })
};
exports.reviewPublished = function (config, review) {
if (!review || !review.id || !published_reviews[config.appId]) return false;
return published_reviews[config.appId].indexOf(review.id) >= 0;
};
exports.publishedReviews = function () {
return published_reviews;
};
exports.resetPublishedReviews = function () {
return published_reviews = {};
};
exports.postToSlack = function (message, config) {
var messageJSON = JSON.stringify(message);
if (config.verbose) {
console.log("INFO: Posting new message to Slack: ");
console.log("INFO: Hook: " + config.slackHook);
console.log("INFO: Message: " + messageJSON);
}
return request.post({
url: config.slackHook,
headers: {
"Content-Type": "application/json"
},
body: messageJSON
});
};