@oxidezap/baileyrs 0.4.0 → 0.4.1

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.
@@ -9,6 +9,7 @@ import { assertArgumentDomain } from './argument-domain.js';
9
9
  import { Boom } from './boom.js';
10
10
  import { getKeyAuthorPortable, randomBytes, sha256Sync, toNumber, unixTimestampSeconds, utf8Encode, hexEncode } from '../Runtime/bytes.js';
11
11
  import { readableFromWeb } from '../Runtime/stream.js';
12
+ import { extractMessageContent, getContentType, normalizeMessageContent } from '../Media/content.js';
12
13
  import { generateThumbnail, getAudioDuration, getAudioWaveform, getStream, toBuffer } from './messages-media-core.js';
13
14
  const MIMETYPE_MAP = {
14
15
  image: 'image/jpeg',
@@ -56,7 +57,31 @@ const assertColor = (color) => {
56
57
  }
57
58
  return parseInt(hex, 16);
58
59
  };
60
+ const toAudioUploadContent = (audio) => {
61
+ if (typeof audio === 'object' && audio !== null && !BufferRuntime.isBuffer(audio) && 'media' in audio) {
62
+ const { media, ...rest } = audio;
63
+ return { audio: media, ...rest };
64
+ }
65
+ return { audio };
66
+ };
59
67
  export const prepareWAMessageMedia = async (message, options) => {
68
+ if ('sticker' in message && message.sticker != null && 'audio' in message && message.audio != null) {
69
+ const { audio, ...stickerInput } = message;
70
+ const audioOptions = options.mediaTypeOverride ? { ...options, mediaTypeOverride: undefined } : options;
71
+ const [stickerObj, audioObj] = await Promise.all([
72
+ prepareSingleWAMessageMedia(stickerInput, options),
73
+ prepareSingleWAMessageMedia(toAudioUploadContent(audio), audioOptions)
74
+ ]);
75
+ if (stickerObj.stickerMessage && audioObj.audioMessage) {
76
+ stickerObj.stickerMessage.audioMessage = audioObj.audioMessage;
77
+ }
78
+ return WAProto.Message.fromObject({
79
+ audioStickerMessage: { message: { stickerMessage: stickerObj.stickerMessage } }
80
+ });
81
+ }
82
+ return prepareSingleWAMessageMedia(message, options);
83
+ };
84
+ const prepareSingleWAMessageMedia = async (message, options) => {
60
85
  const logger = options.logger;
61
86
  let mediaType;
62
87
  for (const key of MEDIA_KEYS) {
@@ -73,6 +98,9 @@ export const prepareWAMessageMedia = async (message, options) => {
73
98
  media: messageRecord[mediaType]
74
99
  };
75
100
  delete uploadData[mediaType];
101
+ if (mediaType === 'sticker') {
102
+ delete uploadData.audio;
103
+ }
76
104
  // check if cacheable + generate cache key
77
105
  const cacheableKey = typeof uploadData.media === 'object' &&
78
106
  'url' in uploadData.media &&
@@ -178,7 +206,7 @@ async function extractBuiltInMetadata(buffer, mediaType, uploadData, options) {
178
206
  try {
179
207
  const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') && typeof uploadData.jpegThumbnail === 'undefined';
180
208
  const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined';
181
- const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true;
209
+ const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true && uploadData.waveform === undefined;
182
210
  const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true;
183
211
  if (requiresThumbnailComputation) {
184
212
  const { thumbnail, originalImageDimensions } = await generateThumbnail(buffer, mediaType, options);
@@ -267,6 +295,9 @@ export const generateForwardMessageContent = (message, forceForward) => {
267
295
  else {
268
296
  nested.contextInfo = contextInfo;
269
297
  }
298
+ if (content.stickerMessage?.audioMessage) {
299
+ return { audioStickerMessage: { message: content } };
300
+ }
270
301
  return content;
271
302
  };
272
303
  export const hasNonNullishProperty = (message, key) => {
@@ -538,14 +569,18 @@ export const generateWAMessageContent = async (message, options) => {
538
569
  if (hasOptionalProperty(message, 'viewOnce') && !!message.viewOnce) {
539
570
  m = { viewOnceMessage: { message: m } };
540
571
  }
572
+ const resolveContextTarget = () => {
573
+ const targetContainer = normalizeMessageContent(m) ?? m;
574
+ const messageType = Object.keys(targetContainer)[0];
575
+ const target = messageType ? targetContainer[messageType] : undefined;
576
+ return target && typeof target === 'object' ? target : undefined;
577
+ };
541
578
  if ((hasOptionalProperty(message, 'mentions') && message.mentions?.length) ||
542
579
  (hasOptionalProperty(message, 'mentionAll') && message.mentionAll)) {
543
- const messageType = Object.keys(m)[0];
544
- const key = m[messageType];
580
+ const target = resolveContextTarget();
545
581
  // `contextInfo` is declared by the proto schema but isn't an own
546
582
  // property on freshly-built messages — create-if-absent, then merge.
547
- if (key && typeof key === 'object') {
548
- const target = key;
583
+ if (target) {
549
584
  const ci = (target.contextInfo ?? (target.contextInfo = {}));
550
585
  if (message.mentions?.length) {
551
586
  ci.mentionedJid = message.mentions;
@@ -566,18 +601,19 @@ export const generateWAMessageContent = async (message, options) => {
566
601
  };
567
602
  }
568
603
  if (hasOptionalProperty(message, 'contextInfo') && !!message.contextInfo) {
569
- const messageType = Object.keys(m)[0];
570
- const key = m[messageType];
571
- if (key) {
572
- key.contextInfo = key.contextInfo ? Object.assign(key.contextInfo, message.contextInfo) : message.contextInfo;
604
+ const target = resolveContextTarget();
605
+ if (target) {
606
+ target.contextInfo = target.contextInfo
607
+ ? Object.assign(target.contextInfo, message.contextInfo)
608
+ : message.contextInfo;
573
609
  }
574
610
  }
575
611
  return WAProto.Message.create(m);
576
612
  };
577
613
  export const generateWAMessageFromContent = (jid, message, options) => {
578
- const innerMessage = normalizeMessageContent(message);
579
614
  const timestamp = unixTimestampSeconds(options.timestamp);
580
- const { quoted, userJid } = options;
615
+ const { quoted, ephemeralExpiration, userJid } = options;
616
+ const innerMessage = quoted || ephemeralExpiration ? normalizeMessageContent(message) : message;
581
617
  // Only the quote and ephemeral branches read the content key, and a plain
582
618
  // send takes neither, so resolve it on demand instead of on every send.
583
619
  let key;
@@ -611,7 +647,7 @@ export const generateWAMessageFromContent = (jid, message, options) => {
611
647
  }
612
648
  if (
613
649
  // if we want to send a disappearing message
614
- !!options?.ephemeralExpiration &&
650
+ !!ephemeralExpiration &&
615
651
  // newsletters don't support ephemeral messages
616
652
  !isJidNewsletter(jid)) {
617
653
  const contentKey = key ?? getContentType(innerMessage);
@@ -621,7 +657,7 @@ export const generateWAMessageFromContent = (jid, message, options) => {
621
657
  /* @ts-ignore */
622
658
  innerMessage[contentKey].contextInfo = {
623
659
  ...innerMessage[contentKey].contextInfo,
624
- expiration: options.ephemeralExpiration || WA_DEFAULT_EPHEMERAL
660
+ expiration: ephemeralExpiration || WA_DEFAULT_EPHEMERAL
625
661
  //ephemeralSettingTimestamp: options.ephemeralOptions.eph_setting_ts?.toString()
626
662
  };
627
663
  }
@@ -659,123 +695,7 @@ export const generateWAMessage = async (jid, content, options) => {
659
695
  : options;
660
696
  return generateWAMessageFromContent(jid, await generateWAMessageContent(content, contentOptions), options);
661
697
  };
662
- /**
663
- * The content keys that carry almost every message on a live socket, settled by
664
- * identity before the structural predicate runs. Every name here already
665
- * satisfies that predicate, so this only shortcuts the substring scan; it never
666
- * changes which key wins.
667
- */
668
- const isCommonContentKey = (key) => {
669
- switch (key) {
670
- case 'conversation':
671
- case 'extendedTextMessage':
672
- case 'imageMessage':
673
- case 'videoMessage':
674
- case 'audioMessage':
675
- case 'stickerMessage':
676
- case 'documentMessage':
677
- case 'reactionMessage':
678
- case 'protocolMessage':
679
- case 'pollCreationMessage':
680
- return true;
681
- default:
682
- return false;
683
- }
684
- };
685
- /** Get the key to access the true type of content */
686
- export const getContentType = (content) => {
687
- if (!content) {
688
- return undefined;
689
- }
690
- // The answer stays positional (the first own key naming content wins), but
691
- // the scan is an indexed loop with an early return instead of `Array.find`,
692
- // so no closure is allocated and the common keys never reach `includes`.
693
- //
694
- // Deliberately still `Object.keys` and not `for..in`: a decoded
695
- // `WAProto.Message` inherits a default for every field of the schema, so
696
- // `for..in` would enumerate ~100 prototype keys per call (and would have to
697
- // filter them back out with `hasOwn` to stay positional) where `Object.keys`
698
- // yields only the handful that were actually set.
699
- const keys = Object.keys(content);
700
- for (let i = 0; i < keys.length; i++) {
701
- const key = keys[i];
702
- if (isCommonContentKey(key)) {
703
- return key;
704
- }
705
- if (key.includes('Message') && key !== 'senderKeyDistributionMessage') {
706
- return key;
707
- }
708
- }
709
- return undefined;
710
- };
711
- const getFutureProofMessage = (message) => message?.ephemeralMessage ||
712
- message?.viewOnceMessage ||
713
- message?.documentWithCaptionMessage ||
714
- message?.viewOnceMessageV2 ||
715
- message?.viewOnceMessageV2Extension ||
716
- message?.editedMessage ||
717
- message?.associatedChildMessage ||
718
- message?.groupStatusMessage ||
719
- message?.groupStatusMessageV2;
720
- const extractFromTemplateMessage = (msg) => {
721
- if (msg.imageMessage) {
722
- return { imageMessage: msg.imageMessage };
723
- }
724
- else if (msg.documentMessage) {
725
- return { documentMessage: msg.documentMessage };
726
- }
727
- else if (msg.videoMessage) {
728
- return { videoMessage: msg.videoMessage };
729
- }
730
- else if (msg.locationMessage) {
731
- return { locationMessage: msg.locationMessage };
732
- }
733
- else {
734
- return {
735
- conversation: 'contentText' in msg ? msg.contentText : 'hydratedContentText' in msg ? msg.hydratedContentText : ''
736
- };
737
- }
738
- };
739
- /**
740
- * Normalizes ephemeral, view once messages to regular message content
741
- * Eg. image messages in ephemeral messages, in view once messages etc.
742
- * @param content
743
- * @returns
744
- */
745
- export const normalizeMessageContent = (content) => {
746
- if (!content) {
747
- return undefined;
748
- }
749
- // set max iterations to prevent an infinite loop
750
- for (let i = 0; i < 5; i++) {
751
- const inner = getFutureProofMessage(content);
752
- if (!inner) {
753
- break;
754
- }
755
- content = inner.message;
756
- }
757
- return content;
758
- };
759
- /**
760
- * Extract the true message content from a message
761
- * Eg. extracts the inner message from a disappearing message/view once message
762
- */
763
- export const extractMessageContent = (content) => {
764
- content = normalizeMessageContent(content);
765
- if (content?.buttonsMessage) {
766
- return extractFromTemplateMessage(content.buttonsMessage);
767
- }
768
- if (content?.templateMessage?.hydratedFourRowTemplate) {
769
- return extractFromTemplateMessage(content?.templateMessage?.hydratedFourRowTemplate);
770
- }
771
- if (content?.templateMessage?.hydratedTemplate) {
772
- return extractFromTemplateMessage(content?.templateMessage?.hydratedTemplate);
773
- }
774
- if (content?.templateMessage?.fourRowTemplate) {
775
- return extractFromTemplateMessage(content?.templateMessage?.fourRowTemplate);
776
- }
777
- return content;
778
- };
698
+ export { extractMessageContent, getContentType, normalizeMessageContent };
779
699
  export const MEDIA_DOWNLOAD_TYPES = ['buffer', 'stream'];
780
700
  /**
781
701
  * Downloads the given message. Throws an error if it's not a media message.
@@ -1,3 +1,4 @@
1
+ import type { proto } from './runtime.js';
1
2
  export type ProtoFieldSchema = readonly [name: string, kind: number, reference: number, flags: number, oneof: string];
2
3
  export type ProtoMessageSchema = readonly [path: string, fields: readonly ProtoFieldSchema[]];
3
4
  export type ProtoEnumSchema = readonly [path: string, entries: readonly (string | number)[]];
@@ -18,8 +19,13 @@ export declare const PROTO_FIELD_FLAG: {
18
19
  readonly map: 2;
19
20
  readonly proto3Optional: 4;
20
21
  readonly oneof: 8;
22
+ readonly bridgeAddition: 16;
21
23
  };
22
- export declare const PROTO_SCHEMA_SOURCE = "baileys@7.0.0-rc14:4bab5324186daf8de5ab592548ee80dfc753651667fa9411e0666837f90e4c8d";
24
+ export declare const PROTO_INSTANCE_SCHEMA: unique symbol;
25
+ export declare const BASELINE_FUTURE_PROOF_FIELDS: readonly ["ephemeralMessage", "viewOnceMessage", "documentWithCaptionMessage", "viewOnceMessageV2", "viewOnceMessageV2Extension", "editedMessage", "associatedChildMessage", "groupStatusMessage", "groupStatusMessageV2"];
26
+ export declare const BRIDGE_FUTURE_PROOF_FIELDS: readonly ["newsletterAdminProfileMessage", "spoilerMessage", "newsletterAdminProfileStatusMessage", "botPlatformRegistrationSuccessMessage", "newsletterScheduledMessage", "acp2SettingMessage", "audioStickerMessage", "botGroupParticipantMessage"];
27
+ export declare const getOrderedFutureProofMessage: (message: proto.IMessage) => proto.Message.IFutureProofMessage | undefined;
28
+ export declare const PROTO_SCHEMA_SOURCE = "baileys@7.0.0-rc14:4bab5324186daf8de5ab592548ee80dfc753651667fa9411e0666837f90e4c8d+@oxidezap/whatsapp-rust-bridge@0.28.0:c5c7786509ff120044f0ca01d062efc6c118fa587a7d4bdae7b42a2590a43bea";
23
29
  export declare const PROTO_MESSAGE_SCHEMAS: readonly ProtoMessageSchema[];
24
30
  export declare const PROTO_ENUM_SCHEMAS: readonly ProtoEnumSchema[];
25
31
  //# sourceMappingURL=compatibility-schema.d.ts.map