Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added WPP.chat.keepMessage function (close #1142) #1517

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/chat/functions/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
);