@yemo-dev/yebail 1.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.
Files changed (106) hide show
  1. package/EXAMPLES.md +641 -0
  2. package/LICENSE +21 -0
  3. package/README.md +141 -0
  4. package/WAProto/GenerateStatics.sh +4 -0
  5. package/WAProto/WAProto.proto +4775 -0
  6. package/WAProto/index.js +116311 -0
  7. package/engine-requirements.js +10 -0
  8. package/lib/Defaults/index.js +142 -0
  9. package/lib/Defaults/phonenumber-mcc.json +223 -0
  10. package/lib/Defaults/yebail-version.json +7 -0
  11. package/lib/Signal/Group/ciphertext-message.js +15 -0
  12. package/lib/Signal/Group/group-session-builder.js +64 -0
  13. package/lib/Signal/Group/group_cipher.js +96 -0
  14. package/lib/Signal/Group/index.js +57 -0
  15. package/lib/Signal/Group/keyhelper.js +55 -0
  16. package/lib/Signal/Group/queue-job.js +57 -0
  17. package/lib/Signal/Group/sender-chain-key.js +34 -0
  18. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  19. package/lib/Signal/Group/sender-key-message.js +69 -0
  20. package/lib/Signal/Group/sender-key-name.js +51 -0
  21. package/lib/Signal/Group/sender-key-record.js +53 -0
  22. package/lib/Signal/Group/sender-key-state.js +99 -0
  23. package/lib/Signal/Group/sender-message-key.js +29 -0
  24. package/lib/Signal/libsignal.js +196 -0
  25. package/lib/Signal/lid-mapping.js +148 -0
  26. package/lib/Socket/Client/index.js +18 -0
  27. package/lib/Socket/Client/types.js +13 -0
  28. package/lib/Socket/Client/websocket.js +72 -0
  29. package/lib/Socket/business.js +368 -0
  30. package/lib/Socket/chats.js +992 -0
  31. package/lib/Socket/communities.js +430 -0
  32. package/lib/Socket/groups.js +323 -0
  33. package/lib/Socket/index.js +10 -0
  34. package/lib/Socket/messages-recv.js +1133 -0
  35. package/lib/Socket/messages-send.js +992 -0
  36. package/lib/Socket/newsletter.js +250 -0
  37. package/lib/Socket/socket.js +631 -0
  38. package/lib/Socket/usync.js +70 -0
  39. package/lib/Store/index.js +8 -0
  40. package/lib/Store/make-in-memory-store.js +421 -0
  41. package/lib/Store/make-ordered-dictionary.js +81 -0
  42. package/lib/Store/object-repository.js +27 -0
  43. package/lib/Types/Auth.js +2 -0
  44. package/lib/Types/Call.js +2 -0
  45. package/lib/Types/Chat.js +4 -0
  46. package/lib/Types/Contact.js +2 -0
  47. package/lib/Types/Events.js +2 -0
  48. package/lib/Types/GroupMetadata.js +2 -0
  49. package/lib/Types/Label.js +27 -0
  50. package/lib/Types/LabelAssociation.js +9 -0
  51. package/lib/Types/Message.js +7 -0
  52. package/lib/Types/Newsletter.js +18 -0
  53. package/lib/Types/Product.js +2 -0
  54. package/lib/Types/Signal.js +2 -0
  55. package/lib/Types/Socket.js +2 -0
  56. package/lib/Types/State.js +2 -0
  57. package/lib/Types/USync.js +2 -0
  58. package/lib/Types/index.js +42 -0
  59. package/lib/Utils/auth-utils.js +188 -0
  60. package/lib/Utils/browser-utils.js +35 -0
  61. package/lib/Utils/business.js +230 -0
  62. package/lib/Utils/chat-utils.js +763 -0
  63. package/lib/Utils/crypto.js +187 -0
  64. package/lib/Utils/decode-wa-message.js +293 -0
  65. package/lib/Utils/event-buffer.js +514 -0
  66. package/lib/Utils/generics.js +453 -0
  67. package/lib/Utils/history.js +94 -0
  68. package/lib/Utils/index.js +37 -0
  69. package/lib/Utils/link-preview.js +121 -0
  70. package/lib/Utils/logger.js +7 -0
  71. package/lib/Utils/lt-hash.js +47 -0
  72. package/lib/Utils/make-mutex.js +43 -0
  73. package/lib/Utils/message-retry-manager.js +128 -0
  74. package/lib/Utils/messages-media.js +910 -0
  75. package/lib/Utils/messages.js +1129 -0
  76. package/lib/Utils/noise-handler.js +150 -0
  77. package/lib/Utils/process-message.js +448 -0
  78. package/lib/Utils/signal.js +150 -0
  79. package/lib/Utils/use-custom-auth-state.js +110 -0
  80. package/lib/Utils/use-multi-file-auth-state.js +43 -0
  81. package/lib/Utils/use-sqlite-auth-state.js +39 -0
  82. package/lib/Utils/validate-connection.js +237 -0
  83. package/lib/Utils/yebail-event-stream.js +55 -0
  84. package/lib/WABinary/constants.js +1303 -0
  85. package/lib/WABinary/decode.js +275 -0
  86. package/lib/WABinary/encode.js +250 -0
  87. package/lib/WABinary/generic-utils.js +110 -0
  88. package/lib/WABinary/index.js +21 -0
  89. package/lib/WABinary/jid-utils.js +136 -0
  90. package/lib/WABinary/types.js +2 -0
  91. package/lib/WAM/BinaryInfo.js +13 -0
  92. package/lib/WAM/constants.js +15350 -0
  93. package/lib/WAM/encode.js +155 -0
  94. package/lib/WAM/index.js +19 -0
  95. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
  96. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  97. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  98. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  99. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  100. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  101. package/lib/WAUSync/Protocols/index.js +20 -0
  102. package/lib/WAUSync/USyncQuery.js +89 -0
  103. package/lib/WAUSync/USyncUser.js +26 -0
  104. package/lib/WAUSync/index.js +19 -0
  105. package/lib/index.js +46 -0
  106. package/package.json +114 -0
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.DisconnectReason = void 0;
18
+ __exportStar(require("./Auth"), exports);
19
+ __exportStar(require("./GroupMetadata"), exports);
20
+ __exportStar(require("./Newsletter"), exports);
21
+ __exportStar(require("./Chat"), exports);
22
+ __exportStar(require("./Contact"), exports);
23
+ __exportStar(require("./State"), exports);
24
+ __exportStar(require("./Message"), exports);
25
+ __exportStar(require("./Socket"), exports);
26
+ __exportStar(require("./Events"), exports);
27
+ __exportStar(require("./Product"), exports);
28
+ __exportStar(require("./Call"), exports);
29
+ __exportStar(require("./Signal"), exports);
30
+ var DisconnectReason;
31
+ (function (DisconnectReason) {
32
+ DisconnectReason[DisconnectReason["connectionClosed"] = 428] = "connectionClosed";
33
+ DisconnectReason[DisconnectReason["connectionLost"] = 408] = "connectionLost";
34
+ DisconnectReason[DisconnectReason["connectionReplaced"] = 440] = "connectionReplaced";
35
+ DisconnectReason[DisconnectReason["timedOut"] = 408] = "timedOut";
36
+ DisconnectReason[DisconnectReason["loggedOut"] = 401] = "loggedOut";
37
+ DisconnectReason[DisconnectReason["badSession"] = 500] = "badSession";
38
+ DisconnectReason[DisconnectReason["restartRequired"] = 515] = "restartRequired";
39
+ DisconnectReason[DisconnectReason["multideviceMismatch"] = 411] = "multideviceMismatch";
40
+ DisconnectReason[DisconnectReason["forbidden"] = 403] = "forbidden";
41
+ DisconnectReason[DisconnectReason["unavailableService"] = 503] = "unavailableService";
42
+ })(DisconnectReason || (exports.DisconnectReason = DisconnectReason = {}));
@@ -0,0 +1,188 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.initAuthCreds = exports.addTransactionCapability = void 0;
7
+ exports.makeCacheableSignalKeyStore = makeCacheableSignalKeyStore;
8
+ const crypto_1 = require("crypto");
9
+ const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
10
+ const Defaults_1 = require("../Defaults");
11
+ const crypto_2 = require("./crypto");
12
+ const generics_1 = require("./generics");
13
+
14
+ function makeCacheableSignalKeyStore(store, logger, _cache) {
15
+ const cache = _cache || new node_cache_1.default({
16
+ stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes
17
+ useClones: false,
18
+ deleteOnExpire: true,
19
+ });
20
+ function getUniqueId(type, id) {
21
+ return `${type}.${id}`;
22
+ }
23
+ return {
24
+ async get(type, ids) {
25
+ const data = {};
26
+ const idsToFetch = [];
27
+ for (const id of ids) {
28
+ const item = cache.get(getUniqueId(type, id));
29
+ if (typeof item !== 'undefined') {
30
+ data[id] = item;
31
+ }
32
+ else {
33
+ idsToFetch.push(id);
34
+ }
35
+ }
36
+ if (idsToFetch.length) {
37
+ logger === null || logger === void 0 ? void 0 : logger.trace({ items: idsToFetch.length }, 'loading from store');
38
+ const fetched = await store.get(type, idsToFetch);
39
+ for (const id of idsToFetch) {
40
+ const item = fetched[id];
41
+ if (item) {
42
+ data[id] = item;
43
+ cache.set(getUniqueId(type, id), item);
44
+ }
45
+ }
46
+ }
47
+ return data;
48
+ },
49
+ async set(data) {
50
+ let keys = 0;
51
+ for (const type in data) {
52
+ for (const id in data[type]) {
53
+ cache.set(getUniqueId(type, id), data[type][id]);
54
+ keys += 1;
55
+ }
56
+ }
57
+ logger === null || logger === void 0 ? void 0 : logger.trace({ keys }, 'updated cache');
58
+ await store.set(data);
59
+ },
60
+ async clear() {
61
+ var _a;
62
+ cache.flushAll();
63
+ await ((_a = store.clear) === null || _a === void 0 ? void 0 : _a.call(store));
64
+ }
65
+ };
66
+ }
67
+
68
+ const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetweenTriesMs }) => {
69
+ // number of queries made to the DB during the transaction
70
+ // only there for logging purposes
71
+ let dbQueriesInTransaction = 0;
72
+ let transactionCache = {};
73
+ let mutations = {};
74
+ let transactionsInProgress = 0;
75
+ return {
76
+ get: async (type, ids) => {
77
+ if (isInTransaction()) {
78
+ const dict = transactionCache[type];
79
+ const idsRequiringFetch = dict
80
+ ? ids.filter(item => typeof dict[item] === 'undefined')
81
+ : ids;
82
+ // only fetch if there are any items to fetch
83
+ if (idsRequiringFetch.length) {
84
+ dbQueriesInTransaction += 1;
85
+ const result = await state.get(type, idsRequiringFetch);
86
+ transactionCache[type] || (transactionCache[type] = {});
87
+ Object.assign(transactionCache[type], result);
88
+ }
89
+ return ids.reduce((dict, id) => {
90
+ var _a;
91
+ const value = (_a = transactionCache[type]) === null || _a === void 0 ? void 0 : _a[id];
92
+ if (value) {
93
+ dict[id] = value;
94
+ }
95
+ return dict;
96
+ }, {});
97
+ }
98
+ else {
99
+ return state.get(type, ids);
100
+ }
101
+ },
102
+ set: data => {
103
+ if (isInTransaction()) {
104
+ logger.trace({ types: Object.keys(data) }, 'caching in transaction');
105
+ for (const key in data) {
106
+ transactionCache[key] = transactionCache[key] || {};
107
+ Object.assign(transactionCache[key], data[key]);
108
+ mutations[key] = mutations[key] || {};
109
+ Object.assign(mutations[key], data[key]);
110
+ }
111
+ }
112
+ else {
113
+ return state.set(data);
114
+ }
115
+ },
116
+ isInTransaction,
117
+ async transaction(work) {
118
+ let result;
119
+ transactionsInProgress += 1;
120
+ if (transactionsInProgress === 1) {
121
+ logger.trace('entering transaction');
122
+ }
123
+ try {
124
+ result = await work();
125
+ // commit if this is the outermost transaction
126
+ if (transactionsInProgress === 1) {
127
+ if (Object.keys(mutations).length) {
128
+ logger.trace('committing transaction');
129
+ // retry mechanism to ensure we've some recovery
130
+ // in case a transaction fails in the first attempt
131
+ let tries = maxCommitRetries;
132
+ while (tries) {
133
+ tries -= 1;
134
+ try {
135
+ await state.set(mutations);
136
+ logger.trace({ dbQueriesInTransaction }, 'committed transaction');
137
+ break;
138
+ }
139
+ catch (error) {
140
+ logger.warn(`failed to commit ${Object.keys(mutations).length} mutations, tries left=${tries}`);
141
+ await (0, generics_1.delay)(delayBetweenTriesMs);
142
+ }
143
+ }
144
+ }
145
+ else {
146
+ logger.trace('no mutations in transaction');
147
+ }
148
+ }
149
+ }
150
+ finally {
151
+ transactionsInProgress -= 1;
152
+ if (transactionsInProgress === 0) {
153
+ transactionCache = {};
154
+ mutations = {};
155
+ dbQueriesInTransaction = 0;
156
+ }
157
+ }
158
+ return result;
159
+ }
160
+ };
161
+ function isInTransaction() {
162
+ return transactionsInProgress > 0;
163
+ }
164
+ };
165
+ exports.addTransactionCapability = addTransactionCapability;
166
+ const initAuthCreds = () => {
167
+ const identityKey = crypto_2.Curve.generateKeyPair();
168
+ return {
169
+ noiseKey: crypto_2.Curve.generateKeyPair(),
170
+ pairingEphemeralKeyPair: crypto_2.Curve.generateKeyPair(),
171
+ signedIdentityKey: identityKey,
172
+ signedPreKey: (0, crypto_2.signedKeyPair)(identityKey, 1),
173
+ registrationId: (0, generics_1.generateRegistrationId)(),
174
+ advSecretKey: (0, crypto_1.randomBytes)(32).toString('base64'),
175
+ processedHistoryMessages: [],
176
+ nextPreKeyId: 1,
177
+ firstUnuploadedPreKeyId: 1,
178
+ accountSyncCounter: 0,
179
+ accountSettings: {
180
+ unarchiveChats: false
181
+ },
182
+ registered: false,
183
+ pairingCode: undefined,
184
+ lastPropHash: undefined,
185
+ routingInfo: undefined,
186
+ };
187
+ };
188
+ exports.initAuthCreds = initAuthCreds;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPlatformId = exports.Browsers = void 0;
4
+
5
+ const os = require("os");
6
+ const { proto } = require("../../WAProto/index.js");
7
+
8
+ const PLATFORM_MAP = {
9
+ 'aix': 'AIX',
10
+ 'darwin': 'Mac OS',
11
+ 'win32': 'Windows',
12
+ 'android': 'Android',
13
+ 'freebsd': 'FreeBSD',
14
+ 'openbsd': 'OpenBSD',
15
+ 'sunos': 'Solaris',
16
+ 'linux': undefined, // Default ke Ubuntu untuk Linux
17
+ 'haiku': undefined,
18
+ 'cygwin': undefined,
19
+ 'netbsd': undefined
20
+ };
21
+
22
+ // Fixed: Browsers sekarang fungsi yang return array [platform, browser, version]
23
+ // Ini kompatibel dengan pemanggilan Browsers('Chrome') di Defaults/index.js
24
+ exports.Browsers = (browser) => {
25
+ const osName = PLATFORM_MAP[os.platform()] || 'Ubuntu'; // Default Ubuntu kalau undefined
26
+ const osRelease = os.release(); // Ambil versi OS real-time
27
+ return [osName, browser, osRelease];
28
+ };
29
+
30
+ const getPlatformId = (browser) => {
31
+ const platformType = proto.DeviceProps.PlatformType[browser.toUpperCase()];
32
+ return platformType ? platformType.toString() : '1'; // Default Chrome
33
+ };
34
+
35
+ exports.getPlatformId = getPlatformId;
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uploadingNecessaryImages = exports.parseProductNode = exports.toProductNode = exports.parseOrderDetailsNode = exports.parseCollectionsNode = exports.parseCatalogNode = void 0;
4
+ exports.uploadingNecessaryImagesOfProduct = uploadingNecessaryImagesOfProduct;
5
+ const boom_1 = require("@hapi/boom");
6
+ const crypto_1 = require("crypto");
7
+ const WABinary_1 = require("../WABinary");
8
+ const messages_media_1 = require("./messages-media");
9
+ const parseCatalogNode = (node) => {
10
+ const catalogNode = (0, WABinary_1.getBinaryNodeChild)(node, 'product_catalog');
11
+ const products = (0, WABinary_1.getBinaryNodeChildren)(catalogNode, 'product').map(exports.parseProductNode);
12
+ const paging = (0, WABinary_1.getBinaryNodeChild)(catalogNode, 'paging');
13
+ return {
14
+ products,
15
+ nextPageCursor: paging
16
+ ? (0, WABinary_1.getBinaryNodeChildString)(paging, 'after')
17
+ : undefined
18
+ };
19
+ };
20
+ exports.parseCatalogNode = parseCatalogNode;
21
+ const parseCollectionsNode = (node) => {
22
+ const collectionsNode = (0, WABinary_1.getBinaryNodeChild)(node, 'collections');
23
+ const collections = (0, WABinary_1.getBinaryNodeChildren)(collectionsNode, 'collection').map(collectionNode => {
24
+ const id = (0, WABinary_1.getBinaryNodeChildString)(collectionNode, 'id');
25
+ const name = (0, WABinary_1.getBinaryNodeChildString)(collectionNode, 'name');
26
+ const products = (0, WABinary_1.getBinaryNodeChildren)(collectionNode, 'product').map(exports.parseProductNode);
27
+ return {
28
+ id,
29
+ name,
30
+ products,
31
+ status: parseStatusInfo(collectionNode)
32
+ };
33
+ });
34
+ return {
35
+ collections
36
+ };
37
+ };
38
+ exports.parseCollectionsNode = parseCollectionsNode;
39
+ const parseOrderDetailsNode = (node) => {
40
+ const orderNode = (0, WABinary_1.getBinaryNodeChild)(node, 'order');
41
+ const products = (0, WABinary_1.getBinaryNodeChildren)(orderNode, 'product').map(productNode => {
42
+ const imageNode = (0, WABinary_1.getBinaryNodeChild)(productNode, 'image');
43
+ return {
44
+ id: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'id'),
45
+ name: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'name'),
46
+ imageUrl: (0, WABinary_1.getBinaryNodeChildString)(imageNode, 'url'),
47
+ price: +(0, WABinary_1.getBinaryNodeChildString)(productNode, 'price'),
48
+ currency: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'currency'),
49
+ quantity: +(0, WABinary_1.getBinaryNodeChildString)(productNode, 'quantity')
50
+ };
51
+ });
52
+ const priceNode = (0, WABinary_1.getBinaryNodeChild)(orderNode, 'price');
53
+ const orderDetails = {
54
+ price: {
55
+ total: +(0, WABinary_1.getBinaryNodeChildString)(priceNode, 'total'),
56
+ currency: (0, WABinary_1.getBinaryNodeChildString)(priceNode, 'currency'),
57
+ },
58
+ products
59
+ };
60
+ return orderDetails;
61
+ };
62
+ exports.parseOrderDetailsNode = parseOrderDetailsNode;
63
+ const toProductNode = (productId, product) => {
64
+ const attrs = {};
65
+ const content = [];
66
+ if (typeof productId !== 'undefined') {
67
+ content.push({
68
+ tag: 'id',
69
+ attrs: {},
70
+ content: Buffer.from(productId)
71
+ });
72
+ }
73
+ if (typeof product.name !== 'undefined') {
74
+ content.push({
75
+ tag: 'name',
76
+ attrs: {},
77
+ content: Buffer.from(product.name)
78
+ });
79
+ }
80
+ if (typeof product.description !== 'undefined') {
81
+ content.push({
82
+ tag: 'description',
83
+ attrs: {},
84
+ content: Buffer.from(product.description)
85
+ });
86
+ }
87
+ if (typeof product.retailerId !== 'undefined') {
88
+ content.push({
89
+ tag: 'retailer_id',
90
+ attrs: {},
91
+ content: Buffer.from(product.retailerId)
92
+ });
93
+ }
94
+ if (product.images.length) {
95
+ content.push({
96
+ tag: 'media',
97
+ attrs: {},
98
+ content: product.images.map(img => {
99
+ if (!('url' in img)) {
100
+ throw new boom_1.Boom('Expected img for product to already be uploaded', { statusCode: 400 });
101
+ }
102
+ return {
103
+ tag: 'image',
104
+ attrs: {},
105
+ content: [
106
+ {
107
+ tag: 'url',
108
+ attrs: {},
109
+ content: Buffer.from(img.url.toString())
110
+ }
111
+ ]
112
+ };
113
+ })
114
+ });
115
+ }
116
+ if (typeof product.price !== 'undefined') {
117
+ content.push({
118
+ tag: 'price',
119
+ attrs: {},
120
+ content: Buffer.from(product.price.toString())
121
+ });
122
+ }
123
+ if (typeof product.currency !== 'undefined') {
124
+ content.push({
125
+ tag: 'currency',
126
+ attrs: {},
127
+ content: Buffer.from(product.currency)
128
+ });
129
+ }
130
+ if ('originCountryCode' in product) {
131
+ if (typeof product.originCountryCode === 'undefined') {
132
+ attrs['compliance_category'] = 'COUNTRY_ORIGIN_EXEMPT';
133
+ }
134
+ else {
135
+ content.push({
136
+ tag: 'compliance_info',
137
+ attrs: {},
138
+ content: [
139
+ {
140
+ tag: 'country_code_origin',
141
+ attrs: {},
142
+ content: Buffer.from(product.originCountryCode)
143
+ }
144
+ ]
145
+ });
146
+ }
147
+ }
148
+ if (typeof product.isHidden !== 'undefined') {
149
+ attrs['is_hidden'] = product.isHidden.toString();
150
+ }
151
+ const node = {
152
+ tag: 'product',
153
+ attrs,
154
+ content
155
+ };
156
+ return node;
157
+ };
158
+ exports.toProductNode = toProductNode;
159
+ const parseProductNode = (productNode) => {
160
+ const isHidden = productNode.attrs.is_hidden === 'true';
161
+ const id = (0, WABinary_1.getBinaryNodeChildString)(productNode, 'id');
162
+ const mediaNode = (0, WABinary_1.getBinaryNodeChild)(productNode, 'media');
163
+ const statusInfoNode = (0, WABinary_1.getBinaryNodeChild)(productNode, 'status_info');
164
+ const product = {
165
+ id,
166
+ imageUrls: parseImageUrls(mediaNode),
167
+ reviewStatus: {
168
+ whatsapp: (0, WABinary_1.getBinaryNodeChildString)(statusInfoNode, 'status'),
169
+ },
170
+ availability: 'in stock',
171
+ name: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'name'),
172
+ retailerId: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'retailer_id'),
173
+ url: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'url'),
174
+ description: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'description'),
175
+ price: +(0, WABinary_1.getBinaryNodeChildString)(productNode, 'price'),
176
+ currency: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'currency'),
177
+ isHidden,
178
+ };
179
+ return product;
180
+ };
181
+ exports.parseProductNode = parseProductNode;
182
+
183
+ async function uploadingNecessaryImagesOfProduct(product, waUploadToServer, timeoutMs = 30000) {
184
+ product = {
185
+ ...product,
186
+ images: product.images ? await (0, exports.uploadingNecessaryImages)(product.images, waUploadToServer, timeoutMs) : product.images
187
+ };
188
+ return product;
189
+ }
190
+
191
+ const uploadingNecessaryImages = async (images, waUploadToServer, timeoutMs = 30000) => {
192
+ const results = await Promise.all(images.map(async (img) => {
193
+ if ('url' in img) {
194
+ const url = img.url.toString();
195
+ if (url.includes('.whatsapp.net')) {
196
+ return { url };
197
+ }
198
+ }
199
+ const { stream } = await (0, messages_media_1.getStream)(img);
200
+ const hasher = (0, crypto_1.createHash)('sha256');
201
+ const contentBlocks = [];
202
+ for await (const block of stream) {
203
+ hasher.update(block);
204
+ contentBlocks.push(block);
205
+ }
206
+ const sha = hasher.digest('base64');
207
+ const { directPath } = await waUploadToServer((0, messages_media_1.toReadable)(Buffer.concat(contentBlocks)), {
208
+ mediaType: 'product-catalog-image',
209
+ fileEncSha256B64: sha,
210
+ timeoutMs
211
+ });
212
+ return { url: (0, messages_media_1.getUrlFromDirectPath)(directPath) };
213
+ }));
214
+ return results;
215
+ };
216
+ exports.uploadingNecessaryImages = uploadingNecessaryImages;
217
+ const parseImageUrls = (mediaNode) => {
218
+ const imgNode = (0, WABinary_1.getBinaryNodeChild)(mediaNode, 'image');
219
+ return {
220
+ requested: (0, WABinary_1.getBinaryNodeChildString)(imgNode, 'request_image_url'),
221
+ original: (0, WABinary_1.getBinaryNodeChildString)(imgNode, 'original_image_url')
222
+ };
223
+ };
224
+ const parseStatusInfo = (mediaNode) => {
225
+ const node = (0, WABinary_1.getBinaryNodeChild)(mediaNode, 'status_info');
226
+ return {
227
+ status: (0, WABinary_1.getBinaryNodeChildString)(node, 'status'),
228
+ canAppeal: (0, WABinary_1.getBinaryNodeChildString)(node, 'can_appeal') === 'true',
229
+ };
230
+ };