Skip to content

Commit

Permalink
feat: Added blocklist functions
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Sep 30, 2021
1 parent df6f1e2 commit 14579c6
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 1 deletion.
80 changes: 80 additions & 0 deletions src/blocklist/Blocklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*!
* Copyright 2021 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 Debug from 'debug';
import Emittery from 'emittery';

import { assertWid } from '../assert';
import * as webpack from '../webpack';
import { BlocklistStore, ContactModel, ContactStore, Wid } from '../whatsapp';
import { blockContact, unblockContact } from '../whatsapp/functions';
import { BlocklistEventTypes, BlocklistResult } from '.';

const debug = Debug('WPP:blocklist');

export class Blocklist extends Emittery<BlocklistEventTypes> {
constructor() {
super();
webpack.onInjected(() => this.initialize());
}

async initialize() {
BlocklistStore.on('sort', () => {
debug('synced');
this.emit('sync');
});

debug('initialized');
}

all(): Wid[] {
return BlocklistStore.models.map((b) => b.id);
}

isBlocked(chatId: string | Wid): boolean {
const wid = assertWid(chatId);

const contact = ContactStore.get(wid) || new ContactModel({ id: wid });

return contact.isBlocked();
}

async blockContact(chatId: string | Wid): Promise<BlocklistResult> {
const wid = assertWid(chatId);

const contact = ContactStore.get(wid) || new ContactModel({ id: wid });

await blockContact(contact);

return {
wid,
isBlocked: contact.isBlocked(),
};
}

async unblockContact(chatId: string | Wid): Promise<BlocklistResult> {
const wid = assertWid(chatId);

const contact = ContactStore.get(wid) || new ContactModel({ id: wid });

await unblockContact(contact);

return {
wid,
isBlocked: contact.isBlocked(),
};
}
}
22 changes: 22 additions & 0 deletions src/blocklist/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*!
* Copyright 2021 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 { Blocklist } from './Blocklist';

export * from './Blocklist';
export * from './types';

export default new Blocklist();
26 changes: 26 additions & 0 deletions src/blocklist/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*!
* Copyright 2021 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 { Wid } from '../whatsapp';

export interface BlocklistEventTypes {
sync: undefined;
}

export interface BlocklistResult {
wid: Wid;
isBlocked: boolean;
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import './deviceName';
import './patch';

import auth from './auth';
import blocklist from './blocklist';
import chat from './chat';
import status from './status';
import * as webpack from './webpack';

export { auth, chat, status, webpack };
export { auth, blocklist, chat, status, webpack };

export * as Auth from './auth';
export * as Chat from './chat';
Expand Down
37 changes: 37 additions & 0 deletions src/whatsapp/functions/blockContact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*!
* Copyright 2021 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';
import { ContactModel } from '../models';

/**
* @whatsapp 2.2138.10:38512
*/
export declare function blockContact(contact: ContactModel): Promise<void>;

/**
* @whatsapp 2.2138.10:38512
*/
export declare function unblockContact(contact: ContactModel): Promise<void>;

exportModule(
exports,
{
blockContact: 'blockContact',
unblockContact: 'unblockContact',
},
(m) => m.blockContact && m.unblockContact
);
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 './blockContact';
export * from './findChat';
export * from './getOrGenerate';
export * from './isAuthenticated';
Expand Down

0 comments on commit 14579c6

Please sign in to comment.