Skip to content

Commit

Permalink
feat: check deleteMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Mar 20, 2021
1 parent 02663c1 commit c525c71
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 57 deletions.
48 changes: 40 additions & 8 deletions src/api/layers/controls.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { UILayer } from './ui.layer';
import { Scope, checkValuesSender } from '../helpers/layers-interface';

let obj: Scope;

export class ControlsLayer extends UILayer {
constructor(public page: Page, session?: string, options?: CreateConfig) {
Expand Down Expand Up @@ -165,14 +168,43 @@ export class ControlsLayer extends UILayer {
*/
public async deleteMessage(
chatId: string,
messageId: string[] | string,
onlyLocal = false
) {
return await this.page.evaluate(
({ contactId, messageId, onlyLocal }) =>
WAPI.deleteMessages(contactId, messageId, onlyLocal),
{ contactId: chatId, messageId, onlyLocal }
);
messageId: string[]
): Promise<Object> {
return new Promise(async (resolve, reject) => {
const typeFunction = 'deleteMessage';
const type = 'string';
const check = [
{
param: 'chatId',
type: type,
value: chatId,
function: typeFunction,
isUser: true,
},
{
param: 'messageId',
type: 'object',
value: messageId,
function: typeFunction,
isUser: true,
},
];

const validating = checkValuesSender(check);
if (typeof validating === 'object') {
return reject(validating);
}
const result = await this.page.evaluate(
({ chatId, messageId }) => WAPI.deleteMessages(chatId, messageId),
{ chatId, messageId }
);

if (result['erro'] == true) {
return reject(result);
} else {
return resolve(result);
}
});
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ import {
stickerSelect,
} from '../helpers';
import { filenameFromMimeType } from '../helpers/filename-from-mimetype';
import {
Message,
SendFileResult,
SendLinkResult,
SendStickerResult,
} from '../model';
import { Message, SendFileResult, SendStickerResult } from '../model';
import { ChatState } from '../model/enum';
import { ListenerLayer } from './listener.layer';
import { Scope, checkValuesSender } from '../helpers/layers-interface';
Expand Down
113 changes: 75 additions & 38 deletions src/lib/wapi/functions/delete-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,85 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import { getMessageById } from './get-message-by-id';

export async function deleteMessages(chatId, messageArray, onlyLocal, done) {
const userId = new Store.WidFactory.createWid(chatId);
const conversation = WAPI.getChat(userId);
if (!conversation) {
if (done !== undefined) {
done(false);
}
return false;
export async function deleteMessages(chatId, messageArray) {
if (typeof chatId != 'string') {
return WAPI.scope(
null,
true,
404,
'enter the chatid variable as an string'
);
}
const chat = await WAPI.sendExist(chatId);
if (chat && chat.status != 404) {
if (!Array.isArray(messageArray)) {
return WAPI.scope(
chat,
true,
404,
'enter the message identification variable as an array'
);
}

if (!Array.isArray(messageArray)) {
messageArray = [messageArray];
}
for (let i in messageArray) {
let ft = await WAPI.loadAndGetAllMessagesInChat(chatId, true);
let fil = ft.filter(
(e) => e.id === messageArray[i] && e.body === undefined
);
let check = ft.filter((e) => e.id === messageArray[i]);
if (!check.length) {
return WAPI.scope(
chat,
true,
404,
`The id ${messageArray[i]} does not exist!`
);
}
if (fil.length) {
return WAPI.scope(
chat,
true,
404,
`The message with id ${messageArray[i]} has already been deleted`
);
}
}

let messagesToDelete = (
await Promise.all(
messageArray.map(async (msgId) =>
typeof msgId == 'string'
? await getMessageById(msgId, null, false)
: msgId
let messagesToDelete = (
await Promise.all(
messageArray.map(
async (msgId) => await WAPI.getMessageById(msgId, null, false)
)
)
)
).filter((x) => x);
if (messagesToDelete.length == 0) return true;
let jobs = onlyLocal
? [conversation.sendDeleteMsgs(messagesToDelete, conversation)]
: [
conversation.sendRevokeMsgs(
messagesToDelete.filter((msg) => msg.isSentByMe),
conversation
),
conversation.sendDeleteMsgs(
messagesToDelete.filter((msg) => !msg.isSentByMe),
conversation
),
];
return Promise.all(jobs).then((_) => {
if (done !== undefined) {
done(true);
).filter((x) => x);

const To = chat.id;
const m = { type: 'deleteMessages' };

let jobs = [
chat.sendRevokeMsgs(
messagesToDelete.filter((msg) => msg.isSentByMe),
chat
),
chat.sendDeleteMsgs(
messagesToDelete.filter((msg) => !msg.isSentByMe),
chat
),
];

const result = (await Promise.all(jobs))[0][0];

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;
}
return true;
});
} else {
return chat;
}
}
6 changes: 1 addition & 5 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ interface WAPI {
contactId: string | string[]
) => GroupCreation;
deleteConversation: (chatId: string) => boolean;
deleteMessages: (
contactId: string,
messageId: string[] | string,
onlyLocal: boolean
) => Promise<boolean>;
deleteMessages: (contactId: string, messageId: string[]) => Promise<object>;
demoteParticipant: (groupId: string, contactId: string | string[]) => void;
downloadFile: (data: string) => Promise<string | boolean>;
downloadMedia: (messageId: string) => Promise<string>;
Expand Down

0 comments on commit c525c71

Please sign in to comment.