feat : send conversation seen (#66)

After this commit, chats will be automatically marked as seen when a message is sent to it. If this is not the intended behavior, `sendSeen: false` can be set as part of the options object. 

You can also manually mark a conversation as seen by calling the sendSeen() function on the Chat.

Co-authored-by: Pedro S. Lopez <pedroslopez@me.com>
This commit is contained in:
sahalMoidu
2020-03-02 00:12:35 +05:30
committed by GitHub
parent 48b9ae1e81
commit 98458f760e
4 changed files with 49 additions and 7 deletions

View File

@@ -282,7 +282,19 @@ class Client extends EventEmitter {
async destroy() {
await this.pupBrowser.close();
}
/**
* Mark as seen for the Chat
* @param {string} chatId
* @returns {Promise<boolean>} result
*
*/
async sendSeen(chatId) {
const result = await this.pupPage.evaluate(async (chatId) => {
return window.WWebJS.sendSeen(chatId);
}, chatId);
return result;
}
/**
* Send a message to a specific chatId
* @param {string} chatId
@@ -296,6 +308,8 @@ class Client extends EventEmitter {
quotedMessageId: options.quotedMessageId,
mentionedJidList: Array.isArray(options.mentions) ? options.mentions.map(contact => contact.id._serialized) : []
};
const sendSeen = typeof options.sendSeen === 'undefined' ? true : options.sendSeen;
if (content instanceof MessageMedia) {
internalOptions.attachment = content;
@@ -308,7 +322,7 @@ class Client extends EventEmitter {
content = '';
}
const newMessage = await this.pupPage.evaluate(async (chatId, message, options) => {
const newMessage = await this.pupPage.evaluate(async (chatId, message, options, sendSeen) => {
let chat = window.Store.Chat.get(chatId);
let msg;
if (!chat) { // The chat is not available in the previously chatted list
@@ -318,16 +332,25 @@ class Client extends EventEmitter {
//get the topmost chat object and assign the new chatId to it .
//This is just a workaround.May cause problem if there are no chats at all. Need to dig in and emulate how whatsapp web does
let chat = window.Store.Chat.models[0];
if (!chat)
throw 'Chat List empty! Need at least one open conversation with any of your contact';
let originalChatObjId = chat.id;
chat.id = newChatId;
msg = await window.WWebJS.sendMessage(chat, message, options);
chat.id = originalChatObjId; //replace the chat with its original id
}
}
else
msg = await window.WWebJS.sendMessage(chat, message, options);
else {
if(sendSeen) {
window.WWebJS.sendSeen(chatId);
}
msg = await window.WWebJS.sendMessage(chat, message, options, sendSeen);
}
return msg.serialize();
}, chatId, content, internalOptions);
}, chatId, content, internalOptions, sendSeen);
return new Message(this, newMessage);
}