@kyyinfinite/baileys 1.0.0 → 2.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 +408 -64
- package/WAProto/WAProto.proto +360 -916
- package/WAProto/fix-imports.js +22 -18
- package/WAProto/index.d.ts +14015 -0
- package/WAProto/index.js +47122 -81757
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.d.ts +15 -14
- package/lib/Defaults/index.d.ts.map +1 -1
- package/lib/Defaults/index.js +99 -107
- package/lib/Defaults/index.js.map +1 -1
- package/lib/Kyyinfinite/AIRich.js +2041 -0
- package/lib/Kyyinfinite/BaseBuilder.js +64 -0
- package/lib/Kyyinfinite/Button.js +341 -0
- package/lib/Kyyinfinite/ButtonV2.js +175 -0
- package/lib/Kyyinfinite/Carousel.js +125 -0
- package/lib/Kyyinfinite/Foad.js +542 -0
- package/lib/Kyyinfinite/Foad2.js +466 -0
- package/lib/Kyyinfinite/Simple/AIBuilder.js +18 -0
- package/lib/Kyyinfinite/Simple/BuilderFactories.js +17 -0
- package/lib/Kyyinfinite/Simple/ButtonBuilder.js +107 -0
- package/lib/Kyyinfinite/Simple/CardBuilder.js +179 -0
- package/lib/Kyyinfinite/Simple/CarouselBuilder.js +97 -0
- package/lib/Kyyinfinite/Simple/FoadBuilder.js +18 -0
- package/lib/Kyyinfinite/Simple/MediaBuilder.js +35 -0
- package/lib/Kyyinfinite/Simple/MessageBuilder.js +41 -0
- package/lib/Kyyinfinite/Simple/MessageTypesBuilder.js +3 -0
- package/lib/Kyyinfinite/Simple/SimpleBuilder.js +28 -0
- package/lib/Kyyinfinite/Simple/SimpleClient.js +83 -0
- package/lib/Kyyinfinite/Simple/TextBuilder.js +22 -0
- package/lib/Kyyinfinite/Simple/index.d.ts +141 -0
- package/lib/Kyyinfinite/Simple/index.js +31 -0
- package/lib/Kyyinfinite/Toolkit.js +453 -0
- package/lib/Kyyinfinite/constants.js +1 -0
- package/lib/Kyyinfinite/errors.js +39 -0
- package/lib/Kyyinfinite/index.d.ts +2 -0
- package/lib/Kyyinfinite/index.js +2 -0
- package/lib/Kyyinfinite/wrapper.d.ts +2 -0
- package/lib/Kyyinfinite/wrapper.js +10 -0
- package/lib/Signal/Group/group_cipher.js.map +1 -1
- package/lib/Signal/Group/keyhelper.d.ts.map +1 -1
- package/lib/Signal/Group/keyhelper.js.map +1 -1
- package/lib/Signal/Group/sender-chain-key.js.map +1 -1
- package/lib/Signal/Group/sender-key-message.js.map +1 -1
- package/lib/Signal/Group/sender-message-key.js.map +1 -1
- package/lib/Signal/libsignal.d.ts.map +1 -1
- package/lib/Signal/libsignal.js +45 -17
- package/lib/Signal/libsignal.js.map +1 -1
- package/lib/Signal/lid-mapping.d.ts +4 -0
- package/lib/Signal/lid-mapping.d.ts.map +1 -1
- package/lib/Signal/lid-mapping.js +6 -0
- package/lib/Signal/lid-mapping.js.map +1 -1
- package/lib/Socket/business.d.ts +213 -532
- package/lib/Socket/business.d.ts.map +1 -1
- package/lib/Socket/business.js +358 -401
- package/lib/Socket/chats.d.ts +16 -2
- package/lib/Socket/chats.d.ts.map +1 -1
- package/lib/Socket/chats.js +247 -40
- package/lib/Socket/chats.js.map +1 -1
- package/lib/Socket/communities.d.ts +269 -641
- package/lib/Socket/communities.d.ts.map +1 -1
- package/lib/Socket/groups.d.ts +13 -2
- package/lib/Socket/groups.d.ts.map +1 -1
- package/lib/Socket/groups.js +21 -1
- package/lib/Socket/groups.js.map +1 -1
- package/lib/Socket/index.d.ts +255 -631
- package/lib/Socket/index.d.ts.map +1 -1
- package/lib/Socket/index.js +9 -10
- package/lib/Socket/index.js.map +1 -1
- package/lib/Socket/messages-recv.d.ts +23 -58
- package/lib/Socket/messages-recv.d.ts.map +1 -1
- package/lib/Socket/messages-recv.js +645 -181
- package/lib/Socket/messages-recv.js.map +1 -1
- package/lib/Socket/messages-send.d.ts +195 -575
- package/lib/Socket/messages-send.d.ts.map +1 -1
- package/lib/Socket/messages-send.js +1099 -1549
- package/lib/Socket/messages-send.js.map +1 -1
- package/lib/Socket/mex.d.ts.map +1 -1
- package/lib/Socket/mex.js +2 -8
- package/lib/Socket/mex.js.map +1 -1
- package/lib/Socket/newsletter.d.ts +11 -18
- package/lib/Socket/newsletter.d.ts.map +1 -1
- package/lib/Socket/newsletter.js +167 -302
- package/lib/Socket/newsletter.js.map +1 -1
- package/lib/Socket/socket.d.ts +6 -2
- package/lib/Socket/socket.d.ts.map +1 -1
- package/lib/Socket/socket.js +59 -25
- package/lib/Socket/socket.js.map +1 -1
- package/lib/Types/Auth.d.ts +1 -0
- package/lib/Types/Auth.d.ts.map +1 -1
- package/lib/Types/Bussines.d.ts +22 -34
- package/lib/Types/Call.d.ts +1 -1
- package/lib/Types/Call.d.ts.map +1 -1
- package/lib/Types/Chat.d.ts +1 -0
- package/lib/Types/Chat.d.ts.map +1 -1
- package/lib/Types/Contact.d.ts +2 -0
- package/lib/Types/Contact.d.ts.map +1 -1
- package/lib/Types/Events.d.ts +20 -1
- package/lib/Types/Events.d.ts.map +1 -1
- package/lib/Types/GroupMetadata.d.ts +4 -1
- package/lib/Types/GroupMetadata.d.ts.map +1 -1
- package/lib/Types/Message.d.ts +239 -341
- package/lib/Types/Message.d.ts.map +1 -1
- package/lib/Types/Message.js.map +1 -1
- package/lib/Types/{Newsletter.d.ts → Mex.d.ts} +11 -5
- package/lib/Types/Mex.d.ts.map +1 -0
- package/lib/Types/{Newsletter.js → Mex.js} +9 -3
- package/lib/Types/Mex.js.map +1 -0
- package/lib/Types/Signal.d.ts +12 -1
- package/lib/Types/Signal.d.ts.map +1 -1
- package/lib/Types/Socket.d.ts +126 -148
- package/lib/Types/Socket.d.ts.map +1 -1
- package/lib/Types/State.d.ts +58 -0
- package/lib/Types/State.d.ts.map +1 -1
- package/lib/Types/State.js +43 -0
- package/lib/Types/State.js.map +1 -1
- package/lib/Types/index.d.ts +52 -52
- package/lib/Types/index.d.ts.map +1 -1
- package/lib/Types/index.js +23 -30
- package/lib/Types/index.js.map +1 -1
- package/lib/Utils/auth-utils.d.ts +5 -0
- package/lib/Utils/auth-utils.d.ts.map +1 -1
- package/lib/Utils/auth-utils.js +12 -0
- package/lib/Utils/auth-utils.js.map +1 -1
- package/lib/Utils/browser-utils.d.ts.map +1 -1
- package/lib/Utils/browser-utils.js +1 -0
- package/lib/Utils/browser-utils.js.map +1 -1
- package/lib/Utils/chat-utils.d.ts +34 -4
- package/lib/Utils/chat-utils.d.ts.map +1 -1
- package/lib/Utils/chat-utils.js +80 -20
- package/lib/Utils/chat-utils.js.map +1 -1
- package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
- package/lib/Utils/companion-reg-client-utils.d.ts.map +1 -0
- package/lib/Utils/companion-reg-client-utils.js +35 -0
- package/lib/Utils/companion-reg-client-utils.js.map +1 -0
- package/lib/Utils/crypto.d.ts +12 -12
- package/lib/Utils/crypto.d.ts.map +1 -1
- package/lib/Utils/crypto.js.map +1 -1
- package/lib/Utils/decode-wa-message.d.ts +18 -0
- package/lib/Utils/decode-wa-message.d.ts.map +1 -1
- package/lib/Utils/decode-wa-message.js +34 -0
- package/lib/Utils/decode-wa-message.js.map +1 -1
- package/lib/Utils/event-buffer.d.ts +2 -0
- package/lib/Utils/event-buffer.d.ts.map +1 -1
- package/lib/Utils/event-buffer.js +49 -1
- package/lib/Utils/event-buffer.js.map +1 -1
- package/lib/Utils/generics.d.ts +3 -2
- package/lib/Utils/generics.d.ts.map +1 -1
- package/lib/Utils/generics.js +10 -1
- package/lib/Utils/generics.js.map +1 -1
- package/lib/Utils/history.d.ts +2 -0
- package/lib/Utils/history.d.ts.map +1 -1
- package/lib/Utils/history.js +12 -9
- package/lib/Utils/history.js.map +1 -1
- package/lib/Utils/identity-change-handler.d.ts +7 -0
- package/lib/Utils/identity-change-handler.d.ts.map +1 -1
- package/lib/Utils/identity-change-handler.js +1 -0
- package/lib/Utils/identity-change-handler.js.map +1 -1
- package/lib/Utils/index.d.ts +22 -25
- package/lib/Utils/index.d.ts.map +1 -1
- package/lib/Utils/index.js +2 -4
- package/lib/Utils/index.js.map +1 -1
- package/lib/Utils/link-preview.js +2 -2
- package/lib/Utils/link-preview.js.map +1 -1
- package/lib/Utils/logger.d.ts +1 -2
- package/lib/Utils/logger.d.ts.map +1 -1
- package/lib/Utils/message-retry-manager.d.ts +5 -0
- package/lib/Utils/message-retry-manager.d.ts.map +1 -1
- package/lib/Utils/message-retry-manager.js +40 -0
- package/lib/Utils/message-retry-manager.js.map +1 -1
- package/lib/Utils/messages-media.d.ts +16 -13
- package/lib/Utils/messages-media.d.ts.map +1 -1
- package/lib/Utils/messages-media.js +85 -79
- package/lib/Utils/messages-media.js.map +1 -1
- package/lib/Utils/messages.d.ts +1 -1
- package/lib/Utils/messages.d.ts.map +1 -1
- package/lib/Utils/messages.js +751 -1096
- package/lib/Utils/messages.js.map +1 -1
- package/lib/Utils/noise-handler.d.ts +1 -1
- package/lib/Utils/process-message.d.ts.map +1 -1
- package/lib/Utils/process-message.js +103 -1
- package/lib/Utils/process-message.js.map +1 -1
- package/lib/Utils/signal.d.ts +13 -0
- package/lib/Utils/signal.d.ts.map +1 -1
- package/lib/Utils/signal.js +42 -0
- package/lib/Utils/signal.js.map +1 -1
- package/lib/Utils/sync-action-utils.d.ts.map +1 -1
- package/lib/Utils/sync-action-utils.js +1 -0
- package/lib/Utils/sync-action-utils.js.map +1 -1
- package/lib/Utils/tc-token-utils.d.ts +26 -1
- package/lib/Utils/tc-token-utils.d.ts.map +1 -1
- package/lib/Utils/tc-token-utils.js +151 -5
- package/lib/Utils/tc-token-utils.js.map +1 -1
- package/lib/Utils/validate-connection.d.ts.map +1 -1
- package/lib/Utils/validate-connection.js +12 -2
- package/lib/Utils/validate-connection.js.map +1 -1
- package/lib/WABinary/decode.d.ts +1 -1
- package/lib/WABinary/decode.d.ts.map +1 -1
- package/lib/WABinary/generic-utils.d.ts +1 -3
- package/lib/WABinary/generic-utils.d.ts.map +1 -1
- package/lib/WABinary/generic-utils.js +0 -78
- package/lib/WABinary/generic-utils.js.map +1 -1
- package/lib/WAM/encode.d.ts +1 -1
- package/lib/WAM/encode.d.ts.map +1 -1
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -1
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +26 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -1
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/index.d.ts +1 -0
- package/lib/WAUSync/Protocols/index.d.ts.map +1 -1
- package/lib/WAUSync/Protocols/index.js +1 -0
- package/lib/WAUSync/Protocols/index.js.map +1 -1
- package/lib/WAUSync/USyncQuery.d.ts +1 -0
- package/lib/WAUSync/USyncQuery.d.ts.map +1 -1
- package/lib/WAUSync/USyncQuery.js +6 -2
- package/lib/WAUSync/USyncQuery.js.map +1 -1
- package/lib/WAUSync/USyncUser.d.ts +4 -0
- package/lib/WAUSync/USyncUser.d.ts.map +1 -1
- package/lib/WAUSync/USyncUser.js +8 -0
- package/lib/WAUSync/USyncUser.js.map +1 -1
- package/lib/index.d.ts +3 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +13 -12
- package/lib/index.js.map +1 -1
- package/package.json +93 -23
- package/LICENSE +0 -21
- package/lib/Socket/dugong.d.ts +0 -130
- package/lib/Socket/dugong.d.ts.map +0 -1
- package/lib/Socket/dugong.js +0 -768
- package/lib/Socket/dugong.js.map +0 -1
- package/lib/Types/Newsletter.d.ts.map +0 -1
- package/lib/Types/Newsletter.js.map +0 -1
- package/lib/Utils/kyy-features.d.ts +0 -158
- package/lib/Utils/kyy-features.js +0 -365
- package/lib/Utils/rich-messages.d.ts +0 -277
- package/lib/Utils/rich-messages.d.ts.map +0 -1
- package/lib/Utils/rich-messages.js +0 -1144
- package/lib/Utils/rich-messages.js.map +0 -1
- package/lib/Utils/sticker-pack.d.ts +0 -10
- package/lib/Utils/sticker-pack.js +0 -111
package/lib/Utils/messages.js
CHANGED
|
@@ -1,51 +1,29 @@
|
|
|
1
|
-
import { Boom } from
|
|
2
|
-
import { randomBytes } from
|
|
3
|
-
import { promises as fs } from
|
|
4
|
-
import {} from
|
|
5
|
-
import { proto } from
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} from
|
|
13
|
-
import { WAMessageStatus, WAProto } from "../Types/index.js";
|
|
14
|
-
import {
|
|
15
|
-
isJidGroup,
|
|
16
|
-
isJidNewsletter,
|
|
17
|
-
isJidStatusBroadcast,
|
|
18
|
-
jidNormalizedUser,
|
|
19
|
-
} from "../WABinary/index.js";
|
|
20
|
-
import { sha256 } from "./crypto.js";
|
|
21
|
-
import {
|
|
22
|
-
generateMessageIDV2,
|
|
23
|
-
getKeyAuthor,
|
|
24
|
-
unixTimestampSeconds,
|
|
25
|
-
} from "./generics.js";
|
|
26
|
-
import {
|
|
27
|
-
downloadContentFromMessage,
|
|
28
|
-
encryptedStream,
|
|
29
|
-
generateThumbnail,
|
|
30
|
-
getAudioDuration,
|
|
31
|
-
getAudioWaveform,
|
|
32
|
-
getRawMediaUploadData,
|
|
33
|
-
} from "./messages-media.js";
|
|
34
|
-
import { shouldIncludeReportingToken } from "./reporting-utils.js";
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { randomBytes } from 'crypto';
|
|
3
|
+
import { promises as fs } from 'fs';
|
|
4
|
+
import {} from 'stream';
|
|
5
|
+
import { proto } from '../../WAProto/index.js';
|
|
6
|
+
import { CALL_AUDIO_PREFIX, CALL_VIDEO_PREFIX, MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults/index.js';
|
|
7
|
+
import { WAMessageStatus, WAProto } from '../Types/index.js';
|
|
8
|
+
import { isJidGroup, isJidNewsletter, isJidStatusBroadcast, jidNormalizedUser } from '../WABinary/index.js';
|
|
9
|
+
import { sha256 } from './crypto.js';
|
|
10
|
+
import { generateMessageIDV2, getKeyAuthor, unixTimestampSeconds } from './generics.js';
|
|
11
|
+
import { downloadContentFromMessage, encryptedStream, generateThumbnail, getAudioDuration, getAudioWaveform, getRawMediaUploadData } from './messages-media.js';
|
|
12
|
+
import { shouldIncludeReportingToken } from './reporting-utils.js';
|
|
35
13
|
const MIMETYPE_MAP = {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
14
|
+
image: 'image/jpeg',
|
|
15
|
+
video: 'video/mp4',
|
|
16
|
+
document: 'application/pdf',
|
|
17
|
+
audio: 'audio/ogg; codecs=opus',
|
|
18
|
+
sticker: 'image/webp',
|
|
19
|
+
'product-catalog-image': 'image/jpeg'
|
|
42
20
|
};
|
|
43
21
|
const MessageTypeProto = {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
22
|
+
image: WAProto.Message.ImageMessage,
|
|
23
|
+
video: WAProto.Message.VideoMessage,
|
|
24
|
+
audio: WAProto.Message.AudioMessage,
|
|
25
|
+
sticker: WAProto.Message.StickerMessage,
|
|
26
|
+
document: WAProto.Message.DocumentMessage
|
|
49
27
|
};
|
|
50
28
|
/**
|
|
51
29
|
* Uses a regex to test whether the string contains a URL, and returns the URL if it does.
|
|
@@ -53,310 +31,204 @@ const MessageTypeProto = {
|
|
|
53
31
|
* @returns the URL, eg. https://google.com
|
|
54
32
|
*/
|
|
55
33
|
export const extractUrlFromText = (text) => text.match(URL_REGEX)?.[0];
|
|
56
|
-
export const generateLinkPreviewIfRequired = async (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
// ignore if fails
|
|
68
|
-
logger?.warn({ trace: error.stack }, "url generation failed");
|
|
34
|
+
export const generateLinkPreviewIfRequired = async (text, getUrlInfo, logger) => {
|
|
35
|
+
const url = extractUrlFromText(text);
|
|
36
|
+
if (!!getUrlInfo && url) {
|
|
37
|
+
try {
|
|
38
|
+
const urlInfo = await getUrlInfo(url);
|
|
39
|
+
return urlInfo;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
// ignore if fails
|
|
43
|
+
logger?.warn({ trace: error.stack }, 'url generation failed');
|
|
44
|
+
}
|
|
69
45
|
}
|
|
70
|
-
}
|
|
71
46
|
};
|
|
72
47
|
const assertColor = async (color) => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
48
|
+
let assertedColor;
|
|
49
|
+
if (typeof color === 'number') {
|
|
50
|
+
assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
let hex = color.trim().replace('#', '');
|
|
54
|
+
if (hex.length <= 6) {
|
|
55
|
+
hex = 'FF' + hex.padStart(6, '0');
|
|
56
|
+
}
|
|
57
|
+
assertedColor = parseInt(hex, 16);
|
|
58
|
+
return assertedColor;
|
|
80
59
|
}
|
|
81
|
-
assertedColor = parseInt(hex, 16);
|
|
82
|
-
return assertedColor;
|
|
83
|
-
}
|
|
84
60
|
};
|
|
85
61
|
export const prepareWAMessageMedia = async (message, options) => {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
62
|
+
const logger = options.logger;
|
|
63
|
+
let mediaType;
|
|
64
|
+
for (const key of MEDIA_KEYS) {
|
|
65
|
+
if (key in message) {
|
|
66
|
+
mediaType = key;
|
|
67
|
+
}
|
|
91
68
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
throw new Boom("Invalid media type", { statusCode: 400 });
|
|
95
|
-
}
|
|
96
|
-
const uploadData = {
|
|
97
|
-
...message,
|
|
98
|
-
media: message[mediaType],
|
|
99
|
-
};
|
|
100
|
-
delete uploadData[mediaType];
|
|
101
|
-
// check if cacheable + generate cache key
|
|
102
|
-
const cacheableKey =
|
|
103
|
-
typeof uploadData.media === "object" &&
|
|
104
|
-
"url" in uploadData.media &&
|
|
105
|
-
!!uploadData.media.url &&
|
|
106
|
-
!!options.mediaCache &&
|
|
107
|
-
mediaType + ":" + uploadData.media.url.toString();
|
|
108
|
-
if (mediaType === "document" && !uploadData.fileName) {
|
|
109
|
-
uploadData.fileName = "file";
|
|
110
|
-
}
|
|
111
|
-
if (!uploadData.mimetype) {
|
|
112
|
-
uploadData.mimetype = MIMETYPE_MAP[mediaType];
|
|
113
|
-
}
|
|
114
|
-
if (cacheableKey) {
|
|
115
|
-
const mediaBuff = await options.mediaCache.get(cacheableKey);
|
|
116
|
-
if (mediaBuff) {
|
|
117
|
-
logger?.debug({ cacheableKey }, "got media cache hit");
|
|
118
|
-
const obj = proto.Message.decode(mediaBuff);
|
|
119
|
-
const key = `${mediaType}Message`;
|
|
120
|
-
Object.assign(obj[key], { ...uploadData, media: undefined });
|
|
121
|
-
return obj;
|
|
69
|
+
if (!mediaType) {
|
|
70
|
+
throw new Boom('Invalid media type', { statusCode: 400 });
|
|
122
71
|
}
|
|
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
|
-
filePath,
|
|
149
|
-
mediaType,
|
|
150
|
-
options,
|
|
151
|
-
);
|
|
152
|
-
uploadData.jpegThumbnail = thumbnail;
|
|
153
|
-
if (!uploadData.width && originalImageDimensions) {
|
|
154
|
-
uploadData.width = originalImageDimensions.width;
|
|
155
|
-
uploadData.height = originalImageDimensions.height;
|
|
156
|
-
logger?.debug("set dimensions");
|
|
72
|
+
const uploadData = {
|
|
73
|
+
...message,
|
|
74
|
+
media: message[mediaType]
|
|
75
|
+
};
|
|
76
|
+
delete uploadData[mediaType];
|
|
77
|
+
// check if cacheable + generate cache key
|
|
78
|
+
const cacheableKey = typeof uploadData.media === 'object' &&
|
|
79
|
+
'url' in uploadData.media &&
|
|
80
|
+
!!uploadData.media.url &&
|
|
81
|
+
!!options.mediaCache &&
|
|
82
|
+
mediaType + ':' + uploadData.media.url.toString();
|
|
83
|
+
if (mediaType === 'document' && !uploadData.fileName) {
|
|
84
|
+
uploadData.fileName = 'file';
|
|
85
|
+
}
|
|
86
|
+
if (!uploadData.mimetype) {
|
|
87
|
+
uploadData.mimetype = MIMETYPE_MAP[mediaType];
|
|
88
|
+
}
|
|
89
|
+
if (cacheableKey) {
|
|
90
|
+
const mediaBuff = await options.mediaCache.get(cacheableKey);
|
|
91
|
+
if (mediaBuff) {
|
|
92
|
+
logger?.debug({ cacheableKey }, 'got media cache hit');
|
|
93
|
+
const obj = proto.Message.decode(mediaBuff);
|
|
94
|
+
const key = `${mediaType}Message`;
|
|
95
|
+
Object.assign(obj[key], { ...uploadData, media: undefined });
|
|
96
|
+
return obj;
|
|
157
97
|
}
|
|
158
|
-
logger?.debug("generated thumbnail");
|
|
159
|
-
}
|
|
160
|
-
if (requiresDurationComputation) {
|
|
161
|
-
uploadData.seconds = await getAudioDuration(filePath);
|
|
162
|
-
logger?.debug("computed audio duration");
|
|
163
|
-
}
|
|
164
|
-
if (requiresWaveformProcessing) {
|
|
165
|
-
uploadData.waveform = await getAudioWaveform(filePath, logger);
|
|
166
|
-
logger?.debug("processed waveform");
|
|
167
|
-
}
|
|
168
|
-
if (requiresAudioBackground) {
|
|
169
|
-
uploadData.backgroundArgb = await assertColor(options.backgroundColor);
|
|
170
|
-
logger?.debug("computed backgroundColor audio status");
|
|
171
|
-
}
|
|
172
|
-
} catch (error) {
|
|
173
|
-
logger?.warn({ trace: error.stack }, "failed to obtain extra info");
|
|
174
98
|
}
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
99
|
+
const isNewsletter = !!options.jid && isJidNewsletter(options.jid);
|
|
100
|
+
if (isNewsletter) {
|
|
101
|
+
logger?.info({ key: cacheableKey }, 'Preparing raw media for newsletter');
|
|
102
|
+
const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(uploadData.media, options.mediaTypeOverride || mediaType, logger);
|
|
103
|
+
const fileSha256B64 = fileSha256.toString('base64');
|
|
104
|
+
const { mediaUrl, directPath } = await options.upload(filePath, {
|
|
105
|
+
fileEncSha256B64: fileSha256B64,
|
|
106
|
+
mediaType: mediaType,
|
|
107
|
+
timeoutMs: options.mediaUploadTimeoutMs
|
|
108
|
+
});
|
|
109
|
+
await fs.unlink(filePath);
|
|
110
|
+
const obj = WAProto.Message.fromObject({
|
|
111
|
+
// todo: add more support here
|
|
112
|
+
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
113
|
+
url: mediaUrl,
|
|
114
|
+
directPath,
|
|
115
|
+
fileSha256,
|
|
116
|
+
fileLength,
|
|
117
|
+
...uploadData,
|
|
118
|
+
media: undefined
|
|
119
|
+
})
|
|
120
|
+
});
|
|
121
|
+
if (uploadData.ptv) {
|
|
122
|
+
obj.ptvMessage = obj.videoMessage;
|
|
123
|
+
delete obj.videoMessage;
|
|
124
|
+
}
|
|
125
|
+
if (obj.stickerMessage) {
|
|
126
|
+
obj.stickerMessage.stickerSentTs = Date.now();
|
|
127
|
+
}
|
|
128
|
+
if (cacheableKey) {
|
|
129
|
+
logger?.debug({ cacheableKey }, 'set cache');
|
|
130
|
+
await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
|
|
131
|
+
}
|
|
132
|
+
return obj;
|
|
133
|
+
}
|
|
134
|
+
const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined';
|
|
135
|
+
const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') && typeof uploadData['jpegThumbnail'] === 'undefined';
|
|
136
|
+
const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true && typeof uploadData.waveform === 'undefined';
|
|
137
|
+
const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true;
|
|
138
|
+
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation;
|
|
139
|
+
const { mediaKey, encFilePath, originalFilePath, fileEncSha256, fileSha256, fileLength } = await encryptedStream(uploadData.media, options.mediaTypeOverride || mediaType, {
|
|
140
|
+
logger,
|
|
141
|
+
saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
|
|
142
|
+
opts: options.options
|
|
143
|
+
});
|
|
144
|
+
const fileEncSha256B64 = fileEncSha256.toString('base64');
|
|
145
|
+
const [{ mediaUrl, directPath }] = await Promise.all([
|
|
146
|
+
(async () => {
|
|
147
|
+
const result = await options.upload(encFilePath, {
|
|
148
|
+
fileEncSha256B64,
|
|
149
|
+
mediaType,
|
|
150
|
+
timeoutMs: options.mediaUploadTimeoutMs
|
|
151
|
+
});
|
|
152
|
+
logger?.debug({ mediaType, cacheableKey }, 'uploaded media');
|
|
153
|
+
return result;
|
|
154
|
+
})(),
|
|
155
|
+
(async () => {
|
|
156
|
+
try {
|
|
157
|
+
if (requiresThumbnailComputation) {
|
|
158
|
+
const { thumbnail, originalImageDimensions } = await generateThumbnail(originalFilePath, mediaType, options);
|
|
159
|
+
uploadData.jpegThumbnail = thumbnail;
|
|
160
|
+
if (!uploadData.width && originalImageDimensions) {
|
|
161
|
+
uploadData.width = originalImageDimensions.width;
|
|
162
|
+
uploadData.height = originalImageDimensions.height;
|
|
163
|
+
logger?.debug('set dimensions');
|
|
164
|
+
}
|
|
165
|
+
logger?.debug('generated thumbnail');
|
|
166
|
+
}
|
|
167
|
+
if (requiresDurationComputation) {
|
|
168
|
+
uploadData.seconds = await getAudioDuration(originalFilePath);
|
|
169
|
+
logger?.debug('computed audio duration');
|
|
170
|
+
}
|
|
171
|
+
if (requiresWaveformProcessing) {
|
|
172
|
+
uploadData.waveform = await getAudioWaveform(originalFilePath, logger);
|
|
173
|
+
logger?.debug('processed waveform');
|
|
174
|
+
}
|
|
175
|
+
if (requiresAudioBackground) {
|
|
176
|
+
uploadData.backgroundArgb = await assertColor(options.backgroundColor);
|
|
177
|
+
logger?.debug('computed backgroundColor audio status');
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
logger?.warn({ trace: error.stack }, 'failed to obtain extra info');
|
|
182
|
+
}
|
|
183
|
+
})()
|
|
184
|
+
]).finally(async () => {
|
|
185
|
+
try {
|
|
186
|
+
await fs.unlink(encFilePath);
|
|
187
|
+
if (originalFilePath) {
|
|
188
|
+
await fs.unlink(originalFilePath);
|
|
189
|
+
}
|
|
190
|
+
logger?.debug('removed tmp files');
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
logger?.warn('failed to remove tmp file');
|
|
194
|
+
}
|
|
181
195
|
});
|
|
182
|
-
await fs.unlink(filePath);
|
|
183
196
|
const obj = WAProto.Message.fromObject({
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
197
|
+
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
198
|
+
url: mediaUrl,
|
|
199
|
+
directPath,
|
|
200
|
+
mediaKey,
|
|
201
|
+
fileEncSha256,
|
|
202
|
+
fileSha256,
|
|
203
|
+
fileLength,
|
|
204
|
+
mediaKeyTimestamp: unixTimestampSeconds(),
|
|
205
|
+
...uploadData,
|
|
206
|
+
media: undefined
|
|
207
|
+
})
|
|
193
208
|
});
|
|
194
209
|
if (uploadData.ptv) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
if (obj.stickerMessage) {
|
|
199
|
-
obj.stickerMessage.stickerSentTs = Date.now();
|
|
210
|
+
obj.ptvMessage = obj.videoMessage;
|
|
211
|
+
delete obj.videoMessage;
|
|
200
212
|
}
|
|
201
213
|
if (cacheableKey) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
cacheableKey,
|
|
205
|
-
WAProto.Message.encode(obj).finish(),
|
|
206
|
-
);
|
|
214
|
+
logger?.debug({ cacheableKey }, 'set cache');
|
|
215
|
+
await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
|
|
207
216
|
}
|
|
208
217
|
return obj;
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const requiresAudioBackground =
|
|
220
|
-
options.backgroundColor && mediaType === "audio" && uploadData.ptt === true;
|
|
221
|
-
const requiresOriginalForSomeProcessing =
|
|
222
|
-
requiresDurationComputation ||
|
|
223
|
-
requiresThumbnailComputation ||
|
|
224
|
-
requiresWaveformProcessing;
|
|
225
|
-
const {
|
|
226
|
-
mediaKey,
|
|
227
|
-
encWriteStream,
|
|
228
|
-
bodyPath,
|
|
229
|
-
fileEncSha256,
|
|
230
|
-
fileSha256,
|
|
231
|
-
fileLength,
|
|
232
|
-
didSaveToTmpPath,
|
|
233
|
-
} = await encryptedStream(
|
|
234
|
-
uploadData.media,
|
|
235
|
-
options.mediaTypeOverride || mediaType,
|
|
236
|
-
{
|
|
237
|
-
logger,
|
|
238
|
-
saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
|
|
239
|
-
opts: options.options,
|
|
240
|
-
},
|
|
241
|
-
);
|
|
242
|
-
const fileEncSha256B64 = fileEncSha256.toString("base64");
|
|
243
|
-
const [{ mediaUrl, directPath }] = await Promise.all([
|
|
244
|
-
(async () => {
|
|
245
|
-
const result = await options.upload(encWriteStream, {
|
|
246
|
-
fileEncSha256B64,
|
|
247
|
-
mediaType,
|
|
248
|
-
timeoutMs: options.mediaUploadTimeoutMs,
|
|
249
|
-
});
|
|
250
|
-
logger?.debug({ mediaType, cacheableKey }, "uploaded media");
|
|
251
|
-
return result;
|
|
252
|
-
})(),
|
|
253
|
-
(async () => {
|
|
254
|
-
try {
|
|
255
|
-
if (requiresThumbnailComputation) {
|
|
256
|
-
const { thumbnail, originalImageDimensions } =
|
|
257
|
-
await generateThumbnail(bodyPath, mediaType, options);
|
|
258
|
-
uploadData.jpegThumbnail = thumbnail;
|
|
259
|
-
if (!uploadData.width && originalImageDimensions) {
|
|
260
|
-
uploadData.width = originalImageDimensions.width;
|
|
261
|
-
uploadData.height = originalImageDimensions.height;
|
|
262
|
-
logger?.debug("set dimensions");
|
|
263
|
-
}
|
|
264
|
-
logger?.debug("generated thumbnail");
|
|
265
|
-
}
|
|
266
|
-
if (requiresDurationComputation) {
|
|
267
|
-
try {
|
|
268
|
-
if (bodyPath) {
|
|
269
|
-
uploadData.seconds = await getAudioDuration(bodyPath);
|
|
218
|
+
};
|
|
219
|
+
export const prepareDisappearingMessageSettingContent = (ephemeralExpiration) => {
|
|
220
|
+
ephemeralExpiration = ephemeralExpiration || 0;
|
|
221
|
+
const content = {
|
|
222
|
+
ephemeralMessage: {
|
|
223
|
+
message: {
|
|
224
|
+
protocolMessage: {
|
|
225
|
+
type: WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
|
|
226
|
+
ephemeralExpiration
|
|
227
|
+
}
|
|
270
228
|
}
|
|
271
|
-
} catch (err) {
|
|
272
|
-
uploadData.seconds = 0;
|
|
273
|
-
}
|
|
274
|
-
if (
|
|
275
|
-
typeof uploadData.seconds !== "number" ||
|
|
276
|
-
isNaN(uploadData.seconds)
|
|
277
|
-
) {
|
|
278
|
-
uploadData.seconds = 0;
|
|
279
|
-
}
|
|
280
|
-
logger?.debug("computed audio duration");
|
|
281
|
-
}
|
|
282
|
-
if (requiresWaveformProcessing) {
|
|
283
|
-
try {
|
|
284
|
-
uploadData.waveform = await getAudioWaveform(bodyPath, logger);
|
|
285
|
-
} catch (err) {}
|
|
286
|
-
if (!uploadData.waveform) {
|
|
287
|
-
uploadData.waveform = new Uint8Array([
|
|
288
|
-
0, 99, 0, 99, 0, 99, 0, 99, 88, 99, 0, 99, 0, 55, 0, 99, 0, 99, 0,
|
|
289
|
-
99, 0, 99, 0, 99, 88, 99, 0, 99, 0, 55, 0, 99,
|
|
290
|
-
]);
|
|
291
|
-
}
|
|
292
|
-
logger?.debug("processed waveform");
|
|
293
229
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
options.backgroundColor,
|
|
297
|
-
);
|
|
298
|
-
logger?.debug("computed backgroundColor audio status");
|
|
299
|
-
}
|
|
300
|
-
} catch (error) {
|
|
301
|
-
logger?.warn({ trace: error.stack }, "failed to obtain extra info");
|
|
302
|
-
}
|
|
303
|
-
})(),
|
|
304
|
-
]).finally(async () => {
|
|
305
|
-
try {
|
|
306
|
-
if (!Buffer.isBuffer(encWriteStream)) {
|
|
307
|
-
encWriteStream.destroy?.();
|
|
308
|
-
}
|
|
309
|
-
if (didSaveToTmpPath && bodyPath) {
|
|
310
|
-
await fs.unlink(bodyPath).catch(() => {});
|
|
311
|
-
}
|
|
312
|
-
} catch (error) {
|
|
313
|
-
logger?.warn("failed to remove tmp file");
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
const obj = WAProto.Message.fromObject({
|
|
317
|
-
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
318
|
-
url: mediaUrl,
|
|
319
|
-
directPath,
|
|
320
|
-
mediaKey,
|
|
321
|
-
fileEncSha256,
|
|
322
|
-
fileSha256,
|
|
323
|
-
fileLength,
|
|
324
|
-
mediaKeyTimestamp: unixTimestampSeconds(),
|
|
325
|
-
...uploadData,
|
|
326
|
-
media: undefined,
|
|
327
|
-
}),
|
|
328
|
-
});
|
|
329
|
-
if (uploadData.ptv) {
|
|
330
|
-
obj.ptvMessage = obj.videoMessage;
|
|
331
|
-
delete obj.videoMessage;
|
|
332
|
-
}
|
|
333
|
-
if (obj.stickerMessage) {
|
|
334
|
-
obj.stickerMessage.stickerSentTs = Date.now();
|
|
335
|
-
}
|
|
336
|
-
if (cacheableKey) {
|
|
337
|
-
logger?.debug({ cacheableKey }, "set cache");
|
|
338
|
-
await options.mediaCache.set(
|
|
339
|
-
cacheableKey,
|
|
340
|
-
WAProto.Message.encode(obj).finish(),
|
|
341
|
-
);
|
|
342
|
-
}
|
|
343
|
-
return obj;
|
|
344
|
-
};
|
|
345
|
-
export const prepareDisappearingMessageSettingContent = (
|
|
346
|
-
ephemeralExpiration,
|
|
347
|
-
) => {
|
|
348
|
-
ephemeralExpiration = ephemeralExpiration || 0;
|
|
349
|
-
const content = {
|
|
350
|
-
ephemeralMessage: {
|
|
351
|
-
message: {
|
|
352
|
-
protocolMessage: {
|
|
353
|
-
type: WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
|
|
354
|
-
ephemeralExpiration,
|
|
355
|
-
},
|
|
356
|
-
},
|
|
357
|
-
},
|
|
358
|
-
};
|
|
359
|
-
return WAProto.Message.fromObject(content);
|
|
230
|
+
};
|
|
231
|
+
return WAProto.Message.fromObject(content);
|
|
360
232
|
};
|
|
361
233
|
/**
|
|
362
234
|
* Generate forwarded message content like WA does
|
|
@@ -364,579 +236,398 @@ export const prepareDisappearingMessageSettingContent = (
|
|
|
364
236
|
* @param options.forceForward will show the message as forwarded even if it is from you
|
|
365
237
|
*/
|
|
366
238
|
export const generateForwardMessageContent = (message, forceForward) => {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
239
|
+
let content = message.message;
|
|
240
|
+
if (!content) {
|
|
241
|
+
throw new Boom('no content in message', { statusCode: 400 });
|
|
242
|
+
}
|
|
243
|
+
// hacky copy
|
|
244
|
+
content = normalizeMessageContent(content);
|
|
245
|
+
content = proto.Message.decode(proto.Message.encode(content).finish());
|
|
246
|
+
let key = Object.keys(content)[0];
|
|
247
|
+
let score = content?.[key]?.contextInfo?.forwardingScore || 0;
|
|
248
|
+
score += message.key.fromMe && !forceForward ? 0 : 1;
|
|
249
|
+
if (key === 'conversation') {
|
|
250
|
+
content.extendedTextMessage = { text: content[key] };
|
|
251
|
+
delete content.conversation;
|
|
252
|
+
key = 'extendedTextMessage';
|
|
253
|
+
}
|
|
254
|
+
const key_ = content?.[key];
|
|
255
|
+
if (score > 0) {
|
|
256
|
+
key_.contextInfo = { forwardingScore: score, isForwarded: true };
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
key_.contextInfo = {};
|
|
260
|
+
}
|
|
261
|
+
return content;
|
|
389
262
|
};
|
|
390
263
|
export const hasNonNullishProperty = (message, key) => {
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
message[key] !== undefined
|
|
397
|
-
);
|
|
264
|
+
return (typeof message === 'object' &&
|
|
265
|
+
message !== null &&
|
|
266
|
+
key in message &&
|
|
267
|
+
message[key] !== null &&
|
|
268
|
+
message[key] !== undefined);
|
|
398
269
|
};
|
|
399
270
|
function hasOptionalProperty(obj, key) {
|
|
400
|
-
|
|
401
|
-
typeof obj === "object" && obj !== null && key in obj && obj[key] !== null
|
|
402
|
-
);
|
|
271
|
+
return typeof obj === 'object' && obj !== null && key in obj && obj[key] !== null;
|
|
403
272
|
}
|
|
404
273
|
export const generateWAMessageContent = async (message, options) => {
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
274
|
+
var _a, _b;
|
|
275
|
+
let m = {};
|
|
276
|
+
if (hasNonNullishProperty(message, 'text')) {
|
|
277
|
+
const extContent = { text: message.text };
|
|
278
|
+
let urlInfo = message.linkPreview;
|
|
279
|
+
if (typeof urlInfo === 'undefined') {
|
|
280
|
+
urlInfo = await generateLinkPreviewIfRequired(message.text, options.getUrlInfo, options.logger);
|
|
281
|
+
}
|
|
282
|
+
if (urlInfo) {
|
|
283
|
+
extContent.matchedText = urlInfo['matched-text'];
|
|
284
|
+
extContent.jpegThumbnail = urlInfo.jpegThumbnail;
|
|
285
|
+
extContent.description = urlInfo.description;
|
|
286
|
+
extContent.title = urlInfo.title;
|
|
287
|
+
extContent.previewType = 0;
|
|
288
|
+
const img = urlInfo.highQualityThumbnail;
|
|
289
|
+
if (img) {
|
|
290
|
+
extContent.thumbnailDirectPath = img.directPath;
|
|
291
|
+
extContent.mediaKey = img.mediaKey;
|
|
292
|
+
extContent.mediaKeyTimestamp = img.mediaKeyTimestamp;
|
|
293
|
+
extContent.thumbnailWidth = img.width;
|
|
294
|
+
extContent.thumbnailHeight = img.height;
|
|
295
|
+
extContent.thumbnailSha256 = img.fileSha256;
|
|
296
|
+
extContent.thumbnailEncSha256 = img.fileEncSha256;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (options.backgroundColor) {
|
|
300
|
+
extContent.backgroundArgb = await assertColor(options.backgroundColor);
|
|
301
|
+
}
|
|
302
|
+
if (options.font) {
|
|
303
|
+
extContent.font = options.font;
|
|
304
|
+
}
|
|
305
|
+
m.extendedTextMessage = extContent;
|
|
433
306
|
}
|
|
434
|
-
if (
|
|
435
|
-
|
|
307
|
+
else if (hasNonNullishProperty(message, 'contacts')) {
|
|
308
|
+
const contactLen = message.contacts.contacts.length;
|
|
309
|
+
if (!contactLen) {
|
|
310
|
+
throw new Boom('require atleast 1 contact', { statusCode: 400 });
|
|
311
|
+
}
|
|
312
|
+
if (contactLen === 1) {
|
|
313
|
+
m.contactMessage = WAProto.Message.ContactMessage.create(message.contacts.contacts[0]);
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
m.contactsArrayMessage = WAProto.Message.ContactsArrayMessage.create(message.contacts);
|
|
317
|
+
}
|
|
436
318
|
}
|
|
437
|
-
if (
|
|
438
|
-
|
|
319
|
+
else if (hasNonNullishProperty(message, 'location')) {
|
|
320
|
+
m.locationMessage = WAProto.Message.LocationMessage.create(message.location);
|
|
439
321
|
}
|
|
440
|
-
if (
|
|
441
|
-
|
|
322
|
+
else if (hasNonNullishProperty(message, 'react')) {
|
|
323
|
+
if (!message.react.senderTimestampMs) {
|
|
324
|
+
message.react.senderTimestampMs = Date.now();
|
|
325
|
+
}
|
|
326
|
+
m.reactionMessage = WAProto.Message.ReactionMessage.create(message.react);
|
|
442
327
|
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
328
|
+
else if (hasNonNullishProperty(message, 'delete')) {
|
|
329
|
+
m.protocolMessage = {
|
|
330
|
+
key: message.delete,
|
|
331
|
+
type: WAProto.Message.ProtocolMessage.Type.REVOKE
|
|
332
|
+
};
|
|
448
333
|
}
|
|
449
|
-
if (
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
334
|
+
else if (hasNonNullishProperty(message, 'forward')) {
|
|
335
|
+
m = generateForwardMessageContent(message.forward, message.force);
|
|
336
|
+
}
|
|
337
|
+
else if (hasNonNullishProperty(message, 'disappearingMessagesInChat')) {
|
|
338
|
+
const exp = typeof message.disappearingMessagesInChat === 'boolean'
|
|
339
|
+
? message.disappearingMessagesInChat
|
|
340
|
+
? WA_DEFAULT_EPHEMERAL
|
|
341
|
+
: 0
|
|
342
|
+
: message.disappearingMessagesInChat;
|
|
343
|
+
m = prepareDisappearingMessageSettingContent(exp);
|
|
344
|
+
}
|
|
345
|
+
else if (hasNonNullishProperty(message, 'groupInvite')) {
|
|
346
|
+
m.groupInviteMessage = {};
|
|
347
|
+
m.groupInviteMessage.inviteCode = message.groupInvite.inviteCode;
|
|
348
|
+
m.groupInviteMessage.inviteExpiration = message.groupInvite.inviteExpiration;
|
|
349
|
+
m.groupInviteMessage.caption = message.groupInvite.text;
|
|
350
|
+
m.groupInviteMessage.groupJid = message.groupInvite.jid;
|
|
351
|
+
m.groupInviteMessage.groupName = message.groupInvite.subject;
|
|
352
|
+
//TODO: use built-in interface and get disappearing mode info etc.
|
|
353
|
+
//TODO: cache / use store!?
|
|
354
|
+
if (options.getProfilePicUrl) {
|
|
355
|
+
const pfpUrl = await options.getProfilePicUrl(message.groupInvite.jid, 'preview');
|
|
356
|
+
if (pfpUrl) {
|
|
357
|
+
const resp = await fetch(pfpUrl, { method: 'GET', dispatcher: options?.options?.dispatcher });
|
|
358
|
+
if (resp.ok) {
|
|
359
|
+
const buf = Buffer.from(await resp.arrayBuffer());
|
|
360
|
+
m.groupInviteMessage.jpegThumbnail = buf;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
457
364
|
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
365
|
+
else if (hasNonNullishProperty(message, 'pin')) {
|
|
366
|
+
m.pinInChatMessage = {};
|
|
367
|
+
m.messageContextInfo = {};
|
|
368
|
+
m.pinInChatMessage.key = message.pin;
|
|
369
|
+
m.pinInChatMessage.type = message.type;
|
|
370
|
+
m.pinInChatMessage.senderTimestampMs = Date.now();
|
|
371
|
+
m.messageContextInfo.messageAddOnDurationInSecs = message.type === 1 ? message.time || 86400 : 0;
|
|
372
|
+
}
|
|
373
|
+
else if (hasNonNullishProperty(message, 'buttonReply')) {
|
|
374
|
+
switch (message.type) {
|
|
375
|
+
case 'template':
|
|
376
|
+
m.templateButtonReplyMessage = {
|
|
377
|
+
selectedDisplayText: message.buttonReply.displayText,
|
|
378
|
+
selectedId: message.buttonReply.id,
|
|
379
|
+
selectedIndex: message.buttonReply.index
|
|
380
|
+
};
|
|
381
|
+
break;
|
|
382
|
+
case 'plain':
|
|
383
|
+
m.buttonsResponseMessage = {
|
|
384
|
+
selectedButtonId: message.buttonReply.id,
|
|
385
|
+
selectedDisplayText: message.buttonReply.displayText,
|
|
386
|
+
type: proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT
|
|
387
|
+
};
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
465
390
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
? WA_DEFAULT_EPHEMERAL
|
|
479
|
-
: 0
|
|
480
|
-
: message.disappearingMessagesInChat;
|
|
481
|
-
m = prepareDisappearingMessageSettingContent(exp);
|
|
482
|
-
} else if (hasNonNullishProperty(message, "groupInvite")) {
|
|
483
|
-
m.groupInviteMessage = {};
|
|
484
|
-
m.groupInviteMessage.inviteCode = message.groupInvite.inviteCode;
|
|
485
|
-
m.groupInviteMessage.inviteExpiration =
|
|
486
|
-
message.groupInvite.inviteExpiration;
|
|
487
|
-
m.groupInviteMessage.caption = message.groupInvite.text;
|
|
488
|
-
m.groupInviteMessage.groupJid = message.groupInvite.jid;
|
|
489
|
-
m.groupInviteMessage.groupName = message.groupInvite.subject;
|
|
490
|
-
//TODO: use built-in interface and get disappearing mode info etc.
|
|
491
|
-
//TODO: cache / use store!?
|
|
492
|
-
if (options.getProfilePicUrl) {
|
|
493
|
-
const pfpUrl = await options.getProfilePicUrl(
|
|
494
|
-
message.groupInvite.jid,
|
|
495
|
-
"preview",
|
|
496
|
-
);
|
|
497
|
-
if (pfpUrl) {
|
|
498
|
-
const resp = await fetch(pfpUrl, {
|
|
499
|
-
method: "GET",
|
|
500
|
-
dispatcher: options?.options?.dispatcher,
|
|
391
|
+
else if (hasOptionalProperty(message, 'ptv') && message.ptv) {
|
|
392
|
+
const { videoMessage } = await prepareWAMessageMedia({ video: message.video }, options);
|
|
393
|
+
m.ptvMessage = videoMessage;
|
|
394
|
+
}
|
|
395
|
+
else if (hasNonNullishProperty(message, 'product')) {
|
|
396
|
+
const { imageMessage } = await prepareWAMessageMedia({ image: message.product.productImage }, options);
|
|
397
|
+
m.productMessage = WAProto.Message.ProductMessage.create({
|
|
398
|
+
...message,
|
|
399
|
+
product: {
|
|
400
|
+
...message.product,
|
|
401
|
+
productImage: imageMessage
|
|
402
|
+
}
|
|
501
403
|
});
|
|
502
|
-
if (resp.ok) {
|
|
503
|
-
const buf = Buffer.from(await resp.arrayBuffer());
|
|
504
|
-
m.groupInviteMessage.jpegThumbnail = buf;
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
404
|
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
405
|
+
else if (hasNonNullishProperty(message, 'listReply')) {
|
|
406
|
+
m.listResponseMessage = { ...message.listReply };
|
|
407
|
+
}
|
|
408
|
+
else if (hasNonNullishProperty(message, 'event')) {
|
|
409
|
+
m.eventMessage = {};
|
|
410
|
+
const startTime = Math.floor(message.event.startDate.getTime() / 1000);
|
|
411
|
+
if (message.event.call && options.getCallLink) {
|
|
412
|
+
const token = await options.getCallLink(message.event.call, { startTime });
|
|
413
|
+
m.eventMessage.joinLink = (message.event.call === 'audio' ? CALL_AUDIO_PREFIX : CALL_VIDEO_PREFIX) + token;
|
|
414
|
+
}
|
|
415
|
+
m.messageContextInfo = {
|
|
416
|
+
// encKey
|
|
417
|
+
messageSecret: message.event.messageSecret || randomBytes(32)
|
|
418
|
+
};
|
|
419
|
+
m.eventMessage.name = message.event.name;
|
|
420
|
+
m.eventMessage.description = message.event.description;
|
|
421
|
+
m.eventMessage.startTime = startTime;
|
|
422
|
+
m.eventMessage.endTime = message.event.endDate ? message.event.endDate.getTime() / 1000 : undefined;
|
|
423
|
+
m.eventMessage.isCanceled = message.event.isCancelled ?? false;
|
|
424
|
+
m.eventMessage.extraGuestsAllowed = message.event.extraGuestsAllowed;
|
|
425
|
+
m.eventMessage.isScheduleCall = message.event.isScheduleCall ?? false;
|
|
426
|
+
m.eventMessage.location = message.event.location;
|
|
427
|
+
}
|
|
428
|
+
else if (hasNonNullishProperty(message, 'poll')) {
|
|
429
|
+
(_a = message.poll).selectableCount || (_a.selectableCount = 0);
|
|
430
|
+
(_b = message.poll).toAnnouncementGroup || (_b.toAnnouncementGroup = false);
|
|
431
|
+
if (!Array.isArray(message.poll.values)) {
|
|
432
|
+
throw new Boom('Invalid poll values', { statusCode: 400 });
|
|
433
|
+
}
|
|
434
|
+
if (message.poll.selectableCount < 0 || message.poll.selectableCount > message.poll.values.length) {
|
|
435
|
+
throw new Boom(`poll.selectableCount in poll should be >= 0 and <= ${message.poll.values.length}`, {
|
|
436
|
+
statusCode: 400
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
m.messageContextInfo = {
|
|
440
|
+
// encKey
|
|
441
|
+
messageSecret: message.poll.messageSecret || randomBytes(32)
|
|
523
442
|
};
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
selectedDisplayText: message.buttonReply.displayText,
|
|
529
|
-
type: proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT,
|
|
443
|
+
const pollCreationMessage = {
|
|
444
|
+
name: message.poll.name,
|
|
445
|
+
selectableOptionsCount: message.poll.selectableCount,
|
|
446
|
+
options: message.poll.values.map(optionName => ({ optionName }))
|
|
530
447
|
};
|
|
531
|
-
|
|
448
|
+
if (message.poll.toAnnouncementGroup) {
|
|
449
|
+
// poll v2 is for community announcement groups (single select and multiple)
|
|
450
|
+
m.pollCreationMessageV2 = pollCreationMessage;
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
if (message.poll.selectableCount === 1) {
|
|
454
|
+
//poll v3 is for single select polls
|
|
455
|
+
m.pollCreationMessageV3 = pollCreationMessage;
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
// poll for multiple choice polls
|
|
459
|
+
m.pollCreationMessage = pollCreationMessage;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
532
462
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
m.ptvMessage = videoMessage;
|
|
539
|
-
} else if (hasNonNullishProperty(message, "product")) {
|
|
540
|
-
const { imageMessage } = await prepareWAMessageMedia(
|
|
541
|
-
{ image: message.product.productImage },
|
|
542
|
-
options,
|
|
543
|
-
);
|
|
544
|
-
m.productMessage = WAProto.Message.ProductMessage.create({
|
|
545
|
-
...message,
|
|
546
|
-
product: {
|
|
547
|
-
...message.product,
|
|
548
|
-
productImage: imageMessage,
|
|
549
|
-
},
|
|
550
|
-
});
|
|
551
|
-
} else if (hasNonNullishProperty(message, "listReply")) {
|
|
552
|
-
m.listResponseMessage = { ...message.listReply };
|
|
553
|
-
} else if (hasNonNullishProperty(message, "event")) {
|
|
554
|
-
m.eventMessage = {};
|
|
555
|
-
const startTime = Math.floor(message.event.startDate.getTime() / 1000);
|
|
556
|
-
if (message.event.call && options.getCallLink) {
|
|
557
|
-
const token = await options.getCallLink(message.event.call, {
|
|
558
|
-
startTime,
|
|
559
|
-
});
|
|
560
|
-
m.eventMessage.joinLink =
|
|
561
|
-
(message.event.call === "audio"
|
|
562
|
-
? CALL_AUDIO_PREFIX
|
|
563
|
-
: CALL_VIDEO_PREFIX) + token;
|
|
463
|
+
else if (hasNonNullishProperty(message, 'album')) {
|
|
464
|
+
m.albumMessage = {
|
|
465
|
+
expectedImageCount: message.album.expectedImageCount,
|
|
466
|
+
expectedVideoCount: message.album.expectedVideoCount
|
|
467
|
+
};
|
|
564
468
|
}
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
m.eventMessage.name = message.event.name;
|
|
570
|
-
m.eventMessage.description = message.event.description;
|
|
571
|
-
m.eventMessage.startTime = startTime;
|
|
572
|
-
m.eventMessage.endTime = message.event.endDate
|
|
573
|
-
? message.event.endDate.getTime() / 1000
|
|
574
|
-
: undefined;
|
|
575
|
-
m.eventMessage.isCanceled = message.event.isCancelled ?? false;
|
|
576
|
-
m.eventMessage.extraGuestsAllowed = message.event.extraGuestsAllowed;
|
|
577
|
-
m.eventMessage.isScheduleCall = message.event.isScheduleCall ?? false;
|
|
578
|
-
m.eventMessage.location = message.event.location;
|
|
579
|
-
} else if (hasNonNullishProperty(message, "poll")) {
|
|
580
|
-
(_a = message.poll).selectableCount || (_a.selectableCount = 0);
|
|
581
|
-
(_b = message.poll).toAnnouncementGroup || (_b.toAnnouncementGroup = false);
|
|
582
|
-
if (!Array.isArray(message.poll.values)) {
|
|
583
|
-
throw new Boom("Invalid poll values", { statusCode: 400 });
|
|
469
|
+
else if (hasNonNullishProperty(message, 'sharePhoneNumber')) {
|
|
470
|
+
m.protocolMessage = {
|
|
471
|
+
type: proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER
|
|
472
|
+
};
|
|
584
473
|
}
|
|
585
|
-
if (
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
) {
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
474
|
+
else if (hasNonNullishProperty(message, 'requestPhoneNumber')) {
|
|
475
|
+
m.requestPhoneNumberMessage = {};
|
|
476
|
+
}
|
|
477
|
+
else if (hasNonNullishProperty(message, 'limitSharing')) {
|
|
478
|
+
m.protocolMessage = {
|
|
479
|
+
type: proto.Message.ProtocolMessage.Type.LIMIT_SHARING,
|
|
480
|
+
limitSharing: {
|
|
481
|
+
sharingLimited: message.limitSharing === true,
|
|
482
|
+
trigger: 1,
|
|
483
|
+
limitSharingSettingTimestamp: Date.now(),
|
|
484
|
+
initiatedByMe: true
|
|
485
|
+
}
|
|
486
|
+
};
|
|
595
487
|
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
messageSecret: message.poll.messageSecret || randomBytes(32),
|
|
599
|
-
};
|
|
600
|
-
const pollCreationMessage = {
|
|
601
|
-
name: message.poll.name,
|
|
602
|
-
selectableOptionsCount: message.poll.selectableCount,
|
|
603
|
-
options: message.poll.values.map((optionName) => ({ optionName })),
|
|
604
|
-
};
|
|
605
|
-
if (message.poll.toAnnouncementGroup) {
|
|
606
|
-
// poll v2 is for community announcement groups (single select and multiple)
|
|
607
|
-
m.pollCreationMessageV2 = pollCreationMessage;
|
|
608
|
-
} else {
|
|
609
|
-
if (message.poll.selectableCount === 1) {
|
|
610
|
-
//poll v3 is for single select polls
|
|
611
|
-
m.pollCreationMessageV3 = pollCreationMessage;
|
|
612
|
-
} else {
|
|
613
|
-
// poll for multiple choice polls
|
|
614
|
-
m.pollCreationMessage = pollCreationMessage;
|
|
615
|
-
}
|
|
488
|
+
else {
|
|
489
|
+
m = await prepareWAMessageMedia(message, options);
|
|
616
490
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
throw new Boom("Sticker pack must contain at least one sticker", {
|
|
639
|
-
statusCode: 400,
|
|
640
|
-
});
|
|
641
|
-
if (stickers.length > 60)
|
|
642
|
-
throw new Boom("Sticker pack exceeds the maximum limit of 60 stickers", {
|
|
643
|
-
statusCode: 400,
|
|
644
|
-
});
|
|
645
|
-
const stickerPackId = packId || generateMessageIDV2();
|
|
646
|
-
const [_sharp] = await Promise.all([import("sharp").catch(() => null)]);
|
|
647
|
-
if (!_sharp)
|
|
648
|
-
throw new Boom("sharp is required for sticker pack generation");
|
|
649
|
-
const sharpLib = _sharp.default || _sharp;
|
|
650
|
-
const isWebP = (buf) =>
|
|
651
|
-
buf.length >= 12 &&
|
|
652
|
-
buf[0] === 0x52 &&
|
|
653
|
-
buf[1] === 0x49 &&
|
|
654
|
-
buf[2] === 0x46 &&
|
|
655
|
-
buf[3] === 0x46 &&
|
|
656
|
-
buf[8] === 0x57 &&
|
|
657
|
-
buf[9] === 0x45 &&
|
|
658
|
-
buf[10] === 0x42 &&
|
|
659
|
-
buf[11] === 0x50;
|
|
660
|
-
const isAnimatedWebP = (buf) => {
|
|
661
|
-
if (!isWebP(buf)) return false;
|
|
662
|
-
let o = 12;
|
|
663
|
-
while (o < buf.length - 8) {
|
|
664
|
-
const tag = buf.toString("ascii", o, o + 4);
|
|
665
|
-
const sz = buf.readUInt32LE(o + 4);
|
|
666
|
-
if (tag === "VP8X" && buf[o + 8] & 0x02) return true;
|
|
667
|
-
if (tag === "ANIM" || tag === "ANMF") return true;
|
|
668
|
-
o += 8 + sz + (sz % 2);
|
|
669
|
-
}
|
|
670
|
-
return false;
|
|
671
|
-
};
|
|
672
|
-
const { Readable } = await import("stream");
|
|
673
|
-
const toReadable = (buf) => {
|
|
674
|
-
const r = new Readable({ read: () => {} });
|
|
675
|
-
r.push(buf);
|
|
676
|
-
r.push(null);
|
|
677
|
-
return r;
|
|
678
|
-
};
|
|
679
|
-
const toBuffer = async (stream) => {
|
|
680
|
-
const c = [];
|
|
681
|
-
for await (const ch of stream) c.push(ch);
|
|
682
|
-
return Buffer.concat(c);
|
|
683
|
-
};
|
|
684
|
-
const getStreamBuf = async (item) => {
|
|
685
|
-
if (Buffer.isBuffer(item)) return item;
|
|
686
|
-
if (item?.stream) return toBuffer(item.stream);
|
|
687
|
-
const u = item?.url?.toString?.() || item?.url;
|
|
688
|
-
if (u?.startsWith("data:")) return Buffer.from(u.split(",")[1], "base64");
|
|
689
|
-
const resp = await fetch(u);
|
|
690
|
-
return Buffer.from(await resp.arrayBuffer());
|
|
691
|
-
};
|
|
692
|
-
const zipData = {};
|
|
693
|
-
const stickerMeta = await Promise.all(
|
|
694
|
-
stickers.map(async (s, i) => {
|
|
695
|
-
const raw = await getStreamBuf(s.data || s.sticker);
|
|
696
|
-
let webp;
|
|
697
|
-
let animated = false;
|
|
698
|
-
if (isWebP(raw)) {
|
|
699
|
-
webp = raw;
|
|
700
|
-
animated = isAnimatedWebP(raw);
|
|
701
|
-
} else {
|
|
702
|
-
webp = await sharpLib(raw).webp().toBuffer();
|
|
491
|
+
if (hasOptionalProperty(message, 'viewOnce') && !!message.viewOnce) {
|
|
492
|
+
m = { viewOnceMessage: { message: m } };
|
|
493
|
+
}
|
|
494
|
+
if ((hasOptionalProperty(message, 'mentions') && message.mentions?.length) ||
|
|
495
|
+
(hasOptionalProperty(message, 'mentionAll') && message.mentionAll)) {
|
|
496
|
+
const messageType = Object.keys(m)[0];
|
|
497
|
+
const key = m[messageType];
|
|
498
|
+
if (key && 'contextInfo' in key) {
|
|
499
|
+
key.contextInfo = key.contextInfo || {};
|
|
500
|
+
if (message.mentions?.length) {
|
|
501
|
+
key.contextInfo.mentionedJid = message.mentions;
|
|
502
|
+
}
|
|
503
|
+
if (message.mentionAll) {
|
|
504
|
+
key.contextInfo.nonJidMentions = 1;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
else if (key) {
|
|
508
|
+
key.contextInfo = {
|
|
509
|
+
mentionedJid: message.mentions,
|
|
510
|
+
nonJidMentions: message.mentionAll ? 1 : 0
|
|
511
|
+
};
|
|
703
512
|
}
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
emojis: s.emojis || [],
|
|
714
|
-
accessibilityLabel: s.accessibilityLabel || "",
|
|
513
|
+
}
|
|
514
|
+
if (hasOptionalProperty(message, 'edit')) {
|
|
515
|
+
m = {
|
|
516
|
+
protocolMessage: {
|
|
517
|
+
key: message.edit,
|
|
518
|
+
editedMessage: m,
|
|
519
|
+
timestampMs: Date.now(),
|
|
520
|
+
type: WAProto.Message.ProtocolMessage.Type.MESSAGE_EDIT
|
|
521
|
+
}
|
|
715
522
|
};
|
|
716
|
-
}),
|
|
717
|
-
);
|
|
718
|
-
const coverRaw = await getStreamBuf(cover);
|
|
719
|
-
const coverWebp = isWebP(coverRaw)
|
|
720
|
-
? coverRaw
|
|
721
|
-
: await sharpLib(coverRaw).webp().toBuffer();
|
|
722
|
-
const trayIconFileName = `${stickerPackId}.webp`;
|
|
723
|
-
zipData[trayIconFileName] = [new Uint8Array(coverWebp), { level: 0 }];
|
|
724
|
-
const zipBuffer = await new Promise((res, rej) =>
|
|
725
|
-
zip(zipData, (e, d) => (e ? rej(e) : res(Buffer.from(d)))),
|
|
726
|
-
);
|
|
727
|
-
const stickerPackUpload = await encryptedStream(zipBuffer, "sticker-pack", {
|
|
728
|
-
logger: options.logger,
|
|
729
|
-
opts: options.options,
|
|
730
|
-
});
|
|
731
|
-
const stickerPackResult = await options.upload(
|
|
732
|
-
stickerPackUpload.encWriteStream,
|
|
733
|
-
{
|
|
734
|
-
fileEncSha256B64: stickerPackUpload.fileEncSha256.toString("base64"),
|
|
735
|
-
mediaType: "sticker-pack",
|
|
736
|
-
timeoutMs: options.mediaUploadTimeoutMs,
|
|
737
|
-
},
|
|
738
|
-
);
|
|
739
|
-
m.stickerPackMessage = {
|
|
740
|
-
name,
|
|
741
|
-
publisher,
|
|
742
|
-
stickerPackId,
|
|
743
|
-
packDescription: description,
|
|
744
|
-
stickerPackOrigin:
|
|
745
|
-
WAProto.Message.StickerPackMessage.StickerPackOrigin.THIRD_PARTY,
|
|
746
|
-
stickerPackSize: zipBuffer.length,
|
|
747
|
-
stickers: stickerMeta,
|
|
748
|
-
fileSha256: stickerPackUpload.fileSha256,
|
|
749
|
-
fileEncSha256: stickerPackUpload.fileEncSha256,
|
|
750
|
-
mediaKey: stickerPackUpload.mediaKey,
|
|
751
|
-
directPath: stickerPackResult.directPath,
|
|
752
|
-
fileLength: stickerPackUpload.fileLength,
|
|
753
|
-
mediaKeyTimestamp: unixTimestampSeconds(),
|
|
754
|
-
trayIconFileName,
|
|
755
|
-
};
|
|
756
|
-
try {
|
|
757
|
-
const thumbBuf = await sharpLib(coverRaw)
|
|
758
|
-
.resize(252, 252)
|
|
759
|
-
.jpeg()
|
|
760
|
-
.toBuffer();
|
|
761
|
-
const thumbUpload = await encryptedStream(
|
|
762
|
-
thumbBuf,
|
|
763
|
-
"thumbnail-sticker-pack",
|
|
764
|
-
{
|
|
765
|
-
logger: options.logger,
|
|
766
|
-
opts: options.options,
|
|
767
|
-
mediaKey: stickerPackUpload.mediaKey,
|
|
768
|
-
},
|
|
769
|
-
);
|
|
770
|
-
const thumbResult = await options.upload(thumbUpload.encWriteStream, {
|
|
771
|
-
fileEncSha256B64: thumbUpload.fileEncSha256.toString("base64"),
|
|
772
|
-
mediaType: "thumbnail-sticker-pack",
|
|
773
|
-
timeoutMs: options.mediaUploadTimeoutMs,
|
|
774
|
-
});
|
|
775
|
-
Object.assign(m.stickerPackMessage, {
|
|
776
|
-
thumbnailDirectPath: thumbResult.directPath,
|
|
777
|
-
thumbnailSha256: thumbUpload.fileSha256,
|
|
778
|
-
thumbnailEncSha256: thumbUpload.fileEncSha256,
|
|
779
|
-
thumbnailHeight: 252,
|
|
780
|
-
thumbnailWidth: 252,
|
|
781
|
-
imageDataHash: sha256(thumbBuf).toString("base64"),
|
|
782
|
-
});
|
|
783
|
-
} catch (e) {
|
|
784
|
-
options.logger?.warn?.(`Sticker pack thumbnail failed: ${e?.message}`);
|
|
785
523
|
}
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
}
|
|
796
|
-
if (
|
|
797
|
-
(hasOptionalProperty(message, "mentions") && message.mentions?.length) ||
|
|
798
|
-
(hasOptionalProperty(message, "mentionAll") && message.mentionAll)
|
|
799
|
-
) {
|
|
800
|
-
const messageType = Object.keys(m)[0];
|
|
801
|
-
const key = m[messageType];
|
|
802
|
-
if (key && "contextInfo" in key) {
|
|
803
|
-
key.contextInfo = key.contextInfo || {};
|
|
804
|
-
if (message.mentions?.length) {
|
|
805
|
-
key.contextInfo.mentionedJid = message.mentions;
|
|
806
|
-
}
|
|
807
|
-
if (message.mentionAll) {
|
|
808
|
-
key.contextInfo.nonJidMentions = 1;
|
|
809
|
-
}
|
|
810
|
-
} else if (key) {
|
|
811
|
-
key.contextInfo = {
|
|
812
|
-
mentionedJid: message.mentions,
|
|
813
|
-
nonJidMentions: message.mentionAll ? 1 : 0,
|
|
814
|
-
};
|
|
524
|
+
if (hasOptionalProperty(message, 'contextInfo') && !!message.contextInfo) {
|
|
525
|
+
const messageType = Object.keys(m)[0];
|
|
526
|
+
const key = m[messageType];
|
|
527
|
+
if ('contextInfo' in key && !!key.contextInfo) {
|
|
528
|
+
key.contextInfo = { ...key.contextInfo, ...message.contextInfo };
|
|
529
|
+
}
|
|
530
|
+
else if (key) {
|
|
531
|
+
key.contextInfo = message.contextInfo;
|
|
532
|
+
}
|
|
815
533
|
}
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
},
|
|
825
|
-
};
|
|
826
|
-
}
|
|
827
|
-
if (hasOptionalProperty(message, "contextInfo") && !!message.contextInfo) {
|
|
828
|
-
const messageType = Object.keys(m)[0];
|
|
829
|
-
const key = m[messageType];
|
|
830
|
-
if ("contextInfo" in key && !!key.contextInfo) {
|
|
831
|
-
key.contextInfo = { ...key.contextInfo, ...message.contextInfo };
|
|
832
|
-
} else if (key) {
|
|
833
|
-
key.contextInfo = message.contextInfo;
|
|
534
|
+
if (hasOptionalProperty(message, 'albumParentKey') && !!message.albumParentKey) {
|
|
535
|
+
m.messageContextInfo = {
|
|
536
|
+
...m.messageContextInfo,
|
|
537
|
+
messageAssociation: {
|
|
538
|
+
associationType: WAProto.MessageAssociation.AssociationType.MEDIA_ALBUM,
|
|
539
|
+
parentMessageKey: message.albumParentKey
|
|
540
|
+
}
|
|
541
|
+
};
|
|
834
542
|
}
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
543
|
+
if (shouldIncludeReportingToken(m)) {
|
|
544
|
+
m.messageContextInfo = m.messageContextInfo || {};
|
|
545
|
+
if (!m.messageContextInfo.messageSecret) {
|
|
546
|
+
m.messageContextInfo.messageSecret = randomBytes(32);
|
|
547
|
+
}
|
|
840
548
|
}
|
|
841
|
-
|
|
842
|
-
return WAProto.Message.create(m);
|
|
549
|
+
return WAProto.Message.create(m);
|
|
843
550
|
};
|
|
844
551
|
export const generateWAMessageFromContent = (jid, message, options) => {
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
contextInfo.remoteJid = quoted.key.remoteJid;
|
|
880
|
-
}
|
|
881
|
-
if (contextInfo && innerMessage[key]) {
|
|
882
|
-
/* @ts-ignore */
|
|
883
|
-
innerMessage[key].contextInfo = contextInfo;
|
|
552
|
+
// set timestamp to now
|
|
553
|
+
// if not specified
|
|
554
|
+
if (!options.timestamp) {
|
|
555
|
+
options.timestamp = new Date();
|
|
556
|
+
}
|
|
557
|
+
const innerMessage = normalizeMessageContent(message);
|
|
558
|
+
const key = getContentType(innerMessage);
|
|
559
|
+
const timestamp = unixTimestampSeconds(options.timestamp);
|
|
560
|
+
const { quoted, userJid } = options;
|
|
561
|
+
if (quoted && !isJidNewsletter(jid)) {
|
|
562
|
+
const participant = quoted.key.fromMe
|
|
563
|
+
? userJid // TODO: Add support for LIDs
|
|
564
|
+
: quoted.participant || quoted.key.participant || quoted.key.remoteJid;
|
|
565
|
+
let quotedMsg = normalizeMessageContent(quoted.message);
|
|
566
|
+
const msgType = getContentType(quotedMsg);
|
|
567
|
+
// strip any redundant properties
|
|
568
|
+
quotedMsg = proto.Message.create({ [msgType]: quotedMsg[msgType] });
|
|
569
|
+
const quotedContent = quotedMsg[msgType];
|
|
570
|
+
if (typeof quotedContent === 'object' && quotedContent && 'contextInfo' in quotedContent) {
|
|
571
|
+
delete quotedContent.contextInfo;
|
|
572
|
+
}
|
|
573
|
+
const contextInfo = ('contextInfo' in innerMessage[key] && innerMessage[key]?.contextInfo) || {};
|
|
574
|
+
contextInfo.participant = jidNormalizedUser(participant);
|
|
575
|
+
contextInfo.stanzaId = quoted.key.id;
|
|
576
|
+
contextInfo.quotedMessage = quotedMsg;
|
|
577
|
+
// if a participant is quoted, then it must be a group
|
|
578
|
+
// hence, remoteJid of group must also be entered
|
|
579
|
+
if (jid !== quoted.key.remoteJid) {
|
|
580
|
+
contextInfo.remoteJid = quoted.key.remoteJid;
|
|
581
|
+
}
|
|
582
|
+
if (contextInfo && innerMessage[key]) {
|
|
583
|
+
/* @ts-ignore */
|
|
584
|
+
innerMessage[key].contextInfo = contextInfo;
|
|
585
|
+
}
|
|
884
586
|
}
|
|
885
|
-
|
|
886
|
-
if (
|
|
587
|
+
if (
|
|
887
588
|
// if we want to send a disappearing message
|
|
888
589
|
!!options?.ephemeralExpiration &&
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
590
|
+
// and it's not a protocol message -- delete, toggle disappear message
|
|
591
|
+
key !== 'protocolMessage' &&
|
|
592
|
+
// already not converted to disappearing message
|
|
593
|
+
key !== 'ephemeralMessage' &&
|
|
594
|
+
// newsletters don't support ephemeral messages
|
|
595
|
+
!isJidNewsletter(jid)) {
|
|
596
|
+
/* @ts-ignore */
|
|
597
|
+
innerMessage[key].contextInfo = {
|
|
598
|
+
...(innerMessage[key].contextInfo || {}),
|
|
599
|
+
expiration: options.ephemeralExpiration || WA_DEFAULT_EPHEMERAL
|
|
600
|
+
//ephemeralSettingTimestamp: options.ephemeralOptions.eph_setting_ts?.toString()
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
message = WAProto.Message.create(message);
|
|
604
|
+
const messageJSON = {
|
|
605
|
+
key: {
|
|
606
|
+
remoteJid: jid,
|
|
607
|
+
fromMe: true,
|
|
608
|
+
id: options?.messageId || generateMessageIDV2()
|
|
609
|
+
},
|
|
610
|
+
message: message,
|
|
611
|
+
messageTimestamp: timestamp,
|
|
612
|
+
messageStubParameters: [],
|
|
613
|
+
participant: isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined, // TODO: Add support for LIDs
|
|
614
|
+
status: WAMessageStatus.PENDING
|
|
901
615
|
};
|
|
902
|
-
|
|
903
|
-
message = WAProto.Message.create(message);
|
|
904
|
-
const messageJSON = {
|
|
905
|
-
key: {
|
|
906
|
-
remoteJid: jid,
|
|
907
|
-
fromMe: true,
|
|
908
|
-
id: options?.messageId || generateMessageIDV2(),
|
|
909
|
-
},
|
|
910
|
-
message: message,
|
|
911
|
-
messageTimestamp: timestamp,
|
|
912
|
-
messageStubParameters: [],
|
|
913
|
-
participant:
|
|
914
|
-
isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined, // TODO: Add support for LIDs
|
|
915
|
-
status: WAMessageStatus.PENDING,
|
|
916
|
-
};
|
|
917
|
-
return WAProto.WebMessageInfo.fromObject(messageJSON);
|
|
616
|
+
return WAProto.WebMessageInfo.fromObject(messageJSON);
|
|
918
617
|
};
|
|
919
618
|
export const generateWAMessage = async (jid, content, options) => {
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
jid,
|
|
925
|
-
await generateWAMessageContent(content, { ...options, jid }),
|
|
926
|
-
options,
|
|
927
|
-
);
|
|
619
|
+
// ensure msg ID is with every log
|
|
620
|
+
options.logger = options?.logger?.child({ msgId: options.messageId });
|
|
621
|
+
// Pass jid in the options to generateWAMessageContent
|
|
622
|
+
return generateWAMessageFromContent(jid, await generateWAMessageContent(content, { ...options, jid }), options);
|
|
928
623
|
};
|
|
929
624
|
/** Get the key to access the true type of content */
|
|
930
625
|
export const getContentType = (content) => {
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
k !== "senderKeyDistributionMessage",
|
|
937
|
-
);
|
|
938
|
-
return key;
|
|
939
|
-
}
|
|
626
|
+
if (content) {
|
|
627
|
+
const keys = Object.keys(content);
|
|
628
|
+
const key = keys.find(k => (k === 'conversation' || k.includes('Message')) && k !== 'senderKeyDistributionMessage');
|
|
629
|
+
return key;
|
|
630
|
+
}
|
|
940
631
|
};
|
|
941
632
|
/**
|
|
942
633
|
* Normalizes ephemeral, view once messages to regular message content
|
|
@@ -945,130 +636,115 @@ export const getContentType = (content) => {
|
|
|
945
636
|
* @returns
|
|
946
637
|
*/
|
|
947
638
|
export const normalizeMessageContent = (content) => {
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
639
|
+
if (!content) {
|
|
640
|
+
return undefined;
|
|
641
|
+
}
|
|
642
|
+
// set max iterations to prevent an infinite loop
|
|
643
|
+
for (let i = 0; i < 5; i++) {
|
|
644
|
+
const inner = getFutureProofMessage(content);
|
|
645
|
+
if (!inner) {
|
|
646
|
+
break;
|
|
647
|
+
}
|
|
648
|
+
content = inner.message;
|
|
649
|
+
}
|
|
650
|
+
return content;
|
|
651
|
+
function getFutureProofMessage(message) {
|
|
652
|
+
return (message?.ephemeralMessage ||
|
|
653
|
+
message?.viewOnceMessage ||
|
|
654
|
+
message?.documentWithCaptionMessage ||
|
|
655
|
+
message?.viewOnceMessageV2 ||
|
|
656
|
+
message?.viewOnceMessageV2Extension ||
|
|
657
|
+
message?.editedMessage ||
|
|
658
|
+
message?.associatedChildMessage ||
|
|
659
|
+
message?.groupStatusMessage ||
|
|
660
|
+
message?.groupStatusMessageV2);
|
|
956
661
|
}
|
|
957
|
-
content = inner.message;
|
|
958
|
-
}
|
|
959
|
-
return content;
|
|
960
|
-
function getFutureProofMessage(message) {
|
|
961
|
-
return (
|
|
962
|
-
message?.ephemeralMessage ||
|
|
963
|
-
message?.viewOnceMessage ||
|
|
964
|
-
message?.documentWithCaptionMessage ||
|
|
965
|
-
message?.viewOnceMessageV2 ||
|
|
966
|
-
message?.viewOnceMessageV2Extension ||
|
|
967
|
-
message?.editedMessage ||
|
|
968
|
-
message?.associatedChildMessage ||
|
|
969
|
-
message?.groupStatusMessage ||
|
|
970
|
-
message?.groupStatusMessageV2
|
|
971
|
-
);
|
|
972
|
-
}
|
|
973
662
|
};
|
|
974
663
|
/**
|
|
975
664
|
* Extract the true message content from a message
|
|
976
665
|
* Eg. extracts the inner message from a disappearing message/view once message
|
|
977
666
|
*/
|
|
978
667
|
export const extractMessageContent = (content) => {
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
668
|
+
const extractFromTemplateMessage = (msg) => {
|
|
669
|
+
if (msg.imageMessage) {
|
|
670
|
+
return { imageMessage: msg.imageMessage };
|
|
671
|
+
}
|
|
672
|
+
else if (msg.documentMessage) {
|
|
673
|
+
return { documentMessage: msg.documentMessage };
|
|
674
|
+
}
|
|
675
|
+
else if (msg.videoMessage) {
|
|
676
|
+
return { videoMessage: msg.videoMessage };
|
|
677
|
+
}
|
|
678
|
+
else if (msg.locationMessage) {
|
|
679
|
+
return { locationMessage: msg.locationMessage };
|
|
680
|
+
}
|
|
681
|
+
else {
|
|
682
|
+
return {
|
|
683
|
+
conversation: 'contentText' in msg ? msg.contentText : 'hydratedContentText' in msg ? msg.hydratedContentText : ''
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
content = normalizeMessageContent(content);
|
|
688
|
+
if (content?.buttonsMessage) {
|
|
689
|
+
return extractFromTemplateMessage(content.buttonsMessage);
|
|
690
|
+
}
|
|
691
|
+
if (content?.templateMessage?.hydratedFourRowTemplate) {
|
|
692
|
+
return extractFromTemplateMessage(content?.templateMessage?.hydratedFourRowTemplate);
|
|
693
|
+
}
|
|
694
|
+
if (content?.templateMessage?.hydratedTemplate) {
|
|
695
|
+
return extractFromTemplateMessage(content?.templateMessage?.hydratedTemplate);
|
|
997
696
|
}
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
return
|
|
1002
|
-
}
|
|
1003
|
-
if (content?.templateMessage?.hydratedFourRowTemplate) {
|
|
1004
|
-
return extractFromTemplateMessage(
|
|
1005
|
-
content?.templateMessage?.hydratedFourRowTemplate,
|
|
1006
|
-
);
|
|
1007
|
-
}
|
|
1008
|
-
if (content?.templateMessage?.hydratedTemplate) {
|
|
1009
|
-
return extractFromTemplateMessage(
|
|
1010
|
-
content?.templateMessage?.hydratedTemplate,
|
|
1011
|
-
);
|
|
1012
|
-
}
|
|
1013
|
-
if (content?.templateMessage?.fourRowTemplate) {
|
|
1014
|
-
return extractFromTemplateMessage(
|
|
1015
|
-
content?.templateMessage?.fourRowTemplate,
|
|
1016
|
-
);
|
|
1017
|
-
}
|
|
1018
|
-
return content;
|
|
697
|
+
if (content?.templateMessage?.fourRowTemplate) {
|
|
698
|
+
return extractFromTemplateMessage(content?.templateMessage?.fourRowTemplate);
|
|
699
|
+
}
|
|
700
|
+
return content;
|
|
1019
701
|
};
|
|
1020
702
|
/**
|
|
1021
703
|
* Returns the device predicted by message ID
|
|
1022
704
|
*/
|
|
1023
|
-
export const getDevice = (id) =>
|
|
1024
|
-
|
|
1025
|
-
? "ios"
|
|
705
|
+
export const getDevice = (id) => /^3A.{18}$/.test(id)
|
|
706
|
+
? 'ios'
|
|
1026
707
|
: /^3E.{20}$/.test(id)
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
708
|
+
? 'web'
|
|
709
|
+
: /^(.{21}|.{32})$/.test(id)
|
|
710
|
+
? 'android'
|
|
711
|
+
: /^(3F|.{18}$)/.test(id)
|
|
712
|
+
? 'desktop'
|
|
713
|
+
: 'unknown';
|
|
1033
714
|
/** Upserts a receipt in the message */
|
|
1034
715
|
export const updateMessageWithReceipt = (msg, receipt) => {
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
716
|
+
msg.userReceipt = msg.userReceipt || [];
|
|
717
|
+
const recp = msg.userReceipt.find(m => m.userJid === receipt.userJid);
|
|
718
|
+
if (recp) {
|
|
719
|
+
Object.assign(recp, receipt);
|
|
720
|
+
}
|
|
721
|
+
else {
|
|
722
|
+
msg.userReceipt.push(receipt);
|
|
723
|
+
}
|
|
1042
724
|
};
|
|
1043
725
|
/** Update the message with a new reaction */
|
|
1044
726
|
export const updateMessageWithReaction = (msg, reaction) => {
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
reactions.push(reaction);
|
|
1051
|
-
msg.reactions = reactions;
|
|
727
|
+
const authorID = getKeyAuthor(reaction.key);
|
|
728
|
+
const reactions = (msg.reactions || []).filter(r => getKeyAuthor(r.key) !== authorID);
|
|
729
|
+
reaction.text = reaction.text || '';
|
|
730
|
+
reactions.push(reaction);
|
|
731
|
+
msg.reactions = reactions;
|
|
1052
732
|
};
|
|
1053
733
|
/** Update the message with a new poll update */
|
|
1054
734
|
export const updateMessageWithPollUpdate = (msg, update) => {
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
reactions
|
|
1061
|
-
}
|
|
1062
|
-
msg.pollUpdates = reactions;
|
|
735
|
+
const authorID = getKeyAuthor(update.pollUpdateMessageKey);
|
|
736
|
+
const reactions = (msg.pollUpdates || []).filter(r => getKeyAuthor(r.pollUpdateMessageKey) !== authorID);
|
|
737
|
+
if (update.vote?.selectedOptions?.length) {
|
|
738
|
+
reactions.push(update);
|
|
739
|
+
}
|
|
740
|
+
msg.pollUpdates = reactions;
|
|
1063
741
|
};
|
|
1064
742
|
/** Update the message with a new event response */
|
|
1065
743
|
export const updateMessageWithEventResponse = (msg, update) => {
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
(
|
|
1069
|
-
|
|
1070
|
-
responses.push(update);
|
|
1071
|
-
msg.eventResponses = responses;
|
|
744
|
+
const authorID = getKeyAuthor(update.eventResponseMessageKey);
|
|
745
|
+
const responses = (msg.eventResponses || []).filter(r => getKeyAuthor(r.eventResponseMessageKey) !== authorID);
|
|
746
|
+
responses.push(update);
|
|
747
|
+
msg.eventResponses = responses;
|
|
1072
748
|
};
|
|
1073
749
|
/**
|
|
1074
750
|
* Aggregates all poll updates in a poll.
|
|
@@ -1077,40 +753,37 @@ export const updateMessageWithEventResponse = (msg, update) => {
|
|
|
1077
753
|
* @returns A list of options & their voters
|
|
1078
754
|
*/
|
|
1079
755
|
export function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
voters: [],
|
|
1090
|
-
};
|
|
1091
|
-
return acc;
|
|
1092
|
-
}, {});
|
|
1093
|
-
for (const update of pollUpdates || []) {
|
|
1094
|
-
const { vote } = update;
|
|
1095
|
-
if (!vote) {
|
|
1096
|
-
continue;
|
|
1097
|
-
}
|
|
1098
|
-
for (const option of vote.selectedOptions || []) {
|
|
1099
|
-
const hash = option.toString();
|
|
1100
|
-
let data = voteHashMap[hash];
|
|
1101
|
-
if (!data) {
|
|
1102
|
-
voteHashMap[hash] = {
|
|
1103
|
-
name: "Unknown",
|
|
1104
|
-
voters: [],
|
|
756
|
+
const opts = message?.pollCreationMessage?.options ||
|
|
757
|
+
message?.pollCreationMessageV2?.options ||
|
|
758
|
+
message?.pollCreationMessageV3?.options ||
|
|
759
|
+
[];
|
|
760
|
+
const voteHashMap = opts.reduce((acc, opt) => {
|
|
761
|
+
const hash = sha256(Buffer.from(opt.optionName || '')).toString();
|
|
762
|
+
acc[hash] = {
|
|
763
|
+
name: opt.optionName || '',
|
|
764
|
+
voters: []
|
|
1105
765
|
};
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
766
|
+
return acc;
|
|
767
|
+
}, {});
|
|
768
|
+
for (const update of pollUpdates || []) {
|
|
769
|
+
const { vote } = update;
|
|
770
|
+
if (!vote) {
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
for (const option of vote.selectedOptions || []) {
|
|
774
|
+
const hash = option.toString();
|
|
775
|
+
let data = voteHashMap[hash];
|
|
776
|
+
if (!data) {
|
|
777
|
+
voteHashMap[hash] = {
|
|
778
|
+
name: 'Unknown',
|
|
779
|
+
voters: []
|
|
780
|
+
};
|
|
781
|
+
data = voteHashMap[hash];
|
|
782
|
+
}
|
|
783
|
+
voteHashMap[hash].voters.push(getKeyAuthor(update.pollUpdateMessageKey, meId));
|
|
784
|
+
}
|
|
1111
785
|
}
|
|
1112
|
-
|
|
1113
|
-
return Object.values(voteHashMap);
|
|
786
|
+
return Object.values(voteHashMap);
|
|
1114
787
|
}
|
|
1115
788
|
/**
|
|
1116
789
|
* Aggregates all event responses in an event message.
|
|
@@ -1119,120 +792,102 @@ export function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
|
|
|
1119
792
|
* @returns A list of response types & their responders
|
|
1120
793
|
*/
|
|
1121
794
|
export function getAggregateResponsesInEventMessage({ eventResponses }, meId) {
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
);
|
|
795
|
+
const responseTypes = ['GOING', 'NOT_GOING', 'MAYBE'];
|
|
796
|
+
const responseMap = {};
|
|
797
|
+
for (const type of responseTypes) {
|
|
798
|
+
responseMap[type] = {
|
|
799
|
+
response: type,
|
|
800
|
+
responders: []
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
for (const update of eventResponses || []) {
|
|
804
|
+
const responseType = update.eventResponse || 'UNKNOWN';
|
|
805
|
+
if (responseType !== 'UNKNOWN' && responseMap[responseType]) {
|
|
806
|
+
responseMap[responseType].responders.push(getKeyAuthor(update.eventResponseMessageKey, meId));
|
|
807
|
+
}
|
|
1136
808
|
}
|
|
1137
|
-
|
|
1138
|
-
return Object.values(responseMap);
|
|
809
|
+
return Object.values(responseMap);
|
|
1139
810
|
}
|
|
1140
811
|
/** Given a list of message keys, aggregates them by chat & sender. Useful for sending read receipts in bulk */
|
|
1141
812
|
export const aggregateMessageKeysNotFromMe = (keys) => {
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
813
|
+
const keyMap = {};
|
|
814
|
+
for (const { remoteJid, id, participant, fromMe } of keys) {
|
|
815
|
+
if (!fromMe) {
|
|
816
|
+
const uqKey = `${remoteJid}:${participant || ''}`;
|
|
817
|
+
if (!keyMap[uqKey]) {
|
|
818
|
+
keyMap[uqKey] = {
|
|
819
|
+
jid: remoteJid,
|
|
820
|
+
participant: participant,
|
|
821
|
+
messageIds: []
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
keyMap[uqKey].messageIds.push(id);
|
|
825
|
+
}
|
|
1154
826
|
}
|
|
1155
|
-
|
|
1156
|
-
return Object.values(keyMap);
|
|
827
|
+
return Object.values(keyMap);
|
|
1157
828
|
};
|
|
1158
829
|
const REUPLOAD_REQUIRED_STATUS = [410, 404];
|
|
1159
830
|
/**
|
|
1160
831
|
* Downloads the given message. Throws an error if it's not a media message
|
|
1161
832
|
*/
|
|
1162
833
|
export const downloadMediaMessage = async (message, type, options, ctx) => {
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
download,
|
|
1208
|
-
mediaType,
|
|
1209
|
-
options,
|
|
1210
|
-
);
|
|
1211
|
-
if (type === "buffer") {
|
|
1212
|
-
const bufferArray = [];
|
|
1213
|
-
for await (const chunk of stream) {
|
|
1214
|
-
bufferArray.push(chunk);
|
|
1215
|
-
}
|
|
1216
|
-
return Buffer.concat(bufferArray);
|
|
834
|
+
const result = await downloadMsg().catch(async (error) => {
|
|
835
|
+
if (ctx &&
|
|
836
|
+
typeof error?.status === 'number' && // treat errors with status as HTTP failures requiring reupload
|
|
837
|
+
REUPLOAD_REQUIRED_STATUS.includes(error.status)) {
|
|
838
|
+
ctx.logger.info({ key: message.key }, 'sending reupload media request...');
|
|
839
|
+
// request reupload
|
|
840
|
+
message = await ctx.reuploadRequest(message);
|
|
841
|
+
const result = await downloadMsg();
|
|
842
|
+
return result;
|
|
843
|
+
}
|
|
844
|
+
throw error;
|
|
845
|
+
});
|
|
846
|
+
return result;
|
|
847
|
+
async function downloadMsg() {
|
|
848
|
+
const mContent = extractMessageContent(message.message);
|
|
849
|
+
if (!mContent) {
|
|
850
|
+
throw new Boom('No message present', { statusCode: 400, data: message });
|
|
851
|
+
}
|
|
852
|
+
const contentType = getContentType(mContent);
|
|
853
|
+
let mediaType = contentType?.replace('Message', '');
|
|
854
|
+
const media = mContent[contentType];
|
|
855
|
+
if (!media || typeof media !== 'object' || (!('url' in media) && !('thumbnailDirectPath' in media))) {
|
|
856
|
+
throw new Boom(`"${contentType}" message is not a media message`);
|
|
857
|
+
}
|
|
858
|
+
let download;
|
|
859
|
+
if ('thumbnailDirectPath' in media && !('url' in media)) {
|
|
860
|
+
download = {
|
|
861
|
+
directPath: media.thumbnailDirectPath,
|
|
862
|
+
mediaKey: media.mediaKey
|
|
863
|
+
};
|
|
864
|
+
mediaType = 'thumbnail-link';
|
|
865
|
+
}
|
|
866
|
+
else {
|
|
867
|
+
download = media;
|
|
868
|
+
}
|
|
869
|
+
const stream = await downloadContentFromMessage(download, mediaType, options);
|
|
870
|
+
if (type === 'buffer') {
|
|
871
|
+
const bufferArray = [];
|
|
872
|
+
for await (const chunk of stream) {
|
|
873
|
+
bufferArray.push(chunk);
|
|
874
|
+
}
|
|
875
|
+
return Buffer.concat(bufferArray);
|
|
876
|
+
}
|
|
877
|
+
return stream;
|
|
1217
878
|
}
|
|
1218
|
-
return stream;
|
|
1219
|
-
}
|
|
1220
879
|
};
|
|
1221
880
|
/** Checks whether the given message is a media message; if it is returns the inner content */
|
|
1222
881
|
export const assertMediaContent = (content) => {
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
data: content,
|
|
1234
|
-
});
|
|
1235
|
-
}
|
|
1236
|
-
return mediaContent;
|
|
882
|
+
content = extractMessageContent(content);
|
|
883
|
+
const mediaContent = content?.documentMessage ||
|
|
884
|
+
content?.imageMessage ||
|
|
885
|
+
content?.videoMessage ||
|
|
886
|
+
content?.audioMessage ||
|
|
887
|
+
content?.stickerMessage;
|
|
888
|
+
if (!mediaContent) {
|
|
889
|
+
throw new Boom('given message is not a media message', { statusCode: 400, data: content });
|
|
890
|
+
}
|
|
891
|
+
return mediaContent;
|
|
1237
892
|
};
|
|
1238
|
-
//# sourceMappingURL=messages.js.map
|
|
893
|
+
//# sourceMappingURL=messages.js.map
|