Main logic implemented

This commit is contained in:
2025-04-03 20:21:34 -03:00
parent 2edb9a45d3
commit 559a14832a
2 changed files with 77 additions and 48 deletions

2
.tool-versions Normal file
View File

@@ -0,0 +1,2 @@
nodejs 22.14.0
pnpm 8.15.4

View File

@@ -1,8 +1,12 @@
const EMOJI_TARGET = "❤"; // Acting like ❤️ on Whatsapp
(function () { (function () {
console.log("WhatsApp Heart Favorites script - Candidate Test"); console.log("WhatsApp Heart Favorites script - Candidate Test");
// Initialize WhatsApp Store access // Initialize WhatsApp Store access
function initializeWAStore() { function initializeWAStore() {
window.Store = null;
// Wait for WhatsApp to initialize its modules // Wait for WhatsApp to initialize its modules
const storeCheckInterval = setInterval(() => { const storeCheckInterval = setInterval(() => {
if (window.Store) { if (window.Store) {
@@ -14,6 +18,10 @@
if (typeof window.require === "function") { if (typeof window.require === "function") {
try { try {
if (!window.require("WAWebCollections").Reactions._models?.length) {
console.log("Not injected. Still not defined");
return;
}
// Load the essential modules // Load the essential modules
window.Store = Object.assign({}, window.require("WAWebCollections")); window.Store = Object.assign({}, window.require("WAWebCollections"));
window.Store.Cmd = window.require("WAWebCmd").Cmd; window.Store.Cmd = window.require("WAWebCmd").Cmd;
@@ -30,9 +38,8 @@
// Set up listeners for heart reactions // Set up listeners for heart reactions
function setupReactionListener() { function setupReactionListener() {
// TODO: Implement the reaction listener here
// //
// CANDIDATE TASK: Write code that will star a message when you react to it with a heart emoji // CANDIDATE TASK: Write code that will star a message when you react to it with a heart emoji
// //
// Hints: // Hints:
// 1. You'll need to listen for reaction events using Store.Reactions // 1. You'll need to listen for reaction events using Store.Reactions
@@ -48,7 +55,27 @@
// require("WAWebCmd") or any other module name. This will show you available methods, // require("WAWebCmd") or any other module name. This will show you available methods,
// _events, and other properties that might be helpful for this task. // _events, and other properties that might be helpful for this task.
console.log("Reaction listener needs to be implemented"); Store.Reactions._models.forEach((model) => {
model.reactions._events.add = [
...model.reactions._events.add,
{
callback: async (...args) => {
if (args[0].__x_aggregateEmoji === EMOJI_TARGET) {
const msgKey = args[1].parent.__x_id._serialized;
const msg = Store.Msg.get(msgKey);
const chat = await Store.Chat.find(msg.id.remote);
const messageHasStar = msg.__x_star;
if (!messageHasStar) {
Store.Cmd.sendStarMsgs(chat, [msg], false);
} else {
Store.Cmd.sendUnstarMsgs(chat, [msg], false);
}
}
},
},
];
});
} }
// Initialize // Initialize