Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feats and fixed functions #1015

Merged
merged 4 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/chat/events/eventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { Label } from '../../labels';
import { ChatModel, MsgKey, MsgModel, Wid } from '../../whatsapp';

export interface ChatEventTypes {
Expand Down Expand Up @@ -167,4 +168,14 @@ export interface ChatEventTypes {
timestamp: number;
sender: Wid;
};

/**
* On Labels update
*/
'chat.update_label': {
chat: ChatModel;
ids: string[];
labels: Label[];
type: 'add' | 'remove';
};
}
1 change: 1 addition & 0 deletions src/chat/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ import './registerPollEvent';
import './registerPresenceChange';
import './registerReactionsEvent';
import './registerRevokeMessageEvent';
import './registerLabelEvent';
64 changes: 64 additions & 0 deletions src/chat/events/registerLabelEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*!
* Copyright 2022 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { isMainReady } from '../../conn';
import { internalEv } from '../../eventEmitter';
import { getLabelById, Label } from '../../labels';
import * as webpack from '../../webpack';
import { ChatModel } from '../../whatsapp';
import { wrapModuleFunction } from '../../whatsapp/exportModule';
import {
addToLabelCollection,
removeLabelFromCollection,
} from '../../whatsapp/functions';
import { get as getChat } from '../functions/';

webpack.onInjected(() => register());

function register() {
async function processLabelEvent(event: 'add' | 'remove', ...args: any) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talvez aqui seria interessante fazer um teste com o ChatStore.on() para capturar os eventos de etiquetas.

Vou fazer mais testes para saber se existe outro ponto melhor.

const data = args[0];
const ids = Array.isArray(data[1]) ? data[1] : [data[1]];
const chatId = data[0];

if (isMainReady()) {
const labels = [] as Label[];
for (const id of ids) {
labels.push(await getLabelById(id));
}
internalEv.emit('chat.update_label', {
chat: getChat(chatId) as ChatModel,
ids: ids,
labels: labels,
type: event,
});
}
}

wrapModuleFunction(addToLabelCollection, async (func, ...args) => {
queueMicrotask(() => {
processLabelEvent('add', args);
});
return func(...args);
});

wrapModuleFunction(removeLabelFromCollection, async (func, ...args) => {
queueMicrotask(() => {
processLabelEvent('remove', args);
});
return func(...args);
});
}
69 changes: 69 additions & 0 deletions src/chat/functions/getAllChats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*!
* Copyright 2023 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ChatModel, ChatStore, Wid } from '../../whatsapp';
import { get } from './get';

export interface GetAllChatsOptions {
count?: number;
direction?: 'after' | 'before';
id?: Wid;
onlyUnread?: boolean;
}
/**
* Get all chats
* * @example
* ```javascript
* // Some chats
* WPP.chat.getAllChats({
* count: 20,
* });
*
* // All chats
* WPP.chat.getAllChats({
* count: -1,
* });
*
* // 20 chats before specific chat
* WPP.chat.getMessages({
* count: 20,
* direction: 'before',
* id: '[number]@c.us'
* });
* ```
* @category Chat
*/
export function getAllChats(options: GetAllChatsOptions = {}): ChatModel[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dê uma olhada no WPP.chat.list, pois basicamente ele é a mesma coisa, talvez seja interessante adicionar a opção de busca lá, como o count e direction

const count = options.count == -1 ? Infinity : options.count || 20;
const direction = options.direction === 'before' ? 'before' : 'after';
const indexChat = options?.id
? get(options.id)
: ChatStore.getModelsArray()[0];
const onlyUnread = options.onlyUnread ? options.onlyUnread : false;
const allChats = onlyUnread
? ChatStore.filter((chat: ChatModel) => chat.unreadCount)
: ChatStore.getModelsArray();

const startIndex = allChats.indexOf(indexChat as any);
if (direction === 'before') {
const fixStartIndex = startIndex - count < 0 ? 0 : startIndex - count;
const fixEndIndex =
fixStartIndex + count >= startIndex ? startIndex : fixStartIndex + count;
return allChats.slice(fixStartIndex, fixEndIndex);
} else {
return allChats.slice(startIndex, startIndex + count);
}
}
1 change: 1 addition & 0 deletions src/chat/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export { forwardMessage, ForwardMessagesOptions } from './forwardMessage';
export { generateMessageID } from './generateMessageID';
export { get } from './get';
export { getActiveChat } from './getActiveChat';
export { getAllChats } from './getAllChats';
export { getLastSeen } from './getLastSeen';
export { getMessageACK } from './getMessageACK';
export { getMessageById } from './getMessageById';
Expand Down
2 changes: 1 addition & 1 deletion src/group/functions/getAllGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { queryAllGroups } from '../../whatsapp/functions';
*
* @example
* ```javascript
* WPP.group.queryAllGroups();
* WPP.group.getAllGroups();
* ```
*
* @category Group
Expand Down
45 changes: 45 additions & 0 deletions src/whatsapp/functions/addToLabelCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*!
* Copyright 2023 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { exportModule } from '../exportModule';

/** @whatsapp 388536
*/
export declare function addToLabelCollection(
e: any,
t: any,
i: any
): Promise<any>;
export declare function createLabelItemId(e: any, t: any, r: any): Promise<any>;
export declare function getParentCollection(e: any): Promise<any>;
export declare function initializeLabels(e: any): Promise<any>;
export declare function removeLabelFromCollection(
e: any,
t: any,
i: any
): Promise<any>;

exportModule(
exports,
{
addToLabelCollection: 'addToLabelCollection',
createLabelItemId: 'createLabelItemId',
getParentCollection: 'getParentCollection',
initializeLabels: 'initializeLabels',
removeLabelFromCollection: 'removeLabelFromCollection',
},
(m) => m.addToLabelCollection
);
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

export * from './addAndSendMsgToChat';
export * from './addToLabelCollection';
export * from './blockContact';
export * from './calculateFilehashFromBlob';
export * from './canEditMsg';
Expand Down