forked from wppconnect-team/wa-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added WPP.chat.mute and unmute functions
- Loading branch information
1 parent
127f32c
commit d2c5c7c
Showing
6 changed files
with
160 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters