@arcaelas/whatsapp 6.2.0 → 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 +132 -152
- package/build/cjs/lib/chat/index.js +138 -219
- 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 -1099
- 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 +132 -152
- package/build/esm/lib/chat/index.js +139 -219
- 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 -1101
- 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,81 +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
|
*/
|
|
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
|
+
};
|
|
66
77
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*/
|
|
72
|
-
async function activity_of(cid) {
|
|
73
|
-
const [raw] = await wa.engine.list(`/chat/${cid}/message`, 0, 1);
|
|
74
|
-
return deserialize(raw ?? null)?.created_at ?? 0;
|
|
75
|
-
}
|
|
76
|
-
async function last_messages(cid) {
|
|
77
|
-
const [raw] = await wa.engine.list(`/chat/${cid}/message`, 0, 1);
|
|
78
|
-
const parsed = deserialize(raw ?? null);
|
|
79
|
-
return parsed
|
|
80
|
-
? [{ key: { remoteJid: cid, id: parsed.id, fromMe: parsed.me }, messageTimestamp: Math.floor(parsed.created_at / 1000) }]
|
|
81
|
-
: [];
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Cuenta los chats fijados distintos del indicado, cortando al llegar al máximo.
|
|
85
|
-
* Counts pinned chats other than the given one, stopping once the max is reached.
|
|
86
|
-
*/
|
|
87
|
-
async function count_pinned(exclude) {
|
|
88
|
-
let total = 0;
|
|
89
|
-
for (let offset = 0; total < MAX_PINNED; offset += 200) {
|
|
90
|
-
const page = await wa.engine.list('/chat', offset, 200);
|
|
91
|
-
if (page.length === 0) {
|
|
92
|
-
break;
|
|
93
|
-
}
|
|
94
|
-
for (const raw of page) {
|
|
95
|
-
const parsed = deserialize(raw);
|
|
96
|
-
if (parsed && parsed.id !== exclude && parsed.pinned != null) {
|
|
97
|
-
total++;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return total;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Participantes del grupo memoizados 15s, para no repetir `groupMetadata` en cada página.
|
|
105
|
-
* Group participants memoized for 15s, avoiding a `groupMetadata` round-trip per page.
|
|
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.
|
|
106
82
|
*/
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
if (!
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
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);
|
|
113
88
|
}
|
|
114
|
-
return
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
* Contacto desde el engine, o ficha mínima local cuando no está persistido (sin red).
|
|
118
|
-
* Contact from the engine, or a minimal local card when not persisted (no network).
|
|
119
|
-
*/
|
|
120
|
-
async function load_contact(id) {
|
|
121
|
-
const cached = deserialize(await wa.engine.get(`/contact/${id}`));
|
|
122
|
-
return new wa.Contact(cached ?? { id });
|
|
123
|
-
}
|
|
124
|
-
return class _Chat extends Chat {
|
|
89
|
+
return groups.get(jid);
|
|
90
|
+
};
|
|
91
|
+
class _Chat extends Chat {
|
|
125
92
|
/**
|
|
126
|
-
* Participantes
|
|
127
|
-
*
|
|
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.
|
|
128
95
|
*
|
|
129
96
|
* @param offset - Desplazamiento / Offset
|
|
130
97
|
* @param limit - Tamaño de página / Page size
|
|
@@ -132,219 +99,172 @@ export function chat(wa) {
|
|
|
132
99
|
*/
|
|
133
100
|
async members(offset = 0, limit = 50) {
|
|
134
101
|
const ids = this.type === 'group'
|
|
135
|
-
? (await
|
|
136
|
-
: [this._raw.id,
|
|
137
|
-
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 })));
|
|
138
105
|
}
|
|
139
106
|
/**
|
|
140
|
-
* Descripción
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
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.
|
|
144
111
|
*
|
|
145
|
-
* @returns Descripción, o cadena vacía
|
|
112
|
+
* @returns Descripción, o cadena vacía / Description, or an empty string
|
|
146
113
|
*/
|
|
147
114
|
async content() {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
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 ?? '';
|
|
152
118
|
}
|
|
153
119
|
/**
|
|
154
|
-
* Mensajes del chat
|
|
155
|
-
* Chat messages
|
|
120
|
+
* Mensajes del chat, del más reciente al más antiguo.
|
|
121
|
+
* Chat messages, from the most recent to the oldest.
|
|
156
122
|
*
|
|
157
123
|
* @param offset - Desplazamiento / Offset
|
|
158
124
|
* @param limit - Tamaño de página / Page size
|
|
159
125
|
* @returns Página de mensajes / Message page
|
|
160
126
|
*/
|
|
161
127
|
async messages(offset = 0, limit = 50) {
|
|
162
|
-
return Message.list(
|
|
128
|
+
return init.wa.Message.list(this._raw.id, offset, limit);
|
|
163
129
|
}
|
|
164
130
|
/**
|
|
165
|
-
*
|
|
166
|
-
*
|
|
131
|
+
* Publica el indicador «escribiendo…».
|
|
132
|
+
* Publishes the "typing…" indicator.
|
|
167
133
|
*
|
|
168
|
-
* @param value - true
|
|
169
|
-
* @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
|
|
170
135
|
*/
|
|
171
136
|
async typing(value) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (socket) {
|
|
175
|
-
await socket.sendPresenceUpdate(value ? 'composing' : 'paused', this._raw.id);
|
|
176
|
-
ok = true;
|
|
177
|
-
}
|
|
178
|
-
return ok;
|
|
137
|
+
await init.socket.sendPresenceUpdate(value ? 'composing' : 'paused', this._raw.id);
|
|
138
|
+
return true;
|
|
179
139
|
}
|
|
180
140
|
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
141
|
+
* Publica el indicador «grabando audio…».
|
|
142
|
+
* Publishes the "recording audio…" indicator.
|
|
183
143
|
*
|
|
184
|
-
* @param value - true
|
|
185
|
-
* @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
|
|
186
145
|
*/
|
|
187
146
|
async recording(value) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (socket) {
|
|
191
|
-
await socket.sendPresenceUpdate(value ? 'recording' : 'paused', this._raw.id);
|
|
192
|
-
ok = true;
|
|
193
|
-
}
|
|
194
|
-
return ok;
|
|
147
|
+
await init.socket.sendPresenceUpdate(value ? 'recording' : 'paused', this._raw.id);
|
|
148
|
+
return true;
|
|
195
149
|
}
|
|
196
150
|
/**
|
|
197
|
-
* Archiva o desarchiva el chat en la cuenta
|
|
198
|
-
* 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.
|
|
199
153
|
*
|
|
200
154
|
* @param value - true archiva, false desarchiva / true archives, false unarchives
|
|
201
|
-
* @returns true si la acción se envió / true when the action was sent
|
|
202
155
|
*/
|
|
203
156
|
async archive(value) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
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;
|
|
213
164
|
}
|
|
214
165
|
/**
|
|
215
|
-
* Fija o desfija el chat
|
|
216
|
-
*
|
|
217
|
-
* Pins or unpins the chat
|
|
218
|
-
*
|
|
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.
|
|
219
170
|
*
|
|
220
171
|
* @param value - true fija, false desfija / true pins, false unpins
|
|
221
|
-
* @returns false si
|
|
172
|
+
* @returns false si ya hay 3 chats fijados / false when 3 chats are already pinned
|
|
222
173
|
*/
|
|
223
174
|
async pin(value) {
|
|
224
|
-
let
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
await socket.chatModify({ pin: value }, this._raw.id);
|
|
230
|
-
this._raw.pinned = value ? Date.now() : null;
|
|
231
|
-
await wa.engine.set(`/chat/${this._raw.id}`, serialize(this._raw), this._raw.activity ?? (await activity_of(this._raw.id)));
|
|
232
|
-
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;
|
|
233
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;
|
|
234
188
|
}
|
|
235
|
-
|
|
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;
|
|
236
193
|
}
|
|
237
194
|
/**
|
|
238
|
-
* Silencia el chat hasta la fecha indicada
|
|
239
|
-
* 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.
|
|
240
197
|
*
|
|
241
|
-
* @param until - Fecha límite (ISO, epoch ms o Date) o false / Deadline (ISO, epoch ms or Date) or false
|
|
242
|
-
* @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
|
|
243
199
|
*/
|
|
244
200
|
async mute(until) {
|
|
245
|
-
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
this._raw.mute_end_time = muted ? end : null;
|
|
252
|
-
await wa.engine.set(`/chat/${this._raw.id}`, serialize(this._raw), this._raw.activity ?? (await activity_of(this._raw.id)));
|
|
253
|
-
ok = true;
|
|
254
|
-
}
|
|
255
|
-
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;
|
|
256
207
|
}
|
|
257
208
|
/**
|
|
258
|
-
* Marca el chat completo como leído en la cuenta
|
|
259
|
-
* Marks the whole chat as read on the
|
|
260
|
-
*
|
|
261
|
-
* @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.
|
|
262
211
|
*/
|
|
263
212
|
async seen() {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
await wa.engine.set(`/chat/${this._raw.id}`, serialize(this._raw), this._raw.activity ?? (await activity_of(this._raw.id)));
|
|
270
|
-
ok = true;
|
|
271
|
-
}
|
|
272
|
-
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;
|
|
273
218
|
}
|
|
274
219
|
/**
|
|
275
|
-
* Vacía los mensajes del chat en la cuenta
|
|
276
|
-
* Clears the chat messages on the
|
|
277
|
-
*
|
|
278
|
-
* @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.
|
|
279
222
|
*/
|
|
280
223
|
async clear() {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
await socket
|
|
284
|
-
.chatModify({ clear: true, lastMessages: await last_messages(this._raw.id) }, this._raw.id)
|
|
285
|
-
.catch(() => null);
|
|
286
|
-
}
|
|
287
|
-
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`);
|
|
288
226
|
return true;
|
|
289
227
|
}
|
|
290
228
|
/**
|
|
291
|
-
* Elimina el chat y sus mensajes en la cuenta
|
|
292
|
-
*
|
|
293
|
-
* Deletes the chat and its messages on the
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
* @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.
|
|
297
233
|
*/
|
|
298
234
|
async delete() {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
else {
|
|
305
|
-
await socket
|
|
306
|
-
.chatModify({ delete: true, lastMessages: await last_messages(this._raw.id) }, this._raw.id)
|
|
307
|
-
.catch(() => null);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
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}`);
|
|
311
239
|
return true;
|
|
312
240
|
}
|
|
313
241
|
/**
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
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.
|
|
318
246
|
*
|
|
319
247
|
* @param cid - Teléfono, JID, LID o id de grupo / Phone, JID, LID or group id
|
|
320
|
-
* @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
|
|
321
249
|
*/
|
|
322
250
|
static async get(cid) {
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
? await internals(wa).resolve_jid(String(cid))
|
|
326
|
-
: digits
|
|
327
|
-
? `${digits}@s.whatsapp.net`
|
|
328
|
-
: null;
|
|
329
|
-
if (jid) {
|
|
330
|
-
const cached = deserialize(await wa.engine.get(`/chat/${jid}`));
|
|
331
|
-
return new _Chat(cached ?? { id: jid });
|
|
332
|
-
}
|
|
333
|
-
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;
|
|
334
253
|
}
|
|
335
254
|
/**
|
|
336
|
-
* Pagina los chats persistidos, del más
|
|
337
|
-
* 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.
|
|
338
257
|
*
|
|
339
258
|
* @param offset - Desplazamiento / Offset
|
|
340
259
|
* @param limit - Tamaño de página / Page size
|
|
341
260
|
* @returns Página de chats / Chat page
|
|
342
261
|
*/
|
|
343
262
|
static async list(offset = 0, limit = 50) {
|
|
344
|
-
return (await
|
|
263
|
+
return (await init.engine.list('/chat', offset, limit))
|
|
345
264
|
.map((raw) => deserialize(raw))
|
|
346
|
-
.filter((
|
|
347
|
-
.map((
|
|
265
|
+
.filter((doc) => doc !== null)
|
|
266
|
+
.map((doc) => new this(doc));
|
|
348
267
|
}
|
|
349
|
-
}
|
|
268
|
+
}
|
|
269
|
+
return _Chat;
|
|
350
270
|
}
|