Skip to content

Commit

Permalink
feat: Added option to filter chat and contacts with label (close #436)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Jun 4, 2022
1 parent b7ed183 commit bff74df
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
32 changes: 28 additions & 4 deletions src/chat/functions/list.ts
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

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

export interface ChatListOptions {
onlyGroups?: boolean;
onlyUsers?: boolean;
onlyWithUnreadMessage?: boolean;
withLabels?: string[];
}

/**
Expand All @@ -28,13 +29,22 @@ export interface ChatListOptions {
* @example
* ```javascript
* // All chats
* const chat = await WPP.chat.list();
* const chats = await WPP.chat.list();
*
* // Only users chats
* const chat = await WPP.chat.list({onlyUsers: true});
* const chats = await WPP.chat.list({onlyUsers: true});
*
* // Only groups chats
* const chat = await WPP.chat.list({onlyGroups: true});
* const chats = await WPP.chat.list({onlyGroups: true});
*
* // Only with label Text
* const chats = await WPP.chat.list({withLabels: ['Test']});
*
* // Only with label id
* const chats = await WPP.chat.list({withLabels: ['1']});
*
* // Only with label with one of text or id
* const chats = await WPP.chat.list({withLabels: ['Alfa','5']});
* ```
*
* @category Chat
Expand All @@ -56,5 +66,19 @@ export async function list(
models = models.filter((c) => c.hasUnread);
}

if (options.withLabels) {
const ids = options.withLabels.map((value) => {
const label = LabelStore.findFirst((l) => l.name === value);

if (label) {
return label.id;
}

return value;
});

models = models.filter((c) => c.labels?.some((id) => ids.includes(id)));
}

return models;
}
27 changes: 25 additions & 2 deletions src/contact/functions/list.ts
Expand Up @@ -14,10 +14,11 @@
* limitations under the License.
*/

import { ContactModel, ContactStore } from '../../whatsapp';
import { ContactModel, ContactStore, LabelStore } from '../../whatsapp';

export interface ContactListOptions {
onlyMyContacts?: boolean;
withLabels?: string[];
}

/**
Expand All @@ -29,7 +30,16 @@ export interface ContactListOptions {
* const contats = await WPP.contact.list();
*
* // Only my contacts
* const contatcs = await WPP.contact.list({onlyMyContacts: true});
* const contacts = await WPP.contact.list({onlyMyContacts: true});
*
* // Only with label Text
* const contacts = await WPP.contact.list({withLabels: ['Test']});
*
* // Only with label id
* const contacts = await WPP.contact.list({withLabels: ['1']});
*
* // Only with label with one of text or id
* const contacts = await WPP.contact.list({withLabels: ['Alfa','5']});
* ```
*
* @category Contact
Expand All @@ -43,5 +53,18 @@ export async function list(
models = models.filter((c) => c.isMyContact);
}

if (options.withLabels) {
const ids = options.withLabels.map((value) => {
const label = LabelStore.findFirst((l) => l.name === value);

if (label) {
return label.id;
}

return value;
});

models = models.filter((c) => c.labels?.some((id) => ids.includes(id)));
}
return models;
}
2 changes: 1 addition & 1 deletion src/whatsapp/models/ContactModel.ts
Expand Up @@ -37,7 +37,7 @@ interface Props {
privacyMode?: any;
statusMute?: any;
sectionHeader?: any;
labels?: any;
labels?: string[];
disappearingModeDuration?: any;
disappearingModeSettingTimestamp?: any;
}
Expand Down
2 changes: 1 addition & 1 deletion src/whatsapp/models/ModelChatBase.ts
Expand Up @@ -23,7 +23,7 @@ import { MsgModel } from './MsgModel';
export interface PropsChatBase {
id: Wid;
pendingMsgs: boolean;
labels: any | undefined;
labels?: string[];
}

export interface SessionChatBase {
Expand Down

0 comments on commit bff74df

Please sign in to comment.