@neelegirl/baileys 2.1.5 → 2.1.7

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.
@@ -10,37 +10,72 @@ const WABinary_1 = require("../WABinary")
10
10
  const generics_1 = require("./generics")
11
11
  const messages_1 = require("./messages")
12
12
  const messages_media_1 = require("./messages-media")
13
- const inflatePromise = util_1.promisify(zlib_1.inflate)
13
+
14
+ const inflatePromise = (0, util_1.promisify)(zlib_1.inflate)
15
+
16
+ const extractPnFromMessages = (messages) => {
17
+ for (const msgItem of messages || []) {
18
+ const message = msgItem.message
19
+ if (!message?.key?.fromMe || !message.userReceipt?.length) {
20
+ continue
21
+ }
22
+ const userJid = message.userReceipt[0]?.userJid
23
+ if (userJid && ((0, WABinary_1.isPnUser)(userJid) || (0, WABinary_1.isHostedPnUser)(userJid))) {
24
+ return userJid
25
+ }
26
+ }
27
+ }
14
28
 
15
29
  const downloadHistory = async (msg, options) => {
16
- const stream = await messages_media_1.downloadContentFromMessage(msg, 'md-msg-hist', { options })
30
+ const stream = await (0, messages_media_1.downloadContentFromMessage)(msg, 'md-msg-hist', { options })
17
31
  const bufferArray = []
18
32
  for await (const chunk of stream) {
19
33
  bufferArray.push(chunk)
20
34
  }
21
35
  let buffer = Buffer.concat(bufferArray)
22
- // decompress buffer
23
36
  buffer = await inflatePromise(buffer)
24
- const syncData = WAProto_1.proto.HistorySync.decode(buffer)
25
- return syncData
37
+ return WAProto_1.proto.HistorySync.decode(buffer)
26
38
  }
27
39
 
28
- const processHistoryMessage = (item) => {
40
+ const processHistoryMessage = (item, logger) => {
29
41
  const messages = []
30
42
  const contacts = []
31
43
  const chats = []
44
+ const lidPnMappings = []
45
+ logger?.trace?.({ progress: item.progress }, `processing history of type ${item.syncType}`)
46
+ for (const mapping of item.phoneNumberToLidMappings || []) {
47
+ if (mapping.lidJid && mapping.pnJid) {
48
+ lidPnMappings.push({ lid: mapping.lidJid, pn: mapping.pnJid })
49
+ }
50
+ }
32
51
  switch (item.syncType) {
33
52
  case WAProto_1.proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP:
34
53
  case WAProto_1.proto.HistorySync.HistorySyncType.RECENT:
35
54
  case WAProto_1.proto.HistorySync.HistorySyncType.FULL:
36
55
  case WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND:
37
- for (const chat of item.conversations) {
38
- contacts.push({
56
+ for (const chat of item.conversations || []) {
57
+ contacts.push({
39
58
  id: chat.id,
40
- name: chat.name || undefined,
41
- lid: chat.lidJid || undefined,
42
- jid: (0, WABinary_1.isJidUser)(chat.id) ? chat.id : undefined
43
- });
59
+ name: chat.displayName || chat.name || chat.username || undefined,
60
+ lid: chat.lidJid || chat.accountLid || undefined,
61
+ jid: ((0, WABinary_1.isPnUser)(chat.id) || (0, WABinary_1.isHostedPnUser)(chat.id)) ? chat.id : undefined,
62
+ phoneNumber: chat.pnJid || (((0, WABinary_1.isPnUser)(chat.id) || (0, WABinary_1.isHostedPnUser)(chat.id)) ? chat.id : undefined)
63
+ })
64
+ const chatId = chat.id
65
+ const isLid = (0, WABinary_1.isLidUser)(chatId) || (0, WABinary_1.isHostedLidUser)(chatId)
66
+ const isPn = (0, WABinary_1.isPnUser)(chatId) || (0, WABinary_1.isHostedPnUser)(chatId)
67
+ if (isLid && chat.pnJid) {
68
+ lidPnMappings.push({ lid: chatId, pn: chat.pnJid })
69
+ }
70
+ else if (isPn && chat.lidJid) {
71
+ lidPnMappings.push({ lid: chat.lidJid, pn: chatId })
72
+ }
73
+ else if (isLid && !chat.pnJid) {
74
+ const pnFromReceipt = extractPnFromMessages(chat.messages || [])
75
+ if (pnFromReceipt) {
76
+ lidPnMappings.push({ lid: chatId, pn: pnFromReceipt })
77
+ }
78
+ }
44
79
  const msgs = chat.messages || []
45
80
  delete chat.messages
46
81
  delete chat.archived
@@ -50,35 +85,33 @@ const processHistoryMessage = (item) => {
50
85
  const message = item.message
51
86
  messages.push(message)
52
87
  if (!chat.messages?.length) {
53
- // keep only the most recent message in the chat array
54
88
  chat.messages = [{ message }]
55
89
  }
56
90
  if (!message.key.fromMe && !chat.lastMessageRecvTimestamp) {
57
- chat.lastMessageRecvTimestamp = generics_1.toNumber(message.messageTimestamp)
91
+ chat.lastMessageRecvTimestamp = (0, generics_1.toNumber)(message.messageTimestamp)
58
92
  }
59
- if (message.messageStubType === Types_1.WAMessageStubType.BIZ_PRIVACY_MODE_TO_BSP
60
- || message.messageStubType === Types_1.WAMessageStubType.BIZ_PRIVACY_MODE_TO_FB
93
+ if ((message.messageStubType === Types_1.WAMessageStubType.BIZ_PRIVACY_MODE_TO_BSP
94
+ || message.messageStubType === Types_1.WAMessageStubType.BIZ_PRIVACY_MODE_TO_FB)
61
95
  && message.messageStubParameters?.[0]) {
62
96
  contacts.push({
63
97
  id: message.key.participant || message.key.remoteJid,
64
- verifiedName: message.messageStubParameters?.[0]
98
+ verifiedName: message.messageStubParameters[0]
65
99
  })
66
100
  }
67
101
  }
68
- if (WABinary_1.isJidUser(chat.id) && chat.readOnly && chat.archived) {
69
- delete chat.readOnly
70
- }
71
102
  chats.push({ ...chat })
72
103
  }
73
104
  break
74
105
  case WAProto_1.proto.HistorySync.HistorySyncType.PUSH_NAME:
75
- for (const c of item.pushnames) {
76
- contacts.push({
77
- id: c.id,
78
- name: c.name || undefined,
79
- lid: c.lidJid || undefined,
80
- jid: WABinary_1.isJidUser(c.id) ? c.id : undefined
81
- })
106
+ for (const c of item.pushnames || []) {
107
+ contacts.push({
108
+ id: c.id,
109
+ name: c.name || undefined,
110
+ notify: c.pushname || undefined,
111
+ lid: c.lidJid || undefined,
112
+ jid: ((0, WABinary_1.isPnUser)(c.id) || (0, WABinary_1.isHostedPnUser)(c.id)) ? c.id : undefined,
113
+ phoneNumber: ((0, WABinary_1.isPnUser)(c.id) || (0, WABinary_1.isHostedPnUser)(c.id)) ? c.id : undefined
114
+ })
82
115
  }
83
116
  break
84
117
  }
@@ -86,25 +119,31 @@ const processHistoryMessage = (item) => {
86
119
  chats,
87
120
  contacts,
88
121
  messages,
122
+ lidPnMappings,
89
123
  syncType: item.syncType,
90
124
  progress: item.progress
91
125
  }
92
126
  }
93
127
 
94
- const downloadAndProcessHistorySyncNotification = async (msg, options) => {
95
- const historyMsg = await downloadHistory(msg, options)
96
- return processHistoryMessage(historyMsg)
128
+ const downloadAndProcessHistorySyncNotification = async (msg, options, logger) => {
129
+ let historyMsg
130
+ if (msg.initialHistBootstrapInlinePayload) {
131
+ historyMsg = WAProto_1.proto.HistorySync.decode(await inflatePromise(msg.initialHistBootstrapInlinePayload))
132
+ }
133
+ else {
134
+ historyMsg = await downloadHistory(msg, options)
135
+ }
136
+ return processHistoryMessage(historyMsg, logger)
97
137
  }
98
138
 
99
139
  const getHistoryMsg = (message) => {
100
- const normalizedContent = !!message ? messages_1.normalizeMessageContent(message) : undefined
101
- const anyHistoryMsg = normalizedContent?.protocolMessage?.historySyncNotification
102
- return anyHistoryMsg
140
+ const normalizedContent = !!message ? (0, messages_1.normalizeMessageContent)(message) : undefined
141
+ return normalizedContent?.protocolMessage?.historySyncNotification
103
142
  }
104
143
 
105
144
  module.exports = {
106
- downloadHistory,
107
- processHistoryMessage,
108
- downloadAndProcessHistorySyncNotification,
109
- getHistoryMsg
110
- }
145
+ downloadHistory,
146
+ processHistoryMessage,
147
+ downloadAndProcessHistorySyncNotification,
148
+ getHistoryMsg
149
+ }
@@ -1,4 +1,5 @@
1
1
  export * from './generics'
2
+ export * from './browser-utils'
2
3
  export * from './decode-wa-message'
3
4
  export * from './messages'
4
5
  export * from './messages-media'
@@ -16,4 +17,6 @@ export * from './use-single-file-auth-state'
16
17
  export * from './use-multi-file-auth-state'
17
18
  export * from './link-preview'
18
19
  export * from './event-buffer'
19
- export * from './process-message'
20
+ export * from './process-message'
21
+ export * from './message-retry-manager'
22
+ export * from './check-npm-version'
@@ -19,6 +19,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
19
  Object.defineProperty(exports, "__esModule", { value: true })
20
20
 
21
21
  __exportStar(require("./generics"), exports)
22
+ __exportStar(require("./browser-utils"), exports)
22
23
  __exportStar(require("./decode-wa-message"), exports)
23
24
  __exportStar(require("./messages"), exports)
24
25
  __exportStar(require("./messages-media"), exports)
@@ -38,4 +39,4 @@ __exportStar(require("./link-preview"), exports)
38
39
  __exportStar(require("./event-buffer"), exports)
39
40
  __exportStar(require("./process-message"), exports)
40
41
  __exportStar(require("./message-retry-manager"), exports)
41
- __exportStar(require("./check-npm-version"), exports)
42
+ __exportStar(require("./check-npm-version"), exports)
@@ -1,6 +1,6 @@
1
1
  import { AxiosRequestConfig } from 'axios'
2
2
  import { proto } from '../../WAProto'
3
- import { AuthenticationCreds, BaileysEventEmitter, CacheStore, SignalKeyStoreWithTransaction, SocketConfig } from '../Types'
3
+ import { AuthenticationCreds, BaileysEventEmitter, CacheStore, SignalKeyStoreWithTransaction, SignalRepositoryWithLIDStore, SocketConfig } from '../Types'
4
4
  import { ILogger } from './logger'
5
5
 
6
6
  type ProcessMessageContext = {
@@ -12,6 +12,7 @@ type ProcessMessageContext = {
12
12
  getMessage: SocketConfig['getMessage']
13
13
  logger?: ILogger
14
14
  options: AxiosRequestConfig<{}>
15
+ signalRepository: SignalRepositoryWithLIDStore
15
16
  }
16
17
 
17
18
  /** Cleans a received message to further processing (Baileys 7.x: hosted Pn/LID + meLid) */
@@ -46,4 +47,4 @@ type PollContext = {
46
47
  */
47
48
  export declare function decryptPollVote({ encPayload, encIv }: proto.Message.IPollEncValue, { pollCreatorJid, pollMsgId, pollEncKey, voterJid, }: PollContext): proto.Message.PollVoteMessage
48
49
 
49
- export declare const processMessage: (message: proto.IWebMessageInfo, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, keyStore, logger, options, getMessage }: ProcessMessageContext) => Promise<void>
50
+ export declare const processMessage: (message: proto.IWebMessageInfo, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, keyStore, logger, options, getMessage, signalRepository }: ProcessMessageContext) => Promise<void>
@@ -112,7 +112,7 @@ function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pol
112
112
  }
113
113
  }
114
114
 
115
- const processMessage = async (message, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, keyStore, logger, options, getMessage }) => {
115
+ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, keyStore, logger, options, getMessage, signalRepository }) => {
116
116
  const meId = creds.me.id
117
117
  const { accountSettings } = creds
118
118
  const chat = { id: WABinary_1.jidNormalizedUser(getChatId(message.key)) }
@@ -155,7 +155,12 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
155
155
  ]
156
156
  })
157
157
  }
158
- const data = await history_1.downloadAndProcessHistorySyncNotification(histNotification, options)
158
+ const data = await history_1.downloadAndProcessHistorySyncNotification(histNotification, options, logger)
159
+ if (data.lidPnMappings?.length) {
160
+ logger?.debug?.({ count: data.lidPnMappings.length }, 'processing LID-PN mappings from history sync')
161
+ await signalRepository?.lidMapping?.storeLIDPNMappings(data.lidPnMappings)
162
+ .catch(err => logger?.warn?.({ err }, 'failed to store LID-PN mappings from history sync'))
163
+ }
159
164
  ev.emit('messaging-history.set', {
160
165
  ...data,
161
166
  isLatest: histNotification.syncType !== WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND
@@ -264,6 +269,27 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
264
269
  })
265
270
  }
266
271
  break
272
+ case WAProto_1.proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
273
+ const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload
274
+ if (encodedPayload?.length && signalRepository?.lidMapping) {
275
+ const { pnToLidMappings, chatDbMigrationTimestamp } = WAProto_1.proto.LIDMigrationMappingSyncPayload.decode(encodedPayload)
276
+ logger?.debug?.({ pnToLidMappings, chatDbMigrationTimestamp }, 'got LID migration mappings')
277
+ const pairs = []
278
+ for (const { pn, latestLid, assignedLid } of pnToLidMappings || []) {
279
+ const lidUser = latestLid || assignedLid
280
+ if (pn && lidUser) {
281
+ pairs.push({
282
+ lid: `${lidUser}@lid`,
283
+ pn: `${pn}@s.whatsapp.net`
284
+ })
285
+ }
286
+ }
287
+ await signalRepository.lidMapping.storeLIDPNMappings(pairs)
288
+ for (const { pn, lid } of pairs) {
289
+ await signalRepository.migrateSession(pn, lid)
290
+ }
291
+ }
292
+ break
267
293
  }
268
294
  }
269
295
  else if (content?.reactionMessage) {
@@ -10,7 +10,14 @@ export declare const STORIES_JID = "status@broadcast"
10
10
 
11
11
  export declare const META_AI_JID = '13135550002@c.us'
12
12
 
13
- export type JidServer = 'c.us' | 'g.us' | 'broadcast' | 's.whatsapp.net' | 'call' | 'lid' | 'newsletter' | 'bot'
13
+ export type JidServer = 'c.us' | 'g.us' | 'broadcast' | 's.whatsapp.net' | 'call' | 'lid' | 'newsletter' | 'bot' | 'hosted' | 'hosted.lid'
14
+
15
+ export declare enum WAJIDDomains {
16
+ WHATSAPP = 0,
17
+ LID = 1,
18
+ HOSTED = 128,
19
+ HOSTED_LID = 129
20
+ }
14
21
 
15
22
  export type JidWithDevice = {
16
23
  user: string
@@ -26,13 +33,21 @@ export declare const jidEncode: (user: string | number | null, server: JidServer
26
33
 
27
34
  export declare const jidDecode: (jid: string | undefined) => FullJid | undefined
28
35
 
36
+ export declare const getServerFromDomainType: (initialServer: string, domainType?: WAJIDDomains) => JidServer
37
+
29
38
  /** is the jid a user */
30
39
  export declare const areJidsSameUser: (jid1: string | undefined, jid2: string | undefined) => boolean
31
40
 
32
- /** is the lid a user */
41
+ /** is the jid Meta AI */
42
+ export declare const isJidMetaAI: (jid: string | undefined) => boolean | undefined
43
+
44
+ /** is the jid a PN user */
45
+ export declare const isPnUser: (jid: string | undefined) => boolean | undefined
46
+
47
+ /** backward-compatible alias */
33
48
  export declare const isJidUser: (jid: string | undefined) => boolean | undefined
34
49
 
35
- /** is the jid a group */
50
+ /** is the lid a user */
36
51
  export declare const isLidUser: (jid: string | undefined) => boolean | undefined
37
52
 
38
53
  /** is the jid a broadcast */
@@ -56,4 +71,6 @@ export declare const isHostedLidUser: (jid: string | undefined) => boolean | und
56
71
  /** is the jid a bot */
57
72
  export declare const isJidBot: (jid: string | undefined) => boolean | undefined
58
73
 
59
- export declare const jidNormalizedUser: (jid: string | undefined) => string
74
+ export declare const jidNormalizedUser: (jid: string | undefined) => string
75
+
76
+ export declare const transferDevice: (fromJid: string, toJid: string) => string
@@ -14,6 +14,27 @@ const STORIES_JID = 'status@broadcast'
14
14
 
15
15
  const META_AI_JID = '13135550002@c.us'
16
16
 
17
+ const WAJIDDomains = {
18
+ WHATSAPP: 0,
19
+ LID: 1,
20
+ HOSTED: 128,
21
+ HOSTED_LID: 129
22
+ }
23
+
24
+ const getServerFromDomainType = (initialServer, domainType = WAJIDDomains.WHATSAPP) => {
25
+ switch (domainType) {
26
+ case WAJIDDomains.LID:
27
+ return 'lid'
28
+ case WAJIDDomains.HOSTED:
29
+ return 'hosted'
30
+ case WAJIDDomains.HOSTED_LID:
31
+ return 'hosted.lid'
32
+ case WAJIDDomains.WHATSAPP:
33
+ default:
34
+ return initialServer
35
+ }
36
+ }
37
+
17
38
  const jidEncode = (user, server, device, agent) => {
18
39
  return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`
19
40
  }
@@ -27,10 +48,10 @@ const jidDecode = (jid) => {
27
48
  const userCombined = jid.slice(0, sepIdx)
28
49
  const [userAgent, device] = userCombined.split(':')
29
50
  const [user, agent] = (userAgent || '').split('_')
30
- let domainType = 0
31
- if (server === 'lid') domainType = 1
32
- else if (server === 'hosted') domainType = 128
33
- else if (server === 'hosted.lid') domainType = 129
51
+ let domainType = WAJIDDomains.WHATSAPP
52
+ if (server === 'lid') domainType = WAJIDDomains.LID
53
+ else if (server === 'hosted') domainType = WAJIDDomains.HOSTED
54
+ else if (server === 'hosted.lid') domainType = WAJIDDomains.HOSTED_LID
34
55
  else if (agent) domainType = parseInt(agent, 10)
35
56
  return {
36
57
  server,
@@ -45,8 +66,14 @@ const areJidsSameUser = (jid1, jid2) => {
45
66
  return jidDecode(jid1)?.user === jidDecode(jid2)?.user
46
67
  }
47
68
 
48
- /** is the jid a user */
49
- const isJidUser = (jid) => jid?.endsWith('@s.whatsapp.net')
69
+ /** is the jid Meta AI */
70
+ const isJidMetaAI = (jid) => jid?.endsWith('@bot')
71
+
72
+ /** is the jid a PN user */
73
+ const isPnUser = (jid) => jid?.endsWith('@s.whatsapp.net')
74
+
75
+ /** backward-compatible alias */
76
+ const isJidUser = isPnUser
50
77
 
51
78
  /** is the lid a user */
52
79
  const isLidUser = (jid) => jid?.endsWith('@lid')
@@ -82,6 +109,16 @@ const jidNormalizedUser = (jid) => {
82
109
  return jidEncode(user, server === 'c.us' ? 's.whatsapp.net' : server)
83
110
  }
84
111
 
112
+ const transferDevice = (fromJid, toJid) => {
113
+ const fromDecoded = jidDecode(fromJid)
114
+ const deviceId = fromDecoded?.device || 0
115
+ const toDecoded = jidDecode(toJid)
116
+ if (!toDecoded) {
117
+ throw new Error(`invalid jid for device transfer: ${toJid}`)
118
+ }
119
+ return jidEncode(toDecoded.user, toDecoded.server, deviceId)
120
+ }
121
+
85
122
  module.exports = {
86
123
  S_WHATSAPP_NET,
87
124
  OFFICIAL_BIZ_JID,
@@ -89,9 +126,13 @@ module.exports = {
89
126
  PSA_WID,
90
127
  STORIES_JID,
91
128
  META_AI_JID,
129
+ WAJIDDomains,
130
+ getServerFromDomainType,
92
131
  jidEncode,
93
132
  jidDecode,
94
133
  areJidsSameUser,
134
+ isJidMetaAI,
135
+ isPnUser,
95
136
  isJidUser,
96
137
  isLidUser,
97
138
  isJidBroadcast,
@@ -101,5 +142,6 @@ module.exports = {
101
142
  isHostedPnUser,
102
143
  isHostedLidUser,
103
144
  isJidBot,
104
- jidNormalizedUser
105
- }
145
+ jidNormalizedUser,
146
+ transferDevice
147
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@neelegirl/baileys",
3
- "version": "2.1.5",
4
- "description": "CJS-compatible Baileys fork for Neelegirl projects, aligned with WhiskeySockets/Baileys 7.0.0-rc.9 and latest web version updates",
3
+ "version": "2.1.7",
4
+ "description": "CommonJS Neelegirl Baileys fork with preserved QR/NEELE logic and selective WhiskeySockets/Baileys compatibility updates",
5
5
  "keywords": [
6
6
  "whatsapp",
7
7
  "js-whatsapp",
@@ -11,6 +11,8 @@
11
11
  "whatsapp-group",
12
12
  "automation",
13
13
  "multi-device",
14
+ "commonjs",
15
+ "lid",
14
16
  "neelegirl",
15
17
  "baileys"
16
18
  ],
@@ -42,7 +44,7 @@
42
44
  "@adiwajshing/keyed-db": "^0.2.4",
43
45
  "@cacheable/node-cache": "^1.5.4",
44
46
  "@hapi/boom": "^9.1.3",
45
- "@neelegirl/libsignal": "^1.0.9",
47
+ "@neelegirl/libsignal": "^1.0.11",
46
48
  "async-mutex": "^0.5.0",
47
49
  "audio-decode": "^2.1.3",
48
50
  "axios": "^1.3.3",