Skip to content

Commit

Permalink
feat: Added WPP.chat.getMessageACK function (close #697)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Nov 7, 2022
1 parent 413fb3b commit ffc378f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
101 changes: 101 additions & 0 deletions src/chat/functions/getMessageACK.ts
@@ -0,0 +1,101 @@
/*!
* 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 {
MsgInfoParticipantModel,
MsgInfoStore,
MsgKey,
Wid,
} from '../../whatsapp';
import { ACK } from '../../whatsapp/enums';
import { getMessageById } from '..';

export interface ParticipantStatusACK {
id: string;
wid: Wid;
deliveredAt?: number;
readAt?: number;
playedAt?: number;
}

/**
* Get message ACK from a message
*
* @example
* ```javascript
* // Get message ACK
* const ackInfo = await WPP.chat.getMessageACK('true_[number]@c.us_ABCDEF');
*
* console.log(ackInfo.deliveryRemaining); // Delivery Remaining
* console.log(ackInfo.readRemaining); // Read Remaining
* console.log(ackInfo.playedRemaining); // PlayedRemaining, for audio(ptt) only
*
* console.log(ackInfo.participants[0].deliveredAt); // Delivered At, in timestamp format
* console.log(ackInfo.participants[0].readAt); // Read At, in timestamp format
* console.log(ackInfo.participants[0].playedAt); // Played At, in timestamp format, for audio(ptt) only
*
* //To get only how was received
* const received = ackInfo.participants.filter(p => p.deliveredAt || p.readAt || p.playedAt);
*
* //To get only how was read
* const read = ackInfo.participants.filter(p => p.readAt || p.playedAt);
*
* //To get only how was played
* const played = ackInfo.participants.filter(p => p.playedAt);
* ```
* @category Message
* @return {any} Any
*/
export async function getMessageACK(msgId: string | MsgKey): Promise<{
ack: ACK;
fromMe: boolean;
deliveryRemaining: number;
readRemaining: number;
playedRemaining: number;
participants: ParticipantStatusACK[];
}> {
const msg = await getMessageById(msgId);

const info = await MsgInfoStore.find(msg.id);

const participants = new Map<string, ParticipantStatusACK>();

const callback = (
m: MsgInfoParticipantModel,
type: 'deliveredAt' | 'readAt' | 'playedAt'
) => {
const id = m.id.toString();

const status = participants.get(id) || { id, wid: m.id };

status[type] = m.t;

participants.set(id, status);
};

info?.delivery.forEach((p) => callback(p, 'deliveredAt'));
info?.read.forEach((p) => callback(p, 'readAt'));
info?.played.forEach((p) => callback(p, 'playedAt'));

return {
ack: msg.ack!,
fromMe: msg.id.fromMe,
deliveryRemaining: info?.deliveryRemaining || 0,
readRemaining: info?.readRemaining || 0,
playedRemaining: info?.playedRemaining || 0,
participants: Array.from(participants.values()),
};
}
1 change: 1 addition & 0 deletions src/chat/functions/index.ts
Expand Up @@ -27,6 +27,7 @@ export { forwardMessage, ForwardMessagesOptions } from './forwardMessage';
export { generateMessageID } from './generateMessageID';
export { get } from './get';
export { getLastSeen } from './getLastSeen';
export { getMessageACK } from './getMessageACK';
export { getMessageById } from './getMessageById';
export { getMessages, GetMessagesOptions } from './getMessages';
export { getPlatformFromMessage } from './getPlatformFromMessage';
Expand Down
3 changes: 2 additions & 1 deletion src/whatsapp/models/MsgModel.ts
Expand Up @@ -20,6 +20,7 @@ import {
MsgCollection,
TemplateButtonCollection,
} from '../collections';
import { ACK } from '../enums';
import { exportProxyModel } from '../exportModule';
import { MediaObject, MsgKey, Wid } from '../misc';
import { ChatModel, MediaDataModel } from '.';
Expand All @@ -45,7 +46,7 @@ interface Props {
/**
* See {@link Constants}
*/
ack?: number;
ack?: ACK;
invis?: any;
isNewMsg: boolean;
star?: any;
Expand Down

0 comments on commit ffc378f

Please sign in to comment.