From 77298e14d34e96c92d5257507b260419ca047c21 Mon Sep 17 00:00:00 2001 From: Cleiton Carvalho Date: Thu, 30 Nov 2023 13:01:42 -0300 Subject: [PATCH] feat: Added WPP.chat.keepMessage function (close #1142) (#1517) * feat: Added WPP.chat.keepMessage function * fix: Typos fix * fix: Fixed imports --- src/chat/functions/index.ts | 1 + src/chat/functions/keepMessage.ts | 60 +++++++++++++++++++++++ src/whatsapp/enums/KIC_ENTRY_POINT_TYP.ts | 37 ++++++++++++++ src/whatsapp/enums/index.ts | 1 + src/whatsapp/functions/index.ts | 1 + src/whatsapp/functions/keepMessage.ts | 37 ++++++++++++++ 6 files changed, 137 insertions(+) create mode 100644 src/chat/functions/keepMessage.ts create mode 100644 src/whatsapp/enums/KIC_ENTRY_POINT_TYP.ts create mode 100644 src/whatsapp/functions/keepMessage.ts diff --git a/src/chat/functions/index.ts b/src/chat/functions/index.ts index a29d09714f..c8001a0ea1 100644 --- a/src/chat/functions/index.ts +++ b/src/chat/functions/index.ts @@ -37,6 +37,7 @@ export { getQuotedMsg } from './getQuotedMsg'; export { getQuotedMsgKey } from './getQuotedMsgKey'; export { getReactions } from './getReactions'; export { getVotes } from './getVotes'; +export { keepMessage } from './keepMessage'; export { ChatListOptions, list } from './list'; export { markIsComposing } from './markIsComposing'; export { markIsPaused } from './markIsPaused'; diff --git a/src/chat/functions/keepMessage.ts b/src/chat/functions/keepMessage.ts new file mode 100644 index 0000000000..cb7d976497 --- /dev/null +++ b/src/chat/functions/keepMessage.ts @@ -0,0 +1,60 @@ +/*! + * Copyright 2023 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 { iAmAdmin } from '../../group'; +import { WPPError } from '../../util'; +import { MsgKey, MsgModel } from '../../whatsapp'; +import { KIC_ENTRY_POINT_TYPE } from '../../whatsapp/enums'; +import { + keepMessage as KeepMessage, + undoKeepMessage, +} from '../../whatsapp/functions'; +import { getMessageById } from './getMessageById'; + +/** + * Keep or unkeep a message in a group chat with expiration + * + * @example + * ```javascript + * // To keep a message in chat + * WPP.chat.keepMessage('true_[number]@c.us_ABCDEF', true); + * + * // To unkeep a message in chat + * WPP.chat.keepMessage('true_[number]@c.us_ABCDEF', false); + * ``` + * @category Chat + */ +export async function keepMessage( + msgId: string | MsgKey, + value = true +): Promise { + const msg = await getMessageById(msgId); + if (!(await iAmAdmin(msg.id.remote))) { + throw new WPPError('you_not_group_admin', 'You is not a group admin'); + } else if (msg.isExpired()) { + throw new WPPError('msg_expired', 'This message has expired'); + } else if (value) { + await KeepMessage(msg, KIC_ENTRY_POINT_TYPE.CHAT); + return await getMessageById(msgId); + } else { + await undoKeepMessage( + msg, + { deleteExpired: true }, + KIC_ENTRY_POINT_TYPE.CHAT + ); + return await getMessageById(msgId); + } +} diff --git a/src/whatsapp/enums/KIC_ENTRY_POINT_TYP.ts b/src/whatsapp/enums/KIC_ENTRY_POINT_TYP.ts new file mode 100644 index 0000000000..6c082c50b2 --- /dev/null +++ b/src/whatsapp/enums/KIC_ENTRY_POINT_TYP.ts @@ -0,0 +1,37 @@ +/*! + * Copyright 2023 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'; + +/** + * @whatsapp 982015 + */ +export declare enum KIC_ENTRY_POINT_TYPE { + CHAT_INFO = 1, + SEARCH = 2, + CHAT = 3, + MEDIA = 4, + DOCS = 5, + LINKS = 6, +} + +exportModule( + exports, + { + KIC_ENTRY_POINT_TYPE: 'KIC_ENTRY_POINT_TYPE', + }, + (m) => m.KIC_ENTRY_POINT_TYPE +); diff --git a/src/whatsapp/enums/index.ts b/src/whatsapp/enums/index.ts index 96e30bf2a8..0cb682ec6d 100644 --- a/src/whatsapp/enums/index.ts +++ b/src/whatsapp/enums/index.ts @@ -17,6 +17,7 @@ export * from './ACK'; export * from './CALL_STATES'; export * from './GROUP_SETTING_TYPE'; +export * from './KIC_ENTRY_POINT_TYP'; export * from './LogoutReason'; export * from './MSG_TYPE'; export * from './OUTWARD_TYPES'; diff --git a/src/whatsapp/functions/index.ts b/src/whatsapp/functions/index.ts index d89ab7b10f..a04618e5af 100644 --- a/src/whatsapp/functions/index.ts +++ b/src/whatsapp/functions/index.ts @@ -74,6 +74,7 @@ export * from './isAuthenticated'; export * from './isRegistered'; export * from './isUnreadTypeMsg'; export * from './joinGroupViaInvite'; +export * from './keepMessage'; export * from './markSeen'; export * from './mediaTypeFromProtobuf'; export * from './membershipApprovalRequestAction'; diff --git a/src/whatsapp/functions/keepMessage.ts b/src/whatsapp/functions/keepMessage.ts new file mode 100644 index 0000000000..566c867f03 --- /dev/null +++ b/src/whatsapp/functions/keepMessage.ts @@ -0,0 +1,37 @@ +/*! + * Copyright 2023 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 { MsgModel } from '..'; +import { exportModule } from '../exportModule'; + +/** + * @whatsapp 170235 + */ +export declare function keepMessage(msg: MsgModel, t: number): Promise; +export declare function undoKeepMessage( + msg: MsgModel, + options: { deleteExpired: boolean }, + n: number +): Promise; + +exportModule( + exports, + { + keepMessage: 'keepMessage', + undoKeepMessage: 'undoKeepMessage', + }, + (m) => m.keepMessage +);