From d2c5c7c51510a87d0b24615b5b6bf51bac96f63b Mon Sep 17 00:00:00 2001 From: Edgard Date: Sat, 15 Jan 2022 18:42:17 -0300 Subject: [PATCH] feat: Added WPP.chat.mute and unmute functions --- src/chat/functions/canMute.ts | 36 +++++++++++++++ src/chat/functions/index.ts | 3 ++ src/chat/functions/mute.ts | 76 ++++++++++++++++++++++++++++++++ src/chat/functions/unmute.ts | 36 +++++++++++++++ src/whatsapp/models/ChatModel.ts | 4 +- src/whatsapp/models/MuteModel.ts | 10 +++-- 6 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 src/chat/functions/canMute.ts create mode 100644 src/chat/functions/mute.ts create mode 100644 src/chat/functions/unmute.ts diff --git a/src/chat/functions/canMute.ts b/src/chat/functions/canMute.ts new file mode 100644 index 0000000000..a3a1e98b5f --- /dev/null +++ b/src/chat/functions/canMute.ts @@ -0,0 +1,36 @@ +/*! + * 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 { assertGetChat, assertWid } from '../../assert'; +import { Wid } from '../../whatsapp'; + +/** + * Check if is possible to mute this chat + * + * @example + * ```javascript + * const canMute = WPP.chat.canMute('@c.us'); + * ``` + * + * @category Chat + */ +export function canMute(chatId: string | Wid): boolean { + const wid = assertWid(chatId); + + const chat = assertGetChat(wid); + + return chat.mute.canMute(); +} diff --git a/src/chat/functions/index.ts b/src/chat/functions/index.ts index 3681981d57..18d543b488 100644 --- a/src/chat/functions/index.ts +++ b/src/chat/functions/index.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +export { canMute } from './canMute'; export { clear } from './clear'; export { delete } from './delete'; export { deleteMessage, DeleteMessageReturn } from './deleteMessage'; @@ -28,6 +29,7 @@ export { markIsPaused } from './markIsPaused'; export { markIsRead } from './markIsRead'; export { markIsRecording } from './markIsRecording'; export { markIsUnread } from './markIsUnread'; +export { mute } from './mute'; export { LinkPreviewOptions, prepareLinkPreview } from './prepareLinkPreview'; export { MessageButtonsOptions, @@ -51,3 +53,4 @@ export { sendVCardContactMessage, VCardContact, } from './sendVCardContactMessage'; +export { unmute } from './unmute'; diff --git a/src/chat/functions/mute.ts b/src/chat/functions/mute.ts new file mode 100644 index 0000000000..56e6cd0473 --- /dev/null +++ b/src/chat/functions/mute.ts @@ -0,0 +1,76 @@ +/*! + * 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 { assertGetChat, assertWid } from '../../assert'; +import { WPPError } from '../../util'; +import { Clock, Wid } from '../../whatsapp'; + +/** + * Mute a chat, you can use duration or expiration + * For expiration, use unix timestamp (seconds only) + * For duration, use seconds + * + * @example + * ```javascript + * // Mute for 60 seconds + * WPP.chat.mute('@c.us', {duration: 60}); + * + * // Mute util 2021-01-01 + * WPP.chat.mute('@c.us', {expiration: 1641006000}); + * + * // or using date + * const expiration = new Date('2022-01-01 00:00:00'); + * WPP.chat.mute('@c.us', {expiration: expiration}); + * ``` + * + * @category Chat + */ +export async function mute( + chatId: string | Wid, + time: { expiration: number | Date } | { duration: number } +) { + const wid = assertWid(chatId); + + const chat = assertGetChat(wid); + + let expiration = 0; + + if ('expiration' in time) { + if (typeof time.expiration === 'number') { + expiration = time.expiration; + } else { + expiration = time.expiration.getTime() / 1000; + } + } else if ('duration' in time) { + expiration = Clock.globalUnixTime() + time.duration; + } else { + throw new WPPError('invalid_time_mute', 'Invalid time for mute', { time }); + } + + if (expiration < Clock.globalUnixTime()) { + throw new WPPError('negative_time_mute', 'Negative duration for mute', { + time, + }); + } + + await chat.mute.mute(expiration, true); + + return { + wid, + expiration: chat.mute.expiration, + isMuted: chat.mute.isMuted, + }; +} diff --git a/src/chat/functions/unmute.ts b/src/chat/functions/unmute.ts new file mode 100644 index 0000000000..68eb10058a --- /dev/null +++ b/src/chat/functions/unmute.ts @@ -0,0 +1,36 @@ +/*! + * 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 { assertGetChat, assertWid } from '../../assert'; +import { Wid } from '../../whatsapp'; + +/** + * Unmute a chat + * + * @example + * ```javascript + * WPP.chat.unmute('@c.us'); + * ``` + * + * @category Chat + */ +export async function unmute(chatId: string | Wid) { + const wid = assertWid(chatId); + + const chat = assertGetChat(wid); + + return chat.mute.unmute(true); +} diff --git a/src/whatsapp/models/ChatModel.ts b/src/whatsapp/models/ChatModel.ts index 34eeef4794..74df352cd7 100644 --- a/src/whatsapp/models/ChatModel.ts +++ b/src/whatsapp/models/ChatModel.ts @@ -18,7 +18,7 @@ import { ChatCollection } from '../collections'; import { SendMsgResult } from '../enums'; import { exportProxyModel } from '../exportModule'; import { MsgKey, MsgLoad, Wid } from '../misc'; -import { GroupMetadataModel, MsgModel } from '.'; +import { GroupMetadataModel, MsgModel, MuteModel } from '.'; import { ModelOptions, ModelPropertiesContructor, ModelProxy } from './Model'; import { ModelChatBase, PropsChatBase, SessionChatBase } from './ModelChatBase'; @@ -68,7 +68,7 @@ interface Session extends SessionChatBase { quotedMsgAdminGroupJid?: any; groupMetadata?: GroupMetadataModel; presence?: any; - mute?: any; + mute: MuteModel; contact?: any; liveLocation?: any; liveLocationQueried?: any; diff --git a/src/whatsapp/models/MuteModel.ts b/src/whatsapp/models/MuteModel.ts index 11e93bc393..617b23944f 100644 --- a/src/whatsapp/models/MuteModel.ts +++ b/src/whatsapp/models/MuteModel.ts @@ -26,7 +26,7 @@ import { interface Props { id: Wid; - expiration?: any; + expiration: number; } interface Session { @@ -51,9 +51,13 @@ export declare class MuteModel extends Model { options?: ModelOptions ); setMute(e?: any, t?: any): any; - mute(e?: any, t?: any, r?: any): any; + mute( + expiration: number, + sendAction?: boolean, + sequence?: number + ): Promise; canMute(): boolean; - unmute(e?: any, t?: any): any; + unmute(sendAction?: boolean, sequence?: number): Promise; getCollection(): MuteCollection; }