wingbot-mongodb 2.18.0 → 2.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.nyc_output/f60575da-064f-476a-a3be-be7e864d586d.json +1 -0
- package/.nyc_output/processinfo/f60575da-064f-476a-a3be-be7e864d586d.json +1 -0
- package/.nyc_output/processinfo/index.json +1 -1
- package/README.md +257 -20
- package/package.json +9 -3
- package/src/AuditLogStorage.js +364 -0
- package/src/BaseStorage.js +86 -14
- package/src/BotConfigStorage.js +15 -5
- package/src/BotTokenStorage.js +1 -1
- package/src/ChatLogStorage.js +59 -28
- package/src/NotificationsStorage.js +7 -7
- package/src/StateStorage.js +2 -2
- package/src/main.js +3 -1
- package/.nyc_output/77359fd4-c391-4d0e-bcba-614b1cf4709e.json +0 -1
- package/.nyc_output/processinfo/77359fd4-c391-4d0e-bcba-614b1cf4709e.json +0 -1
package/src/ChatLogStorage.js
CHANGED
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
*/
|
|
4
4
|
'use strict';
|
|
5
5
|
|
|
6
|
-
const mongodb = require('mongodb'); // eslint-disable-line no-unused-vars
|
|
7
6
|
const BaseStorage = require('./BaseStorage');
|
|
8
7
|
|
|
9
8
|
const PAGE_SENDER_TIMESTAMP = 'pageId_1_senderId_1_timestamp_-1';
|
|
10
9
|
const TIMESTAMP = 'timestamp_1';
|
|
11
10
|
|
|
11
|
+
/** @typedef {import('mongodb/lib/db')} Db */
|
|
12
|
+
|
|
12
13
|
/**
|
|
13
14
|
* Storage for conversation logs
|
|
14
15
|
*
|
|
@@ -18,12 +19,13 @@ class ChatLogStorage extends BaseStorage {
|
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
21
|
*
|
|
21
|
-
* @param {
|
|
22
|
+
* @param {Db|{():Promise<Db>}} mongoDb
|
|
22
23
|
* @param {string} collectionName
|
|
23
24
|
* @param {{error:Function,log:Function}} [log] - console like logger
|
|
24
|
-
* @param {boolean} isCosmo
|
|
25
|
+
* @param {boolean} [isCosmo]
|
|
26
|
+
* @param {string|Promise<string>} [secret]
|
|
25
27
|
*/
|
|
26
|
-
constructor (mongoDb, collectionName = 'chatlogs', log = console, isCosmo = false) {
|
|
28
|
+
constructor (mongoDb, collectionName = 'chatlogs', log = console, isCosmo = false, secret = null) {
|
|
27
29
|
super(mongoDb, collectionName, log, isCosmo);
|
|
28
30
|
|
|
29
31
|
this.addIndex({
|
|
@@ -43,6 +45,7 @@ class ChatLogStorage extends BaseStorage {
|
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
this.muteErrors = true;
|
|
48
|
+
this._secret = secret;
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
/**
|
|
@@ -54,6 +57,7 @@ class ChatLogStorage extends BaseStorage {
|
|
|
54
57
|
* @param {number} [limit]
|
|
55
58
|
* @param {number} [endAt] - iterate backwards to history
|
|
56
59
|
* @param {number} [startAt] - iterate forward to last interaction
|
|
60
|
+
* @returns {Promise<object[]>}
|
|
57
61
|
*/
|
|
58
62
|
async getInteractions (senderId, pageId, limit = 10, endAt = null, startAt = null) {
|
|
59
63
|
const c = await this._getCollection();
|
|
@@ -80,14 +84,33 @@ class ChatLogStorage extends BaseStorage {
|
|
|
80
84
|
const res = await c.find(q)
|
|
81
85
|
.limit(limit)
|
|
82
86
|
.sort({ timestamp: orderBackwards ? 1 : -1 })
|
|
83
|
-
.project({ _id: 0
|
|
87
|
+
.project({ _id: 0 })
|
|
84
88
|
.toArray();
|
|
85
89
|
|
|
86
90
|
if (!orderBackwards) {
|
|
87
91
|
res.reverse();
|
|
88
92
|
}
|
|
89
93
|
|
|
90
|
-
|
|
94
|
+
if (!this._secret) {
|
|
95
|
+
return res.map((r) => Object.assign(r, { ok: null }));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const secret = await Promise.resolve(this._secret);
|
|
99
|
+
|
|
100
|
+
return res.map((r) => {
|
|
101
|
+
const {
|
|
102
|
+
sign,
|
|
103
|
+
...log
|
|
104
|
+
} = r;
|
|
105
|
+
const objToSign = this._objectToSign(log);
|
|
106
|
+
const compare = this._signWithSecret(objToSign, secret);
|
|
107
|
+
const ok = compare === sign;
|
|
108
|
+
if (!ok) {
|
|
109
|
+
this._log.error(`ChatLog: found wrong signature at pageId: "${r.pageId}", senderId: "${r.senderId}", at: ${r.timestamp}`, r);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return Object.assign(log, { ok });
|
|
113
|
+
});
|
|
91
114
|
}
|
|
92
115
|
|
|
93
116
|
/**
|
|
@@ -102,22 +125,38 @@ class ChatLogStorage extends BaseStorage {
|
|
|
102
125
|
log (senderId, responses = [], request = {}, metadata = {}) {
|
|
103
126
|
const log = {
|
|
104
127
|
senderId,
|
|
105
|
-
time: new Date(request.timestamp || Date.now()),
|
|
106
128
|
request,
|
|
107
|
-
responses
|
|
129
|
+
responses,
|
|
130
|
+
...metadata
|
|
108
131
|
};
|
|
109
132
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return this._getCollection()
|
|
113
|
-
.then((c) => c.insertOne(log))
|
|
114
|
-
.catch((err) => {
|
|
115
|
-
this._log.error('Failed to store chat log', err, log);
|
|
133
|
+
return this._storeLog(log);
|
|
134
|
+
}
|
|
116
135
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
136
|
+
async _storeLog (event) {
|
|
137
|
+
let log = event;
|
|
138
|
+
if (!event.timestamp) {
|
|
139
|
+
Object.assign(event, {
|
|
140
|
+
timestamp: event.request.timestamp || Date.now()
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
if (typeof event.pageId === 'undefined') {
|
|
144
|
+
Object.assign(event, {
|
|
145
|
+
pageId: null
|
|
120
146
|
});
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
const c = await this._getCollection();
|
|
150
|
+
log = await this._sign(log);
|
|
151
|
+
// @ts-ignore
|
|
152
|
+
await c.insertOne(log);
|
|
153
|
+
} catch (e) {
|
|
154
|
+
this._log.error('Failed to store chat log', e, log);
|
|
155
|
+
|
|
156
|
+
if (!this.muteErrors) {
|
|
157
|
+
throw e;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
121
160
|
}
|
|
122
161
|
|
|
123
162
|
/**
|
|
@@ -135,23 +174,15 @@ class ChatLogStorage extends BaseStorage {
|
|
|
135
174
|
error (err, senderId, responses = [], request = {}, metadata = {}) {
|
|
136
175
|
const log = {
|
|
137
176
|
senderId,
|
|
138
|
-
time: new Date(request.timestamp || Date.now()),
|
|
139
177
|
request,
|
|
140
178
|
responses,
|
|
141
|
-
err: `${err}
|
|
179
|
+
err: `${err}`,
|
|
180
|
+
...metadata
|
|
142
181
|
};
|
|
143
182
|
|
|
144
183
|
Object.assign(log, metadata);
|
|
145
184
|
|
|
146
|
-
return this.
|
|
147
|
-
.then((c) => c.insertOne(log))
|
|
148
|
-
.catch((storeError) => {
|
|
149
|
-
this._log.error('Failed to store chat log', storeError, log);
|
|
150
|
-
|
|
151
|
-
if (!this.muteErrors) {
|
|
152
|
-
throw storeError;
|
|
153
|
-
}
|
|
154
|
-
});
|
|
185
|
+
return this._storeLog(log);
|
|
155
186
|
}
|
|
156
187
|
|
|
157
188
|
}
|
|
@@ -381,7 +381,7 @@ class NotificationsStorage {
|
|
|
381
381
|
}
|
|
382
382
|
}, {
|
|
383
383
|
sort: { enqueue: 1 },
|
|
384
|
-
|
|
384
|
+
returnDocument: 'after'
|
|
385
385
|
});
|
|
386
386
|
if (found.value) {
|
|
387
387
|
pop.push(this._mapGenericObject(found.value));
|
|
@@ -475,7 +475,7 @@ class NotificationsStorage {
|
|
|
475
475
|
}, {
|
|
476
476
|
$set: data
|
|
477
477
|
}, {
|
|
478
|
-
|
|
478
|
+
returnDocument: 'after'
|
|
479
479
|
});
|
|
480
480
|
|
|
481
481
|
return this._mapGenericObject(res.value);
|
|
@@ -565,7 +565,7 @@ class NotificationsStorage {
|
|
|
565
565
|
[eventType]: ts
|
|
566
566
|
}
|
|
567
567
|
}, {
|
|
568
|
-
|
|
568
|
+
returnDocument: 'after'
|
|
569
569
|
}))
|
|
570
570
|
);
|
|
571
571
|
|
|
@@ -602,7 +602,7 @@ class NotificationsStorage {
|
|
|
602
602
|
id: campaign.id
|
|
603
603
|
}, update, {
|
|
604
604
|
upsert: true,
|
|
605
|
-
|
|
605
|
+
returnDocument: 'after'
|
|
606
606
|
});
|
|
607
607
|
ret = this._mapCampaign(res.value);
|
|
608
608
|
} else {
|
|
@@ -661,7 +661,7 @@ class NotificationsStorage {
|
|
|
661
661
|
}, {
|
|
662
662
|
$set: data
|
|
663
663
|
}, {
|
|
664
|
-
|
|
664
|
+
returnDocument: 'after'
|
|
665
665
|
});
|
|
666
666
|
|
|
667
667
|
return this._mapCampaign(res.value);
|
|
@@ -681,7 +681,7 @@ class NotificationsStorage {
|
|
|
681
681
|
}, {
|
|
682
682
|
$set: { startAt: null }
|
|
683
683
|
}, {
|
|
684
|
-
|
|
684
|
+
returnDocument: 'before'
|
|
685
685
|
});
|
|
686
686
|
|
|
687
687
|
return this._mapCampaign(res.value);
|
|
@@ -807,7 +807,7 @@ class NotificationsStorage {
|
|
|
807
807
|
}, {
|
|
808
808
|
$pull: { subs: tag }
|
|
809
809
|
}, {
|
|
810
|
-
|
|
810
|
+
returnDocument: 'after'
|
|
811
811
|
});
|
|
812
812
|
|
|
813
813
|
if (res.value) {
|
package/src/StateStorage.js
CHANGED
|
@@ -36,7 +36,7 @@ class StateStorage extends BaseStorage {
|
|
|
36
36
|
* @param {{error:Function,log:Function}} [log] - console like logger
|
|
37
37
|
* @param {boolean} isCosmo
|
|
38
38
|
*/
|
|
39
|
-
constructor (mongoDb, collectionName = '
|
|
39
|
+
constructor (mongoDb, collectionName = 'states', log = console, isCosmo = false) {
|
|
40
40
|
super(mongoDb, collectionName, log, isCosmo);
|
|
41
41
|
|
|
42
42
|
this.addIndex(
|
|
@@ -115,7 +115,7 @@ class StateStorage extends BaseStorage {
|
|
|
115
115
|
$set
|
|
116
116
|
}, {
|
|
117
117
|
upsert: true,
|
|
118
|
-
|
|
118
|
+
returnDocument: 'after',
|
|
119
119
|
projection: {
|
|
120
120
|
_id: 0
|
|
121
121
|
}
|
package/src/main.js
CHANGED
|
@@ -9,6 +9,7 @@ const BotTokenStorage = require('./BotTokenStorage');
|
|
|
9
9
|
const ChatLogStorage = require('./ChatLogStorage');
|
|
10
10
|
const BotConfigStorage = require('./BotConfigStorage');
|
|
11
11
|
const AttachmentCache = require('./AttachmentCache');
|
|
12
|
+
const AuditLogStorage = require('./AuditLogStorage');
|
|
12
13
|
const NotificationsStorage = require('./NotificationsStorage');
|
|
13
14
|
|
|
14
15
|
module.exports = {
|
|
@@ -18,5 +19,6 @@ module.exports = {
|
|
|
18
19
|
ChatLogStorage,
|
|
19
20
|
BotConfigStorage,
|
|
20
21
|
AttachmentCache,
|
|
21
|
-
NotificationsStorage
|
|
22
|
+
NotificationsStorage,
|
|
23
|
+
AuditLogStorage
|
|
22
24
|
};
|