Commit 336ebef2f95f81b6aaae5c578f278e3e19840786
1 parent
6f6178078c
Exists in
master
Message archive
Showing 5 changed files with 124 additions and 17 deletions Side-by-side Diff
lib/db-mysql.js
... | ... | @@ -0,0 +1,37 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const mysql = require('mysql'); | |
4 | + | |
5 | +const config = require('komodo-sdk/config'); | |
6 | + | |
7 | +const connectionLimit = config.mysql && config.mysql.pool_connection_limit ? config.mysql.pool_connection_limit : 0; | |
8 | + | |
9 | +const pool = config.mysql ? mysql.createPool({ | |
10 | + connectionLimit: connectionLimit, | |
11 | + host: config.mysql.host || 'localhost', | |
12 | + database: config.mysql.database || 'komodo', | |
13 | + user: config.mysql.user || 'komodo', | |
14 | + password: config.mysql.password | |
15 | +}) : null; | |
16 | + | |
17 | +exports.pool = pool; | |
18 | +exports.query = pool.query; | |
19 | + | |
20 | +exports.format = (sql, values) => { | |
21 | + return new Promise((resolve, reject) => { | |
22 | + if (!pool) { | |
23 | + reject('Missing DB config'); | |
24 | + return; | |
25 | + } | |
26 | + | |
27 | + pool.getConnection((err, connection) => { | |
28 | + if (err) { | |
29 | + reject(err); | |
30 | + return; | |
31 | + } | |
32 | + | |
33 | + resolve(connection.format(sql, values)); | |
34 | + connection.release(); | |
35 | + }); | |
36 | + }) | |
37 | +} | |
0 | 38 | \ No newline at end of file |
lib/http-listener.js
... | ... | @@ -8,6 +8,7 @@ const config = require('komodo-sdk/config'); |
8 | 8 | const logger = require('komodo-sdk/logger'); |
9 | 9 | |
10 | 10 | const commandHandler = require('./command-handler'); |
11 | +const messagesArchive = require('./messages-archive'); | |
11 | 12 | |
12 | 13 | const app = express(); |
13 | 14 | const port = config && config.listener && config.listener.http && config.listener.http.port ? config.listener.http.port : 32979; |
... | ... | @@ -16,24 +17,34 @@ function mainHandler(req, res) { |
16 | 17 | |
17 | 18 | if (!req.body) req.body = {}; |
18 | 19 | |
19 | - if (req.body.do_not_forward_to_core || req.query.do_not_forward_to_core) { | |
20 | - logger.verbose('Ignoring message', { from: req.body.from || req.query.from, msg: req.body.msg || req.query.msg, do_not_forward_to_core: req.body.do_not_forward_to_core || req.query.do_not_forward_to_core }); | |
21 | - res.end('OK'); | |
20 | + if (!req.query.from && !req.body.from) { | |
21 | + res.end('ERROR. Undefined parameter: from'); | |
22 | 22 | return; |
23 | 23 | } |
24 | 24 | |
25 | - if (!req.query.report_port && !req.body.report_port) { | |
26 | - res.end('ERROR. Undefined parameter: report_port'); | |
25 | + if (!req.query.msg && !req.body.msg) { | |
26 | + res.end('ERROR. Undefined parameter: msg'); | |
27 | 27 | return; |
28 | 28 | } |
29 | 29 | |
30 | - if (!req.query.from && !req.body.from) { | |
31 | - res.end('ERROR. Undefined parameter: from'); | |
30 | + messagesArchive.insert( | |
31 | + { | |
32 | + origin_label: req.body.origin_label || req.query.origin_label || req.body.origin || req.query.origin, | |
33 | + origin_transport: req.body.origin_transport || req.query.origin_transport, | |
34 | + partner: req.body.from_raw || req.query.from_raw || req.body.from || req.query.from, | |
35 | + msg: req.body.msg || req.query.msg, | |
36 | + }, | |
37 | + messagesArchive.DIRECTION_INCOMING | |
38 | + ); | |
39 | + | |
40 | + if (req.body.do_not_forward_to_core || req.query.do_not_forward_to_core) { | |
41 | + logger.verbose('Ignoring message', { from: req.body.from || req.query.from, msg: req.body.msg || req.query.msg, do_not_forward_to_core: req.body.do_not_forward_to_core || req.query.do_not_forward_to_core }); | |
42 | + res.end('OK'); | |
32 | 43 | return; |
33 | 44 | } |
34 | 45 | |
35 | - if (!req.query.msg && !req.body.msg) { | |
36 | - res.end('ERROR. Undefined parameter: msg'); | |
46 | + if (!req.query.report_port && !req.body.report_port) { | |
47 | + res.end('ERROR. Undefined parameter: report_port'); | |
37 | 48 | return; |
38 | 49 | } |
39 | 50 |
lib/messages-archive.js
... | ... | @@ -0,0 +1,42 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const logger = require('komodo-sdk/logger'); | |
4 | +const db = require('./db-mysql'); | |
5 | + | |
6 | +const DIRECTION_INCOMING = 0; | |
7 | +const DIRECTION_OUTGOING = 1; | |
8 | + | |
9 | +/** | |
10 | + * Menyimpan pesan ke dalam archive histori pesan di database | |
11 | + * | |
12 | + * @param {object} params - objek pesan yang akan disimpan | |
13 | + * @param {string} [params.origin_label] - label origin | |
14 | + * @param {string} [params.origin=UNKNOWN] - digunakan sebagai label origin jika tdk ditentukan | |
15 | + * @param {string} [params.origin_transport=UNKNOWN] - transport, misal SMS, TELEGRAM | |
16 | + * @param {string} params.partner - pengirim / penerima | |
17 | + * @param {string} [params.msg] - isi pesan | |
18 | + * @param {string} [params.message] - isi pesan, jika params.msg tidak terdefinisi | |
19 | + * @param {number} direction - 0: incoming, 1: outgoing | |
20 | + */ | |
21 | +function insert(params, direction) { | |
22 | + if (!db.pool) return; | |
23 | + | |
24 | + const query = `INSERT INTO messages SET created = NOW(), ?`; | |
25 | + const values = [{ | |
26 | + origin_label: (params.origin_label || params.origin || 'UNKNOWN').trim(), | |
27 | + origin_transport: (params.origin_transport || 'UNKNOWN').trim(), | |
28 | + direction, | |
29 | + partner: params.partner.trim(), | |
30 | + message: (params.msg || params.message).trim(), | |
31 | + }]; | |
32 | + | |
33 | + db.query(query, values, (err) => { | |
34 | + if (err) { | |
35 | + logger.warn(`MESSAGES-ARCHIVE: DB ERROR on inserting message. ${err.toString()}`); | |
36 | + } | |
37 | + }); | |
38 | +} | |
39 | + | |
40 | +exports.insert = insert; | |
41 | +exports.DIRECTION_INCOMING = DIRECTION_INCOMING; | |
42 | +exports.DIRECTION_OUTGOING = DIRECTION_OUTGOING; | |
0 | 43 | \ No newline at end of file |
package-lock.json
... | ... | @@ -287,6 +287,11 @@ |
287 | 287 | "tweetnacl": "^0.14.3" |
288 | 288 | } |
289 | 289 | }, |
290 | + "bignumber.js": { | |
291 | + "version": "7.2.1", | |
292 | + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-7.2.1.tgz", | |
293 | + "integrity": "sha512-S4XzBk5sMB+Rcb/LNcpzXr57VRTxgAvaAEDAl1AwRx27j00hT84O6OkteE7u8UB3NuaaygCRrEpqox4uDOrbdQ==" | |
294 | + }, | |
290 | 295 | "binary-extensions": { |
291 | 296 | "version": "1.13.1", |
292 | 297 | "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-1.13.1.tgz", |
... | ... | @@ -2273,8 +2278,7 @@ |
2273 | 2278 | "isarray": { |
2274 | 2279 | "version": "1.0.0", |
2275 | 2280 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", |
2276 | - "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", | |
2277 | - "optional": true | |
2281 | + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" | |
2278 | 2282 | }, |
2279 | 2283 | "isexe": { |
2280 | 2284 | "version": "2.0.0", |
... | ... | @@ -2593,6 +2597,17 @@ |
2593 | 2597 | "integrity": "sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=", |
2594 | 2598 | "dev": true |
2595 | 2599 | }, |
2600 | + "mysql": { | |
2601 | + "version": "2.17.1", | |
2602 | + "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.17.1.tgz", | |
2603 | + "integrity": "sha512-7vMqHQ673SAk5C8fOzTG2LpPcf3bNt0oL3sFpxPEEFp1mdlDcrLK0On7z8ZYKaaHrHwNcQ/MTUz7/oobZ2OyyA==", | |
2604 | + "requires": { | |
2605 | + "bignumber.js": "7.2.1", | |
2606 | + "readable-stream": "2.3.6", | |
2607 | + "safe-buffer": "5.1.2", | |
2608 | + "sqlstring": "2.3.1" | |
2609 | + } | |
2610 | + }, | |
2596 | 2611 | "nan": { |
2597 | 2612 | "version": "2.14.0", |
2598 | 2613 | "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz", |
... | ... | @@ -3013,8 +3028,7 @@ |
3013 | 3028 | "process-nextick-args": { |
3014 | 3029 | "version": "2.0.1", |
3015 | 3030 | "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", |
3016 | - "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", | |
3017 | - "optional": true | |
3031 | + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" | |
3018 | 3032 | }, |
3019 | 3033 | "progress": { |
3020 | 3034 | "version": "2.0.3", |
... | ... | @@ -3101,7 +3115,6 @@ |
3101 | 3115 | "version": "2.3.6", |
3102 | 3116 | "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", |
3103 | 3117 | "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", |
3104 | - "optional": true, | |
3105 | 3118 | "requires": { |
3106 | 3119 | "core-util-is": "~1.0.0", |
3107 | 3120 | "inherits": "~2.0.3", |
... | ... | @@ -3696,6 +3709,11 @@ |
3696 | 3709 | "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", |
3697 | 3710 | "dev": true |
3698 | 3711 | }, |
3712 | + "sqlstring": { | |
3713 | + "version": "2.3.1", | |
3714 | + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz", | |
3715 | + "integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A=" | |
3716 | + }, | |
3699 | 3717 | "sshpk": { |
3700 | 3718 | "version": "1.16.1", |
3701 | 3719 | "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz", |
... | ... | @@ -3767,7 +3785,6 @@ |
3767 | 3785 | "version": "1.1.1", |
3768 | 3786 | "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", |
3769 | 3787 | "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", |
3770 | - "optional": true, | |
3771 | 3788 | "requires": { |
3772 | 3789 | "safe-buffer": "~5.1.0" |
3773 | 3790 | } |
... | ... | @@ -4075,8 +4092,7 @@ |
4075 | 4092 | "util-deprecate": { |
4076 | 4093 | "version": "1.0.2", |
4077 | 4094 | "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", |
4078 | - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", | |
4079 | - "optional": true | |
4095 | + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" | |
4080 | 4096 | }, |
4081 | 4097 | "utils-merge": { |
4082 | 4098 | "version": "1.0.1", |
package.json