-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
get-data-messages.js
196 lines (182 loc) · 5.41 KB
/
get-data-messages.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Return messages by dates!
* @param {string} id contact number id
* @param {string} type
types:
lowerThan: Return all messages before the date informed;
higherThan: Return all messages after the date informed;
equal: Return all messages from the informed date;
full: Return all messages, with two new stringdate parameters, dateNumeric;
* @param {string} dateStart Pass the example date 00/00/0000 or 00-00-0000
* @param {string} time Pass the example time 00:00 24 hours
*/
export async function getAllMessagesDate(
id,
type = 'full',
dateStart = undefined,
time = undefined,
limit = 10,
output = [],
idCheck = [],
stop = true
) {
const types = ['higherThan', 'equal', 'lowerThan', 'full'];
if (!types.includes(type)) {
return WAPI.scope(
undefined,
true,
null,
`wrong type! use the types: ${types.join()}`
);
}
if (!!time && dateStart === undefined) {
return WAPI.scope(
undefined,
true,
null,
`it is necessary to inform the date field`
);
}
const chat = await WAPI.sendExist(id);
if (chat && chat.status != 404) {
const statusMsg = chat.msgs.msgLoadState.noEarlierMsgs;
if (statusMsg === false) {
await chat.onEmptyMRM();
}
let messages = chat.msgs._models;
let dateStartTimeStamp, msg;
if (time !== undefined && dateStart !== undefined) {
const splitTimeStart =
typeof time === 'string' ? time.split(/[:]/) : undefined;
const splitDateStart =
typeof dateStart === 'string' ? dateStart.split(/[-,/]/) : undefined;
dateStartTimeStamp = timeStampConvert(splitDateStart, splitTimeStart)
? timeStampConvert(splitDateStart, splitTimeStart)
: false;
if (dateStartTimeStamp === false || isNaN(dateStartTimeStamp)) {
const date = new Date();
const year = date.toLocaleString('en-US', { year: 'numeric' });
return WAPI.scope(
undefined,
true,
null,
`Date and time with invalid format! use as an example: data: 01/01/${year} or 01-01-${year} Tima 01:01`
);
}
} else {
if (dateStart !== undefined) {
const splitDateStart =
typeof dateStart === 'string' ? dateStart.split(/[-,/]/) : undefined;
dateStartTimeStamp = timeStampConvert(splitDateStart)
? timeStampConvert(splitDateStart)
: false;
if (dateStartTimeStamp === false || isNaN(dateStartTimeStamp)) {
const date = new Date();
const year = date.toLocaleString('en-US', { year: 'numeric' });
return WAPI.scope(
undefined,
true,
null,
`Date with invalid format! use as an example: 01/01/${year} or 01-01-${year}`
);
}
}
}
messages = messages.reverse();
for (const i in messages) {
if (i === 'remove') {
continue;
}
if (output.length < limit || limit === 0) {
const messageObj = messages[i];
const message = await WAPI._serializeMessageObj(messageObj);
if (message.id && idCheck.includes(message.id) === true) {
continue;
}
if (type === 'higherThan') {
if (
parseInt(dateStartTimeStamp.getTime() / 1000) <= message.timestamp
) {
msg = getMenssage(message);
}
}
if (type === 'equal') {
if (
parseInt(dateStartTimeStamp.getTime() / 1000) === message.timestamp
) {
msg = getMenssage(message);
}
}
if (type === 'lowerThan') {
if (
parseInt(dateStartTimeStamp.getTime() / 1000) >= message.timestamp
) {
msg = getMenssage(message);
}
}
if (type === 'full') {
msg = getMenssage(message);
}
if (msg && idCheck.includes(msg.id) === false) {
stop = false;
idCheck.push(msg.id);
output.push(msg);
}
}
}
if (statusMsg === false && stop === false && output.length < limit) {
return await getAllMessagesDate(
id,
type,
dateStart,
time,
limit,
output,
idCheck,
true
);
} else {
return output;
}
} else {
return chat;
}
}
function timeStampConvert(date, time) {
var newdate = undefined;
if (date !== undefined) {
if (time !== undefined) {
newdate = new Date(date[2], date[1] - 1, date[0], time[0], time[1]);
} else {
newdate = new Date(date[2], date[1] - 1, date[0]);
}
return newdate;
} else {
return false;
}
}
function getMenssage(message) {
const date = new Date(message.timestamp * 1000);
const stringdate = date.toLocaleString();
const day = '0' + date.toLocaleString('en-US', { day: 'numeric' });
const month = '0' + date.toLocaleString('en-US', { month: 'numeric' });
const minutes = '0' + date.getUTCMinutes();
const seconds = '0' + date.getSeconds();
const hours = '0' + date.getHours();
const _d = {
id: message.id,
timestamp: date.getTime(),
stringdate,
dateNumeric: {
day: day.substr(-2),
month: month.substr(-2),
year: date.toLocaleString('en-US', { year: 'numeric' }),
hours: hours.substr(-2),
minutes: minutes.substr(-2),
seconds: seconds.substr(-2)
},
type: message.type,
fromMe: message.fromMe
};
return Object.assign(message, _d);
}