Skip to content

Commit

Permalink
fix: checkNumberStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Oct 29, 2024
1 parent 5a4c95d commit d46388d
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 95 deletions.
29 changes: 16 additions & 13 deletions src/webpack/assets/api.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {
sleep,
injectConfig,
injectParasite,
processFiles,
base64ToFile,
generateMediaKey,
arrayBufferToBase64,
encryptAndUploadFile,
getFileHash,
getFileHash
} from './help';

import {
Expand All @@ -19,6 +16,8 @@ import {
checkNumberStatus,
isMD,
sendCheckType,
returnChat,
checkChatExist
} from './functions/help';

import {
Expand All @@ -29,7 +28,9 @@ import {
addParticipant,
setGroupDescription,
getHost,
setGroupImage
setGroupImage,
getAllChats,
getContact
} from './functions';

import {
Expand All @@ -50,7 +51,7 @@ initParasite();
if (typeof window.API === 'undefined') {
window.API = {};

// Helps
// Helps Functions
window.API.getChat = getChat;
window.API.scope = scope;
window.API.getNewId = getNewId;
Expand All @@ -66,23 +67,25 @@ if (typeof window.API === 'undefined') {
window.API.encryptAndUploadFile = encryptAndUploadFile;
window.API.getFileHash = getFileHash;
window.API.sendCheckType = sendCheckType;
window.API.returnChat = returnChat;
window.API.checkChatExist = checkChatExist;

// Functions

// Send
// Send Functions
window.API.sendMessage = sendMessage;

// Host
// Host Functions
window.API.getAllContacts = getAllContacts;
window.API.getHost = getHost;
window.API.getAllChats = getAllChats;
window.API.getContact = getContact

// Group
// Group Functions
window.API.createGroup = createGroup;
window.API.addParticipant = addParticipant;
window.API.setGroupDescription = setGroupDescription;
window.API.setGroupImage = setGroupImage;

// Serialize
// Serialize Functions
window.API.serializeMessageObj = serializeMessageObj;
window.API.serializeChatObj = serializeChatObj;
window.API.serializeContactObj = serializeContactObj;
Expand Down
16 changes: 16 additions & 0 deletions src/webpack/assets/functions/get-all-chats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Function to get all chats
* @returns - Array of chats
*/
export const getAllChats = async () => {
const fromwWid = await Store.MaybeMeUser.getMaybeMeUser();
if (fromwWid) {
const idUser = await API.returnChat(fromwWid._serialized);
if (idUser && idUser.status !== 404) {
const chats = Store.Chat.map((chat) =>
API.serializeChatObj(chat)
);
return chats;
}
}
};
12 changes: 12 additions & 0 deletions src/webpack/assets/functions/get-contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Function to get contact by id
* @param {*} id
* @returns
*/
export const getContact = (id) => {
const found = window.Store.Contact.get(id);
if (found) {
return window.API.serializeContactObj(found);
}
return false;
};
15 changes: 15 additions & 0 deletions src/webpack/assets/functions/help/check-chat-exist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Function to check if a number is registered on WhatsApp chat
* @param {*} id - The phone number
* @returns - Return the status of the number
*/
export const checkChatExist = async (id) => {
try {
const chat = await Store.Chat.find(id);
if (chat?.chatEntryPoint === 'Chatlist') {
return chat.id;
}
} catch (error) {
return false;
}
};
149 changes: 92 additions & 57 deletions src/webpack/assets/functions/help/check-number-status.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,106 @@
export async function checkNumberStatus(id, conn = false) {
try {
/**
* Function to check if a number is registered on WhatsApp
* @param {*} id - The phone number
* @param {*} conn - Check if the connection is connected
* @returns {Promise<Object>} - Return the status of the number
*/
export const checkNumberStatus = async (id, conn = false) => {
return new Promise(async (resolve, reject) => {
try {
const err = {
error: 404
};
const connection =
Store &&
Store.State &&
Store.State.Socket &&
Store.State.Socket.state
? Store.State.Socket.state
: '';

const err = {
error: 404
};

const connection =
window.Store &&
window.Store.State &&
window.Store.State.Socket &&
window.Store.State.Socket.state ?
window.Store.State.Socket.state :
'';

if (conn === true) {
if (connection !== 'CONNECTED') {
const checkType = API.sendCheckType(id);
if (!!checkType && checkType.status === 404) {
Object.assign(err, {
text: 'No connection with WhatsApp',
connection: connection,
text: checkType.text,
numberExists: null,
});
throw err;
}
}

const lid = await API.getChat(id);
if (lid) {
return await Store.checkNumberMD
.queryExists(lid.id)
.then((result) => {
if (!!result && typeof result === 'object') {
const data = {
status: 200,
numberExists: true,
id: result.wid,
};
return data;
}
throw Object.assign(err, {

if (conn === true) {
if (connection !== 'CONNECTED') {
Object.assign(err, {
text: 'No connection with WhatsApp',
connection: connection,
numberExists: false,
text: `The number does not exist`,
numberExists: null,
});
})
.catch((err) => {
if (err.text) {
throw err;
}
}

const chat = await API.checkChatExist(id);
if (chat) {
const data = {
status: 200,
numberExists: true,
id: chat,
erro: false,
checkNumberStatus: false,
};
return resolve(data);
}

const lid = await API.getChat(id);
if (lid) {
return await Store.checkNumber
.queryWidExists(lid.id)
.then((result) => {
if (!!result && typeof result === 'object') {
const data = {
status: 200,
numberExists: true,
id: result.wid,
erro: false,
checkNumberStatus: true,
};
return resolve(data);
}
Object.assign(err, {
connection: connection,
numberExists: false,
text: `The number does not exist`,
});
throw err;
})
.catch((err) => {
if (err.text) {
throw err;
}
Object.assign(err, {
connection: connection,
numberExists: false,
text: err,
checkNumberStatus: true,
});
throw err;
}
throw Object.assign(err, {
connection: connection,
numberExists: false,
text: err,
});
} else {
Object.assign(err, {
connection: connection,
numberExists: false,
});
} else {
throw Object.assign(err, {
connection: connection,
numberExists: false,
});
throw err;
}
} catch (e) {
const scope = {
status: e.error,
text: e.text,
numberExists: e.numberExists,
connection: e.connection,
erro: true,
};
reject(scope);
}

} catch (e) {
return {
status: e.error,
text: e.text,
numberExists: e.numberExists,
connection: e.connection,
};
}
});
}
9 changes: 7 additions & 2 deletions src/webpack/assets/functions/help/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@

export { getChat } from './get-chat';
export { scope } from './scope';
export { getNewId } from './get-mew-id';
export { getNewMessageId } from './get-new-message';
export { checkChatExist } from './check-chat-exist';

export { sendExist } from './send-exist';
export { checkNumberStatus } from './check-number-status';
export { sendCheckType } from './send-check-type';

export { checkNumberStatus } from './check-number-status';
export { isMD } from './is-md';
export { returnChat } from './return-to-chat';
export { scope } from './scope';
70 changes: 70 additions & 0 deletions src/webpack/assets/functions/help/return-to-chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

/**
* Function to return to chat or create a new chat
* @param {*} chatId - Chat ID
* @param {*} returnChat - Return Chat
* @param {*} Send - Seen to message
* @returns
*/
export const returnChat = async (chatId, returnChat = true, Send = true) => {
const checkType = API.sendCheckType(chatId);
if (!!checkType && checkType.status === 404) {
return checkType;
}

let chat = await window.API.getChat(chatId);
if (!chat) {
var idUser = new Store.UserConstructor(chatId, {
intentionallyUsePrivateConstructor: true,
});
const chatWid = new Store.WidFactory.createWid(chatId);
await Store.Chat.add(
{
createdLocally: true,
id: chatWid
},
{
merge: true
}
);
chat = await Store.Chat.find(idUser);
}

if (chat === undefined) {
const chatWid = new Store.WidFactory.createWid(chatId);
await Store.Chat.add(
{
createdLocally: true,
id: chatWid
},
{
merge: true
}
);
const storeChat = await Store.Chat.find(chatId);
if (storeChat) {
chat =
storeChat && storeChat.id && storeChat.id._serialized
? await window.API.getChat(storeChat.id._serialized)
: undefined;
}
}

if (!chat) {
return API.scope(chatId, true, 404);
}

if (Send) {
await window.Store.ReadSeen.sendSeen(chat, false);
}

if (returnChat) {
return chat;
}

return API.scope(
chatId,
false,
200
);
}
15 changes: 14 additions & 1 deletion src/webpack/assets/functions/help/send-exist.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
export async function sendExist(chatId, returnChat = true, Send = true) {
/**
* Function to check if the number exists and return the chat
* @param {*} chatId
* @param {*} returnChat
* @param {*} Send
* @returns
*/
export const sendExist = async (chatId, returnChat = true, Send = true) => {

const checkContact = API.getContact(chatId);
if (checkContact) {
return await API.returnChat(chatId);
}

const checkType = await API.sendCheckType(chatId);
if (!!checkType && checkType.status === 404) {
return checkType;
Expand Down
Loading

0 comments on commit d46388d

Please sign in to comment.