wppconnect-api / src /lib /wapi /functions /get-unread-messages.js
AUXteam's picture
Upload folder using huggingface_hub
4c34106 verified
/*
* This file is part of WPPConnect.
*
* WPPConnect is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WPPConnect is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
*/
export function getUnreadMessages(
includeMe,
includeNotifications,
useUnreadCount,
done
) {
const chats = WPP.whatsapp.ChatStore.getModelsArray();
const output = [];
for (const chat in chats) {
if (isNaN(chat)) {
continue;
}
const messageGroupObj = chats[chat];
let messageGroup = WAPI._serializeChatObj(messageGroupObj);
messageGroup.messages = [];
const messages = messageGroupObj.msgs._models;
for (let i = messages.length - 1; i >= 0; i--) {
const messageObj = messages[i];
if (
typeof messageObj.isNewMsg != 'boolean' ||
messageObj.isNewMsg === false
) {
continue;
} else {
messageObj.isNewMsg = false;
let message = WAPI.processMessageObj(
messageObj,
includeMe,
includeNotifications
);
if (message) {
messageGroup.messages.push(message);
}
}
}
if (messageGroup.messages.length > 0) {
output.push(messageGroup);
} else {
// No messages with isNewMsg true
if (useUnreadCount) {
// Will use unreadCount attribute to fetch last n messages from sender
let n = messageGroupObj.unreadCount;
for (let i = messages.length - 1; i >= 0; i--) {
const messageObj = messages[i];
if (n > 0) {
if (!messageObj.isSentByMe) {
let message = WAPI.processMessageObj(
messageObj,
includeMe,
includeNotifications
);
messageGroup.messages.unshift(message);
n -= 1;
}
} else if (n === -1) {
// chat was marked as unread so will fetch last message as unread
if (!messageObj.isSentByMe) {
let message = WAPI.processMessageObj(
messageObj,
includeMe,
includeNotifications
);
messageGroup.messages.unshift(message);
break;
}
} else {
// unreadCount = 0
break;
}
}
if (messageGroup.messages.length > 0) {
messageGroupObj.unreadCount = 0; // reset unread counter
output.push(messageGroup);
}
}
}
}
if (done !== undefined) {
done(output);
}
return output;
}