Skip to content

Commit

Permalink
Add ui layer for human mode
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcardeenas committed Apr 12, 2020
1 parent 4955e07 commit 76da1b1
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 40 deletions.
14 changes: 3 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
"@types/node": "^13.11.1",
"@types/puppeteer": "^2.0.1",
"@types/sharp": "^0.24.0",
"auto-changelog": "^1.16.4",
"auto-changelog": "^2.0.0",
"concurrently": "^5.1.0",
"copy-webpack-plugin": "^5.1.1",
"gulp": "^4.0.2",
"husky": "^4.2.4",
"husky": "^4.2.5",
"nodemon": "^2.0.3",
"prettier": "^2.0.4",
"pretty-quick": "^2.0.1",
Expand Down
3 changes: 2 additions & 1 deletion src/api/layers/controls.layer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Page } from 'puppeteer';
import { GroupLayer } from './group.layer';
import { UILayer } from './ui.layer';

declare module WAPI {
const deleteConversation: (chatId: string) => boolean;
Expand All @@ -11,7 +12,7 @@ declare module WAPI {
) => any;
}

export class ControlsLayer extends GroupLayer {
export class ControlsLayer extends UILayer {
constructor(page: Page) {
super(page);
}
Expand Down
40 changes: 40 additions & 0 deletions src/api/layers/ui.layer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Page } from 'puppeteer';
import { GroupLayer } from './group.layer';

declare module WAPI {
const openChat: (chatId: string) => boolean;
const openChatAt: (
chatId: string,
messageId: string
) => { wasVisible: boolean; alignAt: string };
}

export class UILayer extends GroupLayer {
constructor(page: Page) {
super(page);
}

/**
* Opens given chat at last message (bottom)
* Will fire natural workflow events of whatsapp web
* @param chatId
*/
public async openChat(chatId: string) {
return this.page.evaluate(
(chatId: string) => WAPI.openChat(chatId),
chatId
);
}

/**
* Opens chat at given message position
* @param chatId Chat id
* @param messageId Message id (For example: '06D3AB3D0EEB9D077A3F9A3EFF4DD030')
*/
public async openChatAt(chatId: string, messageId: string) {
return this.page.evaluate(
(chatId: string) => WAPI.openChatAt(chatId, messageId),
chatId
);
}
}
20 changes: 0 additions & 20 deletions src/api/model/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,6 @@ export interface Chat {
lastSeen: null;
}

export interface Contact {
id: Id;
name: string;
shortName: string;
pushname: string;
type: string;
isBusiness: boolean;
isEnterprise: boolean;
statusMute: boolean;
labels: any[];
formattedName: string;
isMe: boolean;
isMyContact: boolean;
isPSA: boolean;
isUser: boolean;
isWAContact: boolean;
profilePicThumbObj: ProfilePicThumbObj;
msgs: null;
}

export interface ProfilePicThumbObj {
eurl: string;
id: Id;
Expand Down
4 changes: 2 additions & 2 deletions src/api/whatsapp.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Page } from 'puppeteer';
import { decrypt } from './helpers/decrypt';
import { GroupLayer } from './layers/group.layer';
import { ControlsLayer } from './layers/controls.layer';
import { Message } from './model';

declare module WAPI {
const arrayBufferToBase64: (buffer: ArrayBuffer) => string;
}

export class Whatsapp extends GroupLayer {
export class Whatsapp extends ControlsLayer {
constructor(public page: Page) {
super(page);
}
Expand Down
8 changes: 7 additions & 1 deletion src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function initWhatsapp(session: string, options: CreateConfig) {
}

export async function injectApi(page: Page) {
page.waitForFunction(() => {
await page.waitForFunction(() => {
// @ts-ignore
return webpackJsonp !== undefined;
});
Expand All @@ -33,6 +33,12 @@ export async function injectApi(page: Page) {
),
});

// Make sure WAPI is initialized
await page.waitForFunction(() => {
// @ts-ignore
return !!WAPI.getWAVersion;
});

return page;
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/wapi/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ export { removeParticipant } from './remove-participant';
export { addParticipant } from './add-participant';
export { promoteParticipant } from './promote-participant';
export { demoteParticipant } from './demote-participant';
export { openChat, openChatAt } from './open-chat';
31 changes: 30 additions & 1 deletion src/lib/wapi/functions/open-chat.js
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
export function openChat(chatId) {}
/**
* Opens chat as UI, will fire natural workflow events of whatsapp web
* This is a UI proccess, use this in a queue
* @param {string} chatId chat id
* @returns {boolean}
*/
export async function openChat(chatId) {
const chat = Store.Chat.get(chatId);
const result = Store.Cmd.default.openChatBottom(chat);
return result;
}

/**
* Opens chat at given message position
* This is a UI proccess, use this in a queue
* @param {string} chatId Chat id
* @param {string} messageId Message id: (For example: '06D3AB3D0EEB9D077A3F9A3EFF4DD030')
* @returns {{wasVisible: boolean, alignAt: string}}: {wasVisible: false, alignAt: "center"}
*/
export async function openChatAt(chatId, messageId) {
const chat = Store.Chat.get(chatId);
const atMessage = chat.msgs.models.find((model) => model.id.id === messageId);
const args = {
collection: chat.msgs,
msg: atMessage,
isUnreadDivider: false,
};
const result = await Store.Cmd.default._openChat(chat, args);
return result;
}
4 changes: 4 additions & 0 deletions src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ import {
startTyping,
stopTyping,
unblockContact,
openChat,
openChatAt,
} from './functions';
import {
base64ToFile,
Expand Down Expand Up @@ -172,6 +174,8 @@ window.WAPI.stopTyping = stopTyping;
window.WAPI.sendLocation = sendLocation;
window.WAPI.blockContact = blockContact;
window.WAPI.unblockContact = unblockContact;
window.WAPI.openChat = openChat;
window.WAPI.openChatAt = openChatAt;

// Retrieving functions
window.WAPI.getAllContacts = getAllContacts;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/wapi/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const path = require('path');

module.exports = {
entry: './wapi.js',
mode: 'development',
devtool: 'source-map',
// mode: 'development',
// devtool: 'source-map',
output: {
path: path.resolve(__dirname, '../../../dist/lib/wapi'),
filename: 'wapi.js',
Expand Down

0 comments on commit 76da1b1

Please sign in to comment.