Skip to content

Commit

Permalink
feat: Added WPP.chat.keepMessage function (close #1142) (#1517)
Browse files Browse the repository at this point in the history
* feat: Added WPP.chat.keepMessage function

* fix: Typos fix

* fix: Fixed imports
  • Loading branch information
icleitoncosta committed Nov 30, 2023
1 parent 576750a commit 77298e1
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/chat/functions/index.ts
Expand Up @@ -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';
Expand Down
60 changes: 60 additions & 0 deletions 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<MsgModel> {
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);
}
}
37 changes: 37 additions & 0 deletions 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
);
1 change: 1 addition & 0 deletions src/whatsapp/enums/index.ts
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Expand Up @@ -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';
Expand Down
37 changes: 37 additions & 0 deletions 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<any>;
export declare function undoKeepMessage(
msg: MsgModel,
options: { deleteExpired: boolean },
n: number
): Promise<any>;

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

0 comments on commit 77298e1

Please sign in to comment.