whatsapp-web-sj.js 1.26.0 → 1.31.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.
@@ -5,61 +5,66 @@ exports.LoadUtils = () => {
5
5
 
6
6
  window.WWebJS.forwardMessage = async (chatId, msgId) => {
7
7
  const msg = window.Store.Msg.get(msgId) || (await window.Store.Msg.getMessagesById([msgId]))?.messages?.[0];
8
- let chat = window.Store.Chat.get(chatId);
8
+ const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
9
9
 
10
10
  if (window.compareWwebVersions(window.Debug.VERSION, '>', '2.3000.0')) {
11
- return window.Store.ForwardUtils.forwardMessagesToChats([msg], [chat], false);
11
+ return window.Store.ForwardUtils.forwardMessagesToChats([msg], [chat], true);
12
12
  } else {
13
13
  return chat.forwardMessages([msg]);
14
14
  }
15
15
  };
16
16
 
17
17
  window.WWebJS.sendSeen = async (chatId) => {
18
- let chat = window.Store.Chat.get(chatId);
19
- if (chat !== undefined) {
20
- await window.Store.SendSeen.sendSeen(chat, false);
18
+ const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
19
+ if (chat) {
20
+ await window.Store.SendSeen.sendSeen(chat);
21
21
  return true;
22
22
  }
23
23
  return false;
24
-
25
24
  };
26
25
 
27
26
  window.WWebJS.sendMessage = async (chat, content, options = {}) => {
28
- let attOptions = {};
29
- if (options.attachment) {
30
- attOptions = options.sendMediaAsSticker
31
- ? await window.WWebJS.processStickerData(options.attachment)
32
- : await window.WWebJS.processMediaData(options.attachment, {
27
+ const isChannel = window.Store.ChatGetters.getIsNewsletter(chat);
28
+
29
+ let mediaOptions = {};
30
+ if (options.media) {
31
+ mediaOptions = options.sendMediaAsSticker && !isChannel
32
+ ? await window.WWebJS.processStickerData(options.media)
33
+ : await window.WWebJS.processMediaData(options.media, {
34
+ forceSticker: options.sendMediaAsSticker,
35
+ forceGif: options.sendVideoAsGif,
33
36
  forceVoice: options.sendAudioAsVoice,
34
37
  forceDocument: options.sendMediaAsDocument,
35
- forceGif: options.sendVideoAsGif
38
+ forceMediaHd: options.sendMediaAsHd,
39
+ sendToChannel: isChannel
36
40
  });
37
-
38
- attOptions.caption = options.caption;
39
- content = options.sendMediaAsSticker ? undefined : attOptions.preview;
40
- attOptions.isViewOnce = options.isViewOnce;
41
-
42
- delete options.attachment;
41
+ mediaOptions.caption = options.caption;
42
+ content = options.sendMediaAsSticker ? undefined : mediaOptions.preview;
43
+ mediaOptions.isViewOnce = options.isViewOnce;
44
+ delete options.media;
43
45
  delete options.sendMediaAsSticker;
44
46
  }
47
+
45
48
  let quotedMsgOptions = {};
46
49
  if (options.quotedMessageId) {
47
- let quotedMessage = await window.Store.Msg.getMessagesById([options.quotedMessageId]);
48
-
49
- if (quotedMessage['messages'].length != 1) {
50
- throw new Error('Could not get the quoted message.');
51
- }
50
+ let quotedMessage = window.Store.Msg.get(options.quotedMessageId);
51
+ !quotedMessage && (quotedMessage = (await window.Store.Msg.getMessagesById([options.quotedMessageId]))?.messages?.[0]);
52
+ if (quotedMessage) {
52
53
 
53
- quotedMessage = quotedMessage['messages'][0];
54
+ const canReply = window.Store.ReplyUtils
55
+ ? window.Store.ReplyUtils.canReplyMsg(quotedMessage.unsafe())
56
+ : quotedMessage.canReply();
54
57
 
55
- // TODO remove .canReply() once all clients are updated to >= v2.2241.6
56
- const canReply = window.Store.ReplyUtils ?
57
- window.Store.ReplyUtils.canReplyMsg(quotedMessage.unsafe()) :
58
- quotedMessage.canReply();
59
-
60
- if (canReply) {
61
- quotedMsgOptions = quotedMessage.msgContextInfo(chat);
58
+ if (canReply) {
59
+ quotedMsgOptions = quotedMessage.msgContextInfo(chat);
60
+ }
61
+ } else {
62
+ if (!options.ignoreQuoteErrors) {
63
+ throw new Error('Could not get the quoted message.');
64
+ }
62
65
  }
66
+
67
+ delete options.ignoreQuoteErrors;
63
68
  delete options.quotedMessageId;
64
69
  }
65
70
 
@@ -102,14 +107,15 @@ exports.LoadUtils = () => {
102
107
  const { pollName, pollOptions } = options.poll;
103
108
  const { allowMultipleAnswers, messageSecret } = options.poll.options;
104
109
  _pollOptions = {
110
+ kind: 'pollCreation',
105
111
  type: 'poll_creation',
106
112
  pollName: pollName,
107
113
  pollOptions: pollOptions,
108
114
  pollSelectableOptionsCount: allowMultipleAnswers ? 0 : 1,
109
115
  messageSecret:
110
- Array.isArray(messageSecret) && messageSecret.length === 32
111
- ? new Uint8Array(messageSecret)
112
- : window.crypto.getRandomValues(new Uint8Array(32))
116
+ Array.isArray(messageSecret) && messageSecret.length === 32
117
+ ? new Uint8Array(messageSecret)
118
+ : window.crypto.getRandomValues(new Uint8Array(32))
113
119
  };
114
120
  delete options.poll;
115
121
  }
@@ -129,11 +135,12 @@ exports.LoadUtils = () => {
129
135
  vcardOptions = {
130
136
  type: 'multi_vcard',
131
137
  vcardList: vcards,
132
- body: undefined
138
+ body: null
133
139
  };
134
140
  delete options.contactCardList;
135
141
  } else if (options.parseVCards && typeof (content) === 'string' && content.startsWith('BEGIN:VCARD')) {
136
142
  delete options.parseVCards;
143
+ delete options.linkPreview;
137
144
  try {
138
145
  const parsed = window.Store.VCard.parseVcard(content);
139
146
  if (parsed) {
@@ -160,9 +167,9 @@ exports.LoadUtils = () => {
160
167
  }
161
168
  }
162
169
  }
163
-
170
+
164
171
  let buttonOptions = {};
165
- if(options.buttons){
172
+ if (options.buttons) {
166
173
  let caption;
167
174
  if (options.buttons.type === 'chat') {
168
175
  content = options.buttons.body;
@@ -210,14 +217,22 @@ exports.LoadUtils = () => {
210
217
  delete options.invokedBotWid;
211
218
  }
212
219
 
220
+ const lidUser = window.Store.User.getMaybeMeLidUser();
213
221
  const meUser = window.Store.User.getMaybeMeUser();
214
222
  const newId = await window.Store.MsgKey.newId();
215
-
216
- const newMsgId = new window.Store.MsgKey({
217
- from: meUser,
223
+ let from = chat.id.isLid() ? lidUser : meUser;
224
+ let participant;
225
+
226
+ if (chat.isGroup) {
227
+ from = chat.groupMetadata && chat.groupMetadata.isLidAddressingMode ? lidUser : meUser;
228
+ participant = window.Store.WidFactory.toUserWid(from);
229
+ }
230
+
231
+ const newMsgKey = new window.Store.MsgKey({
232
+ from: from,
218
233
  to: chat.id,
219
234
  id: newId,
220
- participant: chat.id.isGroup() ? meUser : undefined,
235
+ participant: participant,
221
236
  selfDir: 'out',
222
237
  });
223
238
 
@@ -228,7 +243,7 @@ exports.LoadUtils = () => {
228
243
 
229
244
  const message = {
230
245
  ...options,
231
- id: newMsgId,
246
+ id: newMsgKey,
232
247
  ack: 0,
233
248
  body: content,
234
249
  from: meUser,
@@ -239,11 +254,11 @@ exports.LoadUtils = () => {
239
254
  isNewMsg: true,
240
255
  type: 'chat',
241
256
  ...ephemeralFields,
257
+ ...mediaOptions,
258
+ ...(mediaOptions.toJSON ? mediaOptions.toJSON() : {}),
259
+ ...quotedMsgOptions,
242
260
  ...locationOptions,
243
261
  ..._pollOptions,
244
- ...attOptions,
245
- ...(attOptions.toJSON ? attOptions.toJSON() : {}),
246
- ...quotedMsgOptions,
247
262
  ...vcardOptions,
248
263
  ...buttonOptions,
249
264
  ...listOptions,
@@ -256,12 +271,44 @@ exports.LoadUtils = () => {
256
271
  delete message.canonicalUrl;
257
272
  }
258
273
 
274
+ if (isChannel) {
275
+ const msg = new window.Store.Msg.modelClass(message);
276
+ const msgDataFromMsgModel = window.Store.SendChannelMessage.msgDataFromMsgModel(msg);
277
+ const isMedia = Object.keys(mediaOptions).length > 0;
278
+ await window.Store.SendChannelMessage.addNewsletterMsgsRecords([msgDataFromMsgModel]);
279
+ chat.msgs.add(msg);
280
+ chat.t = msg.t;
281
+
282
+ const sendChannelMsgResponse = await window.Store.SendChannelMessage.sendNewsletterMessageJob({
283
+ msg: msg,
284
+ type: message.type === 'chat' ? 'text' : isMedia ? 'media' : 'pollCreation',
285
+ newsletterJid: chat.id.toJid(),
286
+ ...(isMedia
287
+ ? {
288
+ mediaMetadata: msg.avParams(),
289
+ mediaHandle: isMedia ? mediaOptions.mediaHandle : null,
290
+ }
291
+ : {}
292
+ )
293
+ });
294
+
295
+ if (sendChannelMsgResponse.success) {
296
+ msg.t = sendChannelMsgResponse.ack.t;
297
+ msg.serverId = sendChannelMsgResponse.serverId;
298
+ }
299
+ msg.updateAck(1, true);
300
+ await window.Store.SendChannelMessage.updateNewsletterMsgRecord(msg);
301
+ return msg;
302
+ }
303
+
259
304
  await window.Store.SendMessage.addAndSendMsgToChat(chat, message);
260
- return window.Store.Msg.get(newMsgId._serialized);
305
+ await window.Store.HistorySync.sendPeerDataOperationRequest(3, {
306
+ chatId: chat.id
307
+ });
308
+ return window.Store.Msg.get(newMsgKey._serialized);
261
309
  };
262
310
 
263
311
  window.WWebJS.editMessage = async (msg, content, options = {}) => {
264
-
265
312
  const extraOptions = options.extraOptions || {};
266
313
  delete options.extraOptions;
267
314
 
@@ -347,46 +394,56 @@ exports.LoadUtils = () => {
347
394
  return stickerInfo;
348
395
  };
349
396
 
350
- window.WWebJS.processMediaData = async (mediaInfo, { forceVoice, forceDocument, forceGif }) => {
397
+ window.WWebJS.processMediaData = async (mediaInfo, { forceSticker, forceGif, forceVoice, forceDocument, forceMediaHd, sendToChannel }) => {
351
398
  const file = window.WWebJS.mediaInfoToFile(mediaInfo);
352
- const mData = await window.Store.OpaqueData.createFromData(file, file.type);
353
- const mediaPrep = window.Store.MediaPrep.prepRawMedia(mData, { asDocument: forceDocument });
399
+ const opaqueData = await window.Store.OpaqueData.createFromData(file, file.type);
400
+ const mediaParams = {
401
+ asSticker: forceSticker,
402
+ asGif: forceGif,
403
+ isPtt: forceVoice,
404
+ asDocument: forceDocument
405
+ };
406
+
407
+ if (forceMediaHd && file.type.indexOf('image/') === 0) {
408
+ mediaParams.maxDimension = 2560;
409
+ }
410
+
411
+ const mediaPrep = window.Store.MediaPrep.prepRawMedia(opaqueData, mediaParams);
354
412
  const mediaData = await mediaPrep.waitForPrep();
355
413
  const mediaObject = window.Store.MediaObject.getOrCreateMediaObject(mediaData.filehash);
356
-
357
414
  const mediaType = window.Store.MediaTypes.msgToMediaType({
358
415
  type: mediaData.type,
359
- isGif: mediaData.isGif
416
+ isGif: mediaData.isGif,
417
+ isNewsletter: sendToChannel,
360
418
  });
361
419
 
362
- if (forceVoice && mediaData.type === 'audio') {
363
- mediaData.type = 'ptt';
420
+ if (forceVoice && mediaData.type === 'ptt') {
364
421
  const waveform = mediaObject.contentInfo.waveform;
365
422
  mediaData.waveform =
366
- waveform ?? await window.WWebJS.generateWaveform(file);
367
- }
368
-
369
- if (forceGif && mediaData.type === 'video') {
370
- mediaData.isGif = true;
371
- }
372
-
373
- if (forceDocument) {
374
- mediaData.type = 'document';
423
+ waveform || await window.WWebJS.generateWaveform(file);
375
424
  }
376
425
 
377
426
  if (!(mediaData.mediaBlob instanceof window.Store.OpaqueData)) {
378
- mediaData.mediaBlob = await window.Store.OpaqueData.createFromData(mediaData.mediaBlob, mediaData.mediaBlob.type);
427
+ mediaData.mediaBlob = await window.Store.OpaqueData.createFromData(
428
+ mediaData.mediaBlob,
429
+ mediaData.mediaBlob.type
430
+ );
379
431
  }
380
432
 
381
433
  mediaData.renderableUrl = mediaData.mediaBlob.url();
382
434
  mediaObject.consolidate(mediaData.toJSON());
383
435
  mediaData.mediaBlob.autorelease();
384
436
 
385
- const uploadedMedia = await window.Store.MediaUpload.uploadMedia({
437
+ const dataToUpload = {
386
438
  mimetype: mediaData.mimetype,
387
439
  mediaObject,
388
- mediaType
389
- });
440
+ mediaType,
441
+ ...(sendToChannel ? { calculateToken: window.Store.SendChannelMessage.getRandomFilehash } : {})
442
+ };
443
+
444
+ const uploadedMedia = !sendToChannel
445
+ ? await window.Store.MediaUpload.uploadMedia(dataToUpload)
446
+ : await window.Store.MediaUpload.uploadUnencryptedMedia(dataToUpload);
390
447
 
391
448
  const mediaEntry = uploadedMedia.mediaEntry;
392
449
  if (!mediaEntry) {
@@ -404,13 +461,14 @@ exports.LoadUtils = () => {
404
461
  uploadhash: mediaEntry.uploadHash,
405
462
  size: mediaObject.size,
406
463
  streamingSidecar: mediaEntry.sidecar,
407
- firstFrameSidecar: mediaEntry.firstFrameSidecar
464
+ firstFrameSidecar: mediaEntry.firstFrameSidecar,
465
+ mediaHandle: sendToChannel ? mediaEntry.handle : null,
408
466
  });
409
467
 
410
468
  return mediaData;
411
469
  };
412
470
 
413
- window.WWebJS.getMessageModel = message => {
471
+ window.WWebJS.getMessageModel = (message) => {
414
472
  const msg = message.serialize();
415
473
 
416
474
  msg.isEphemeral = message.isEphemeral;
@@ -448,49 +506,115 @@ exports.LoadUtils = () => {
448
506
  return _vote;
449
507
  };
450
508
 
451
- window.WWebJS.getChatModel = async chat => {
452
-
453
- let res = chat.serialize();
454
- res.isGroup = chat.isGroup;
455
- res.formattedTitle = chat.formattedTitle;
456
- res.isMuted = chat.mute && chat.mute.isMuted;
509
+ window.WWebJS.getChat = async (chatId, { getAsModel = true } = {}) => {
510
+ const isChannel = /@\w*newsletter\b/.test(chatId);
511
+ const chatWid = window.Store.WidFactory.createWid(chatId);
512
+ let chat;
457
513
 
458
- if (chat.groupMetadata) {
459
- const chatWid = window.Store.WidFactory.createWid((chat.id._serialized));
460
- await window.Store.GroupMetadata.update(chatWid);
461
- res.groupMetadata = chat.groupMetadata.serialize();
462
- }
463
-
464
- res.lastMessage = null;
465
- if (res.msgs && res.msgs.length) {
466
- const lastMessage = chat.lastReceivedKey
467
- ? window.Store.Msg.get(chat.lastReceivedKey._serialized) || (await window.Store.Msg.getMessagesById([chat.lastReceivedKey._serialized]))?.messages?.[0]
468
- : null;
469
- if (lastMessage) {
470
- res.lastMessage = window.WWebJS.getMessageModel(lastMessage);
514
+ if (isChannel) {
515
+ try {
516
+ chat = window.Store.NewsletterCollection.get(chatId);
517
+ if (!chat) {
518
+ await window.Store.ChannelUtils.loadNewsletterPreviewChat(chatId);
519
+ chat = await window.Store.NewsletterCollection.find(chatWid);
520
+ }
521
+ } catch (err) {
522
+ chat = null;
471
523
  }
524
+ } else {
525
+ chat = window.Store.Chat.get(chatWid) || (await window.Store.Chat.find(chatWid));
472
526
  }
473
-
474
- delete res.msgs;
475
- delete res.msgUnsyncedButtonReplyMsgs;
476
- delete res.unsyncedButtonReplies;
477
527
 
478
- return res;
528
+ return getAsModel && chat
529
+ ? await window.WWebJS.getChatModel(chat, { isChannel: isChannel })
530
+ : chat;
479
531
  };
480
532
 
481
- window.WWebJS.getChat = async chatId => {
482
- const chatWid = window.Store.WidFactory.createWid(chatId);
483
- const chat = await window.Store.Chat.find(chatWid);
484
- return await window.WWebJS.getChatModel(chat);
533
+ window.WWebJS.getChannelMetadata = async (inviteCode) => {
534
+ const response =
535
+ await window.Store.ChannelUtils.queryNewsletterMetadataByInviteCode(
536
+ inviteCode,
537
+ window.Store.ChannelUtils.getRoleByIdentifier(inviteCode)
538
+ );
539
+
540
+ const picUrl = response.newsletterPictureMetadataMixin?.picture[0]?.queryPictureDirectPathOrEmptyResponseMixinGroup.value.directPath;
541
+
542
+ return {
543
+ id: response.idJid,
544
+ createdAtTs: response.newsletterCreationTimeMetadataMixin.creationTimeValue,
545
+ titleMetadata: {
546
+ title: response.newsletterNameMetadataMixin.nameElementValue,
547
+ updatedAtTs: response.newsletterNameMetadataMixin.nameUpdateTime
548
+ },
549
+ descriptionMetadata: {
550
+ description: response.newsletterDescriptionMetadataMixin.descriptionQueryDescriptionResponseMixin.elementValue,
551
+ updatedAtTs: response.newsletterDescriptionMetadataMixin.descriptionQueryDescriptionResponseMixin.updateTime
552
+ },
553
+ inviteLink: `https://whatsapp.com/channel/${response.newsletterInviteLinkMetadataMixin.inviteCode}`,
554
+ membershipType: window.Store.ChannelUtils.getRoleByIdentifier(inviteCode),
555
+ stateType: response.newsletterStateMetadataMixin.stateType,
556
+ pictureUrl: picUrl ? `https://pps.whatsapp.net${picUrl}` : null,
557
+ subscribersCount: response.newsletterSubscribersMetadataMixin.subscribersCount,
558
+ isVerified: response.newsletterVerificationMetadataMixin.verificationState === 'verified'
559
+ };
485
560
  };
486
561
 
487
562
  window.WWebJS.getChats = async () => {
488
563
  const chats = window.Store.Chat.getModelsArray();
489
-
490
564
  const chatPromises = chats.map(chat => window.WWebJS.getChatModel(chat));
491
565
  return await Promise.all(chatPromises);
492
566
  };
493
567
 
568
+ window.WWebJS.getChannels = async () => {
569
+ const channels = window.Store.NewsletterCollection.getModelsArray();
570
+ const channelPromises = channels?.map((channel) => window.WWebJS.getChatModel(channel, { isChannel: true }));
571
+ return await Promise.all(channelPromises);
572
+ };
573
+
574
+ window.WWebJS.getChatModel = async (chat, { isChannel = false } = {}) => {
575
+ if (!chat) return null;
576
+
577
+ const model = chat.serialize();
578
+ model.isGroup = false;
579
+ model.isMuted = chat.mute?.expiration !== 0;
580
+ if (isChannel) {
581
+ model.isChannel = window.Store.ChatGetters.getIsNewsletter(chat);
582
+ } else {
583
+ model.formattedTitle = chat.formattedTitle;
584
+ }
585
+
586
+ if (chat.groupMetadata) {
587
+ model.isGroup = true;
588
+ const chatWid = window.Store.WidFactory.createWid(chat.id._serialized);
589
+ await window.Store.GroupMetadata.update(chatWid);
590
+ chat.groupMetadata.participants._models
591
+ .filter(x => x.id?._serialized?.endsWith('@lid'))
592
+ .forEach(x => x.contact?.phoneNumber && (x.id = x.contact.phoneNumber));
593
+ model.groupMetadata = chat.groupMetadata.serialize();
594
+ model.isReadOnly = chat.groupMetadata.announce;
595
+ }
596
+
597
+ if (chat.newsletterMetadata) {
598
+ await window.Store.NewsletterMetadataCollection.update(chat.id);
599
+ model.channelMetadata = chat.newsletterMetadata.serialize();
600
+ model.channelMetadata.createdAtTs = chat.newsletterMetadata.creationTime;
601
+ }
602
+
603
+ model.lastMessage = null;
604
+ if (model.msgs && model.msgs.length) {
605
+ const lastMessage = chat.lastReceivedKey
606
+ ? window.Store.Msg.get(chat.lastReceivedKey._serialized) || (await window.Store.Msg.getMessagesById([chat.lastReceivedKey._serialized]))?.messages?.[0]
607
+ : null;
608
+ lastMessage && (model.lastMessage = window.WWebJS.getMessageModel(lastMessage));
609
+ }
610
+
611
+ delete model.msgs;
612
+ delete model.msgUnsyncedButtonReplyMsgs;
613
+ delete model.unsyncedButtonReplies;
614
+
615
+ return model;
616
+ };
617
+
494
618
  window.WWebJS.getContactModel = contact => {
495
619
  let res = contact.serialize();
496
620
  res.isBusiness = contact.isBusiness === undefined ? false : contact.isBusiness;
@@ -499,57 +623,30 @@ exports.LoadUtils = () => {
499
623
  res.businessProfile = contact.businessProfile.serialize();
500
624
  }
501
625
 
502
- // TODO: remove useOldImplementation and its checks once all clients are updated to >= v2.2327.4
503
- const useOldImplementation
504
- = window.compareWwebVersions(window.Debug.VERSION, '<', '2.2327.4');
505
-
506
- res.isMe = useOldImplementation
507
- ? contact.isMe
508
- : window.Store.ContactMethods.getIsMe(contact);
509
- res.isUser = useOldImplementation
510
- ? contact.isUser
511
- : window.Store.ContactMethods.getIsUser(contact);
512
- res.isGroup = useOldImplementation
513
- ? contact.isGroup
514
- : window.Store.ContactMethods.getIsGroup(contact);
515
- res.isWAContact = useOldImplementation
516
- ? contact.isWAContact
517
- : window.Store.ContactMethods.getIsWAContact(contact);
518
- res.isMyContact = useOldImplementation
519
- ? contact.isMyContact
520
- : window.Store.ContactMethods.getIsMyContact(contact);
626
+ res.isMe = window.Store.ContactMethods.getIsMe(contact);
627
+ res.isUser = window.Store.ContactMethods.getIsUser(contact);
628
+ res.isGroup = window.Store.ContactMethods.getIsGroup(contact);
629
+ res.isWAContact = window.Store.ContactMethods.getIsWAContact(contact);
630
+ res.isMyContact = window.Store.ContactMethods.getIsMyContact(contact);
521
631
  res.isBlocked = contact.isContactBlocked;
522
- res.userid = useOldImplementation
523
- ? contact.userid
524
- : window.Store.ContactMethods.getUserid(contact);
525
- res.isEnterprise = useOldImplementation
526
- ? contact.isEnterprise
527
- : window.Store.ContactMethods.getIsEnterprise(contact);
528
- res.verifiedName = useOldImplementation
529
- ? contact.verifiedName
530
- : window.Store.ContactMethods.getVerifiedName(contact);
531
- res.verifiedLevel = useOldImplementation
532
- ? contact.verifiedLevel
533
- : window.Store.ContactMethods.getVerifiedLevel(contact);
534
- res.statusMute = useOldImplementation
535
- ? contact.statusMute
536
- : window.Store.ContactMethods.getStatusMute(contact);
537
- res.name = useOldImplementation
538
- ? contact.name
539
- : window.Store.ContactMethods.getName(contact);
540
- res.shortName = useOldImplementation
541
- ? contact.shortName
542
- : window.Store.ContactMethods.getShortName(contact);
543
- res.pushname = useOldImplementation
544
- ? contact.pushname
545
- : window.Store.ContactMethods.getPushname(contact);
632
+ res.userid = window.Store.ContactMethods.getUserid(contact);
633
+ res.isEnterprise = window.Store.ContactMethods.getIsEnterprise(contact);
634
+ res.verifiedName = window.Store.ContactMethods.getVerifiedName(contact);
635
+ res.verifiedLevel = window.Store.ContactMethods.getVerifiedLevel(contact);
636
+ res.statusMute = window.Store.ContactMethods.getStatusMute(contact);
637
+ res.name = window.Store.ContactMethods.getName(contact);
638
+ res.shortName = window.Store.ContactMethods.getShortName(contact);
639
+ res.pushname = window.Store.ContactMethods.getPushname(contact);
546
640
 
547
641
  return res;
548
642
  };
549
643
 
550
644
  window.WWebJS.getContact = async contactId => {
551
645
  const wid = window.Store.WidFactory.createWid(contactId);
552
- const contact = await window.Store.Contact.find(wid);
646
+ let contact = await window.Store.Contact.find(wid);
647
+ if (contact.id._serialized.endsWith('@lid')) {
648
+ contact.id = contact.phoneNumber;
649
+ }
553
650
  const bizProfile = await window.Store.BusinessProfile.fetchBizProfile(wid);
554
651
  bizProfile.profileOptions && (contact.businessProfile = bizProfile);
555
652
  return window.WWebJS.getContactModel(contact);
@@ -616,10 +713,6 @@ exports.LoadUtils = () => {
616
713
  return result;
617
714
  };
618
715
 
619
- /**
620
- * Referenced from and modified:
621
- * @see https://github.com/wppconnect-team/wa-js/commit/290ebfefe6021b3d17f7fdfdda5545bb0473b26f
622
- */
623
716
  window.WWebJS.generateWaveform = async (audioFile) => {
624
717
  try {
625
718
  const audioData = await audioFile.arrayBuffer();
@@ -653,7 +746,7 @@ exports.LoadUtils = () => {
653
746
  };
654
747
 
655
748
  window.WWebJS.sendClearChat = async (chatId) => {
656
- let chat = window.Store.Chat.get(chatId);
749
+ let chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
657
750
  if (chat !== undefined) {
658
751
  await window.Store.SendClear.sendClear(chat, false);
659
752
  return true;
@@ -662,7 +755,7 @@ exports.LoadUtils = () => {
662
755
  };
663
756
 
664
757
  window.WWebJS.sendDeleteChat = async (chatId) => {
665
- let chat = window.Store.Chat.get(chatId);
758
+ let chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
666
759
  if (chat !== undefined) {
667
760
  await window.Store.SendDelete.sendDelete(chat);
668
761
  return true;
@@ -782,19 +875,19 @@ exports.LoadUtils = () => {
782
875
  });
783
876
  };
784
877
 
785
- window.WWebJS.setPicture = async (chatid, media) => {
878
+ window.WWebJS.setPicture = async (chatId, media) => {
786
879
  const thumbnail = await window.WWebJS.cropAndResizeImage(media, { asDataUrl: true, mimetype: 'image/jpeg', size: 96 });
787
880
  const profilePic = await window.WWebJS.cropAndResizeImage(media, { asDataUrl: true, mimetype: 'image/jpeg', size: 640 });
788
881
 
789
- const chatWid = window.Store.WidFactory.createWid(chatid);
882
+ const chatWid = window.Store.WidFactory.createWid(chatId);
790
883
  try {
791
- const collection = window.Store.ProfilePicThumb.get(chatid);
792
- if (!collection.canSet()) return;
884
+ const collection = window.Store.ProfilePicThumb.get(chatId) || await window.Store.ProfilePicThumb.find(chatId);
885
+ if (!collection?.canSet()) return false;
793
886
 
794
887
  const res = await window.Store.GroupUtils.sendSetPicture(chatWid, thumbnail, profilePic);
795
888
  return res ? res.status === 200 : false;
796
889
  } catch (err) {
797
- if(err.name === 'ServerStatusCodeError') return false;
890
+ if (err.name === 'ServerStatusCodeError') return false;
798
891
  throw err;
799
892
  }
800
893
  };
@@ -892,7 +985,7 @@ exports.LoadUtils = () => {
892
985
  }
893
986
 
894
987
  if (rpcResult.name === 'AddParticipantsResponseSuccess') {
895
- const code = resultArgs?.value.error ?? '200';
988
+ const code = resultArgs?.value.error || '200';
896
989
  data.name = resultArgs?.name;
897
990
  data.code = +code;
898
991
  data.inviteV4Code = resultArgs?.value.addRequestCode;
@@ -920,7 +1013,7 @@ exports.LoadUtils = () => {
920
1013
  let response;
921
1014
  let result = [];
922
1015
 
923
- await window.Store.GroupQueryAndUpdate(groupWid);
1016
+ await window.Store.GroupQueryAndUpdate({ id: groupId });
924
1017
 
925
1018
  if (!requesterIds?.length) {
926
1019
  membershipRequests = group.groupMetadata.membershipApprovalRequests._models.map(({ id }) => id);
@@ -1007,11 +1100,40 @@ exports.LoadUtils = () => {
1007
1100
  }
1008
1101
  };
1009
1102
 
1103
+ window.WWebJS.subscribeToUnsubscribeFromChannel = async (channelId, action, options = {}) => {
1104
+ const channel = await window.WWebJS.getChat(channelId, { getAsModel: false });
1105
+
1106
+ if (!channel || channel.newsletterMetadata.membershipType === 'owner') return false;
1107
+ options = { eventSurface: 3, deleteLocalModels: options.deleteLocalModels ?? true };
1108
+
1109
+ try {
1110
+ if (action === 'Subscribe') {
1111
+ await window.Store.ChannelUtils.subscribeToNewsletterAction(channel, options);
1112
+ } else if (action === 'Unsubscribe') {
1113
+ await window.Store.ChannelUtils.unsubscribeFromNewsletterAction(channel, options);
1114
+ } else return false;
1115
+ return true;
1116
+ } catch (err) {
1117
+ if (err.name === 'ServerStatusCodeError') return false;
1118
+ throw err;
1119
+ }
1120
+ };
1121
+
1010
1122
  window.WWebJS.pinUnpinMsgAction = async (msgId, action, duration) => {
1011
1123
  const message = window.Store.Msg.get(msgId) || (await window.Store.Msg.getMessagesById([msgId]))?.messages?.[0];
1012
1124
  if (!message) return false;
1013
1125
  const response = await window.Store.pinUnpinMsg(message, action, duration);
1014
1126
  return response.messageSendResult === 'OK';
1015
1127
  };
1128
+
1129
+ window.WWebJS.getStatusModel = status => {
1130
+ const res = status.serialize();
1131
+ delete res._msgs;
1132
+ return res;
1133
+ };
1016
1134
 
1135
+ window.WWebJS.getAllStatuses = () => {
1136
+ const statuses = window.Store.Status.getModelsArray();
1137
+ return statuses.map(status => window.WWebJS.getStatusModel(status));
1138
+ };
1017
1139
  };