diff --git a/README.md b/README.md index bbcfc841f..789399499 100644 --- a/README.md +++ b/README.md @@ -685,7 +685,7 @@ const status = await client.getStatus('000000000000@c.us'); const user = await client.getNumberProfile('000000000000@c.us'); // Retrieve all unread message -const messages = await client.getAllUnreadMessages(); +const messages = await client.getUnreadMessages(); // Retrieve profile fic (as url) const url = await client.getProfilePicFromServer('000000000000@c.us'); diff --git a/src/api/layers/retriever.layer.ts b/src/api/layers/retriever.layer.ts index 665140246..2c9fbbe9a 100644 --- a/src/api/layers/retriever.layer.ts +++ b/src/api/layers/retriever.layer.ts @@ -315,7 +315,6 @@ export class RetrieverLayer extends SenderLayer { return await this.page.evaluate(() => WAPI.isBeta()); } - //PRO /** * Retrieves all undread Messages */ diff --git a/src/lib/wapi/functions/get-all-unread-messages.js b/src/lib/wapi/functions/get-all-unread-messages.js deleted file mode 100644 index 98d7159b5..000000000 --- a/src/lib/wapi/functions/get-all-unread-messages.js +++ /dev/null @@ -1,18 +0,0 @@ -import { getAllChatsWithNewMessages } from './get-chats-with-new-messages'; - -/** - * Retrieves undread messages - * x.ack === -1 - * TODO: Test this fn, seems incorrect, should not be async - */ -export const getAllUnreadMessages = async function () { - const _partials = JSON.stringify( - getAllChatsWithNewMessages() - .map((c) => WAPI.getChat(c.id._serialized)) - .map((c) => c.msgs._models.filter((x) => x.ack === -1)) - .flatMap((x) => x) || [] - ); - - const partials = JSON.parse(_partials); - return partials; -}; diff --git a/src/lib/wapi/functions/get-chats-with-new-messages.js b/src/lib/wapi/functions/get-chats-with-new-messages.js deleted file mode 100644 index 9c6aedaae..000000000 --- a/src/lib/wapi/functions/get-chats-with-new-messages.js +++ /dev/null @@ -1,10 +0,0 @@ -import { hasUndreadMessages } from './has-unread-messages'; - -export const getAllChatsWithNewMessages = function (done) { - const chats = window.Store.Chat.filter(hasUndreadMessages).map((chat) => - WAPI._serializeChatObj(chat) - ); - - if (done !== undefined) done(chats); - return chats; -}; diff --git a/src/lib/wapi/functions/get-unread-messages.js b/src/lib/wapi/functions/get-unread-messages.js index e31327288..c96c74093 100644 --- a/src/lib/wapi/functions/get-unread-messages.js +++ b/src/lib/wapi/functions/get-unread-messages.js @@ -1,27 +1,19 @@ export async function getUnreadMessages(undread = true) { - let arr = []; + const arr = []; let chats; - // console.log('.....................', typeof undread); - - if (undread === true) { + if (undread) { chats = await Store.Chat.filter((e) => e.unreadCount > 0); } else { chats = await Store.Chat.filter((e) => e.unreadCount <= 0); } - for (let i in chats) { - let f = Number(i); - if (!isNaN(f)) { - let t = chats[i].msgs._models.slice(-chats[i].unreadCount); - for (let r in t) { - let h = Number(r); - if (!isNaN(h)) { - let message = await WAPI.processMessageObj(t[r], true, true); - if (message) { - arr.push(message); - } - } + for (const chat of chats) { + const t = chat.msgs._models.slice(-chat.unreadCount); + for (const messageObj of t) { + const message = await WAPI.processMessageObj(messageObj, true, true); + if (message) { + arr.push(message); } } } diff --git a/src/lib/wapi/functions/has-unread-messages.js b/src/lib/wapi/functions/has-unread-messages.js deleted file mode 100644 index 7af5de603..000000000 --- a/src/lib/wapi/functions/has-unread-messages.js +++ /dev/null @@ -1,3 +0,0 @@ -export const hasUndreadMessages = function (chat) { - return chat.unreadCount > 0; -}; diff --git a/src/lib/wapi/functions/index.js b/src/lib/wapi/functions/index.js index 0fdae3ebc..e77c4350e 100644 --- a/src/lib/wapi/functions/index.js +++ b/src/lib/wapi/functions/index.js @@ -12,12 +12,10 @@ export { getAllContacts } from './get-all-contacts'; export { getAllGroupMetadata } from './get-all-group-metadata'; export { getAllGroups } from './get-all-groups'; export { getAllMessagesInChat } from './get-all-messages-in-chat'; -export { getAllUnreadMessages } from './get-all-unread-messages'; export { getBatteryLevel } from './get-battery-level'; export { getChat } from './get-chat'; export { getChatById } from './get-chat-by-id'; export { getChatByName } from './get-chat-by-name'; -export { getAllChatsWithNewMessages } from './get-chats-with-new-messages'; export { getCommonGroups } from './get-common-groups'; export { getContact } from './get-contact'; export { getGroupAdmins } from './get-group-admins'; @@ -32,7 +30,6 @@ export { getNumberProfile } from './get-number-profile'; export { getProfilePicFromServer } from './get-profile-pic-from-server'; export { getStatus } from './get-status'; export { getUnreadMessages } from './get-unread-messages'; -export { hasUndreadMessages } from './has-unread-messages'; export { isConnected } from './is-connected'; export { isLoggedIn } from './is-logged-in'; export { leaveGroup } from './leave-group'; diff --git a/src/lib/wapi/wapi.js b/src/lib/wapi/wapi.js index 994445c54..29045df19 100644 --- a/src/lib/wapi/wapi.js +++ b/src/lib/wapi/wapi.js @@ -16,12 +16,10 @@ import { getAllChatIds, getAllChats, getAllChatsWithMessages, - getAllChatsWithNewMessages, getAllContacts, getAllGroupMetadata, getAllGroups, getAllMessagesInChat, - getAllUnreadMessages, getBatteryLevel, getChat, getChatById, @@ -41,7 +39,6 @@ import { getProfilePicFromServer, getStatus, getUnreadMessages, - hasUndreadMessages, isConnected, isLoggedIn, leaveGroup, @@ -313,10 +310,7 @@ if (typeof window.WAPI === 'undefined') { window.WAPI.getMyContacts = getMyContacts; window.WAPI.getContact = getContact; window.WAPI.getAllChats = getAllChats; - window.WAPI.haveNewMsg = hasUndreadMessages; - window.WAPI.getAllChatsWithNewMsg = getAllChatsWithNewMessages; window.WAPI.getAllChatIds = getAllChatIds; - window.WAPI.getAllUnreadMessages = getAllUnreadMessages; window.WAPI.getAllChatsWithMessages = getAllChatsWithMessages; window.WAPI.getAllGroups = getAllGroups; window.WAPI.getChat = getChat; diff --git a/src/types/WAPI.d.ts b/src/types/WAPI.d.ts index e0df1cc11..cacb8c6fd 100644 --- a/src/types/WAPI.d.ts +++ b/src/types/WAPI.d.ts @@ -50,7 +50,6 @@ interface WAPI { limit: number ) => Message[]; - getAllUnreadMessages: () => PartialMessage[]; getBatteryLevel: () => number; getBlockList: () => Contact[]; getBusinessProfilesProducts: (to: string) => any; diff --git a/test/index.js b/test/index.js index ddd73ac54..b5b470107 100644 --- a/test/index.js +++ b/test/index.js @@ -14,8 +14,6 @@ async function start(client) { // client.onMessage((message) => { // console.log(message); // }); - // const allMessages = await client.loadAndGetAllMessagesInChat( - // '557592600184@c.us' - // ); + // const allMessages = await client.getAllUnreadMessages(); // console.log(allMessages); }