wa-multi-mongodb 3.10.2 → 3.10.4

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 (85) hide show
  1. package/dist/Defaults/index.js +4 -9
  2. package/dist/Error/index.js +1 -5
  3. package/dist/Messaging/index.d.ts +1 -1
  4. package/dist/Messaging/index.js +123 -137
  5. package/dist/Profile/index.d.ts +1 -1
  6. package/dist/Profile/index.js +6 -10
  7. package/dist/Socket/index.d.ts +160 -1
  8. package/dist/Socket/index.d.ts.map +1 -1
  9. package/dist/Socket/index.js +462 -200
  10. package/dist/Types/index.d.ts +21 -0
  11. package/dist/Types/index.d.ts.map +1 -1
  12. package/dist/Types/index.js +1 -2
  13. package/dist/Types/profile.js +1 -2
  14. package/dist/Utils/create-delay.js +2 -7
  15. package/dist/Utils/error.js +1 -4
  16. package/dist/Utils/group-cache.js +8 -15
  17. package/dist/Utils/index.d.ts +5 -5
  18. package/dist/Utils/index.js +5 -21
  19. package/dist/Utils/is-exist.d.ts +1 -1
  20. package/dist/Utils/is-exist.js +7 -10
  21. package/dist/Utils/lid-utils.js +13 -20
  22. package/dist/Utils/message-status.d.ts +1 -1
  23. package/dist/Utils/message-status.js +7 -11
  24. package/dist/Utils/mongo-auth-state.js +10 -13
  25. package/dist/Utils/phone-to-jid.js +6 -14
  26. package/dist/Utils/save-media.d.ts +1 -1
  27. package/dist/Utils/save-media.js +16 -26
  28. package/dist/cjs/Defaults/index.d.ts +21 -0
  29. package/dist/cjs/Defaults/index.d.ts.map +1 -0
  30. package/dist/cjs/Defaults/index.js +31 -0
  31. package/dist/cjs/Error/index.d.ts +5 -0
  32. package/dist/cjs/Error/index.d.ts.map +1 -0
  33. package/dist/cjs/Error/index.js +14 -0
  34. package/dist/cjs/Messaging/index.d.ts +45 -0
  35. package/dist/cjs/Messaging/index.d.ts.map +1 -0
  36. package/dist/cjs/Messaging/index.js +784 -0
  37. package/dist/cjs/Profile/index.d.ts +9 -0
  38. package/dist/cjs/Profile/index.d.ts.map +1 -0
  39. package/dist/cjs/Profile/index.js +34 -0
  40. package/dist/cjs/Socket/index.d.ts +264 -0
  41. package/dist/cjs/Socket/index.d.ts.map +1 -0
  42. package/dist/cjs/Socket/index.js +1210 -0
  43. package/dist/cjs/Types/index.d.ts +95 -0
  44. package/dist/cjs/Types/index.d.ts.map +1 -0
  45. package/dist/cjs/Types/index.js +2 -0
  46. package/dist/cjs/Types/profile.d.ts +5 -0
  47. package/dist/cjs/Types/profile.d.ts.map +1 -0
  48. package/dist/cjs/Types/profile.js +2 -0
  49. package/dist/cjs/Utils/create-delay.d.ts +17 -0
  50. package/dist/cjs/Utils/create-delay.d.ts.map +1 -0
  51. package/dist/cjs/Utils/create-delay.js +38 -0
  52. package/dist/cjs/Utils/error.d.ts +4 -0
  53. package/dist/cjs/Utils/error.d.ts.map +1 -0
  54. package/dist/cjs/Utils/error.js +8 -0
  55. package/dist/cjs/Utils/group-cache.d.ts +23 -0
  56. package/dist/cjs/Utils/group-cache.d.ts.map +1 -0
  57. package/dist/cjs/Utils/group-cache.js +176 -0
  58. package/dist/cjs/Utils/index.d.ts +6 -0
  59. package/dist/cjs/Utils/index.d.ts.map +1 -0
  60. package/dist/cjs/Utils/index.js +23 -0
  61. package/dist/cjs/Utils/is-exist.d.ts +6 -0
  62. package/dist/cjs/Utils/is-exist.d.ts.map +1 -0
  63. package/dist/cjs/Utils/is-exist.js +53 -0
  64. package/dist/cjs/Utils/lid-utils.d.ts +26 -0
  65. package/dist/cjs/Utils/lid-utils.d.ts.map +1 -0
  66. package/dist/cjs/Utils/lid-utils.js +81 -0
  67. package/dist/cjs/Utils/message-status.d.ts +4 -0
  68. package/dist/cjs/Utils/message-status.d.ts.map +1 -0
  69. package/dist/cjs/Utils/message-status.js +18 -0
  70. package/dist/cjs/Utils/mongo-auth-state.d.ts +15 -0
  71. package/dist/cjs/Utils/mongo-auth-state.d.ts.map +1 -0
  72. package/dist/cjs/Utils/mongo-auth-state.js +109 -0
  73. package/dist/cjs/Utils/phone-to-jid.d.ts +17 -0
  74. package/dist/cjs/Utils/phone-to-jid.d.ts.map +1 -0
  75. package/dist/cjs/Utils/phone-to-jid.js +51 -0
  76. package/dist/cjs/Utils/save-media.d.ts +6 -0
  77. package/dist/cjs/Utils/save-media.d.ts.map +1 -0
  78. package/dist/cjs/Utils/save-media.js +55 -0
  79. package/dist/cjs/index.d.ts +8 -0
  80. package/dist/cjs/index.d.ts.map +1 -0
  81. package/dist/cjs/index.js +46 -0
  82. package/dist/index.d.ts +6 -6
  83. package/dist/index.js +7 -46
  84. package/package.json +57 -42
  85. package/readme.md +170 -3
@@ -0,0 +1,95 @@
1
+ import { WAMessageUpdate, proto } from "baileys";
2
+ /**
3
+ * Supported browser types for WhatsApp connection
4
+ * - ubuntu: Uses Ubuntu browser agent (default)
5
+ * - macOS: Uses macOS browser agent
6
+ * - windows: Uses Windows browser agent
7
+ * - appropriate: Uses appropriate browser based on platform
8
+ */
9
+ export type BrowserType = "ubuntu" | "macOS" | "windows" | "appropriate";
10
+ export interface SendMessageTypes {
11
+ to: string | number;
12
+ text?: string;
13
+ sessionId: string;
14
+ isGroup?: boolean;
15
+ answering?: proto.IWebMessageInfo;
16
+ }
17
+ export interface SendMessageWithMentionTypes extends SendMessageTypes {
18
+ mentions?: string[];
19
+ }
20
+ export interface SendMediaTypes extends SendMessageTypes {
21
+ media: string | Buffer;
22
+ type: "image" | "video" | "audio" | "pdf" | "xls" | "xlsx" | "doc" | "docx" | "zip" | "mp3";
23
+ caption?: string;
24
+ fileName?: string;
25
+ }
26
+ export interface SendTypingTypes extends SendMessageTypes {
27
+ duration: number;
28
+ }
29
+ export interface SendReadTypes {
30
+ sessionId: string;
31
+ key: proto.IMessageKey;
32
+ }
33
+ export interface MessageReceived extends proto.IWebMessageInfo {
34
+ /**
35
+ * Your Session ID
36
+ */
37
+ sessionId: string;
38
+ /**
39
+ * @param path save image location path with extension
40
+ * @example "./myimage.jpg"
41
+ */
42
+ saveImage: (path: string) => Promise<void>;
43
+ /**
44
+ * @param path save video location path with extension
45
+ * @example "./myvideo.mp4"
46
+ */
47
+ saveVideo: (path: string) => Promise<void>;
48
+ /**
49
+ * @param path save audio location path with extension
50
+ * @example "./myaudio.mp3"
51
+ */
52
+ saveAudio: (path: string) => Promise<void>;
53
+ /**
54
+ * @param path save image location path without extension
55
+ * @example "./mydocument"
56
+ */
57
+ saveDocument: (path: string) => Promise<void>;
58
+ }
59
+ export interface StartSessionParams {
60
+ /**
61
+ * Print QR Code into Terminal
62
+ */
63
+ printQR?: boolean;
64
+ /**
65
+ * Browser type for WhatsApp connection
66
+ * Options: "ubuntu" (default), "macOS", "windows", "appropriate"
67
+ * @default "ubuntu"
68
+ */
69
+ browserType?: BrowserType;
70
+ /**
71
+ * Custom browser/app name displayed in WhatsApp
72
+ * This name will appear in "Linked Devices" on your phone
73
+ * @default "Chrome"
74
+ * @example "My Bot App"
75
+ */
76
+ browserName?: string;
77
+ onQRUpdated?: (qr: string) => void;
78
+ onConnected?: () => void;
79
+ onConnecting?: () => void;
80
+ onDisconnected?: () => void;
81
+ onPairingCode?: (code: string) => void;
82
+ onMessageReceived?: (message: MessageReceived) => void;
83
+ onMessageUpdated?: (message: MessageUpdated) => void;
84
+ }
85
+ export interface StartSessionWithPairingCodeParams {
86
+ /**
87
+ * Phone Number with Country Code
88
+ */
89
+ phoneNumber: string;
90
+ }
91
+ export type MessageUpdated = WAMessageUpdate & {
92
+ sessionId: string;
93
+ messageStatus: "error" | "pending" | "server" | "delivered" | "read" | "played";
94
+ };
95
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/Types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEjD;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,OAAO,GAAG,SAAS,GAAG,aAAa,CAAC;AAEzE,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,SAAS,CAAC,EAAE,KAAK,CAAC,eAAe,CAAC;CACnC;AAED,MAAM,WAAW,2BAA4B,SAAQ,gBAAgB;IACnE,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB;AAED,MAAM,WAAW,cAAe,SAAQ,gBAAgB;IACtD,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,IAAI,EAAE,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,CAAC;IAC5F,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,eAAgB,SAAQ,gBAAgB;IACvD,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,KAAK,CAAC,WAAW,CAAC;CACxB;AAED,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,eAAe;IAC5D;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3C;;;OAGG;IACH,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3C;;;OAGG;IACH,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3C;;;OAGG;IACH,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/C;AAED,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;;;OAIG;IACH,WAAW,CAAC,EAAE,WAAW,CAAC;IAE1B;;;;;OAKG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAGrB,WAAW,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,WAAW,CAAC,EAAE,MAAM,IAAI,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,IAAI,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,IAAI,CAAC;IAC5B,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAGvC,iBAAiB,CAAC,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;IACvD,gBAAgB,CAAC,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC;CACtD;AAED,MAAM,WAAW,iCAAiC;IAChD;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,MAAM,cAAc,GAAG,eAAe,GAAG;IAC7C,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EACX,OAAO,GACP,SAAS,GACT,QAAQ,GACR,WAAW,GACX,MAAM,GACN,QAAQ,CAAC;CACZ,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export type GetProfileInfoProps = {
2
+ sessionId: string;
3
+ target: string;
4
+ };
5
+ //# sourceMappingURL=profile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"profile.d.ts","sourceRoot":"","sources":["../../../src/Types/profile.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Create delay for certain milliseconds
3
+ *
4
+ * @param ms - Delay in milliseconds
5
+ * @returns Promise that resolves after the specified delay
6
+ */
7
+ export declare const createDelay: (ms: number) => Promise<unknown>;
8
+ /**
9
+ * Execute a promise with a timeout
10
+ *
11
+ * @param promise - The promise to execute
12
+ * @param timeoutMs - Timeout in milliseconds
13
+ * @param errorMessage - Custom error message for timeout
14
+ * @returns Promise that resolves with the result or rejects with timeout error
15
+ */
16
+ export declare const withTimeout: <T>(promise: Promise<T>, timeoutMs?: number, errorMessage?: string) => Promise<T>;
17
+ //# sourceMappingURL=create-delay.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-delay.d.ts","sourceRoot":"","sources":["../../../src/Utils/create-delay.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,IAAI,MAAM,qBAErC,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,EAC3B,SAAS,OAAO,CAAC,CAAC,CAAC,EACnB,YAAW,MAAc,EACzB,eAAc,MAA8B,KAC3C,OAAO,CAAC,CAAC,CAgBX,CAAC"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withTimeout = exports.createDelay = void 0;
4
+ /**
5
+ * Create delay for certain milliseconds
6
+ *
7
+ * @param ms - Delay in milliseconds
8
+ * @returns Promise that resolves after the specified delay
9
+ */
10
+ const createDelay = (ms) => {
11
+ return new Promise((resolve) => setTimeout(resolve, ms));
12
+ };
13
+ exports.createDelay = createDelay;
14
+ /**
15
+ * Execute a promise with a timeout
16
+ *
17
+ * @param promise - The promise to execute
18
+ * @param timeoutMs - Timeout in milliseconds
19
+ * @param errorMessage - Custom error message for timeout
20
+ * @returns Promise that resolves with the result or rejects with timeout error
21
+ */
22
+ const withTimeout = (promise, timeoutMs = 10000, errorMessage = 'Operation timed out') => {
23
+ return new Promise((resolve, reject) => {
24
+ const timeoutId = setTimeout(() => {
25
+ reject(new Error(errorMessage));
26
+ }, timeoutMs);
27
+ promise
28
+ .then((result) => {
29
+ clearTimeout(timeoutId);
30
+ resolve(result);
31
+ })
32
+ .catch((error) => {
33
+ clearTimeout(timeoutId);
34
+ reject(error);
35
+ });
36
+ });
37
+ };
38
+ exports.withTimeout = withTimeout;
@@ -0,0 +1,4 @@
1
+ export default class ValidationError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ //# sourceMappingURL=error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../../src/Utils/error.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,KAAK;gBACpC,OAAO,EAAE,MAAM;CAG5B"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ValidationError extends Error {
4
+ constructor(message) {
5
+ super(message);
6
+ }
7
+ }
8
+ exports.default = ValidationError;
@@ -0,0 +1,23 @@
1
+ import { GroupMetadata } from "baileys";
2
+ export declare class GroupMetadataCache {
3
+ private static instance;
4
+ private cache;
5
+ private mongoClient;
6
+ private mongoCollection;
7
+ private mongoURI;
8
+ private constructor();
9
+ static getInstance(): GroupMetadataCache;
10
+ setMongoURI(uri: string): Promise<void>;
11
+ private createCacheKey;
12
+ get(sessionId: string, groupId: string): Promise<GroupMetadata | null>;
13
+ set(sessionId: string, groupId: string, metadata: GroupMetadata): Promise<void>;
14
+ delete(sessionId: string, groupId: string): Promise<void>;
15
+ clearSessionCache(sessionId: string): Promise<void>;
16
+ setConfig(options: {
17
+ stdTTL?: number;
18
+ checkperiod?: number;
19
+ }): void;
20
+ flush(): void;
21
+ }
22
+ export declare const groupCache: GroupMetadataCache;
23
+ //# sourceMappingURL=group-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"group-cache.d.ts","sourceRoot":"","sources":["../../../src/Utils/group-cache.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAKxC,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAqB;IAC5C,OAAO,CAAC,KAAK,CAAY;IACzB,OAAO,CAAC,WAAW,CAA4B;IAC/C,OAAO,CAAC,eAAe,CAAa;IACpC,OAAO,CAAC,QAAQ,CAAuB;IAGvC,OAAO;WAUO,WAAW,IAAI,kBAAkB;IAQlC,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAuBpD,OAAO,CAAC,cAAc;IAKT,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAoCtE,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IA4B/E,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAqBzD,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAkBzD,SAAS,CAAC,OAAO,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE;IAU5D,KAAK,IAAI,IAAI;CAGrB;AAGD,eAAO,MAAM,UAAU,oBAAmC,CAAC"}
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.groupCache = exports.GroupMetadataCache = void 0;
16
+ const node_cache_1 = __importDefault(require("node-cache"));
17
+ const mongodb_1 = require("mongodb");
18
+ const Defaults_1 = require("../Defaults");
19
+ // Class untuk mengelola cache group metadata dengan pendekatan hybrid (NodeCache + MongoDB)
20
+ class GroupMetadataCache {
21
+ // Konstruktor private untuk singleton pattern
22
+ constructor() {
23
+ this.mongoClient = null;
24
+ this.mongoCollection = null;
25
+ this.mongoURI = null;
26
+ // Inisialisasi node-cache dengan standar TTL 5 menit
27
+ this.cache = new node_cache_1.default({
28
+ stdTTL: 5 * 60, // 5 menit dalam detik
29
+ checkperiod: 60, // Cek expired keys setiap 1 menit
30
+ useClones: false // Untuk performa lebih baik
31
+ });
32
+ }
33
+ // Mendapatkan instance singleton
34
+ static getInstance() {
35
+ if (!GroupMetadataCache.instance) {
36
+ GroupMetadataCache.instance = new GroupMetadataCache();
37
+ }
38
+ return GroupMetadataCache.instance;
39
+ }
40
+ // Mengatur MongoDB URI dan menginisialisasi koneksi
41
+ setMongoURI(uri) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ if (this.mongoURI !== uri) {
44
+ this.mongoURI = uri;
45
+ // Tutup koneksi yang ada jika ada
46
+ if (this.mongoClient) {
47
+ yield this.mongoClient.close();
48
+ }
49
+ this.mongoClient = new mongodb_1.MongoClient(uri);
50
+ yield this.mongoClient.connect();
51
+ // Inisialisasi collection
52
+ const dbName = Defaults_1.CREDENTIALS.MONGO_DB_NAME;
53
+ const collectionName = "group_metadata";
54
+ this.mongoCollection = this.mongoClient.db(dbName).collection(collectionName);
55
+ // Buat indeks pada id grup dan sessionId untuk performa query
56
+ yield this.mongoCollection.createIndex({ id: 1, sessionId: 1 });
57
+ }
58
+ });
59
+ }
60
+ // Membuat kunci cache yang mencakup sessionId
61
+ createCacheKey(sessionId, groupId) {
62
+ return `${sessionId}:${groupId}`;
63
+ }
64
+ // Mendapatkan metadata grup dari cache atau MongoDB
65
+ get(sessionId, groupId) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ // Buat kunci cache dengan kombinasi sessionId dan groupId
68
+ const cacheKey = this.createCacheKey(sessionId, groupId);
69
+ // Coba ambil dari cache memory dulu
70
+ const cachedData = this.cache.get(cacheKey);
71
+ if (cachedData) {
72
+ return cachedData;
73
+ }
74
+ // Jika tidak ada di cache dan MongoDB tersedia, coba ambil dari MongoDB
75
+ if (this.mongoCollection) {
76
+ try {
77
+ const data = yield this.mongoCollection.findOne({
78
+ id: groupId,
79
+ sessionId: sessionId
80
+ });
81
+ if (data) {
82
+ // Hapus _id dari MongoDB yang tidak diperlukan
83
+ delete data._id;
84
+ delete data.sessionId; // Hapus sessionId dari data yang dikembalikan
85
+ // Simpan ke cache memory untuk akses cepat
86
+ this.cache.set(cacheKey, data);
87
+ return data;
88
+ }
89
+ }
90
+ catch (error) {
91
+ console.error("Error fetching group metadata from MongoDB:", error);
92
+ }
93
+ }
94
+ return null;
95
+ });
96
+ }
97
+ // Menyimpan metadata grup ke cache dan MongoDB
98
+ set(sessionId, groupId, metadata) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ // Buat kunci cache dengan kombinasi sessionId dan groupId
101
+ const cacheKey = this.createCacheKey(sessionId, groupId);
102
+ // Simpan ke cache memory
103
+ this.cache.set(cacheKey, metadata);
104
+ // Jika MongoDB tersedia, simpan juga ke MongoDB
105
+ if (this.mongoCollection) {
106
+ try {
107
+ // Tambahkan sessionId ke data yang disimpan
108
+ const dataToStore = Object.assign(Object.assign({}, metadata), { sessionId });
109
+ yield this.mongoCollection.updateOne({
110
+ id: groupId,
111
+ sessionId: sessionId
112
+ }, { $set: dataToStore }, { upsert: true });
113
+ }
114
+ catch (error) {
115
+ console.error("Error saving group metadata to MongoDB:", error);
116
+ }
117
+ }
118
+ });
119
+ }
120
+ // Menghapus metadata grup dari cache dan MongoDB
121
+ delete(sessionId, groupId) {
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ // Buat kunci cache dengan kombinasi sessionId dan groupId
124
+ const cacheKey = this.createCacheKey(sessionId, groupId);
125
+ // Hapus dari cache memory
126
+ this.cache.del(cacheKey);
127
+ // Jika MongoDB tersedia, hapus juga dari MongoDB
128
+ if (this.mongoCollection) {
129
+ try {
130
+ yield this.mongoCollection.deleteOne({
131
+ id: groupId,
132
+ sessionId: sessionId
133
+ });
134
+ }
135
+ catch (error) {
136
+ console.error("Error deleting group metadata from MongoDB:", error);
137
+ }
138
+ }
139
+ });
140
+ }
141
+ // Menghapus semua cache untuk session tertentu
142
+ clearSessionCache(sessionId) {
143
+ return __awaiter(this, void 0, void 0, function* () {
144
+ // Hapus dari MongoDB
145
+ if (this.mongoCollection) {
146
+ try {
147
+ yield this.mongoCollection.deleteMany({ sessionId: sessionId });
148
+ }
149
+ catch (error) {
150
+ console.error(`Error clearing session ${sessionId} cache from MongoDB:`, error);
151
+ }
152
+ }
153
+ // Hapus dari memory cache
154
+ // Karena NodeCache tidak mendukung wildcard delete, kita harus mendapatkan semua kunci dulu
155
+ const allKeys = this.cache.keys();
156
+ const sessionKeys = allKeys.filter(key => key.startsWith(`${sessionId}:`));
157
+ sessionKeys.forEach(key => this.cache.del(key));
158
+ });
159
+ }
160
+ // Mengatur atau mengubah konfigurasi cache
161
+ setConfig(options) {
162
+ if (options.stdTTL) {
163
+ this.cache.options.stdTTL = options.stdTTL;
164
+ }
165
+ if (options.checkperiod) {
166
+ this.cache.options.checkperiod = options.checkperiod;
167
+ }
168
+ }
169
+ // Membersihkan seluruh cache
170
+ flush() {
171
+ this.cache.flushAll();
172
+ }
173
+ }
174
+ exports.GroupMetadataCache = GroupMetadataCache;
175
+ // Export singleton instance
176
+ exports.groupCache = GroupMetadataCache.getInstance();
@@ -0,0 +1,6 @@
1
+ export * from "./phone-to-jid";
2
+ export * from "./is-exist";
3
+ export * from "./create-delay";
4
+ export * from "./group-cache";
5
+ export * from "./lid-utils";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/Utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,23 @@
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
+ __exportStar(require("./phone-to-jid"), exports);
18
+ __exportStar(require("./is-exist"), exports);
19
+ __exportStar(require("./create-delay"), exports);
20
+ __exportStar(require("./group-cache"), exports);
21
+ __exportStar(require("./lid-utils"), exports);
22
+ // Note: setCredentialsDir & setMongoDBNames have been moved to src/Socket/index.ts
23
+ // Please import them directly from there instead
@@ -0,0 +1,6 @@
1
+ import { SendMessageTypes } from "../Types";
2
+ export declare function isExist(media: string | Buffer): {
3
+ url: string;
4
+ } | Buffer;
5
+ export declare function isExist(params: SendMessageTypes): Promise<boolean>;
6
+ //# sourceMappingURL=is-exist.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-exist.d.ts","sourceRoot":"","sources":["../../../src/Utils/is-exist.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAG5C,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC;AAC1E,wBAAgB,OAAO,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC"}
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.isExist = isExist;
13
+ const Error_1 = require("../Error");
14
+ const Socket_1 = require("../Socket");
15
+ const phone_to_jid_1 = require("./phone-to-jid");
16
+ function isExist(mediaOrParams) {
17
+ // Jika parameter adalah SendMessageTypes, gunakan fungsi asli
18
+ if (typeof mediaOrParams === 'object' && 'sessionId' in mediaOrParams) {
19
+ return isExistSession(mediaOrParams);
20
+ }
21
+ // Jika parameter adalah string (URL), kembalikan sebagai objek url
22
+ if (typeof mediaOrParams === 'string') {
23
+ return { url: mediaOrParams };
24
+ }
25
+ // Jika parameter adalah Buffer, kembalikan langsung
26
+ return mediaOrParams;
27
+ }
28
+ // Fungsi asli untuk memeriksa keberadaan session
29
+ function isExistSession(_a) {
30
+ return __awaiter(this, arguments, void 0, function* ({ sessionId, to, isGroup = false, }) {
31
+ var _b, _c;
32
+ try {
33
+ const session = (0, Socket_1.getSession)(sessionId);
34
+ if (!session)
35
+ throw new Error_1.WhatsappError("Session ID Not Found!");
36
+ const receiver = (0, phone_to_jid_1.phoneToJid)({
37
+ to: to,
38
+ isGroup: isGroup,
39
+ });
40
+ if (!isGroup) {
41
+ const one = Boolean((_c = (_b = (yield (session === null || session === void 0 ? void 0 : session.onWhatsApp(receiver)))) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.exists);
42
+ return one;
43
+ }
44
+ else {
45
+ return Boolean((yield session.groupMetadata(receiver)).id);
46
+ }
47
+ }
48
+ catch (error) {
49
+ throw error;
50
+ }
51
+ });
52
+ }
53
+ ;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Fungsi untuk mendeteksi apakah sebuah JID menggunakan format PN (@s.whatsapp.net)
3
+ * @param jid JID yang akan dicek
4
+ * @returns true jika JID menggunakan format PN
5
+ */
6
+ export declare const isPnJid: (jid: string) => boolean;
7
+ /**
8
+ * Fungsi untuk mengekstrak nomor telepon dari JID (baik format @s.whatsapp.net atau @lid)
9
+ * @param jid JID yang akan diekstrak
10
+ * @returns nomor telepon tanpa suffix
11
+ */
12
+ export declare const extractPhoneNumber: (jid: string) => string;
13
+ /**
14
+ * Fungsi untuk konversi JID dari format LID ke format PN jika diperlukan
15
+ * Catatan: Fungsi ini hanya untuk kompatibilitas, karena WhatsApp sekarang mendukung kedua format
16
+ * @param jid JID yang akan dikonversi
17
+ * @returns JID dalam format yang sesuai
18
+ */
19
+ export declare const normalizeJid: (jid: string) => string;
20
+ /**
21
+ * Fungsi untuk memvalidasi format JID
22
+ * @param jid JID yang akan divalidasi
23
+ * @returns true jika JID valid
24
+ */
25
+ export declare const isValidJid: (jid: string) => boolean;
26
+ //# sourceMappingURL=lid-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lid-utils.d.ts","sourceRoot":"","sources":["../../../src/Utils/lid-utils.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,eAAO,MAAM,OAAO,GAAI,KAAK,MAAM,KAAG,OAErC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,GAAI,KAAK,MAAM,KAAG,MAQhD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,KAAK,MAAM,KAAG,MAe1C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,UAAU,GAAI,KAAK,MAAM,KAAG,OA6BxC,CAAC"}
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidJid = exports.normalizeJid = exports.extractPhoneNumber = exports.isPnJid = void 0;
4
+ const Error_1 = require("../Error");
5
+ const phone_to_jid_1 = require("./phone-to-jid");
6
+ /**
7
+ * Fungsi untuk mendeteksi apakah sebuah JID menggunakan format PN (@s.whatsapp.net)
8
+ * @param jid JID yang akan dicek
9
+ * @returns true jika JID menggunakan format PN
10
+ */
11
+ const isPnJid = (jid) => {
12
+ return jid.includes("@s.whatsapp.net");
13
+ };
14
+ exports.isPnJid = isPnJid;
15
+ /**
16
+ * Fungsi untuk mengekstrak nomor telepon dari JID (baik format @s.whatsapp.net atau @lid)
17
+ * @param jid JID yang akan diekstrak
18
+ * @returns nomor telepon tanpa suffix
19
+ */
20
+ const extractPhoneNumber = (jid) => {
21
+ if (!jid)
22
+ throw new Error_1.WhatsappError('parameter "jid" is required');
23
+ // Hapus suffix @s.whatsapp.net atau @lid
24
+ const phoneNumber = jid.replace(/@(s\.whatsapp\.net|lid)$/, '');
25
+ // Hapus karakter non-digit
26
+ return phoneNumber.replace(/\D/g, '');
27
+ };
28
+ exports.extractPhoneNumber = extractPhoneNumber;
29
+ /**
30
+ * Fungsi untuk konversi JID dari format LID ke format PN jika diperlukan
31
+ * Catatan: Fungsi ini hanya untuk kompatibilitas, karena WhatsApp sekarang mendukung kedua format
32
+ * @param jid JID yang akan dikonversi
33
+ * @returns JID dalam format yang sesuai
34
+ */
35
+ const normalizeJid = (jid) => {
36
+ if (!jid)
37
+ throw new Error_1.WhatsappError('parameter "jid" is required');
38
+ // Jika sudah dalam format yang valid, kembalikan apa adanya
39
+ if ((0, phone_to_jid_1.isLidJid)(jid) || (0, exports.isPnJid)(jid) || jid.includes('@g.us') || jid.includes('@broadcast')) {
40
+ return jid;
41
+ }
42
+ // Jika hanya nomor telepon, tambahkan suffix default
43
+ const phoneNumber = jid.replace(/\D/g, '');
44
+ if (phoneNumber.length > 0) {
45
+ return `${phoneNumber}@s.whatsapp.net`;
46
+ }
47
+ throw new Error_1.WhatsappError(`Invalid JID format: ${jid}`);
48
+ };
49
+ exports.normalizeJid = normalizeJid;
50
+ /**
51
+ * Fungsi untuk memvalidasi format JID
52
+ * @param jid JID yang akan divalidasi
53
+ * @returns true jika JID valid
54
+ */
55
+ const isValidJid = (jid) => {
56
+ if (!jid)
57
+ return false;
58
+ // Validasi format @s.whatsapp.net
59
+ if ((0, exports.isPnJid)(jid)) {
60
+ const phoneNumber = (0, exports.extractPhoneNumber)(jid);
61
+ return phoneNumber.length >= 10 && phoneNumber.length <= 15;
62
+ }
63
+ // Validasi format @lid
64
+ if ((0, phone_to_jid_1.isLidJid)(jid)) {
65
+ const lidPart = jid.split('@')[0];
66
+ return lidPart.length > 0;
67
+ }
68
+ // Validasi format grup
69
+ if (jid.includes('@g.us')) {
70
+ const groupPart = jid.split('@')[0];
71
+ // Format grup WhatsApp bisa dengan atau tanpa tanda hubung
72
+ // Beberapa grup baru mungkin tidak menggunakan tanda hubung
73
+ return groupPart.length > 0;
74
+ }
75
+ // Validasi format broadcast
76
+ if (jid.includes('@broadcast')) {
77
+ return true;
78
+ }
79
+ return false;
80
+ };
81
+ exports.isValidJid = isValidJid;
@@ -0,0 +1,4 @@
1
+ import { proto } from "baileys";
2
+ import { MessageUpdated } from "../Types";
3
+ export declare const parseMessageStatusCodeToReadable: (code: proto.WebMessageInfo.Status) => MessageUpdated["messageStatus"];
4
+ //# sourceMappingURL=message-status.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message-status.d.ts","sourceRoot":"","sources":["../../../src/Utils/message-status.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE1C,eAAO,MAAM,gCAAgC,GAC3C,MAAM,KAAK,CAAC,cAAc,CAAC,MAAM,KAChC,cAAc,CAAC,eAAe,CAQhC,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseMessageStatusCodeToReadable = void 0;
4
+ const baileys_1 = require("baileys");
5
+ const parseMessageStatusCodeToReadable = (code) => {
6
+ if (code == baileys_1.proto.WebMessageInfo.Status.PENDING)
7
+ return "pending";
8
+ if (code == baileys_1.proto.WebMessageInfo.Status.SERVER_ACK)
9
+ return "server";
10
+ if (code == baileys_1.proto.WebMessageInfo.Status.DELIVERY_ACK)
11
+ return "delivered";
12
+ if (code == baileys_1.proto.WebMessageInfo.Status.READ)
13
+ return "read";
14
+ if (code == baileys_1.proto.WebMessageInfo.Status.PLAYED)
15
+ return "played";
16
+ return "error";
17
+ };
18
+ exports.parseMessageStatusCodeToReadable = parseMessageStatusCodeToReadable;