diff --git a/Example/example.ts b/Example/example.ts index 0da3a98..3c5c6bf 100644 --- a/Example/example.ts +++ b/Example/example.ts @@ -55,8 +55,8 @@ async function example() { if (chat.presences) { // receive presence updates -- composing, available, etc. Object.values(chat.presences).forEach(presence => console.log( `${presence.name}'s presence is ${presence.lastKnownPresence} in ${chat.jid}`)) } - // only do something when a new message is received; i.e. the unread count is updated - if (!chat.count) return + // only do something when a new message is received + if (!chat.hasNewMessage) return const m = chat.messages.all()[0] // pull the new message from the update const messageStubType = WA_MESSAGE_STUB_TYPES[m.messageStubType] || 'MESSAGE' diff --git a/package.json b/package.json index f8358a3..8aa47dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adiwajshing/baileys", - "version": "3.3.0", + "version": "3.3.1", "description": "WhatsApp Web API", "homepage": "https://github.com/adiwajshing/Baileys", "main": "lib/WAConnection/WAConnection.js", @@ -43,7 +43,6 @@ "pino-pretty": "^4.3.0", "protobufjs": "^6.10.1", "qrcode-terminal": "^0.12.0", - "ts-protoc-gen": "^0.13.0", "ws": "^7.3.1" }, "files": [ diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index 2bba50a..207130d 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -1,6 +1,6 @@ import * as QR from 'qrcode-terminal' import { WAConnection as Base } from './3.Connect' -import { WAMessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, PresenceUpdate, BaileysEvent, DisconnectReason, WAOpenResult, Presence, AuthenticationCredentials, WAParticipantAction, WAGroupMetadata, WAUser, WANode, WAPresenceData } from './Constants' +import { WAMessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, PresenceUpdate, BaileysEvent, DisconnectReason, WAOpenResult, Presence, AuthenticationCredentials, WAParticipantAction, WAGroupMetadata, WAUser, WANode, WAPresenceData, WAChatUpdate } from './Constants' import { whatsappID, unixTimestampSeconds, isGroupID, GET_MESSAGE_ID, WA_MESSAGE_ID, waMessageKey, newMessagesDB, shallowChanges, toNumber } from './Utils' import KeyedDB from '@adiwajshing/keyed-db' import { Mutex } from './Mutex' @@ -59,6 +59,7 @@ export class WAConnection extends Base { const lastMessages = {} // messages received const messagesUpdate = (json, style: 'prepend' | 'append') => { + const isLast = json[1].last const messages = json[2] as WANode[] if (messages) { const updates: { [k: string]: KeyedDB } = {} @@ -91,6 +92,9 @@ export class WAConnection extends Base { ) } } + if (isLast) { + this.emit('chats-received', { hasReceivedLastMessage: true }) + } } this.on('CB:action,add:last', json => messagesUpdate(json, 'append')) this.on('CB:action,add:before', json => messagesUpdate(json, 'prepend')) @@ -379,7 +383,7 @@ export class WAConnection extends Base { } protected chatAddMessage (message: WAMessage, chat: WAChat) { // store updates in this - const chatUpdate: Partial & { jid: string } = { jid: chat.jid } + const chatUpdate: WAChatUpdate = { jid: chat.jid } // add to count if the message isn't from me & there exists a message if (!message.key.fromMe && message.message) { @@ -429,6 +433,7 @@ export class WAConnection extends Base { this.chatUpdateTime (chat, +toNumber(message.messageTimestamp)) chatUpdate.t = chat.t } + chatUpdate.hasNewMessage = true chatUpdate.messages = newMessagesDB([ message ]) // emit deprecated this.emit('message-new', message) @@ -530,12 +535,12 @@ export class WAConnection extends Base { on (event: 'chat-new', listener: (chat: WAChat) => void): this /** when contacts are sent by WA */ on (event: 'contacts-received', listener: () => void): this - /** when chats are sent by WA */ - on (event: 'chats-received', listener: (update: {hasNewChats: boolean}) => void): this + /** when chats are sent by WA, and when all messages are received */ + on (event: 'chats-received', listener: (update: {hasNewChats?: boolean, hasReceivedLastMessage?: boolean}) => void): this /** when multiple chats are updated (new message, updated message, deleted, pinned, etc) */ - on (event: 'chats-update', listener: (chats: (Partial & { jid: string })[]) => void): this + on (event: 'chats-update', listener: (chats: WAChatUpdate[]) => void): this /** when a chat is updated (new message, updated message, deleted, pinned, presence updated etc) */ - on (event: 'chat-update', listener: (chat: Partial & { jid: string }) => void): this + on (event: 'chat-update', listener: (chat: WAChatUpdate) => void): this /** * when a new message is relayed * @deprecated use `chat-update` diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index d3ab542..14fe3e3 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -219,6 +219,7 @@ export interface WAChat { imgUrl?: string presences?: { [k: string]: WAPresenceData } } +export type WAChatUpdate = Partial & { jid: string, hasNewMessage?: boolean } export enum WAMetric { debugLog = 1, queryResume = 2, diff --git a/yarn.lock b/yarn.lock index 740dc1c..18b7f4a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -392,11 +392,6 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.31.tgz#b8fc04d46bc22959a99fbdba71b15f37a48da3ec" integrity sha512-gBk54XbcRj8EKTi7Syo4JU4purbRJaZpkvMVs7+t+b9JaOtwsGo7vCbXdVJN3gH/wu/GyZGD8lAKo0qpQuNjOw== -"@types/object-hash@^1.3.0": - version "1.3.4" - resolved "https://registry.yarnpkg.com/@types/object-hash/-/object-hash-1.3.4.tgz#079ba142be65833293673254831b5e3e847fe58b" - integrity sha512-xFdpkAkikBgqBdG9vIlsqffDV8GpvnPEzs0IUtr1v3BEB97ijsFQ4RXVbUZwjFThhB4MDSTUfvmxUD5PGx0wXA== - "@types/pino-std-serializers@*": version "2.4.1" resolved "https://registry.yarnpkg.com/@types/pino-std-serializers/-/pino-std-serializers-2.4.1.tgz#f8bd52a209c8b3c97d1533b1ba27f57c816382bf" @@ -739,11 +734,6 @@ curve25519-js@^0.0.4: resolved "https://registry.yarnpkg.com/curve25519-js/-/curve25519-js-0.0.4.tgz#e6ad967e8cd284590d657bbfc90d8b50e49ba060" integrity sha512-axn2UMEnkhyDUPWOwVKBMVIzSQy2ejH2xRGy1wq81dqRwApXfIzfbE3hIX0ZRFBIihf/KDqK158DLwESu4AK1w== -dataloader@^1.4.0: - version "1.4.0" - resolved "https://registry.yarnpkg.com/dataloader/-/dataloader-1.4.0.tgz#bca11d867f5d3f1b9ed9f737bd15970c65dff5c8" - integrity sha512-68s5jYdlvasItOJnCuI2Q9s4q98g0pCyL3HrcKJu8KNugUl8ahgmZYg38ysLTgQjjXX3H8CJLkAvWrclWfcalw== - dateformat@^3.0.3: version "3.0.3" resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae" @@ -1045,11 +1035,6 @@ global@~4.3.0: min-document "^2.19.0" process "~0.5.1" -google-protobuf@^3.6.1: - version "3.14.0" - resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.14.0.tgz#20373d22046e63831a5110e11a84f713cc43651e" - integrity sha512-bwa8dBuMpOxg7COyqkW6muQuvNnWgVN8TX/epDRGW5m0jcrmq2QJyCyiV8ZE2/6LaIIqJtiv9bYokFhfpy/o6w== - graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0: version "4.2.4" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb" @@ -1557,11 +1542,6 @@ object-assign@^4.0.1: resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM= -object-hash@^1.3.1: - version "1.3.1" - resolved "https://registry.yarnpkg.com/object-hash/-/object-hash-1.3.1.tgz#fde452098a951cb145f039bb7d455449ddc126df" - integrity sha512-OSuu/pU4ENM9kmREg0BdNrUDIl1heYa4mBZacJc+vVWz4GtAwu7jO8s4AIt2aGRUTqxykpWzI3Oqnsm13tTMDA== - object-inspect@^1.8.0: version "1.8.0" resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.8.0.tgz#df807e5ecf53a609cc6bfe93eac3cc7be5b3a9d0" @@ -1794,7 +1774,7 @@ progress@^2.0.3: resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8" integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA== -protobufjs@^6.10.1, protobufjs@^6.8.8: +protobufjs@^6.10.1: version "6.10.2" resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-6.10.2.tgz#b9cb6bd8ec8f87514592ba3fdfd28e93f33a469b" integrity sha512-27yj+04uF6ya9l+qfpH187aqEzfCF4+Uit0I9ZBQVqK09hk/SQzKa2MUqUpXaVa7LOFRg1TSSr3lVxGOk6c0SQ== @@ -2172,11 +2152,6 @@ trim-newlines@^1.0.0: resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613" integrity sha1-WIeWa7WCpFA6QetST301ARgVphM= -ts-imm@0.4.0: - version "0.4.0" - resolved "https://registry.yarnpkg.com/ts-imm/-/ts-imm-0.4.0.tgz#01a7916dd1b214ab3c578403ff0cc1faafc282da" - integrity sha512-slzrsTGPwoEQFTr8N9A2ZcqRLu0U3+aFpCwFtDOVmjD2b9AGSzYnHJQVDVP54kmoSD1BIM3bLN2VeOkFpu7Ycw== - ts-node-dev@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/ts-node-dev/-/ts-node-dev-1.0.0.tgz#24a2270d225c29ce269de2a31f88b1b259fc84cb" @@ -2205,31 +2180,6 @@ ts-node@^9.0.0: source-map-support "^0.5.17" yn "3.1.1" -ts-poet@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/ts-poet/-/ts-poet-1.0.1.tgz#7dcba4fda3666dda0b72fcd1ea7b1d4cf48310c9" - integrity sha512-wGzYjiNKtDZqC5CXCAxwxhR0d/MJYIVvV5H+C2I2ntwK5HcFtoLNTSwfrXo0asO5WtBJ4upULCCAM02NYX7eUg== - dependencies: - lodash "^4.17.15" - ts-imm "0.4.0" - -"ts-proto@github:ssilve1989/ts-proto": - version "1.38.0" - resolved "https://codeload.github.com/ssilve1989/ts-proto/tar.gz/adfa489896a5e80f9ed7b38e059cbaabbfd7b38d" - dependencies: - "@types/object-hash" "^1.3.0" - dataloader "^1.4.0" - object-hash "^1.3.1" - protobufjs "^6.8.8" - ts-poet "^1.0.1" - -ts-protoc-gen@^0.13.0: - version "0.13.0" - resolved "https://registry.yarnpkg.com/ts-protoc-gen/-/ts-protoc-gen-0.13.0.tgz#2763ae4e4a1a7d7001d53d2f3043357c691701ea" - integrity sha512-j18X4rkDBbG/ZHUJy88WFeZP6mStGow5uREaohowlHXTu3/N7WcpyPhb7Vh6wN38ERmc/AkT9gqT98+vtlRhJA== - dependencies: - google-protobuf "^3.6.1" - tsconfig@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/tsconfig/-/tsconfig-7.0.0.tgz#84538875a4dc216e5c4a5432b3a4dec3d54e91b7"