Skip to content

Commit

Permalink
feat: refactored the reply function
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Mar 31, 2021
1 parent 6ae385e commit d74f96b
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 31 deletions.
38 changes: 32 additions & 6 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,18 +361,44 @@ export class SenderLayer extends ListenerLayer {
to: string,
content: string,
quotedMsg: string
): Promise<Message> {
): Promise<Message | object> {
return new Promise(async (resolve, reject) => {
const messageId: string = await this.page.evaluate(
const typeFunction = 'reply';
const type = 'string';
const check = [
{
param: 'to',
type: type,
value: to,
function: typeFunction,
isUser: true,
},
{
param: 'content',
type: type,
value: content,
function: typeFunction,
isUser: true,
},
{
param: 'quotedMsg',
type: type,
value: quotedMsg,
function: typeFunction,
isUser: false,
},
];
const validating = checkValuesSender(check);
if (typeof validating === 'object') {
return reject(validating);
}
const result: object = await this.page.evaluate(
({ to, content, quotedMsg }) => {
return WAPI.reply(to, content, quotedMsg);
},
{ to, content, quotedMsg }
);
const result = (await this.page.evaluate(
(messageId: any) => WAPI.getMessageById(messageId),
messageId
)) as Message;

if (result['erro'] == true) {
reject(result);
} else {
Expand Down
109 changes: 85 additions & 24 deletions src/lib/wapi/functions/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,97 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import { getMessageById } from './get-message-by-id';

export async function reply(chatId, content, quotedMessageId) {
const chat = Store.Chat.get(chatId);
if (typeof chatId != 'string') {
return WAPI.scope(
null,
true,
404,
'enter the chatid variable as an string'
);
}
if (typeof content != 'string') {
return WAPI.scope(
null,
true,
404,
'enter the content variable as an string'
);
}
if (typeof quotedMessageId != 'string') {
return WAPI.scope(
null,
true,
404,
'enter the content variable as an string'
);
}
const chat = await WAPI.sendExist(chatId);
if (chat && chat.status != 404) {
const To = chat.id;
const m = { type: 'deleteMessages' };
let quotedMsgOptions = {};

let quotedMessage = await WAPI.getMessageById(quotedMessageId, null, false);
if (quotedMessage.erro == undefined) {
let checkID = await WAPI.checkIdMessage(
quotedMessage.to._serialized,
quotedMessageId
);
if (checkID.erro == true) {
return checkID;
}
} else {
let obj = WAPI.scope(
To,
true,
404,
`The id ${quotedMessageId} does not exist!`
);
Object.assign(obj, m);
return obj;
}

let quotedMsgOptions = {};
if (quotedMessageId) {
let quotedMessage = await getMessageById(quotedMessageId, null, false);
if (quotedMessage && quotedMessage.canReply()) {
quotedMsgOptions = quotedMessage.msgContextInfo(chat);
}
}

const newMsgId = await window.WAPI.getNewMessageId(chat.id);
const fromwWid = await window.Store.Conn.wid;
const message = {
id: newMsgId,
ack: 0,
body: content,
from: fromwWid,
to: chat.id,
local: !0,
self: 'out',
t: parseInt(new Date().getTime() / 1000),
isNewMsg: !0,
type: 'chat',
...quotedMsgOptions,
};
let checkID = await WAPI.checkIdMessage(chatId, quotedMessageId);
if (checkID.erro == true) {
return checkID;
}

const newMsgId = await window.WAPI.getNewMessageId(chat.id);
const fromwWid = await window.Store.Conn.wid;

await window.Store.addAndSendMsgToChat(chat, message);
const message = {
id: newMsgId,
ack: 0,
body: content,
from: fromwWid,
to: chat.id,
local: !0,
self: 'out',
t: parseInt(new Date().getTime() / 1000),
isNewMsg: !0,
type: 'chat',
...quotedMsgOptions,
};

return newMsgId._serialized;
const result = (
await Promise.all(window.Store.addAndSendMsgToChat(chat, message))
)[1];

if (result === 'success' || result === 'OK') {
let obj = WAPI.scope(To, false, result, '');
Object.assign(obj, m);
return obj;
} else {
let obj = WAPI.scope(To, true, result, '');
Object.assign(obj, m);
return obj;
}
} else {
return chat;
}
}
3 changes: 2 additions & 1 deletion src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ interface WAPI {
) => Promise<object>;
promoteParticipant: (groupId: string, contactId: string | string[]) => void;
removeParticipant: (groupId: string, contactId: string | string[]) => void;
reply: (to: string, content: string, quotedMsg: string) => Promise<string>;
reply: (to: string, content: string, quotedMsg: string) => Promise<object>;
restartService: () => boolean;
sendChatstate: (chatState: string, chatId: string) => void;
sendContactVcard: (
Expand Down Expand Up @@ -245,6 +245,7 @@ interface WAPI {
waitNewAcknowledgements: (callback: Function) => void;
waitNewMessages: (rmCallback: boolean, callback: Function) => void;
sendSeen: (to: string) => void;
returnReply: (message: object) => object;
}

declare global {
Expand Down

0 comments on commit d74f96b

Please sign in to comment.