Skip to content

Commit

Permalink
feat: Added WPP.chat.archive function
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed May 26, 2022
1 parent f4bb9bf commit 8e7b0c7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/chat/functions/archive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*!
* 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 { setArchive } from '../../whatsapp/functions';

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

const chat = assertGetChat(wid);

if (chat.archive === archive) {
throw new WPPError(
`${archive ? 'archive' : 'unarchive'}_error`,
`The chat ${wid.toString()} is already ${
archive ? 'archived' : 'unarchived'
}`,
{ wid, archive }
);
}

await setArchive(chat, archive);

return {
wid,
archive,
};
}

/**
* Unarchive a chat
*
* @alias archive
*
* @example
* ```javascript
* // Unarchive a chat
* WPP.chat.unarchive('[number]@c.us');
*
* // Alias for
* WPP.chat.archive('[number]@c.us', false);
* ```
* @category Chat
*/
export async function unarchive(chatId: string | Wid) {
return archive(chatId, false);
}
1 change: 1 addition & 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 { archive, unarchive } from './archive';
export { canMute } from './canMute';
export { clear } from './clear';
export { delete } from './delete';
Expand Down
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export * from './sendQueryGroupInvite';
export * from './sendReactionToMsg';
export * from './sendRevokeGroupInviteCode';
export * from './sendTextMsgToChat';
export * from './setArchive';
export * from './setGroup';
export * from './status';
export * from './typeAttributeFromProtobuf';
Expand Down
35 changes: 35 additions & 0 deletions src/whatsapp/functions/setArchive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*!
* 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 59992
*/
export declare function setArchive(
chat: ChatModel,
archive: boolean,
id?: string
): Promise<void>;

exportModule(
exports,
{
setArchive: 'setArchive',
},
(m) => m.setArchive
);

0 comments on commit 8e7b0c7

Please sign in to comment.