@rexxhayanasi/elaina-baileys 1.2.0-rc.4 → 1.2.0-rc.5

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.
@@ -1,5 +1,9 @@
1
1
  "use strict";
2
2
 
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.prepareWAMessageMediaNewsletter = prepareWAMessageMediaNewsletter;
5
+ exports.getWAUploadToServerNewsletter = getWAUploadToServerNewsletter;
6
+
3
7
  const fs = require("fs");
4
8
  const Boom = require("@hapi/boom");
5
9
  const { WAProto } = require("../../WAProto");
@@ -7,137 +11,131 @@ const { MEDIA_PATH_MAP, uploadMedia } = require("./Utils");
7
11
  const { DEFAULT_ORIGIN } = require("../Defaults");
8
12
  const { unixTimestampSeconds } = require("./generics");
9
13
  const {
10
- generateThumbnail,
11
- encryptedStream,
12
- getAudioDuration,
13
- getAudioWaveform,
14
- assertColor,
14
+ generateThumbnail,
15
+ encryptedStream,
16
+ getAudioDuration,
17
+ getAudioWaveform,
18
+ assertColor,
15
19
  } = require("./messages-media");
16
20
 
21
+ /**
22
+ * Encode string ke Base64
23
+ */
17
24
  function encodeBase64EncodedStringForUpload(str) {
18
- if (!str) return "";
19
- return Buffer.from(str).toString("base64");
25
+ if (!str) return "";
26
+ return Buffer.from(str).toString("base64");
20
27
  }
21
28
 
22
-
29
+ /**
30
+ * Placeholder function, harus diimplementasi sesuai Baileys v6
31
+ */
23
32
  async function getRawMediaUploadData(media, mediaType, logger) {
24
- throw new Error("Implement getRawMediaUploadData sesuai Baileys v6 mu");
33
+ throw new Error("Implement getRawMediaUploadData sesuai Baileys v6 mu");
25
34
  }
26
35
 
36
+ /**
37
+ * Upload ke server newsletter
38
+ */
27
39
  function getWAUploadToServerNewsletter({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) {
28
- return async (filePath, { mediaType, fileEncSha256B64, timeoutMs }) => {
29
- let uploadInfo = await refreshMediaConn(false);
30
- let urls;
31
- const hosts = [...customUploadHosts, ...uploadInfo.hosts];
32
-
33
- fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
34
-
35
- const headers = {
36
- "Content-Type": "application/octet-stream",
37
- Origin: DEFAULT_ORIGIN,
38
- ...(options?.headers || {}),
39
- };
40
-
41
- for (const { hostname } of hosts) {
42
- logger.debug(`uploading to "${hostname}"`);
43
- const auth = encodeURIComponent(uploadInfo.auth);
44
- const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
45
-
46
- let result;
47
- try {
48
- result = await uploadMedia({ url, filePath, headers, timeoutMs, agent: fetchAgent }, logger);
49
-
50
- if (result?.url || result?.direct_path) {
51
- urls = {
52
- mediaUrl: result.url,
53
- directPath: result.direct_path,
54
- meta_hmac: result.meta_hmac,
55
- fbid: result.fbid,
56
- ts: result.ts,
57
- };
58
- break;
59
- } else {
60
- uploadInfo = await refreshMediaConn(true);
61
- throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
62
- }
63
- } catch (error) {
64
- const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
65
- logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? "" : ", retrying..."}`);
66
- }
67
- }
68
-
69
- if (!urls) throw new Boom("Media upload failed on all hosts", { statusCode: 500 });
70
- return urls;
71
-
72
- };
40
+ return async (filePath, { mediaType, fileEncSha256B64, timeoutMs }) => {
41
+ let uploadInfo = await refreshMediaConn(false);
42
+ let urls;
43
+ const hosts = [...customUploadHosts, ...uploadInfo.hosts];
44
+
45
+ fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
46
+
47
+ const headers = {
48
+ "Content-Type": "application/octet-stream",
49
+ Origin: DEFAULT_ORIGIN,
50
+ ...(options?.headers || {}),
51
+ };
52
+
53
+ for (const { hostname } of hosts) {
54
+ logger.debug(`uploading to "${hostname}"`);
55
+ const auth = encodeURIComponent(uploadInfo.auth);
56
+ const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
57
+
58
+ let result;
59
+ try {
60
+ result = await uploadMedia({ url, filePath, headers, timeoutMs, agent: fetchAgent }, logger);
61
+
62
+ if (result?.url || result?.direct_path) {
63
+ urls = {
64
+ mediaUrl: result.url,
65
+ directPath: result.direct_path,
66
+ meta_hmac: result.meta_hmac,
67
+ fbid: result.fbid,
68
+ ts: result.ts,
69
+ };
70
+ break;
71
+ } else {
72
+ uploadInfo = await refreshMediaConn(true);
73
+ throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
74
+ }
75
+ } catch (error) {
76
+ const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
77
+ logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname}${isLast ? "" : ", retrying..."}`);
78
+ }
79
+ }
80
+
81
+ if (!urls) throw new Boom("Media upload failed on all hosts", { statusCode: 500 });
82
+ return urls;
83
+ };
73
84
  }
74
85
 
86
+ /**
87
+ * Prepare WA message untuk newsletter
88
+ */
75
89
  async function prepareWAMessageMediaNewsletter(message, options) {
76
- const logger = options.logger;
77
-
78
- let mediaType;
79
- for (const key of Object.keys(MEDIA_PATH_MAP)) {
80
- if (key in message) {
81
- mediaType = key;
82
- }
83
- }
84
- if (!mediaType) {
85
- throw new Boom("Invalid media type", { statusCode: 400 });
86
- }
87
-
88
- const uploadData = {
89
- ...message,
90
- media: message[mediaType],
91
- };
92
- delete uploadData[mediaType];
93
-
94
- if (mediaType === "document" && !uploadData.fileName) {
95
- uploadData.fileName = "file";
90
+ const logger = options.logger;
91
+
92
+ let mediaType;
93
+ for (const key of Object.keys(MEDIA_PATH_MAP)) {
94
+ if (key in message) {
95
+ mediaType = key;
96
+ break;
97
+ }
98
+ }
99
+
100
+ if (!mediaType) {
101
+ throw new Boom("Invalid media type", { statusCode: 400 });
102
+ }
103
+
104
+ const uploadData = { ...message, media: message[mediaType] };
105
+ delete uploadData[mediaType];
106
+
107
+ if (mediaType === "document" && !uploadData.fileName) uploadData.fileName = "file";
108
+ if (!uploadData.mimetype) uploadData.mimetype = MEDIA_PATH_MAP[mediaType]?.mimetype || "application/octet-stream";
109
+
110
+ const isNewsletter = options.newsletter || (options.jid && options.jid.endsWith("@newsletter"));
111
+ if (!isNewsletter) {
112
+ throw new Boom("This function is only for newsletter", { statusCode: 400 });
113
+ }
114
+
115
+ const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(uploadData.media, mediaType, logger);
116
+ const fileSha256B64 = fileSha256.toString("base64");
117
+
118
+ const urls = await getWAUploadToServerNewsletter(options, options.refreshMediaConn)(
119
+ filePath,
120
+ { mediaType, fileEncSha256B64: fileSha256B64, timeoutMs: options.mediaUploadTimeoutMs }
121
+ );
122
+
123
+ await fs.promises.unlink(filePath);
124
+
125
+ const messageObj = {};
126
+ messageObj[`${mediaType}Message`] = {
127
+ url: urls.mediaUrl,
128
+ directPath: urls.directPath,
129
+ fileSha256,
130
+ fileLength,
131
+ ...uploadData,
132
+ media: undefined,
133
+ };
134
+
135
+ if (uploadData.ptv && mediaType === "video") {
136
+ messageObj.ptvMessage = messageObj.videoMessage;
137
+ delete messageObj.videoMessage;
138
+ }
139
+
140
+ return WAProto.Message.fromObject(messageObj);
96
141
  }
97
- if (!uploadData.mimetype) {
98
- uploadData.mimetype = MEDIA_PATH_MAP[mediaType]?.mimetype || "application/octet-stream";
99
- }
100
-
101
- const isNewsletter = options.newsletter || (options.jid && options.jid.endsWith("@newsletter"));
102
- if (!isNewsletter) {
103
- throw new Boom("This function is only for newsletter", { statusCode: 400 });
104
- }
105
-
106
- const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(
107
- uploadData.media,
108
- mediaType,
109
- logger
110
- );
111
-
112
- const fileSha256B64 = fileSha256.toString("base64");
113
-
114
- const urls = await getWAUploadToServerNewsletter(options, options.refreshMediaConn)(
115
- filePath,
116
- { mediaType, fileEncSha256B64: fileSha256B64, timeoutMs: options.mediaUploadTimeoutMs }
117
- );
118
-
119
- await fs.promises.unlink(filePath);
120
-
121
- const obj = WAProto.Message.fromObject({
122
- [${mediaType}Message]: {
123
- url: urls.mediaUrl,
124
- directPath: urls.directPath,
125
- fileSha256,
126
- fileLength,
127
- ...uploadData,
128
- media: undefined,
129
- },
130
- });
131
-
132
- if (uploadData.ptv) {
133
- obj.ptvMessage = obj.videoMessage;
134
- delete obj.videoMessage;
135
- }
136
-
137
- return obj;
138
- }
139
-
140
- module.exports = {
141
- prepareWAMessageMediaNewsletter,
142
- getWAUploadToServerNewsletter,
143
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rexxhayanasi/elaina-baileys",
3
- "version": "1.2.0-rc.4",
3
+ "version": "1.2.0-rc.5",
4
4
  "description": "Custom Baileys WhatsApp API",
5
5
  "keywords": [
6
6
  "baileys",