Skip to content

Commit

Permalink
feat: Added WPP.chat.pin function (close wppconnect-team#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed May 27, 2022
1 parent 9ed3103 commit 0fdd8fe
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/chat/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export { mute } from './mute';
export { openChatAt } from './openChatAt';
export { openChatBottom } from './openChatBottom';
export { openChatFromUnread } from './openChatFromUnread';
export { pin, unpin } from './pin';
export { LinkPreviewOptions, prepareLinkPreview } from './prepareLinkPreview';
export {
MessageButtonsOptions,
Expand Down
75 changes: 75 additions & 0 deletions src/chat/functions/pin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*!
* 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 { Wid } from '../../whatsapp';
import { setPin } from '../../whatsapp/functions';

/**
* Pin a chat
*
* @example
* ```javascript
* // Pin a chat
* WPP.chat.pin('[number]@c.us');
*
* // Unpin a chat
* WPP.chat.pin('[number]@c.us', false);
* // or
* WPP.chat.unpin('[number]@c.us');
* ```
* @category Chat
*/
export async function pin(chatId: string | Wid, pin = true) {
const wid = assertWid(chatId);

const chat = assertGetChat(wid);

if (chat.pin === pin) {
throw new WPPError(
`${pin ? 'pin' : 'unpin'}_error`,
`The chat ${wid.toString()} is already ${pin ? 'pinned' : 'unpinned'}`,
{ wid, pin: pin }
);
}

await setPin(chat, pin);

return {
wid,
pin: pin,
};
}

/**
* Unpin a chat
*
* @alias pin
*
* @example
* ```javascript
* // Unpin a chat
* WPP.chat.unpin('[number]@c.us');
*
* // Alias for
* WPP.chat.pin('[number]@c.us', false);
* ```
* @category Chat
*/
export async function unpin(chatId: string | Wid) {
return pin(chatId, false);
}
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export * from './sendRevokeGroupInviteCode';
export * from './sendTextMsgToChat';
export * from './setArchive';
export * from './setGroup';
export * from './setPin';
export * from './status';
export * from './typeAttributeFromProtobuf';
export * from './uploadProductImage';
Expand Down
31 changes: 31 additions & 0 deletions src/whatsapp/functions/setPin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*!
* 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 { ChatModel } from '../models';

/**
* @whatsapp 10236
*/
export declare function setPin(chat: ChatModel, pin: boolean): Promise<void>;

exportModule(
exports,
{
setPin: 'setPin',
},
(m) => m.setPin && !m.unpinAll
);

0 comments on commit 0fdd8fe

Please sign in to comment.