Skip to content

Commit

Permalink
feat: Added WPP.chat.mute and unmute functions
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Jan 15, 2022
1 parent 127f32c commit d2c5c7c
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 5 deletions.
36 changes: 36 additions & 0 deletions src/chat/functions/canMute.ts
Original file line number Diff line number Diff line change
@@ -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('<number>@c.us');
* ```
*
* @category Chat
*/
export function canMute(chatId: string | Wid): boolean {
const wid = assertWid(chatId);

const chat = assertGetChat(wid);

return chat.mute.canMute();
}
3 changes: 3 additions & 0 deletions src/chat/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -51,3 +53,4 @@ export {
sendVCardContactMessage,
VCardContact,
} from './sendVCardContactMessage';
export { unmute } from './unmute';
76 changes: 76 additions & 0 deletions src/chat/functions/mute.ts
Original file line number Diff line number Diff line change
@@ -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('<number>@c.us', {duration: 60});
*
* // Mute util 2021-01-01
* WPP.chat.mute('<number>@c.us', {expiration: 1641006000});
*
* // or using date
* const expiration = new Date('2022-01-01 00:00:00');
* WPP.chat.mute('<number>@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,
};
}
36 changes: 36 additions & 0 deletions src/chat/functions/unmute.ts
Original file line number Diff line number Diff line change
@@ -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('<number>@c.us');
* ```
*
* @category Chat
*/
export async function unmute(chatId: string | Wid) {
const wid = assertWid(chatId);

const chat = assertGetChat(wid);

return chat.mute.unmute(true);
}
4 changes: 2 additions & 2 deletions src/whatsapp/models/ChatModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -68,7 +68,7 @@ interface Session extends SessionChatBase {
quotedMsgAdminGroupJid?: any;
groupMetadata?: GroupMetadataModel;
presence?: any;
mute?: any;
mute: MuteModel;
contact?: any;
liveLocation?: any;
liveLocationQueried?: any;
Expand Down
10 changes: 7 additions & 3 deletions src/whatsapp/models/MuteModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {

interface Props {
id: Wid;
expiration?: any;
expiration: number;
}

interface Session {
Expand All @@ -51,9 +51,13 @@ export declare class MuteModel extends Model<MuteCollection> {
options?: ModelOptions
);
setMute(e?: any, t?: any): any;
mute(e?: any, t?: any, r?: any): any;
mute(
expiration: number,
sendAction?: boolean,
sequence?: number
): Promise<number>;
canMute(): boolean;
unmute(e?: any, t?: any): any;
unmute(sendAction?: boolean, sequence?: number): Promise<void>;
getCollection(): MuteCollection;
}

Expand Down

0 comments on commit d2c5c7c

Please sign in to comment.