@arcaelas/whatsapp 6.1.3 → 7.0.0
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/README.md +19 -15
- package/build/cjs/index.d.ts +9 -5
- package/build/cjs/index.js +8 -7
- package/build/cjs/lib/bot/decorator.d.ts +2 -9
- package/build/cjs/lib/bot/decorator.js +0 -1
- package/build/cjs/lib/bot/decorators.d.ts +48 -4
- package/build/cjs/lib/bot/decorators.js +2 -2
- package/build/cjs/lib/chat/index.d.ts +144 -152
- package/build/cjs/lib/chat/index.js +138 -209
- package/build/cjs/lib/contact/index.d.ts +114 -80
- package/build/cjs/lib/contact/index.js +187 -94
- package/build/cjs/lib/message/index.d.ts +400 -321
- package/build/cjs/lib/message/index.js +425 -913
- package/build/cjs/lib/status/index.d.ts +22 -33
- package/build/cjs/lib/status/index.js +52 -93
- package/build/cjs/lib/store/index.d.ts +16 -0
- package/build/cjs/lib/store/index.js +29 -3
- package/build/cjs/lib/whatsapp/index.d.ts +44 -212
- package/build/cjs/lib/whatsapp/index.js +473 -1067
- package/build/esm/index.d.ts +9 -5
- package/build/esm/index.js +6 -4
- package/build/esm/lib/bot/decorator.d.ts +2 -9
- package/build/esm/lib/bot/decorator.js +1 -1
- package/build/esm/lib/bot/decorators.d.ts +48 -4
- package/build/esm/lib/bot/decorators.js +2 -2
- package/build/esm/lib/chat/index.d.ts +144 -152
- package/build/esm/lib/chat/index.js +139 -209
- package/build/esm/lib/contact/index.d.ts +114 -80
- package/build/esm/lib/contact/index.js +186 -94
- package/build/esm/lib/message/index.d.ts +400 -321
- package/build/esm/lib/message/index.js +422 -913
- package/build/esm/lib/status/index.d.ts +22 -33
- package/build/esm/lib/status/index.js +49 -93
- package/build/esm/lib/store/index.d.ts +16 -0
- package/build/esm/lib/store/index.js +25 -0
- package/build/esm/lib/whatsapp/index.d.ts +44 -212
- package/build/esm/lib/whatsapp/index.js +476 -1069
- package/package.json +1 -1
- package/build/cjs/lib/internal.d.ts +0 -40
- package/build/cjs/lib/internal.js +0 -38
- package/build/esm/lib/internal.d.ts +0 -40
- package/build/esm/lib/internal.js +0 -34
|
@@ -3,19 +3,17 @@
|
|
|
3
3
|
* @description Entidad Chat — conversaciones individuales y grupales.
|
|
4
4
|
* Chat entity — individual and group conversations.
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
7
|
-
import { Message } from '../../lib/message/index.js';
|
|
8
|
-
import { deserialize, serialize } from '../../lib/store/index.js';
|
|
6
|
+
import { deserialize, jid_of, serialize } from '../../lib/store/index.js';
|
|
9
7
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
8
|
+
* Chat: recibe el raw y deriva todo con getters.
|
|
9
|
+
* Chat: receives the raw and derives everything via getters.
|
|
12
10
|
*/
|
|
13
|
-
export class Chat {
|
|
11
|
+
export default class Chat {
|
|
14
12
|
/**
|
|
15
|
-
* @internal Documento crudo
|
|
16
|
-
*
|
|
17
|
-
* Raw
|
|
18
|
-
* `mute_end_time` are epoch ms.
|
|
13
|
+
* @internal Documento crudo. `id` es el identificador del engine (JID, LID o `@g.us`);
|
|
14
|
+
* `pinned`, `mute_end_time` y `activity` son epoch ms.
|
|
15
|
+
* Raw document. `id` is the engine identifier (JID, LID or `@g.us`); `pinned`,
|
|
16
|
+
* `mute_end_time` and `activity` are epoch ms.
|
|
19
17
|
*/
|
|
20
18
|
constructor(_raw) {
|
|
21
19
|
this._raw = _raw;
|
|
@@ -50,71 +48,50 @@ export class Chat {
|
|
|
50
48
|
return end != null && end > Date.now() ? new Date(end).toISOString() : null;
|
|
51
49
|
}
|
|
52
50
|
}
|
|
53
|
-
/** Máximo de chats fijados que acepta WhatsApp; el cuarto se descarta en silencio. / Max pinned chats WhatsApp accepts; the fourth is silently dropped. */
|
|
54
|
-
const MAX_PINNED = 3;
|
|
55
51
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
52
|
+
* Chat ligado a la sesión viva: el socket y el engine llegan resueltos, así que sus métodos
|
|
53
|
+
* no vuelven a comprobarlos.
|
|
54
|
+
* Chat bound to the live session: socket and engine arrive resolved, so its methods do not
|
|
55
|
+
* check them again.
|
|
58
56
|
*
|
|
59
|
-
* @param
|
|
57
|
+
* @param init - Sesión activa: cliente, motor y socket / Active session: client, engine and socket
|
|
58
|
+
* @returns Clase `Chat` con acceso a la sesión / `Chat` class with session access
|
|
60
59
|
*/
|
|
61
|
-
export function chat(
|
|
60
|
+
export function chat(init) {
|
|
61
|
+
// WhatsApp acepta 3 chats fijados y descarta el cuarto sin avisar.
|
|
62
|
+
// WhatsApp accepts 3 pinned chats and silently drops the fourth.
|
|
63
|
+
const MAX_PINNED = 3;
|
|
62
64
|
/**
|
|
63
|
-
* Último mensaje del chat en
|
|
64
|
-
*
|
|
65
|
+
* Último mensaje del chat, en las dos formas que hacen falta: la marca de actividad que
|
|
66
|
+
* ordena la lista y el `lastMessages` que exige `chatModify`.
|
|
67
|
+
* The chat's last message, in the two shapes needed: the activity stamp that orders the
|
|
68
|
+
* list and the `lastMessages` shape `chatModify` requires.
|
|
65
69
|
*/
|
|
66
|
-
async
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
? [{ key: { remoteJid: cid, id:
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Cuenta los chats fijados distintos del indicado, cortando al llegar al máximo.
|
|
75
|
-
* Counts pinned chats other than the given one, stopping once the max is reached.
|
|
76
|
-
*/
|
|
77
|
-
async function count_pinned(exclude) {
|
|
78
|
-
let total = 0;
|
|
79
|
-
for (let offset = 0; total < MAX_PINNED; offset += 200) {
|
|
80
|
-
const page = await wa.engine.list('/chat', offset, 200);
|
|
81
|
-
if (page.length === 0) {
|
|
82
|
-
break;
|
|
83
|
-
}
|
|
84
|
-
for (const raw of page) {
|
|
85
|
-
const parsed = deserialize(raw);
|
|
86
|
-
if (parsed && parsed.id !== exclude && parsed.pinned != null) {
|
|
87
|
-
total++;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return total;
|
|
92
|
-
}
|
|
70
|
+
const tail = async (cid) => {
|
|
71
|
+
const doc = deserialize((await init.engine.list(`/chat/${cid}/message`, 0, 1))[0] ?? null);
|
|
72
|
+
return {
|
|
73
|
+
at: doc?.created_at ?? 0,
|
|
74
|
+
messages: doc ? [{ key: { remoteJid: cid, id: doc.id, fromMe: doc.me }, messageTimestamp: Math.floor(doc.created_at / 1_000) }] : [],
|
|
75
|
+
};
|
|
76
|
+
};
|
|
93
77
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
78
|
+
* Metadatos del grupo memoizados 15s: `members` y `content` los piden por cada chat de
|
|
79
|
+
* una página y sin caché sería un round-trip por fila.
|
|
80
|
+
* Group metadata memoized for 15s: `members` and `content` ask for them on every chat of
|
|
81
|
+
* a page, and without a cache that is one round-trip per row.
|
|
96
82
|
*/
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
if (!
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
setTimeout(() => groups_cache.delete(jid), 15_000);
|
|
83
|
+
const groups = new Map();
|
|
84
|
+
const meta = (jid) => {
|
|
85
|
+
if (!groups.has(jid)) {
|
|
86
|
+
groups.set(jid, init.socket.groupMetadata(jid).catch(() => ({ participants: [] })));
|
|
87
|
+
setTimeout(() => groups.delete(jid), 15_000);
|
|
103
88
|
}
|
|
104
|
-
return
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
* Contacto desde el engine, o ficha mínima local cuando no está persistido (sin red).
|
|
108
|
-
* Contact from the engine, or a minimal local card when not persisted (no network).
|
|
109
|
-
*/
|
|
110
|
-
async function load_contact(id) {
|
|
111
|
-
const cached = deserialize(await wa.engine.get(`/contact/${id}`));
|
|
112
|
-
return new wa.Contact(cached ?? { id });
|
|
113
|
-
}
|
|
114
|
-
return class _Chat extends Chat {
|
|
89
|
+
return groups.get(jid);
|
|
90
|
+
};
|
|
91
|
+
class _Chat extends Chat {
|
|
115
92
|
/**
|
|
116
|
-
* Participantes
|
|
117
|
-
*
|
|
93
|
+
* Participantes: los integrantes en grupos, el contacto y yo en 1:1.
|
|
94
|
+
* Participants: members for groups, the contact and myself for 1:1.
|
|
118
95
|
*
|
|
119
96
|
* @param offset - Desplazamiento / Offset
|
|
120
97
|
* @param limit - Tamaño de página / Page size
|
|
@@ -122,219 +99,172 @@ export function chat(wa) {
|
|
|
122
99
|
*/
|
|
123
100
|
async members(offset = 0, limit = 50) {
|
|
124
101
|
const ids = this.type === 'group'
|
|
125
|
-
? (await
|
|
126
|
-
: [this._raw.id,
|
|
127
|
-
return Promise.all(ids.slice(offset, offset + limit).map(
|
|
102
|
+
? (await meta(this._raw.id)).participants.map((participant) => participant.id)
|
|
103
|
+
: [this._raw.id, init.socket.user?.id].filter((id) => Boolean(id));
|
|
104
|
+
return Promise.all(ids.slice(offset, offset + limit).map(async (id) => new init.wa.Contact(deserialize(await init.engine.get(`/contact/${id}`)) ?? { id })));
|
|
128
105
|
}
|
|
129
106
|
/**
|
|
130
|
-
* Descripción
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
107
|
+
* Descripción: el asunto del grupo o, en un 1:1, la bio del contacto. Es asíncrono
|
|
108
|
+
* porque ninguna de las dos vive en el documento del chat.
|
|
109
|
+
* Description: the group's subject or, on a 1:1, the contact's bio. It is async because
|
|
110
|
+
* neither of them lives in the chat document.
|
|
134
111
|
*
|
|
135
|
-
* @returns Descripción, o cadena vacía
|
|
112
|
+
* @returns Descripción, o cadena vacía / Description, or an empty string
|
|
136
113
|
*/
|
|
137
114
|
async content() {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return deserialize(await wa.engine.get(`/contact/${this._raw.id}`))?.status ?? '';
|
|
115
|
+
return this.type === 'group'
|
|
116
|
+
? (await meta(this._raw.id)).desc ?? ''
|
|
117
|
+
: deserialize(await init.engine.get(`/contact/${this._raw.id}`))?.status ?? '';
|
|
142
118
|
}
|
|
143
119
|
/**
|
|
144
|
-
* Mensajes del chat
|
|
145
|
-
* Chat messages
|
|
120
|
+
* Mensajes del chat, del más reciente al más antiguo.
|
|
121
|
+
* Chat messages, from the most recent to the oldest.
|
|
146
122
|
*
|
|
147
123
|
* @param offset - Desplazamiento / Offset
|
|
148
124
|
* @param limit - Tamaño de página / Page size
|
|
149
125
|
* @returns Página de mensajes / Message page
|
|
150
126
|
*/
|
|
151
127
|
async messages(offset = 0, limit = 50) {
|
|
152
|
-
return Message.list(
|
|
128
|
+
return init.wa.Message.list(this._raw.id, offset, limit);
|
|
153
129
|
}
|
|
154
130
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
131
|
+
* Publica el indicador «escribiendo…».
|
|
132
|
+
* Publishes the "typing…" indicator.
|
|
157
133
|
*
|
|
158
|
-
* @param value - true
|
|
159
|
-
* @returns true si el socket estaba disponible / true when the socket was available
|
|
134
|
+
* @param value - true escribe, false vuelve a pausa / true types, false returns to paused
|
|
160
135
|
*/
|
|
161
136
|
async typing(value) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (socket) {
|
|
165
|
-
await socket.sendPresenceUpdate(value ? 'composing' : 'paused', this._raw.id);
|
|
166
|
-
ok = true;
|
|
167
|
-
}
|
|
168
|
-
return ok;
|
|
137
|
+
await init.socket.sendPresenceUpdate(value ? 'composing' : 'paused', this._raw.id);
|
|
138
|
+
return true;
|
|
169
139
|
}
|
|
170
140
|
/**
|
|
171
|
-
*
|
|
172
|
-
*
|
|
141
|
+
* Publica el indicador «grabando audio…».
|
|
142
|
+
* Publishes the "recording audio…" indicator.
|
|
173
143
|
*
|
|
174
|
-
* @param value - true
|
|
175
|
-
* @returns true si el socket estaba disponible / true when the socket was available
|
|
144
|
+
* @param value - true graba, false vuelve a pausa / true records, false returns to paused
|
|
176
145
|
*/
|
|
177
146
|
async recording(value) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (socket) {
|
|
181
|
-
await socket.sendPresenceUpdate(value ? 'recording' : 'paused', this._raw.id);
|
|
182
|
-
ok = true;
|
|
183
|
-
}
|
|
184
|
-
return ok;
|
|
147
|
+
await init.socket.sendPresenceUpdate(value ? 'recording' : 'paused', this._raw.id);
|
|
148
|
+
return true;
|
|
185
149
|
}
|
|
186
150
|
/**
|
|
187
|
-
* Archiva o desarchiva el chat en la cuenta
|
|
188
|
-
* Archives or unarchives the chat on the
|
|
151
|
+
* Archiva o desarchiva el chat en la cuenta.
|
|
152
|
+
* Archives or unarchives the chat on the account.
|
|
189
153
|
*
|
|
190
154
|
* @param value - true archiva, false desarchiva / true archives, false unarchives
|
|
191
|
-
* @returns true si la acción se envió / true when the action was sent
|
|
192
155
|
*/
|
|
193
156
|
async archive(value) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
return ok;
|
|
157
|
+
const last = await tail(this._raw.id);
|
|
158
|
+
await init.socket.chatModify({ archive: value, lastMessages: last.messages }, this._raw.id);
|
|
159
|
+
this._raw.archived = value;
|
|
160
|
+
// Archivar no es actividad: el chat conserva su posición en la lista.
|
|
161
|
+
// Archiving is not activity: the chat keeps its position in the list.
|
|
162
|
+
await init.engine.set(`/chat/${this._raw.id}`, serialize(this._raw), this._raw.activity ?? last.at);
|
|
163
|
+
return true;
|
|
203
164
|
}
|
|
204
165
|
/**
|
|
205
|
-
* Fija o desfija el chat
|
|
206
|
-
*
|
|
207
|
-
* Pins or unpins the chat
|
|
208
|
-
*
|
|
166
|
+
* Fija o desfija el chat. Al fijar comprueba el límite, porque WhatsApp descarta el
|
|
167
|
+
* cuarto chat fijado sin avisar.
|
|
168
|
+
* Pins or unpins the chat. When pinning it checks the limit, since WhatsApp silently
|
|
169
|
+
* drops the fourth pinned chat.
|
|
209
170
|
*
|
|
210
171
|
* @param value - true fija, false desfija / true pins, false unpins
|
|
211
|
-
* @returns false si
|
|
172
|
+
* @returns false si ya hay 3 chats fijados / false when 3 chats are already pinned
|
|
212
173
|
*/
|
|
213
174
|
async pin(value) {
|
|
214
|
-
let
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
await socket.chatModify({ pin: value }, this._raw.id);
|
|
220
|
-
this._raw.pinned = value ? Date.now() : null;
|
|
221
|
-
await wa.engine.set(`/chat/${this._raw.id}`, serialize(this._raw));
|
|
222
|
-
ok = true;
|
|
175
|
+
let pinned = 0;
|
|
176
|
+
for (let offset = 0; value && pinned < MAX_PINNED; offset += 200) {
|
|
177
|
+
const page = await init.engine.list('/chat', offset, 200);
|
|
178
|
+
if (page.length === 0) {
|
|
179
|
+
break;
|
|
223
180
|
}
|
|
181
|
+
pinned += page.filter((raw) => {
|
|
182
|
+
const doc = deserialize(raw);
|
|
183
|
+
return doc?.pinned != null && doc.id !== this._raw.id;
|
|
184
|
+
}).length;
|
|
185
|
+
}
|
|
186
|
+
if (pinned >= MAX_PINNED) {
|
|
187
|
+
return false;
|
|
224
188
|
}
|
|
225
|
-
|
|
189
|
+
await init.socket.chatModify({ pin: value }, this._raw.id);
|
|
190
|
+
this._raw.pinned = value ? Date.now() : null;
|
|
191
|
+
await init.engine.set(`/chat/${this._raw.id}`, serialize(this._raw), this._raw.activity ?? (await tail(this._raw.id)).at);
|
|
192
|
+
return true;
|
|
226
193
|
}
|
|
227
194
|
/**
|
|
228
|
-
* Silencia el chat hasta la fecha indicada
|
|
229
|
-
* Mutes the chat until the given date
|
|
195
|
+
* Silencia el chat hasta la fecha indicada; `false` o una fecha pasada lo reactivan.
|
|
196
|
+
* Mutes the chat until the given date; `false` or a past date unmutes it.
|
|
230
197
|
*
|
|
231
|
-
* @param until - Fecha límite (ISO, epoch ms o Date) o false / Deadline (ISO, epoch ms or Date) or false
|
|
232
|
-
* @returns true si la acción se envió / true when the action was sent
|
|
198
|
+
* @param until - Fecha límite (ISO, epoch ms o Date), o false / Deadline (ISO, epoch ms or Date), or false
|
|
233
199
|
*/
|
|
234
200
|
async mute(until) {
|
|
235
|
-
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
this._raw.mute_end_time = muted ? end : null;
|
|
242
|
-
await wa.engine.set(`/chat/${this._raw.id}`, serialize(this._raw));
|
|
243
|
-
ok = true;
|
|
244
|
-
}
|
|
245
|
-
return ok;
|
|
201
|
+
const end = until === false ? 0 : new Date(until).getTime();
|
|
202
|
+
const muted = end > Date.now();
|
|
203
|
+
await init.socket.chatModify({ mute: muted ? end : null }, this._raw.id);
|
|
204
|
+
this._raw.mute_end_time = muted ? end : null;
|
|
205
|
+
await init.engine.set(`/chat/${this._raw.id}`, serialize(this._raw), this._raw.activity ?? (await tail(this._raw.id)).at);
|
|
206
|
+
return true;
|
|
246
207
|
}
|
|
247
208
|
/**
|
|
248
|
-
* Marca el chat completo como leído en la cuenta
|
|
249
|
-
* Marks the whole chat as read on the
|
|
250
|
-
*
|
|
251
|
-
* @returns true si la acción se envió / true when the action was sent
|
|
209
|
+
* Marca el chat completo como leído en la cuenta.
|
|
210
|
+
* Marks the whole chat as read on the account.
|
|
252
211
|
*/
|
|
253
212
|
async seen() {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
await wa.engine.set(`/chat/${this._raw.id}`, serialize(this._raw));
|
|
260
|
-
ok = true;
|
|
261
|
-
}
|
|
262
|
-
return ok;
|
|
213
|
+
const last = await tail(this._raw.id);
|
|
214
|
+
await init.socket.chatModify({ markRead: true, lastMessages: last.messages }, this._raw.id);
|
|
215
|
+
this._raw.unread_count = 0;
|
|
216
|
+
await init.engine.set(`/chat/${this._raw.id}`, serialize(this._raw), this._raw.activity ?? last.at);
|
|
217
|
+
return true;
|
|
263
218
|
}
|
|
264
219
|
/**
|
|
265
|
-
* Vacía los mensajes del chat en la cuenta
|
|
266
|
-
* Clears the chat messages on the
|
|
267
|
-
*
|
|
268
|
-
* @returns true siempre; la limpieza local es idempotente / always true; local cleanup is idempotent
|
|
220
|
+
* Vacía los mensajes del chat en la cuenta y en el engine, conservando el chat.
|
|
221
|
+
* Clears the chat messages on the account and in the engine, keeping the chat.
|
|
269
222
|
*/
|
|
270
223
|
async clear() {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
await socket
|
|
274
|
-
.chatModify({ clear: true, lastMessages: await last_messages(this._raw.id) }, this._raw.id)
|
|
275
|
-
.catch(() => null);
|
|
276
|
-
}
|
|
277
|
-
await wa.engine.unset(`/chat/${this._raw.id}/message`);
|
|
224
|
+
await init.socket.chatModify({ clear: true, lastMessages: (await tail(this._raw.id)).messages }, this._raw.id).catch(() => null);
|
|
225
|
+
await init.engine.unset(`/chat/${this._raw.id}/message`);
|
|
278
226
|
return true;
|
|
279
227
|
}
|
|
280
228
|
/**
|
|
281
|
-
* Elimina el chat y sus mensajes en la cuenta
|
|
282
|
-
*
|
|
283
|
-
* Deletes the chat and its messages on the
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
* @returns true siempre; la limpieza local es idempotente / always true; local cleanup is idempotent
|
|
229
|
+
* Elimina el chat y sus mensajes en la cuenta y en el engine; en grupos, además sale
|
|
230
|
+
* del grupo.
|
|
231
|
+
* Deletes the chat and its messages on the account and in the engine; for groups it
|
|
232
|
+
* also leaves the group.
|
|
287
233
|
*/
|
|
288
234
|
async delete() {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
else {
|
|
295
|
-
await socket
|
|
296
|
-
.chatModify({ delete: true, lastMessages: await last_messages(this._raw.id) }, this._raw.id)
|
|
297
|
-
.catch(() => null);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
await wa.engine.unset(`/chat/${this._raw.id}`);
|
|
235
|
+
await (this.type === 'group'
|
|
236
|
+
? init.socket.groupLeave(this._raw.id)
|
|
237
|
+
: init.socket.chatModify({ delete: true, lastMessages: (await tail(this._raw.id)).messages }, this._raw.id)).catch(() => null);
|
|
238
|
+
await init.engine.unset(`/chat/${this._raw.id}`);
|
|
301
239
|
return true;
|
|
302
240
|
}
|
|
303
241
|
/**
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
*
|
|
242
|
+
* Chat por teléfono, JID, LID o id de grupo: el persistido o, si no existe todavía,
|
|
243
|
+
* una instancia mínima lista para usar que no se persiste.
|
|
244
|
+
* Chat by phone, JID, LID or group id: the persisted one or, when it does not exist
|
|
245
|
+
* yet, a minimal ready-to-use instance that is not persisted.
|
|
308
246
|
*
|
|
309
247
|
* @param cid - Teléfono, JID, LID o id de grupo / Phone, JID, LID or group id
|
|
310
|
-
* @returns Chat o null si el identificador es irresoluble / Chat or null when the identifier cannot be resolved
|
|
248
|
+
* @returns Chat, o null si el identificador es irresoluble / Chat, or null when the identifier cannot be resolved
|
|
311
249
|
*/
|
|
312
250
|
static async get(cid) {
|
|
313
|
-
const
|
|
314
|
-
|
|
315
|
-
? await internals(wa).resolve_jid(String(cid))
|
|
316
|
-
: digits
|
|
317
|
-
? `${digits}@s.whatsapp.net`
|
|
318
|
-
: null;
|
|
319
|
-
if (jid) {
|
|
320
|
-
const cached = deserialize(await wa.engine.get(`/chat/${jid}`));
|
|
321
|
-
return new _Chat(cached ?? { id: jid });
|
|
322
|
-
}
|
|
323
|
-
return null;
|
|
251
|
+
const id = await jid_of(init.engine, String(cid), init.socket);
|
|
252
|
+
return id ? new this(deserialize(await init.engine.get(`/chat/${id}`)) ?? { id }) : null;
|
|
324
253
|
}
|
|
325
254
|
/**
|
|
326
|
-
* Pagina los chats persistidos, del más
|
|
327
|
-
* Paginates persisted chats, from the most
|
|
255
|
+
* Pagina los chats persistidos, del más activo al más antiguo.
|
|
256
|
+
* Paginates persisted chats, from the most active to the oldest.
|
|
328
257
|
*
|
|
329
258
|
* @param offset - Desplazamiento / Offset
|
|
330
259
|
* @param limit - Tamaño de página / Page size
|
|
331
260
|
* @returns Página de chats / Chat page
|
|
332
261
|
*/
|
|
333
262
|
static async list(offset = 0, limit = 50) {
|
|
334
|
-
return (await
|
|
263
|
+
return (await init.engine.list('/chat', offset, limit))
|
|
335
264
|
.map((raw) => deserialize(raw))
|
|
336
|
-
.filter((
|
|
337
|
-
.map((
|
|
265
|
+
.filter((doc) => doc !== null)
|
|
266
|
+
.map((doc) => new this(doc));
|
|
338
267
|
}
|
|
339
|
-
}
|
|
268
|
+
}
|
|
269
|
+
return _Chat;
|
|
340
270
|
}
|