Skip to content

Commit 1434919

Browse files
committed
push - guidelines
1 parent bbc010b commit 1434919

File tree

12 files changed

+561
-120
lines changed

12 files changed

+561
-120
lines changed

plugins/push/api/api.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var plugin = {},
1414
plugins.internalEvents.push('[CLY]_push_action');
1515
plugins.internalDrillEvents.push('[CLY]_push_action');
1616

17+
/** function sets up commons */
1718
function setUpCommons() {
1819
let creds = require('./parts/credentials.js');
1920
for (let k in creds.DB_MAP) {
@@ -67,15 +68,15 @@ var plugin = {},
6768
}
6869
else {
6970
pushEvents.forEach(event => {
70-
var msg = msgs.filter(msg => ('' + msg._id) === event.segmentation.i)[0],
71+
var msg = msgs.filter(msg1 => ('' + msg1._id) === event.segmentation.i)[0],
7172
inc = {};
7273
if (msg) {
7374
event.segmentation.a = msg.auto || false;
7475

75-
if (event.key == '[CLY]_push_open') {
76+
if (event.key === '[CLY]_push_open') {
7677
inc['result.delivered'] = event.count;
7778
}
78-
else if (event.key == '[CLY]_push_action') {
79+
else if (event.key === '[CLY]_push_action') {
7980
inc['result.actioned'] = event.count;
8081
if (event.segmentation && event.segmentation.b !== undefined) {
8182
inc['result.actioned|' + event.segmentation.b] = event.count;
@@ -187,7 +188,7 @@ var plugin = {},
187188
updateUsersMonth['d.' + params.time.day + '.' + common.dbMap['messaging-enabled']] = 1;
188189
}
189190

190-
if (userLastSeenDate.getFullYear() == params.time.yearly &&
191+
if (userLastSeenDate.getFullYear() === parseInt(params.time.yearly) &&
191192
Math.ceil(common.moment(userLastSeenDate).tz(params.appTimezone).format('DDD') / 7) < params.time.weekly && messagingTokenKeys(dbAppUser).length) {
192193
updateUsersZero['d.w' + params.time.weekly + '.' + common.dbMap['messaging-enabled']] = 1;
193194
}
@@ -230,7 +231,10 @@ var plugin = {},
230231
plugins.register('/consent/change', ({params, changes}) => {
231232
push.onConsentChange(params, changes);
232233
});
233-
234+
/** collects messaging token keys
235+
* @param {object} dbAppUser - data
236+
* @returns {array} list of tokens
237+
*/
234238
function messagingTokenKeys(dbAppUser) {
235239
var a = [];
236240
for (var k in dbAppUser[common.dbUserMap.tokens]) {

plugins/push/api/jobs/clear.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@ const job = require('../../../../api/parts/jobs/job.js'),
44
log = require('../../../../api/utils/log.js')('job:push:clear'),
55
N = require('../parts/note.js'),
66
retry = require('../../../../api/parts/jobs/retry.js');
7-
7+
/** clear job class */
88
class ClearJob extends job.Job {
9+
/** constructs clear job
10+
* @param {string} name - name
11+
* @param {object} data - data
12+
*/
913
constructor(name, data) {
1014
super(name, data);
1115
log.d('Preparing to clear %j', this.data);
1216
}
1317

18+
/** function returns NoretryPolicy
19+
@returns {object} retrypolicy
20+
*/
1421
retryPolicy() {
1522
return new retry.NoRetryPolicy();
1623
}
1724

25+
/** function runs clearing job
26+
* @param {object} db - db connection
27+
* @param {function} done - callback function, having error as first returned parameter
28+
*/
1829
run(db, done) {
1930
log.d('Clearing %j', this.data);
2031

@@ -49,12 +60,12 @@ class ClearJob extends job.Job {
4960
done();
5061
}
5162
else {
52-
db.collection('credentials').remove({_id: db.ObjectID(this.data.cid)}, (err, ok) => {
53-
if (err) {
54-
done(err);
63+
db.collection('credentials').remove({_id: db.ObjectID(this.data.cid)}, (err1, ok1) => {
64+
if (err1) {
65+
done(err1);
5566
}
5667
else {
57-
log.d('Cleared credentials %j: %j', this.data.cid, ok);
68+
log.d('Cleared credentials %j: %j', this.data.cid, ok1);
5869
done();
5970
}
6071
});

plugins/push/api/jobs/process.js

+71-7
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ const FORK_WHEN_MORE_THAN = 100000,
1313
FORK_MAX = 5,
1414
SEND_AHEAD = 5 * 60000,
1515
BATCH = 50000;
16-
16+
/** proces jo class */
1717
class ProcessJob extends J.IPCJob {
18+
/** class constructr
19+
* @param {string} name - name
20+
* @param {object} data - data
21+
*/
1822
constructor(name, data) {
1923
super(name, data);
2024
if (this.isFork) {
@@ -23,26 +27,46 @@ class ProcessJob extends J.IPCJob {
2327
log.d('initializing ProcessJob with %j & %j', name, data);
2428
}
2529

30+
/** gets cid
31+
* @returns {string} cid
32+
*/
2633
get cid() {
2734
return this.data.cid;
2835
}
2936

37+
/** gets aid
38+
* @returns {string} aid
39+
*/
3040
get aid() {
3141
return this.data.aid;
3242
}
3343

44+
/** gets data.field
45+
* @returns {object} data.field
46+
*/
3447
get field() {
3548
return this.data.field;
3649
}
3750

51+
/** gets platform
52+
* @returns {string} data.field.substr(0, 1)
53+
*/
3854
get platform() {
3955
return this.data.field.substr(0, 1);
4056
}
4157

58+
/** checks if is fork
59+
* @returns {boolean} true - if fork
60+
*/
4261
get isFork() {
4362
return !!this.data.fork;
4463
}
4564

65+
/** prepares job
66+
* @param {object} manager - manager
67+
* @param {object} db - db connection
68+
* @returns {Promise} - resolved or rejected
69+
*/
4670
prepare(manager, db) {
4771
log.d('Loading credentials for %j', this.data);
4872
this.creds = new C.Credentials(this.data.cid);
@@ -77,22 +101,41 @@ class ProcessJob extends J.IPCJob {
77101
});
78102
}
79103

104+
/** gets resource name
105+
* @returns {string} name
106+
*/
80107
resourceName() {
81108
return 'process:' + this.cid + ':' + this.field;
82109
}
83110

111+
/** creates resource
112+
* @param {string} _id - id
113+
* @param {string} name - name
114+
* @param {object} db - db connection
115+
* @returns {object} Resource
116+
*/
84117
createResource(_id, name, db) {
85118
return new Resource(_id, name, {cid: this.cid, field: this.field}, db);
86119
}
87120

121+
/** gets new retry policy
122+
* @returns {object} retry policy
123+
*/
88124
retryPolicy() {
89125
return new R.IPCRetryPolicy(3);
90126
}
91127

128+
/** rescedule
129+
* @param {number} date - timestamp
130+
* @returns {Promise} - resolved if updated
131+
*/
92132
reschedule(date) {
93133
return this.replaceAfter(date);
94134
}
95135

136+
/** fork
137+
* @returns {Promise} promise
138+
*/
96139
fork() {
97140
if (!this.maxFork) {
98141
this.maxFork = 0;
@@ -102,10 +145,18 @@ class ProcessJob extends J.IPCJob {
102145
return ProcessJob.insert(this.db(), {name: this.name, status: 0, data: data, next: Date.now()});
103146
}
104147

148+
/** gets current timestamp
149+
* @returns {number} timestamp
150+
*/
105151
now() {
106152
return Date.now();
107153
}
108154

155+
/**
156+
* @param {object} notes - notes
157+
* @param {object} msgs - messages
158+
* @returns {object} m
159+
*/
109160
compile(notes, msgs) {
110161
// let pm, pn, pp, po;
111162

@@ -126,6 +177,9 @@ class ProcessJob extends J.IPCJob {
126177
}).filter(m => !!m.m);
127178
}
128179

180+
/** finish
181+
* @param {object} err - error message or object
182+
*/
129183
async _finish(err) {
130184
if (err) {
131185
let counts = await this.loader.counts(Date.now() + SEND_AHEAD, this._id);
@@ -148,18 +202,23 @@ class ProcessJob extends J.IPCJob {
148202
return await super._finish(err);
149203
}
150204

205+
/** run
206+
* @param {object} db - db connection
207+
* @param {function} done - callback function
208+
*/
151209
async run(db, done) {
152210
let resourceError, affected;
153211
try {
154212
let count = await this.loader.count(this.now() + SEND_AHEAD), recheck = [], sending = new Set();
155213

156-
if (count == 0) {
214+
if (count === 0) {
157215
return done();
158216
}
159217
else if (this.isFork && count < FORK_WHEN_MORE_THAN) {
160218
return done();
161219
}
162220

221+
163222
do {
164223
let date = this.now() + SEND_AHEAD,
165224
discarded = await this.loader.discard(date - 3600000);
@@ -181,9 +240,9 @@ class ProcessJob extends J.IPCJob {
181240
// check for aborted or deleted messages, delete notes if needed
182241
if (!this.isFork) {
183242
await Promise.all(Object.values(notes).filter(n => (n.result.status & (N.Status.Aborted | N.Status.Deleted)) > 0).map(note => {
184-
let count = counts[note._id.toString()];
185-
log.w('Note %s has been aborted, clearing %d notifications', note._id, count);
186-
return this.loader.abortNote(note._id, count, this.now(), this.field, (note.result.status & N.Status.Aborted) ? 'Aborted' : 'Deleted');
243+
let count1 = counts[note._id.toString()];
244+
log.w('Note %s has been aborted, clearing %d notifications', note._id, count1);
245+
return this.loader.abortNote(note._id, count1, this.now(), this.field, (note.result.status & N.Status.Aborted) ? 'Aborted' : 'Deleted');
187246
}));
188247
}
189248

@@ -473,13 +532,18 @@ class ProcessJob extends J.IPCJob {
473532
try {
474533
await this.loader.reload(this._id);
475534
}
476-
catch (e) {
477-
log.e('Error when reloading for %s: %j', this._id, e);
535+
catch (err) {
536+
log.e('Error when reloading for %s: %j', this._id, err);
478537
}
479538
done(e);
480539
}
481540
}
482541

542+
/** result handling
543+
* @param {object} resourceError - error object or string
544+
* @param {array} affected - list of affected
545+
* @returns {boolean} true or false
546+
*/
483547
async handleResults(resourceError, affected) {
484548
// in case main job ends with resource error, record it in all messages affected
485549
// when too much same errors gathered in messages within retry period of 30 minutes, don't reschedule this process job

plugins/push/api/jobs/schedule.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,39 @@ const J = require('../../../../api/parts/jobs/job.js'),
44
log = require('../../../../api/utils/log.js')('job:push:schedule:' + process.pid),
55
S = require('../parts/store.js'),
66
N = require('../parts/note.js');
7-
7+
/** schedule job class */
88
class ScheduleJob extends J.Job {
9+
/** class constructr
10+
* @param {string} name - name
11+
* @param {object} data - data
12+
*/
913
constructor(name, data) {
1014
super(name, data);
1115
log.d('initializing ScheduleJob with %j & %j', name, data);
1216
}
1317

18+
/** prepares job
19+
* @param {object} manager - not used
20+
* @param {object} db - db connection
21+
*/
1422
async prepare(manager, db /*, apps */) {
1523
log.d('Loading notification %s', this.data.mid);
1624
this.note = await N.Note.load(db, this.data.mid);
1725
log.d('Loaded notification %s', this.note && this.note._id);
1826
this.sg = new S.StoreGroup(db);
1927
}
2028

29+
/** _timeoutCancelled()
30+
* @returns {boolean} true(always)
31+
*/
2132
_timeoutCancelled() {
2233
return true;
2334
}
2435

36+
/** run
37+
* @param {object} db - data base connection
38+
* @param {function} done - callback function
39+
*/
2540
async run(db, done) {
2641
let update, error;
2742

plugins/push/api/jobs/validate.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,59 @@
44

55
const job = require('../../../../api/parts/jobs/job.js'),
66
log = require('../../../../api/utils/log.js')('job:push:validate'),
7-
creds = require('../parts/credentials.js'),
7+
//creds = require('../parts/credentials.js'),
88
ConnectionResource = require('../parts/res.js'),
99
retry = require('../../../../api/parts/jobs/retry.js');
1010

11-
11+
/** class - ValidateJob */
1212
class ValidateJob extends job.TransientJob {
13+
/** constructor
14+
* @param {string} name - name
15+
* @param {object} data - data
16+
*/
1317
constructor(name, data) {
1418
super(name, data);
1519
}
1620

17-
prepare(manager, db) {
21+
/** prepare
22+
* @returns {Promise} promise, resolved(always)
23+
*/
24+
prepare(/*manager, db*/) {
1825
return Promise.resolve();
1926
}
2027

28+
/** resource name
29+
* @returns {string} 'validate:' + this.data.cid
30+
*/
2131
resourceName() {
2232
return 'validate:' + this.data.cid;
2333
}
2434

35+
/** create resource
36+
* @param {string} _id - id
37+
* @param {string} name - name
38+
* @returns {object} ConnectionResource
39+
*/
2540
createResource(_id, name) {
2641
return new ConnectionResource(_id, name, {cid: this.data.cid, test: false, field: 'ip'}, this.db());
2742
}
2843

44+
/** release Resource (call close() on it, returns result)
45+
* @param {object} resource to call on
46+
* @returns {object} result on close
47+
*/
2948
releaseResource(resource) {
3049
return resource.close();
3150
}
3251

52+
/** function returns NoretryPolicy
53+
@returns {object} retrypolicy
54+
*/
3355
retryPolicy() {
3456
return new retry.NoRetryPolicy();
3557
}
3658

59+
/** function runs job */
3760
async run() {
3861
log.i('[%d] validating credentials %s', process.pid, this.data.cid);
3962

0 commit comments

Comments
 (0)