-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.chats.js
94 lines (81 loc) · 2.62 KB
/
data.chats.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
86
87
88
89
90
91
92
93
94
const _set = require('lodash.set');
const _get = require('lodash.get');
const clone = require('clone');
const chats = require('./data.storage.file').get('chats');
function get(bot_token, chat_id, fieldName) {
if (!chats.data[bot_token]) chats.data[bot_token] = {}
if (!chats.data[bot_token][chat_id]) {
chats.data[bot_token][chat_id] = {context:{}, history:[]};
chats.changed = true;
}
if (!fieldName) {
ret = clone( chats.data[bot_token][chat_id]);
} else {
ret = clone( _get( chats.data[bot_token][chat_id], fieldName));
}
return ret;
}
function getAll(bot_token) {
if (!chats.data[bot_token]) chats.data[bot_token] = {}
return clone( chats.data[bot_token]);
}
function set(bot_token, chat_id, fieldName, fieldValue) {
if (!fieldName) return;
if (!chats.data[bot_token]) chats.data[bot_token] = {}
if (!chats.data[bot_token][chat_id]) {
chats.data[bot_token][chat_id] = {context:{}, history:[]};
chats.changed = true;
}
_set(chats.data[bot_token][chat_id], fieldName, fieldValue);
chats.changed = true;
}
function addHistory(bot_token, chat_id, value) {
chats.data[bot_token][chat_id].history.push(value);
chats.changed = true;
}
/*
function findId(fieldName, fieldValue) {
const idList = Object.keys(chats.data);
for (let i=0; i<idList.length; i++) {
const chat_id = idList[i];
console.log( 'fieldName', fieldName, 'chat_id', chat_id, 'value', _get(chats.data[chat_id], fieldName), 'fieldValue', fieldValue);
if (_get(chats.data[chat_id], fieldName) == fieldValue) {
return chat_id;
}
}
}
*/
function findChatIdWithFieldThatContains(bot_token, fieldName, fieldValue){
const idList = Object.keys(chats.data[bot_token]);
for (let i=0; i<idList.length; i++) {
const chat_id = idList[i];
const value = _get(chats.data[bot_token][chat_id], fieldName);
if (typeof value === 'string' && value.indexOf(fieldValue) !== -1) {
return chat_id;
}
}
}
function findHistoryItemWithFieldThatContains(bot_token, chat_id, fieldName, fieldValue, cb) {
const chat = chats.data[bot_token][chat_id] || {history:[]};
let found;
for( let i=chat.history.length-1; i>=0 && !found; i--) {
let itm = chat.history[i];
//console.log(i, 'fieldName', fieldName, '_get(itm, fieldName)', _get(itm, fieldName), 'value', fieldValue);
if (_get(itm, fieldName) === fieldValue) {
found = i;
cb(itm);
//console.log('findHistoryIndexWithFieldThatContains itm', itm);
chats.changed = true;
}
}
// return found;
}
module.exports = {
get,
getAll,
set,
// findId,
findChatIdWithFieldThatContains,
findHistoryItemWithFieldThatContains,
addHistory,
}